2024-05-14
1219. Path with Maximum Gold
Topic: Array, Backtracking, Matrix
Difficulty: Medium
Problem:
In a gold mine
Return the maximum amount of gold you can collect under the conditions:
• Every time you are located in a cell you will collect all the gold in that cell.
• From your position, you can walk one step to the left, right, up, or down.
• You can't visit the same cell more than once.
• Never visit a cell with
• You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Example 2:
Constraints:
•
•
•
•
• There are at most 25 cells containing gold.
1219. Path with Maximum Gold
Topic: Array, Backtracking, Matrix
Difficulty: Medium
Problem:
In a gold mine
grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.Return the maximum amount of gold you can collect under the conditions:
• Every time you are located in a cell you will collect all the gold in that cell.
• From your position, you can walk one step to the left, right, up, or down.
• You can't visit the same cell more than once.
• Never visit a cell with
0 gold.• You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 15•
0 <= grid[i][j] <= 100• There are at most 25 cells containing gold.
2024-05-15
2812. Find the Safest Path in a Grid
Topic: Array, Binary Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed 2D matrix
• A cell containing a thief if
• An empty cell if
You are initially positioned at cell
The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid.
Return the maximum safeness factor of all paths leading to cell
An adjacent cell of cell
The Manhattan distance between two cells
Example 1:
Image: https://assets.leetcode.com/uploads/2023/07/02/example1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/07/02/example2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2023/07/02/example3.png
Constraints:
•
•
•
• There is at least one thief in the
2812. Find the Safest Path in a Grid
Topic: Array, Binary Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed 2D matrix
grid of size n x n, where (r, c) represents:• A cell containing a thief if
grid[r][c] = 1• An empty cell if
grid[r][c] = 0You are initially positioned at cell
(0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves.The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid.
Return the maximum safeness factor of all paths leading to cell
(n - 1, n - 1).An adjacent cell of cell
(r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) and (r - 1, c) if it exists.The Manhattan distance between two cells
(a, b) and (x, y) is equal to |a - x| + |b - y|, where |val| denotes the absolute value of val.Example 1:
Image: https://assets.leetcode.com/uploads/2023/07/02/example1.png
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
Output: 0
Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
Example 2:
Image: https://assets.leetcode.com/uploads/2023/07/02/example2.png
Input: grid = [[0,0,1],[0,0,0],[0,0,0]]
Output: 2
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
Example 3:
Image: https://assets.leetcode.com/uploads/2023/07/02/example3.png
Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
Output: 2
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
Constraints:
•
1 <= grid.length == n <= 400•
grid[i].length == n•
grid[i][j] is either 0 or 1.• There is at least one thief in the
grid.2024-05-16
2331. Evaluate Boolean Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
You are given the
• Leaf nodes have either the value
• Non-leaf nodes have either the value
The evaluation of a node is as follows:
• If the node is a leaf node, the evaluation is the value of the node, i.e.
• Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
Return the boolean result of evaluating the
A full binary tree is a binary tree where each node has either
A leaf node is a node that has zero children.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/16/example1drawio1.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
• Every node has either
• Leaf nodes have a value of
• Non-leaf nodes have a value of
2331. Evaluate Boolean Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
You are given the
root of a full binary tree with the following properties:• Leaf nodes have either the value
0 or 1, where 0 represents False and 1 represents True.• Non-leaf nodes have either the value
2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.The evaluation of a node is as follows:
• If the node is a leaf node, the evaluation is the value of the node, i.e.
True or False.• Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
Return the boolean result of evaluating the
root node.A full binary tree is a binary tree where each node has either
0 or 2 children.A leaf node is a node that has zero children.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/16/example1drawio1.png
Input: root = [2,1,3,null,null,0,1]
Output: true
Explanation: The above diagram illustrates the evaluation process.
The AND node evaluates to False AND True = False.
The OR node evaluates to True OR False = True.
The root node evaluates to True, so we return true.
Example 2:
Input: root = [0]
Output: false
Explanation: The root node is a leaf node and it evaluates to false, so we return false.
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
0 <= Node.val <= 3• Every node has either
0 or 2 children.• Leaf nodes have a value of
0 or 1.• Non-leaf nodes have a value of
2 or 3.2024-05-17
1325. Delete Leaves With a Given Value
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
Note that once you delete a leaf node with value
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png
Example 3:
Image: https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png
Constraints:
• The number of nodes in the tree is in the range
•
1325. Delete Leaves With a Given Value
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
root and an integer target, delete all the leaf nodes with value target.Note that once you delete a leaf node with value
target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png
Input: root = [1,2,3,2,null,2,4], target = 2
Output: [1,null,3,null,4]
Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left).
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png
Input: root = [1,3,3,3,2], target = 3
Output: [1,3,null,null,2]
Example 3:
Image: https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png
Input: root = [1,2,null,2,null,2], target = 2
Output: [1]
Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
Constraints:
• The number of nodes in the tree is in the range
[1, 3000].•
1 <= Node.val, target <= 10002024-05-18
979. Distribute Coins in Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.
Return the minimum number of moves required to make every node have exactly one coin.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/01/18/tree1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/01/18/tree2.png
Constraints:
• The number of nodes in the tree is
•
•
• The sum of all
979. Distribute Coins in Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.
Return the minimum number of moves required to make every node have exactly one coin.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/01/18/tree1.png
Input: root = [3,0,0]
Output: 2
Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
Example 2:
Image: https://assets.leetcode.com/uploads/2019/01/18/tree2.png
Input: root = [0,3,0]
Output: 3
Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
Constraints:
• The number of nodes in the tree is
n.•
1 <= n <= 100•
0 <= Node.val <= n• The sum of all
Node.val is n.2024-05-19
3068. Find the Maximum Sum of Node Values
Topic: Array, Dynamic Programming, Greedy, Bit Manipulation, Tree, Sorting
Difficulty: Hard
Problem:
There exists an undirected tree with
Alice wants the sum of values of tree nodes to be maximum, for which Alice can perform the following operation any number of times (including zero) on the tree:
• Choose any edge
•
•
Return the maximum possible sum of the values Alice can achieve by performing the operation any number of times.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png
Example 2:
Image: https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png
Example 3:
Image: https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png
Constraints:
•
•
•
•
•
•
• The input is generated such that
3068. Find the Maximum Sum of Node Values
Topic: Array, Dynamic Programming, Greedy, Bit Manipulation, Tree, Sorting
Difficulty: Hard
Problem:
There exists an undirected tree with
n nodes numbered 0 to n - 1. You are given a 0-indexed 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i in the tree. You are also given a positive integer k, and a 0-indexed array of non-negative integers nums of length n, where nums[i] represents the value of the node numbered i.Alice wants the sum of values of tree nodes to be maximum, for which Alice can perform the following operation any number of times (including zero) on the tree:
• Choose any edge
[u, v] connecting the nodes u and v, and update their values as follows:•
nums[u] = nums[u] XOR k•
nums[v] = nums[v] XOR kReturn the maximum possible sum of the values Alice can achieve by performing the operation any number of times.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png
Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
Output: 6
Explanation: Alice can achieve the maximum sum of 6 using a single operation:
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
The total sum of values is 2 + 2 + 2 = 6.
It can be shown that 6 is the maximum achievable sum of values.
Example 2:
Image: https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png
Input: nums = [2,3], k = 7, edges = [[0,1]]
Output: 9
Explanation: Alice can achieve the maximum sum of 9 using a single operation:
- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
The total sum of values is 5 + 4 = 9.
It can be shown that 9 is the maximum achievable sum of values.
Example 3:
Image: https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png
Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
Output: 42
Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
Constraints:
•
2 <= n == nums.length <= 2 * 10^4•
1 <= k <= 10^9•
0 <= nums[i] <= 10^9•
edges.length == n - 1•
edges[i].length == 2•
0 <= edges[i][0], edges[i][1] <= n - 1• The input is generated such that
edges represent a valid tree.2024-05-20
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
• For example, the XOR total of the array
Given an array
Note: Subsets with the same elements should be counted multiple times.
An array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
XOR of all its elements, or 0 if the array is empty.• For example, the XOR total of the array
[2,5,6] is 2 XOR 5 XOR 6 = 1.Given an array
nums, return the sum of all XOR totals for every subset of nums. Note: Subsets with the same elements should be counted multiple times.
An array
a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.Example 1:
Input: nums = [1,3]
Output: 6
Explanation: The 4 subsets of [1,3] are:
- The empty subset has an XOR total of 0.
- [1] has an XOR total of 1.
- [3] has an XOR total of 3.
- [1,3] has an XOR total of 1 XOR 3 = 2.
0 + 1 + 3 + 2 = 6
Example 2:
Input: nums = [5,1,6]
Output: 28
Explanation: The 8 subsets of [5,1,6] are:
- The empty subset has an XOR total of 0.
- [5] has an XOR total of 5.
- [1] has an XOR total of 1.
- [6] has an XOR total of 6.
- [5,1] has an XOR total of 5 XOR 1 = 4.
- [5,6] has an XOR total of 5 XOR 6 = 3.
- [1,6] has an XOR total of 1 XOR 6 = 7.
- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
Example 3:
Input: nums = [3,4,5,6,7,8]
Output: 480
Explanation: The sum of all XOR totals for every subset is 480.
Constraints:
•
1 <= nums.length <= 12•
1 <= nums[i] <= 202024-05-21
78. Subsets
Topic: Array, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Example 2:
Constraints:
•
•
• All the numbers of
78. Subsets
Topic: Array, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
nums of unique elements, return all possible subsets (the power set).The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
•
1 <= nums.length <= 10•
-10 <= nums[i] <= 10• All the numbers of
nums are unique.2024-05-22
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
Constraints:
•
1 <= s.length <= 16•
s contains only lowercase English letters.2024-05-23
2597. The Number of Beautiful Subsets
Topic: Array, Dynamic Programming, Backtracking, Sorting
Difficulty: Medium
Problem:
You are given an array
A subset of
Return the number of non-empty beautiful subsets of the array
A subset of
Example 1:
Example 2:
Constraints:
•
•
2597. The Number of Beautiful Subsets
Topic: Array, Dynamic Programming, Backtracking, Sorting
Difficulty: Medium
Problem:
You are given an array
nums of positive integers and a positive integer k.A subset of
nums is beautiful if it does not contain two integers with an absolute difference equal to k.Return the number of non-empty beautiful subsets of the array
nums.A subset of
nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.Example 1:
Input: nums = [2,4,6], k = 2
Output: 4
Explanation: The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
Example 2:
Input: nums = [1], k = 1
Output: 1
Explanation: The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
Constraints:
•
1 <= nums.length <= 20•
1 <= nums[i], k <= 10002024-05-24
1255. Maximum Score Words Formed by Letters
Topic: Array, String, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
Given a list of
Return the maximum score of any valid set of words formed by using the given letters (
It is not necessary to use all characters in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
•
•
1255. Maximum Score Words Formed by Letters
Topic: Array, String, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
Given a list of
words, list of single letters (might be repeating) and score of every character.Return the maximum score of any valid set of words formed by using the given letters (
words[i] cannot be used two or more times).It is not necessary to use all characters in
letters and each letter can only be used once. Score of letters 'a', 'b', 'c', ... ,'z' is given by score[0], score[1], ... , score[25] respectively.Example 1:
Input: words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]
Output: 23
Explanation:
Score a=1, c=9, d=5, g=3, o=2
Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23.
Words "dad" and "dog" only get a score of 21.
Example 2:
Input: words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10]
Output: 27
Explanation:
Score a=4, b=4, c=4, x=5, z=10
Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27.
Word "xxxz" only get a score of 25.
Example 3:
Input: words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0]
Output: 0
Explanation:
Letter "e" can only be used once.
Constraints:
•
1 <= words.length <= 14•
1 <= words[i].length <= 15•
1 <= letters.length <= 100•
letters[i].length == 1•
score.length == 26•
0 <= score[i] <= 10•
words[i], letters[i] contains only lower case English letters.2024-05-25
140. Word Break II
Topic: Array, Hash Table, String, Dynamic Programming, Backtracking, Trie, Memoization
Difficulty: Hard
Problem:
Given a string
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• All the strings of
• Input is generated in a way that the length of the answer doesn't exceed 10^5.
140. Word Break II
Topic: Array, Hash Table, String, Dynamic Programming, Backtracking, Trie, Memoization
Difficulty: Hard
Problem:
Given a string
s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Input: s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
Output: ["cats and dog","cat sand dog"]
Example 2:
Input: s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
Output: ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: []
Constraints:
•
1 <= s.length <= 20•
1 <= wordDict.length <= 1000•
1 <= wordDict[i].length <= 10•
s and wordDict[i] consist of only lowercase English letters.• All the strings of
wordDict are unique.• Input is generated in a way that the length of the answer doesn't exceed 10^5.
2024-05-26
552. Student Attendance Record II
Topic: Dynamic Programming
Difficulty: Hard
Problem:
An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
•
•
•
Any student is eligible for an attendance award if they meet both of the following criteria:
• The student was absent (
• The student was never late (
Given an integer
Example 1:
Example 2:
Example 3:
Constraints:
•
552. Student Attendance Record II
Topic: Dynamic Programming
Difficulty: Hard
Problem:
An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
•
'A': Absent.•
'L': Late.•
'P': Present.Any student is eligible for an attendance award if they meet both of the following criteria:
• The student was absent (
'A') for strictly fewer than 2 days total.• The student was never late (
'L') for 3 or more consecutive days.Given an integer
n, return the number of possible attendance records of length n that make a student eligible for an attendance award. The answer may be very large, so return it modulo 10^9 + 7.Example 1:
Input: n = 2
Output: 8
Explanation: There are 8 records with length 2 that are eligible for an award:
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
Example 2:
Input: n = 1
Output: 3
Example 3:
Input: n = 10101
Output: 183236316
Constraints:
•
1 <= n <= 10^52024-05-27
1608. Special Array With X Elements Greater Than or Equal X
Topic: Array, Binary Search, Sorting
Difficulty: Easy
Problem:
You are given an array
Notice that
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1608. Special Array With X Elements Greater Than or Equal X
Topic: Array, Binary Search, Sorting
Difficulty: Easy
Problem:
You are given an array
nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.Notice that
x does not have to be an element in nums.Return
x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique.Example 1:
Input: nums = [3,5]
Output: 2
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
Example 2:
Input: nums = [0,0]
Output: -1
Explanation: No numbers fit the criteria for x.
If x = 0, there should be 0 numbers >= x, but there are 2.
If x = 1, there should be 1 number >= x, but there are 0.
If x = 2, there should be 2 numbers >= x, but there are 0.
x cannot be greater since there are only 2 numbers in nums.
Example 3:
Input: nums = [0,4,3,0,4]
Output: 3
Explanation: There are 3 values that are greater than or equal to 3.
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 10002024-05-28
1208. Get Equal Substrings Within Budget
Topic: String, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given two strings
You want to change
Return the maximum length of a substring of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1208. Get Equal Substrings Within Budget
Topic: String, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given two strings
s and t of the same length and an integer maxCost.You want to change
s to t. Changing the i^th character of s to i^th character of t costs |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters).Return the maximum length of a substring of
s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost. If there is no substring from s that can be changed to its corresponding substring from t, return 0.Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd".
That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to character in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You cannot make any change, so the maximum length is 1.
Constraints:
•
1 <= s.length <= 10^5•
t.length == s.length•
0 <= maxCost <= 10^6•
s and t consist of only lowercase English letters.2024-05-29
1404. Number of Steps to Reduce a Number in Binary Representation to One
Topic: String, Bit Manipulation
Difficulty: Medium
Problem:
Given the binary representation of an integer as a string
• If the current number is even, you have to divide it by
• If the current number is odd, you have to add
It is guaranteed that you can always reach one for all test cases.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1404. Number of Steps to Reduce a Number in Binary Representation to One
Topic: String, Bit Manipulation
Difficulty: Medium
Problem:
Given the binary representation of an integer as a string
s, return the number of steps to reduce it to 1 under the following rules:• If the current number is even, you have to divide it by
2.• If the current number is odd, you have to add
1 to it.It is guaranteed that you can always reach one for all test cases.
Example 1:
Input: s = "1101"
Output: 6
Explanation: "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14.
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.
Step 5) 4 is even, divide by 2 and obtain 2.
Step 6) 2 is even, divide by 2 and obtain 1.
Example 2:
Input: s = "10"
Output: 1
Explanation: "10" corressponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1.
Example 3:
Input: s = "1"
Output: 0
Constraints:
•
1 <= s.length <= 500•
s consists of characters '0' or '1'•
s[0] == '1'2024-05-30
1442. Count Triplets That Can Form Two Arrays of Equal XOR
Topic: Array, Hash Table, Math, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given an array of integers
We want to select three indices
Let's define
•
•
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (
Example 1:
Example 2:
Constraints:
•
•
1442. Count Triplets That Can Form Two Arrays of Equal XOR
Topic: Array, Hash Table, Math, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given an array of integers
arr.We want to select three indices
i, j and k where (0 <= i < j <= k < arr.length).Let's define
a and b as follows:•
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]•
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (
i, j and k) Where a == b.Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Constraints:
•
1 <= arr.length <= 300•
1 <= arr[i] <= 10^82024-05-31
260. Single Number III
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• Each integer in
260. Single Number III
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Example 1:
Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation: [5, 3] is also a valid answer.
Example 2:
Input: nums = [-1,0]
Output: [-1,0]
Example 3:
Input: nums = [0,1]
Output: [1,0]
Constraints:
•
2 <= nums.length <= 3 * 10^4•
-2^31 <= nums[i] <= 2^31 - 1• Each integer in
nums will appear twice, only two integers will appear once.2024-06-01
3110. Score of a String
Topic: String
Difficulty: Easy
Problem:
You are given a string
Return the score of
Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in
Example 2:
Input: s = "zaz"
Output: 50
Explanation:
The ASCII values of the characters in
Constraints:
•
•
3110. Score of a String
Topic: String
Difficulty: Easy
Problem:
You are given a string
s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.Return the score of
s.Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in
s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.Example 2:
Input: s = "zaz"
Output: 50
Explanation:
The ASCII values of the characters in
s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.Constraints:
•
2 <= s.length <= 100•
s consists only of lowercase English letters.2024-06-02
344. Reverse String
Topic: Two Pointers, String
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
You must do this by modifying the input array in-place with
Example 1:
Example 2:
Constraints:
•
•
344. Reverse String
Topic: Two Pointers, String
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
s.You must do this by modifying the input array in-place with
O(1) extra memory.Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
•
1 <= s.length <= 10^5•
s[i] is a printable ascii character.