2022-11-28
2225. Find Players With Zero or One Losses
Topic: Array, Hash Table, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
Return a list
•
•
The values in the two lists should be returned in increasing order.
Note:
• You should only consider the players that have played at least one match.
• The testcases will be generated such that no two matches will have the same outcome.
Example 1:
Example 2:
Constraints:
•
•
•
•
• All
2225. Find Players With Zero or One Losses
Topic: Array, Hash Table, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
matches where matches[i] = [winner_i, loser_i] indicates that the player winner_i defeated player loser_i in a match.Return a list
answer of size 2 where:•
answer[0] is a list of all players that have not lost any matches.•
answer[1] is a list of all players that have lost exactly one match.The values in the two lists should be returned in increasing order.
Note:
• You should only consider the players that have played at least one match.
• The testcases will be generated such that no two matches will have the same outcome.
Example 1:
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
Example 2:
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
Output: [[1,2,5,6],[]]
Explanation:
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
Constraints:
•
1 <= matches.length <= 10^5•
matches[i].length == 2•
1 <= winner_i, loser_i <= 10^5•
winner_i != loser_i• All
matches[i] are unique.2022-11-29
380. Insert Delete GetRandom O(1)
Topic: Array, Hash Table, Math, Design, Randomized
Difficulty: Medium
Problem:
Implement the
•
•
•
•
You must implement the functions of the class such that each function works in average
Example 1:
Constraints:
•
• At most
• There will be at least one element in the data structure when
380. Insert Delete GetRandom O(1)
Topic: Array, Hash Table, Math, Design, Randomized
Difficulty: Medium
Problem:
Implement the
RandomizedSet class:•
RandomizedSet() Initializes the RandomizedSet object.•
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.•
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.•
int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.You must implement the functions of the class such that each function works in average
O(1) time complexity.Example 1:
Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]
Explanation
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
Constraints:
•
-2^31 <= val <= 2^31 - 1• At most
2 *10^5 calls will be made to insert, remove, and getRandom.• There will be at least one element in the data structure when
getRandom is called.2022-11-30
1207. Unique Number of Occurrences
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given an array of integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1207. Unique Number of Occurrences
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given an array of integers
arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.Example 1:
Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
Example 2:
Input: arr = [1,2]
Output: false
Example 3:
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true
Constraints:
•
1 <= arr.length <= 1000•
-1000 <= arr[i] <= 10002022-12-01
1704. Determine if String Halves Are Alike
Topic: String, Counting
Difficulty: Easy
Problem:
You are given a string
Two strings are alike if they have the same number of vowels (
Return
Example 1:
Example 2:
Constraints:
•
•
•
1704. Determine if String Halves Are Alike
Topic: String, Counting
Difficulty: Easy
Problem:
You are given a string
s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.Two strings are alike if they have the same number of vowels (
'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.Return
true if a and b are alike. Otherwise, return false.Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Constraints:
•
2 <= s.length <= 1000•
s.length is even.•
s consists of uppercase and lowercase letters.2022-12-02
1657. Determine if Two Strings Are Close
Topic: Hash Table, String, Sorting
Difficulty: Medium
Problem:
Two strings are considered close if you can attain one from the other using the following operations:
• Operation 1: Swap any two existing characters.
• For example,
• Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
• For example,
You can use the operations on either string as many times as necessary.
Given two strings,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1657. Determine if Two Strings Are Close
Topic: Hash Table, String, Sorting
Difficulty: Medium
Problem:
Two strings are considered close if you can attain one from the other using the following operations:
• Operation 1: Swap any two existing characters.
• For example,
abcde -> aecdb• Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
• For example,
aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)You can use the operations on either string as many times as necessary.
Given two strings,
word1 and word2, return true if word1 and word2 are close, and false otherwise.Example 1:
Input: word1 = "abc", word2 = "bca"
Output: true
Explanation: You can attain word2 from word1 in 2 operations.
Apply Operation 1: "abc" -> "acb"
Apply Operation 1: "acb" -> "bca"
Example 2:
Input: word1 = "a", word2 = "aa"
Output: false
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
Example 3:
Input: word1 = "cabbba", word2 = "abbccc"
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -> "caabbb"
Apply Operation 2: "caabbb" -> "baaccc"
Apply Operation 2: "baaccc" -> "abbccc"
Constraints:
•
1 <= word1.length, word2.length <= 10^5•
word1 and word2 contain only lowercase English letters.2022-12-03
451. Sort Characters By Frequency
Topic: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given a string
Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
451. Sort Characters By Frequency
Topic: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given a string
s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Input: s = "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input: s = "cccaaa"
Output: "aaaccc"
Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input: s = "Aabb"
Output: "bbAa"
Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
Constraints:
•
1 <= s.length <= 5 * 10^5•
s consists of uppercase and lowercase English letters and digits.2022-12-04
2256. Minimum Average Difference
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
The average difference of the index
Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.
Note:
• The absolute difference of two numbers is the absolute value of their difference.
• The average of
• The average of
Example 1:
Example 2:
Constraints:
•
•
2256. Minimum Average Difference
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums of length n.The average difference of the index
i is the absolute difference between the average of the first i + 1 elements of nums and the average of the last n - i - 1 elements. Both averages should be rounded down to the nearest integer.Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.
Note:
• The absolute difference of two numbers is the absolute value of their difference.
• The average of
n elements is the sum of the n elements divided (integer division) by n.• The average of
0 elements is considered to be 0.Example 1:
Input: nums = [2,5,3,9,5,3]
Output: 3
Explanation:
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
Example 2:
Input: nums = [0]
Output: 0
Explanation:
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^52022-12-05
876. Middle of the Linked List
Topic: Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the
If there are two middle nodes, return the second middle node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg
Constraints:
• The number of nodes in the list is in the range
•
876. Middle of the Linked List
Topic: Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the
head of a singly linked list, return the middle node of the linked list.If there are two middle nodes, return the second middle node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
Constraints:
• The number of nodes in the list is in the range
[1, 100].•
1 <= Node.val <= 1002022-12-06
328. Odd Even Linked List
Topic: Linked List
Difficulty: Medium
Problem:
Given the
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/10/oddeven-linked-list.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/10/oddeven2-linked-list.jpg
Constraints:
• The number of nodes in the linked list is in the range
•
328. Odd Even Linked List
Topic: Linked List
Difficulty: Medium
Problem:
Given the
head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in
O(1) extra space complexity and O(n) time complexity.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/10/oddeven-linked-list.jpg
Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/10/oddeven2-linked-list.jpg
Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]
Constraints:
• The number of nodes in the linked list is in the range
[0, 10^4].•
-10^6 <= Node.val <= 10^62022-12-07
938. Range Sum of BST
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
•
• All
938. Range Sum of BST
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
Constraints:
• The number of nodes in the tree is in the range
[1, 2 * 10^4].•
1 <= Node.val <= 10^5•
1 <= low <= high <= 10^5• All
Node.val are unique.2022-12-08
872. Leaf-Similar Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png
For example, in the given tree above, the leaf value sequence is
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg
Constraints:
• The number of nodes in each tree will be in the range
• Both of the given trees will have values in the range
872. Leaf-Similar Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png
For example, in the given tree above, the leaf value sequence is
(6, 7, 4, 9, 8).Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return
true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg
Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg
Input: root1 = [1,2,3], root2 = [1,3,2]
Output: false
Constraints:
• The number of nodes in each tree will be in the range
[1, 200].• Both of the given trees will have values in the range
[0, 200].2022-12-09
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
A node
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Constraints:
• The number of nodes in the tree is in the range
•
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.A node
a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Input: root = [1,null,2,null,0,3]
Output: 3
Constraints:
• The number of nodes in the tree is in the range
[2, 5000].•
0 <= Node.val <= 10^52022-12-10
1339. Maximum Product of Splitted Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo
Note that you need to maximize the answer before taking the mod and not after taking it.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/21/sample_1_1699.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/21/sample_2_1699.png
Constraints:
• The number of nodes in the tree is in the range
•
1339. Maximum Product of Splitted Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo
10^9 + 7.Note that you need to maximize the answer before taking the mod and not after taking it.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/21/sample_1_1699.png
Input: root = [1,2,3,4,5,6]
Output: 110
Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/21/sample_2_1699.png
Input: root = [1,null,2,3,4,null,null,5,6]
Output: 90
Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
Constraints:
• The number of nodes in the tree is in the range
[2, 5 * 10^4].•
1 <= Node.val <= 10^42022-12-11
124. Binary Tree Maximum Path Sum
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the path.
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
124. Binary Tree Maximum Path Sum
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the path.
Given the
root of a binary tree, return the maximum path sum of any non-empty path.Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg
Input: root = [1,2,3]
Output: 6
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg
Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
Constraints:
• The number of nodes in the tree is in the range
[1, 3 * 10^4].•
-1000 <= Node.val <= 10002022-12-12
70. Climbing Stairs
Topic: Math, Dynamic Programming, Memoization
Difficulty: Easy
Problem:
You are climbing a staircase. It takes
Each time you can either climb
Example 1:
Example 2:
Constraints:
•
70. Climbing Stairs
Topic: Math, Dynamic Programming, Memoization
Difficulty: Easy
Problem:
You are climbing a staircase. It takes
n steps to reach the top.Each time you can either climb
1 or 2 steps. In how many distinct ways can you climb to the top?Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
•
1 <= n <= 452022-12-13
931. Minimum Falling Path Sum
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
Given an
A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg
Constraints:
•
•
•
931. Minimum Falling Path Sum
Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium
Problem:
Given an
n x n array of integers matrix, return the minimum sum of any falling path through matrix.A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position
(row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output: 13
Explanation: There are two falling paths with a minimum sum as shown.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg
Input: matrix = [[-19,57],[-40,-5]]
Output: -59
Explanation: The falling path with a minimum sum is shown.
Constraints:
•
n == matrix.length == matrix[i].length•
1 <= n <= 100•
-100 <= matrix[i][j] <= 1002022-12-14
198. House Robber
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
198. House Robber
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array
nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 4002022-12-15
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
"ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
•
1 <= text1.length, text2.length <= 1000•
text1 and text2 consist of only lowercase English characters.2022-12-16
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
Implement the
•
•
•
•
Notes:
• You must use only standard operations of a stack, which means only
• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Constraints:
•
• At most
• All the calls to
Follow-up: Can you implement the queue such that each operation is amortized
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
push, peek, pop, and empty).Implement the
MyQueue class:•
void push(int x) Pushes element x to the back of the queue.•
int pop() Removes the element from the front of the queue and returns it.•
int peek() Returns the element at the front of the queue.•
boolean empty() Returns true if the queue is empty, false otherwise.Notes:
• You must use only standard operations of a stack, which means only
push to top, peek/pop from top, size, and is empty operations are valid.• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Constraints:
•
1 <= x <= 9• At most
100 calls will be made to push, pop, peek, and empty.• All the calls to
pop and peek are valid.Follow-up: Can you implement the queue such that each operation is amortized
O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.2022-12-17
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
+, -, *, and /. Each operand may be an integer or another expression.Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
•
1 <= tokens.length <= 10^4•
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].