2022-10-05
623. Add One Row to Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Note that the
The adding rule is:
• Given the integer
•
•
• If
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
• The depth of the tree is in the range
•
•
•
623. Add One Row to Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.Note that the
root node is at depth 1.The adding rule is:
• Given the integer
depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.•
cur's original left subtree should be the left subtree of the new left subtree root.•
cur's original right subtree should be the right subtree of the new right subtree root.• If
depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg
Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].• The depth of the tree is in the range
[1, 10^4].•
-100 <= Node.val <= 100•
-10^5 <= val <= 10^5•
1 <= depth <= the depth of tree + 12022-10-06
981. Time Based Key-Value Store
Topic: Hash Table, String, Binary Search, Design
Difficulty: Medium
Problem:
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the
•
•
•
Example 1:
Constraints:
•
•
•
• All the timestamps
• At most
981. Time Based Key-Value Store
Topic: Hash Table, String, Binary Search, Design
Difficulty: Medium
Problem:
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the
TimeMap class:•
TimeMap() Initializes the object of the data structure.•
void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.•
String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".Example 1:
Input
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output
[null, null, "bar", "bar", null, "bar2", "bar2"]
Explanation
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
timeMap.get("foo", 1); // return "bar"
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
timeMap.get("foo", 4); // return "bar2"
timeMap.get("foo", 5); // return "bar2"
Constraints:
•
1 <= key.length, value.length <= 100•
key and value consist of lowercase English letters and digits.•
1 <= timestamp <= 10^7• All the timestamps
timestamp of set are strictly increasing.• At most
2 * 10^5 calls will be made to set and get.2022-10-07
732. My Calendar III
Topic: Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Hard
Problem:
A
You are given some events
Implement the
•
•
Example 1:
Constraints:
•
• At most
732. My Calendar III
Topic: Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Hard
Problem:
A
k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)You are given some events
[start, end), after each given event, return an integer k representing the maximum k-booking between all the previous events.Implement the
MyCalendarThree class:•
MyCalendarThree() Initializes the object.•
int book(int start, int end) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.Example 1:
Input
["MyCalendarThree", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, 1, 1, 2, 3, 3, 3]
Explanation
MyCalendarThree myCalendarThree = new MyCalendarThree();
myCalendarThree.book(10, 20); // return 1, The first event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
myCalendarThree.book(50, 60); // return 1, The second event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
myCalendarThree.book(10, 40); // return 2, The third event [10, 40) intersects the first event, and the maximum k-booking is a 2-booking.
myCalendarThree.book(5, 15); // return 3, The remaining events cause the maximum K-booking to be only a 3-booking.
myCalendarThree.book(5, 10); // return 3
myCalendarThree.book(25, 55); // return 3
Constraints:
•
0 <= start < end <= 10^9• At most
400 calls will be made to book.2022-10-08
16. 3Sum Closest
Topic: Array, Two Pointers, Sorting
Difficulty: Medium
Problem:
Given an integer array
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Example 1:
Example 2:
Constraints:
•
•
•
16. 3Sum Closest
Topic: Array, Two Pointers, Sorting
Difficulty: Medium
Problem:
Given an integer array
nums of length n and an integer target, find three integers in nums such that the sum is closest to target.Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Example 1:
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Example 2:
Input: nums = [0,0,0], target = 1
Output: 0
Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
Constraints:
•
3 <= nums.length <= 1000•
-1000 <= nums[i] <= 1000•
-10^4 <= target <= 10^42022-10-09
653. Two Sum IV - Input is a BST
Topic: Hash Table, Two Pointers, Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
•
•
653. Two Sum IV - Input is a BST
Topic: Hash Table, Two Pointers, Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg
Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg
Input: root = [5,3,6,2,4,null,7], k = 28
Output: false
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-10^4 <= Node.val <= 10^4•
root is guaranteed to be a valid binary search tree.•
-10^5 <= k <= 10^52022-10-10
1328. Break a Palindrome
Topic: String, Greedy
Difficulty: Medium
Problem:
Given a palindromic string of lowercase English letters
Return the resulting string. If there is no way to replace a character to make it not a palindrome, return an empty string.
A string
Example 1:
Example 2:
Constraints:
•
•
1328. Break a Palindrome
Topic: String, Greedy
Difficulty: Medium
Problem:
Given a palindromic string of lowercase English letters
palindrome, replace exactly one character with any lowercase English letter so that the resulting string is not a palindrome and that it is the lexicographically smallest one possible.Return the resulting string. If there is no way to replace a character to make it not a palindrome, return an empty string.
A string
a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, a has a character strictly smaller than the corresponding character in b. For example, "abcc" is lexicographically smaller than "abcd" because the first position they differ is at the fourth character, and 'c' is smaller than 'd'.Example 1:
Input: palindrome = "abccba"
Output: "aaccba"
Explanation: There are many ways to make "abccba" not a palindrome, such as "zbccba", "aaccba", and "abacba".
Of all the ways, "aaccba" is the lexicographically smallest.
Example 2:
Input: palindrome = "a"
Output: ""
Explanation: There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
Constraints:
•
1 <= palindrome.length <= 1000•
palindrome consists of only lowercase English letters.2022-10-11
334. Increasing Triplet Subsequence
Topic: Array, Greedy
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
Follow up: Could you implement a solution that runs in
334. Increasing Triplet Subsequence
Topic: Array, Greedy
Difficulty: Medium
Problem:
Given an integer array
nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.
Example 2:
Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.
Example 3:
Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
Constraints:
•
1 <= nums.length <= 5 * 10^5•
-2^31 <= nums[i] <= 2^31 - 1Follow up: Could you implement a solution that runs in
O(n) time complexity and O(1) space complexity?2022-10-12
976. Largest Perimeter Triangle
Topic: Array, Math, Greedy, Sorting
Difficulty: Easy
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
976. Largest Perimeter Triangle
Topic: Array, Math, Greedy, Sorting
Difficulty: Easy
Problem:
Given an integer array
nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.Example 1:
Input: nums = [2,1,2]
Output: 5
Example 2:
Input: nums = [1,2,1]
Output: 0
Constraints:
•
3 <= nums.length <= 10^4•
1 <= nums[i] <= 10^62022-10-13
237. Delete Node in a Linked List
Topic: Linked List
Difficulty: Medium
Problem:
There is a singly-linked list
You are given the node to be deleted
All the values of the linked list are unique, and it is guaranteed that the given node
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
• The value of the given node should not exist in the linked list.
• The number of nodes in the linked list should decrease by one.
• All the values before
• All the values after
Custom testing:
• For the input, you should provide the entire linked list
• We will build the linked list and pass the node to your function.
• The output will be the entire list after calling your function.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/01/node1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/01/node2.jpg
Constraints:
• The number of the nodes in the given list is in the range
•
• The value of each node in the list is unique.
• The
237. Delete Node in a Linked List
Topic: Linked List
Difficulty: Medium
Problem:
There is a singly-linked list
head and we want to delete a node node in it.You are given the node to be deleted
node. You will not be given access to the first node of head.All the values of the linked list are unique, and it is guaranteed that the given node
node is not the last node in the linked list.Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
• The value of the given node should not exist in the linked list.
• The number of nodes in the linked list should decrease by one.
• All the values before
node should be in the same order.• All the values after
node should be in the same order.Custom testing:
• For the input, you should provide the entire linked list
head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.• We will build the linked list and pass the node to your function.
• The output will be the entire list after calling your function.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/01/node1.jpg
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/01/node2.jpg
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
Constraints:
• The number of the nodes in the given list is in the range
[2, 1000].•
-1000 <= Node.val <= 1000• The value of each node in the list is unique.
• The
node to be deleted is in the list and is not a tail node.2022-10-14
2095. Delete the Middle Node of a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
The middle node of a linked list of size
• For
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png
Constraints:
• The number of nodes in the list is in the range
•
2095. Delete the Middle Node of a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
head of a linked list. Delete the middle node, and return the head of the modified linked list.The middle node of a linked list of size
n is the ⌊n / 2⌋^th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.• For
n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png
Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
Explanation:
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
We return the new list after removing this node.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png
Input: head = [1,2,3,4]
Output: [1,2,4]
Explanation:
The above figure represents the given linked list.
For n = 4, node 2 with value 3 is the middle node, which is marked in red.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png
Input: head = [2,1]
Output: [2]
Explanation:
The above figure represents the given linked list.
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.
Constraints:
• The number of nodes in the list is in the range
[1, 10^5].•
1 <= Node.val <= 10^52022-10-15
1531. String Compression II
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
Notice that in this problem, we are not adding
Given a string
Find the minimum length of the run-length encoded version of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1531. String Compression II
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
"aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".Notice that in this problem, we are not adding
'1' after single characters.Given a string
s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.Find the minimum length of the run-length encoded version of
s after deleting at most k characters.Example 1:
Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
Example 2:
Input: s = "aabbaa", k = 2
Output: 2
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
Example 3:
Input: s = "aaaaaaaaaaa", k = 0
Output: 3
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
Constraints:
•
1 <= s.length <= 100•
0 <= k <= s.length•
s contains only lowercase English letters.2022-10-16
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
You are given an integer array
Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Example 2:
Example 3:
Constraints:
•
•
•
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
d days. Jobs are dependent (i.e To work on the i^th job, you have to finish all the jobs j where 0 <= j < i).You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
d days. The difficulty of a day is the maximum difficulty of a job done on that day.You are given an integer array
jobDifficulty and an integer d. The difficulty of the i^th job is jobDifficulty[i].Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
Output: 7
Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7
Example 2:
Input: jobDifficulty = [9,9,9], d = 4
Output: -1
Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
Example 3:
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.
Constraints:
•
1 <= jobDifficulty.length <= 300•
0 <= jobDifficulty[i] <= 1000•
1 <= d <= 102022-10-17
1832. Check if the Sentence Is Pangram
Topic: Hash Table, String
Difficulty: Easy
Problem:
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string
Example 1:
Example 2:
Constraints:
•
•
1832. Check if the Sentence Is Pangram
Topic: Hash Table, String
Difficulty: Easy
Problem:
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string
sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.
Example 2:
Input: sentence = "leetcode"
Output: false
Constraints:
•
1 <= sentence.length <= 1000•
sentence consists of lowercase English letters.2022-10-18
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
•
To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string
Image: https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg
Given a positive integer
Example 1:
Example 2:
Constraints:
•
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
countAndSay(1) = "1"•
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string
"3322251":Image: https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg
Given a positive integer
n, return the n^th term of the count-and-say sequence.Example 1:
Input: n = 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
Constraints:
•
1 <= n <= 302022-10-19
692. Top K Frequent Words
Topic: Hash Table, String, Trie, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given an array of strings
Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow-up: Could you solve it in
692. Top K Frequent Words
Topic: Hash Table, String, Trie, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given an array of strings
words and an integer k, return the k most frequent strings.Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
Constraints:
•
1 <= words.length <= 500•
1 <= words[i].length <= 10•
words[i] consists of lowercase English letters.•
k is in the range [1, The number of unique words[i]]Follow-up: Could you solve it in
O(n log(k)) time and O(n) extra space?2022-10-20
12. Integer to Roman
Topic: Hash Table, Math, String
Difficulty: Medium
Problem:
Roman numerals are represented by seven different symbols:
For example,
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
•
•
•
Given an integer, convert it to a roman numeral.
Example 1:
Example 2:
Example 3:
Constraints:
•
12. Integer to Roman
Topic: Hash Table, Math, String
Difficulty: Medium
Problem:
Roman numerals are represented by seven different symbols:
I, V, X, L, C, D and M.Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example,
2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:•
I can be placed before V (5) and X (10) to make 4 and 9.•
X can be placed before L (50) and C (100) to make 40 and 90.•
C can be placed before D (500) and M (1000) to make 400 and 900.Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
•
1 <= num <= 39992022-10-21
219. Contains Duplicate II
Topic: Array, Hash Table, Sliding Window
Difficulty: Easy
Problem:
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
219. Contains Duplicate II
Topic: Array, Hash Table, Sliding Window
Difficulty: Easy
Problem:
Given an integer array
nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Constraints:
•
1 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^9•
0 <= k <= 10^52022-10-22
76. Minimum Window Substring
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
Given two strings
The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
Follow up: Could you find an algorithm that runs in
76. Minimum Window Substring
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
Given two strings
s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.
Example 3:
Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.
Constraints:
•
m == s.length•
n == t.length•
1 <= m, n <= 10^5•
s and t consist of uppercase and lowercase English letters.Follow up: Could you find an algorithm that runs in
O(m + n) time?2022-10-23
645. Set Mismatch
Topic: Array, Hash Table, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You have a set of integers
You are given an integer array
Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Example 2:
Constraints:
•
•
645. Set Mismatch
Topic: Array, Hash Table, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You have a set of integers
s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.You are given an integer array
nums representing the data status of this set after the error.Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Example 2:
Input: nums = [1,1]
Output: [1,2]
Constraints:
•
2 <= nums.length <= 10^4•
1 <= nums[i] <= 10^42022-10-24
1239. Maximum Length of a Concatenated String with Unique Characters
Topic: Array, String, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
You are given an array of strings
Return the maximum possible length of
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1239. Maximum Length of a Concatenated String with Unique Characters
Topic: Array, String, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
You are given an array of strings
arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.Return the maximum possible length of
s.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Explanation: The only string in arr has all 26 characters.
Constraints:
•
1 <= arr.length <= 16•
1 <= arr[i].length <= 26•
arr[i] contains only lowercase English letters.