Problem: Determine if a cell is reachable at a given time
URL: [https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&envId=2023-11-08](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&envId=2023-11-08)
Text:
You are given four integers
In an infinite 2D grid, you start at the cell
Return
A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.
Example 1:
Input:
Output:
Explanation: Starting at cell
Example 2:
Input:
Output:
Explanation: Starting at cell
Constraints:
-
- `0 <= t <= 10^9
URL: [https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&envId=2023-11-08](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&envId=2023-11-08)
Text:
You are given four integers
sx
, sy
, fx
, fy
, and a non-negative integer t
.In an infinite 2D grid, you start at the cell
(sx, sy)
. Each second, you must move to any of its adjacent cells.Return
true
if you can reach cell (fx, fy)
after exactly t
seconds, or false
otherwise.A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.
Example 1:
Input:
sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output:
true
Explanation: Starting at cell
(2, 4)
, we can reach cell (7, 7)
in exactly 6 seconds by going through the cells depicted in the picture above.Example 2:
Input:
sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output:
false
Explanation: Starting at cell
(3, 1)
, it takes at least 4 seconds to reach cell (7, 3)
by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3)
at the third second.Constraints:
-
1 <= sx, sy, fx, fy <= 10^9
- `0 <= t <= 10^9
Problem URL: [https://leetcode.com/problems/count-number-of-homogenous-substrings/?envType=daily-question&envId=2023-11-09](https://leetcode.com/problems/count-number-of-homogenous-substrings/?envType=daily-question&envId=2023-11-09)
Given a string s, return the number of homogenous substrings of s. Modulo 109 + 7 is used for returning the answer, as it may be too large.
A string is homogenous if all its characters are the same. A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "abbcccaa"
Output: 13
Explanation: The homogenous substrings are as follows:
- "a" appears 3 times.
- "aa" appears 1 time.
- "b" appears 2 times.
- "bb" appears 1 time.
- "c" appears 3 times.
- "cc" appears 2 times.
- "ccc" appears 1 time.
Total count: 3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
Example 2:
Input: s = "xy"
Output: 2
Explanation: The homogenous substrings are "x" and "y".
Example 3:
Input: s = "zzzzz"
Output: 15
Constraints:
- 1 <= s.length <= 105
- s consists of lowercase letters.
Given a string s, return the number of homogenous substrings of s. Modulo 109 + 7 is used for returning the answer, as it may be too large.
A string is homogenous if all its characters are the same. A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "abbcccaa"
Output: 13
Explanation: The homogenous substrings are as follows:
- "a" appears 3 times.
- "aa" appears 1 time.
- "b" appears 2 times.
- "bb" appears 1 time.
- "c" appears 3 times.
- "cc" appears 2 times.
- "ccc" appears 1 time.
Total count: 3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
Example 2:
Input: s = "xy"
Output: 2
Explanation: The homogenous substrings are "x" and "y".
Example 3:
Input: s = "zzzzz"
Output: 15
Constraints:
- 1 <= s.length <= 105
- s consists of lowercase letters.
Problem URL: [https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/?envType=daily-question&envId=2023-11-10](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/?envType=daily-question&envId=2023-11-10)
Text: There is an integer array
You are given a 2D integer array
It is guaranteed that every adjacent pair of elements
Return the original array
Example 1:
Example 2:
Example 3:
Constraints:
-
-
-
-
-
- There exists some
Text: There is an integer array
nums
that consists of n
unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums
.You are given a 2D integer array
adjacentPairs
of size n - 1
where each adjacentPairs[i] = [ui, vi]
indicates that the elements ui
and vi
are adjacent in nums
.It is guaranteed that every adjacent pair of elements
nums[i]
and nums[i+1]
will exist in adjacentPairs
, either as [nums[i], nums[i+1]]
or [nums[i+1], nums[i]]
. The pairs can appear in any order.Return the original array
nums
. If there are multiple solutions, return any of them.Example 1:
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
Output: [1,2,3,4]
Explanation: This array has all its adjacent pairs in adjacentPairs.
Notice that adjacentPairs[i] may not be in left-to-right order.
Example 2:
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
Output: [-2,4,1,-3]
Explanation: There can be negative numbers.
Another solution is [-3,1,4,-2], which would also be accepted.
Example 3:
Input: adjacentPairs = [[100000,-100000]]
Output: [100000,-100000]
Constraints:
-
nums.length == n
-
adjacentPairs.length == n - 1
-
adjacentPairs[i].length == 2
-
2 <= n <= 10^5
-
-10^5 <= nums[i], ui, vi <= 10^5
- There exists some
nums
that has adjacentPairs
as its pairs.Problem URL: [Design Graph With Shortest Path Calculator - LeetCode](https://leetcode.com/problems/design-graph-with-shortest-path-calculator/?envType=daily-question&envId=2023-11-11)
Text:
There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.
Implement the Graph class:
Example:
Constraints:
- 1 <= n <= 100
- 0 <= edges.length <= n * (n - 1)
- edges[i].length == edge.length == 3
- 0 <= fromi, toi, from, to, node1, node2 <= n - 1
- 1 <= edgeCosti, edgeCost <= 10^6
- There are no repeated edges and no self-loops in the graph at any point.
- At most 100 calls will be made for addEdge.
- At most 100 calls will be made for shortestPath.
Text:
There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.
Implement the Graph class:
Graph(int n, int[][] edges)
- initializes the object with n nodes and the given edges. addEdge(int[] edge)
- adds an edge to the list of edges where edge = [from, to, edgeCost]. It is guaranteed that there is no edge between the two nodes before adding this one. int shortestPath(int node1, int node2)
- returns the minimum cost of a path from node1 to node2. If no path exists, return -1. The cost of a path is the sum of the costs of the edges in the path.Example:
Input
["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
Output
[null, 6, -1, null, 6]
Explanation
Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
Constraints:
- 1 <= n <= 100
- 0 <= edges.length <= n * (n - 1)
- edges[i].length == edge.length == 3
- 0 <= fromi, toi, from, to, node1, node2 <= n - 1
- 1 <= edgeCosti, edgeCost <= 10^6
- There are no repeated edges and no self-loops in the graph at any point.
- At most 100 calls will be made for addEdge.
- At most 100 calls will be made for shortestPath.
Problem URL: [Bus Routes LeetCode](https://leetcode.com/problems/bus-routes/?envType=daily-question&envId=2023-11-12)
Problem Description: You are given an array
You will start at the bus stop
Return the least number of buses you must take to travel from
Examples:
Example 1:
Example 2:
Constraints:
- 1 <=
- 1 <=
- All the values of
-
- 0 <=
- 0 <=
Problem Description: You are given an array
routes
representing bus routes where routes[i]
is a bus route that the i`th bus repeats forever. For example, if `routes[0] = [1, 5, 7]
, this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...
forever.You will start at the bus stop
source
(You are not on any bus initially), and you want to go to the bus stop target
. You can travel between bus stops by buses only.Return the least number of buses you must take to travel from
source
to target
. Return -1 if it is not possible.Examples:
Example 1:
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
Output: 2
Explanation: The best strategy is to take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Example 2:
Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
Output: -1
Constraints:
- 1 <=
routes.length
<= 500- 1 <=
routes[i].length
<= 105- All the values of
routes[i]
are unique.-
sum(routes[i].length)
<= 105- 0 <=
routes[i][j]
< 106- 0 <=
source
, target
< 106Problem Statement:
Given a 0-indexed string
1. All consonants remain in their original places. More formally, if there is an index
2. The vowels must be sorted in the non-decreasing order of their ASCII values. More formally, for pairs of indices
Return the resulting string.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.
Example 1:
Input:
Output:
Explanation: 'E', 'O', and 'e' are the vowels in
Example 2:
Input:
Output:
Explanation: There are no vowels in
Constraints:
1 <=
[LeetCode Link](https://leetcode.com/problems/sort-vowels-in-a-string/?envType=daily-question&envId=2023-11-13)
Given a 0-indexed string
s
, permute s
to get a new string t
such that:1. All consonants remain in their original places. More formally, if there is an index
i
with 0 <= i < s.length
such that s[i]
is a consonant, then t[i] = s[i]
.2. The vowels must be sorted in the non-decreasing order of their ASCII values. More formally, for pairs of indices
i
and j
with 0 <= i < j < s.length
such that s[i]
and s[j]
are vowels, then t[i]
must not have a higher ASCII value than t[j]
.Return the resulting string.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.
Example 1:
Input:
s = "lEetcOde"
Output:
"lEOtcede"
Explanation: 'E', 'O', and 'e' are the vowels in
s
; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.Example 2:
Input:
s = "lYmpH"
Output:
"lYmpH"
Explanation: There are no vowels in
s
(all characters in s
are consonants), so we return "lYmpH"
.Constraints:
1 <=
s.length
<= 105s
consists only of letters of the English alphabet in uppercase and lowercase.[LeetCode Link](https://leetcode.com/problems/sort-vowels-in-a-string/?envType=daily-question&envId=2023-11-13)
Problem url: [https://leetcode.com/problems/unique-length-3-palindromic-subsequences/?envType=daily-question&envId=2023-11-14](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/?envType=daily-question&envId=2023-11-14)
Text: Given a string
Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
A palindrome is a string that reads the same forwards and backwards.
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".
Example 1:
Input:
Output:
Explanation: The 3 palindromic subsequences of length 3 are:
- "aba" (subsequence of "aabca")
- "aaa" (subsequence of "aabca")
- "aca" (subsequence of "aabca")
Example 2:
Input:
Output:
Explanation: There are no palindromic subsequences of length 3 in "adc".
Example 3:
Input:
Output:
Explanation: The 4 palindromic subsequences of length 3 are:
- "bbb" (subsequence of "bbcbaba")
- "bcb" (subsequence of "bbcbaba")
- "bab" (subsequence of "bbcbaba")
- "aba" (subsequence of "bbcbaba")
Constraints:
- 3 <= s.length <= 105
-
Text: Given a string
s
, return the number of unique palindromes of length three that are a subsequence of s
.Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
A palindrome is a string that reads the same forwards and backwards.
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".
Example 1:
Input:
s = "aabca"
Output:
3
Explanation: The 3 palindromic subsequences of length 3 are:
- "aba" (subsequence of "aabca")
- "aaa" (subsequence of "aabca")
- "aca" (subsequence of "aabca")
Example 2:
Input:
s = "adc"
Output:
0
Explanation: There are no palindromic subsequences of length 3 in "adc".
Example 3:
Input:
s = "bbcbaba"
Output:
4
Explanation: The 4 palindromic subsequences of length 3 are:
- "bbb" (subsequence of "bbcbaba")
- "bcb" (subsequence of "bbcbaba")
- "bab" (subsequence of "bbcbaba")
- "aba" (subsequence of "bbcbaba")
Constraints:
- 3 <= s.length <= 105
-
s
consists of only lowercase English letters.Problem Statement
You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions:
1. The value of the first element in arr must be 1.
2. The absolute difference between any two adjacent elements must be less than or equal to 1. In other words, abs(arr[i] - arr[i - 1]) <= 1 for each i where 1 <= i < arr.length (0-indexed). abs(x) is the absolute value of x.
There are 2 types of operations that you can perform any number of times:
1. Decrease the value of any element of arr to a smaller positive integer.
2. Rearrange the elements of arr to be in any order.
Return the maximum possible value of an element in arr after performing the operations to satisfy the conditions.
Example 1
Input: arr = [2,2,1,2,1]
Output: 2
Explanation: We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1]. The largest element in arr is 2.
Example 2
Input: arr = [100,1,1000]
Output: 3
Explanation: One possible way to satisfy the conditions is by doing the following:
1. Rearrange arr so it becomes [1,100,1000].
2. Decrease the value of the second element to 2.
3. Decrease the value of the third element to 3.
Now arr = [1,2,3], which satisfies the conditions. The largest element in arr is 3.
Example 3
Input: arr = [1,2,3,4,5]
Output: 5
Explanation: The array already satisfies the conditions, and the largest element is 5.
Constraints
1 <= arr.length <= 10^5
1 <= arr[i] <= 10^9
You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions:
1. The value of the first element in arr must be 1.
2. The absolute difference between any two adjacent elements must be less than or equal to 1. In other words, abs(arr[i] - arr[i - 1]) <= 1 for each i where 1 <= i < arr.length (0-indexed). abs(x) is the absolute value of x.
There are 2 types of operations that you can perform any number of times:
1. Decrease the value of any element of arr to a smaller positive integer.
2. Rearrange the elements of arr to be in any order.
Return the maximum possible value of an element in arr after performing the operations to satisfy the conditions.
Example 1
Input: arr = [2,2,1,2,1]
Output: 2
Explanation: We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1]. The largest element in arr is 2.
Example 2
Input: arr = [100,1,1000]
Output: 3
Explanation: One possible way to satisfy the conditions is by doing the following:
1. Rearrange arr so it becomes [1,100,1000].
2. Decrease the value of the second element to 2.
3. Decrease the value of the third element to 3.
Now arr = [1,2,3], which satisfies the conditions. The largest element in arr is 3.
Example 3
Input: arr = [1,2,3,4,5]
Output: 5
Explanation: The array already satisfies the conditions, and the largest element is 5.
Constraints
1 <= arr.length <= 10^5
1 <= arr[i] <= 10^9
[Problem url](https://leetcode.com/problems/find-unique-binary-string/?envType=daily-question&envId=2023-11-16)
Given an array of strings
Example 1:
Example 2:
Example 3:
Constraints:
-
-
-
-
- All the strings of
Given an array of strings
nums
containing n
unique binary strings each of length n
, return a binary string of length n
that does not appear in nums
. If there are multiple answers, you may return any of them.Example 1:
Input: nums = ["01","10"]
Output: "11"
Example 2:
Input: nums = ["00","01"]
Output: "11"
Example 3:
Input: nums = ["111","011","001"]
Output: "101"
Constraints:
-
n == nums.length
-
1 <= n <= 16
-
nums[i].length == n
-
nums[i]
is either '0' or '1'.- All the strings of
nums
are unique.Problem Description
Given an array
- Each element of nums is in exactly one pair, and
- The maximum pair sum is minimized.
Return the minimized maximum pair sum after optimally pairing up the elements.
Example 1:
Example 2:
Constraints:
- n == nums.length
- 2 <= n <= 10^5
- n is even.
- 1 <= nums[i] <= 10^5
*Source: [LeetCode](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/?envType=daily-question&envId=2023-11-17)*
Given an array
nums
of even length n
, pair up the elements of nums into n / 2 pairs such that:- Each element of nums is in exactly one pair, and
- The maximum pair sum is minimized.
Return the minimized maximum pair sum after optimally pairing up the elements.
Example 1:
Input: nums = [3,5,2,3]
Output: 7
Explanation: The elements can be paired up into pairs (3,3) and (5,2).
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
Example 2:
Input: nums = [3,5,4,2,4,6]
Output: 8
Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
Constraints:
- n == nums.length
- 2 <= n <= 10^5
- n is even.
- 1 <= nums[i] <= 10^5
*Source: [LeetCode](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/?envType=daily-question&envId=2023-11-17)*
Problem url: [Frequency of the Most Frequent Element - LeetCode](https://leetcode.com/problems/frequency-of-the-most-frequent-element/?envType=daily-question&envId=2023-11-18)
Problem statement:
- Given an integer array
- The frequency of an element is the number of times it occurs in the array.
- In one operation, you can choose an index of
- Return the maximum possible frequency of an element after performing at most
Example 1:
- Input:
- Output:
- Explanation: Increment the first element three times and the second element two times to make
Example 2:
- Input:
- Output:
- Explanation: There are multiple optimal solutions:
- Increment the first element three times to make
- Increment the second element four times to make
- Increment the third element five times to make
Example 3:
- Input:
- Output:
Constraints:
-
-
-
Problem statement:
- Given an integer array
nums
and an integer k
.- The frequency of an element is the number of times it occurs in the array.
- In one operation, you can choose an index of
nums
and increment the element at that index by 1.- Return the maximum possible frequency of an element after performing at most
k
operations.Example 1:
- Input:
nums = [1,2,4], k = 5
- Output:
3
- Explanation: Increment the first element three times and the second element two times to make
nums = [4,4,4]
. The element 4
has a frequency of 3
.Example 2:
- Input:
nums = [1,4,8,13], k = 5
- Output:
2
- Explanation: There are multiple optimal solutions:
- Increment the first element three times to make
nums = [4,4,8,13]
. The element 4
has a frequency of 2
.- Increment the second element four times to make
nums = [1,8,8,13]
. The element 8
has a frequency of 2
.- Increment the third element five times to make
nums = [1,4,13,13]
. The element 13
has a frequency of 2
.Example 3:
- Input:
nums = [3,9,6], k = 2
- Output:
1
Constraints:
-
1 <= nums.length <= 10^5
-
1 <= nums[i] <= 10^5
-
1 <= k <= 10^5
[Problem URL](https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal/?envType=daily-question&envId=2023-11-19)
Given an integer array nums, goal is to make all elements in nums equal.
To complete one operation, follow these steps:
1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
2. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
3. Reduce nums[i] to nextLargest.
4. Return the number of operations to make all elements in nums equal.
Example 1:
Input: nums = [5,1,3]
Output: 3
Explanation: It takes 3 operations to make all elements in nums equal:
1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
Example 2:
Input: nums = [1,1,1]
Output: 0
Explanation: All elements in nums are already equal.
Example 3:
Input: nums = [1,1,2,2,3]
Output: 4
Explanation: It takes 4 operations to make all elements in nums equal:
1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 5 * 10^4
Given an integer array nums, goal is to make all elements in nums equal.
To complete one operation, follow these steps:
1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
2. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
3. Reduce nums[i] to nextLargest.
4. Return the number of operations to make all elements in nums equal.
Example 1:
Input: nums = [5,1,3]
Output: 3
Explanation: It takes 3 operations to make all elements in nums equal:
1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
Example 2:
Input: nums = [1,1,1]
Output: 0
Explanation: All elements in nums are already equal.
Example 3:
Input: nums = [1,1,2,2,3]
Output: 4
Explanation: It takes 4 operations to make all elements in nums equal:
1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 5 * 10^4
Problem Description
You are given an array
You are also given an array
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order. However, they do not need to visit every house.
Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.
Return the minimum number of minutes needed to pick up all the garbage.
Examples:
Example 1:
Example 2:
Constraints:
- 2 ≤
-
- 1 ≤
-
- 1 ≤
You are given an array
garbage
representing the assortment of garbage at each house. Each element in garbage
consists of characters 'M', 'P', and 'G' representing one unit of metal, paper, and glass garbage, respectively. Picking up one unit of any type of garbage takes 1 minute.You are also given an array
travel
representing the number of minutes needed to go from each house to the next.There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order. However, they do not need to visit every house.
Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.
Return the minimum number of minutes needed to pick up all the garbage.
Examples:
Example 1:
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
Output: 21
Explanation:
The paper garbage truck:
1. Travels from house 0 to house 1
2. Collects the paper garbage at house 1
3. Travels from house 1 to house 2
4. Collects the paper garbage at house 2
Altogether, it takes 8 minutes to pick up all the paper garbage.
The glass garbage truck:
1. Collects the glass garbage at house 0
2. Travels from house 0 to house 1
3. Travels from house 1 to house 2
4. Collects the glass garbage at house 2
5. Travels from house 2 to house 3
6. Collects the glass garbage at house 3
Altogether, it takes 13 minutes to pick up all the glass garbage.
Since there is no metal garbage, we do not need to consider the metal garbage truck.
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
Example 2:
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
Output: 37
Explanation:
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
Constraints:
- 2 ≤
garbage.length
≤ 10^5-
garbage[i]
consists of only the letters 'M', 'P', and 'G'- 1 ≤
garbage[i].length
≤ 10-
travel.length
= garbage.length
- 1- 1 ≤
travel[i]
≤ 100[Problem Statement](https://leetcode.com/problems/count-nice-pairs-in-an-array/?envType=daily-question&envId=2023-11-21)
You are given an array
-
-
Return the number of nice pairs of indices. Since that number can be too large, return it modulo
Example 1:
*Input:*
*Output:*
*Explanation:* The two pairs are:
-
-
Example 2:
*Input:*
*Output:*
Constraints:
-
-
You are given an array
nums
that consists of non-negative integers. Let us define rev(x)
as the reverse of the non-negative integer x
. For example, rev(123) = 321
, and rev(120) = 21
. A pair of indices (i, j)
is nice if it satisfies all of the following conditions:-
0 <= i < j < nums.length
-
nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
Return the number of nice pairs of indices. Since that number can be too large, return it modulo
10^9 + 7
.Example 1:
*Input:*
nums = [42,11,1,97]
*Output:*
2
*Explanation:* The two pairs are:
-
(0,3)
: 42 + rev(97) = 42 + 79 = 121
, 97 + rev(42) = 97 + 24 = 121
.-
(1,2)
: 11 + rev(1) = 11 + 1 = 12
, 1 + rev(11) = 1 + 11 = 12
.Example 2:
*Input:*
nums = [13,10,35,24,76]
*Output:*
4
Constraints:
-
1 <= nums.length <= 10^5
-
0 <= nums[i] <= 10^9
Problem url: [https://leetcode.com/problems/diagonal-traverse-ii/?envType=daily-question&envId=2023-11-22](https://leetcode.com/problems/diagonal-traverse-ii/?envType=daily-question&envId=2023-11-22)
Text:
Given a 2D integer array
Example 1:
Input:
Output:
Example 2:
Input:
Output:
Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i].length <= 10^5
- 1 <= sum(nums[i].length) <= 10^5
- 1 <= nums[i][j] <= 10^5
Text:
Given a 2D integer array
nums
, return all elements of nums
in diagonal order as shown in the below images.Example 1:
Input:
nums = [[1,2,3],[4,5,6],[7,8,9]]
Output:
[1,4,2,7,5,3,8,6,9]
Example 2:
Input:
nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output:
[1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i].length <= 10^5
- 1 <= sum(nums[i].length) <= 10^5
- 1 <= nums[i][j] <= 10^5
Problem url: [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/?envType=daily-question&envId=2023-11-23)
Text:
A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
For example, these are arithmetic sequences:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
The following sequence is not arithmetic:
1, 1, 2, 5, 7
You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
Example 1:
Input:
Output:
Explanation:
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.
Example 2:
Input:
Output:
Constraints:
- n == nums.length
- m == l.length
- m == r.length
- 2 <= n <= 500
- 1 <= m <= 500
- 0 <= l[i] < r[i] < n
- -105 <= nums[i] <= 10^5
Text:
A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
For example, these are arithmetic sequences:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
The following sequence is not arithmetic:
1, 1, 2, 5, 7
You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
Example 1:
Input:
nums = [4,6,5,9,3,7],
l = [0,0,2],
r = [2,3,5]
Output:
[true,false,true]
Explanation:
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.
Example 2:
Input:
nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10],
l = [0,1,6,4,8,7],
r = [4,4,9,7,9,10]
Output:
[false,true,false,false,true,true]
Constraints:
- n == nums.length
- m == l.length
- m == r.length
- 2 <= n <= 500
- 1 <= m <= 500
- 0 <= l[i] < r[i] < n
- -105 <= nums[i] <= 10^5
# Problem Statement
Problem URL: [Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/?envType=daily-question&envId=2023-11-24)
There are 3n piles of coins of varying size. You and your friends will take piles of coins as follows:
- In each step, you will choose any 3 piles of coins (not necessarily consecutive).
- Of your choice, Alice will pick the pile with the maximum number of coins.
- You will pick the next pile with the maximum number of coins.
- Your friend Bob will pick the last pile.
- Repeat this process until there are no more piles of coins.
Given an array of integers
## Example 1:
Input:
Output: 9
Explanation:
- Choose the triplet (2, 7, 8), Alice picks the pile with 8 coins, you pick the pile with 7 coins, and Bob picks the last one.
- Choose the triplet (1, 2, 4), Alice picks the pile with 4 coins, you pick the pile with 2 coins, and Bob picks the last one.
- The maximum number of coins which you can have are: 7 + 2 = 9.
On the other hand, if we choose the arrangement (1, 2, 8), (2, 4, 7), you only get 2 + 4 = 6 coins, which is not optimal.
## Example 2:
Input:
Output: 4
## Example 3:
Input:
Output: 18
## Constraints:
-
-
-
Problem URL: [Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/?envType=daily-question&envId=2023-11-24)
There are 3n piles of coins of varying size. You and your friends will take piles of coins as follows:
- In each step, you will choose any 3 piles of coins (not necessarily consecutive).
- Of your choice, Alice will pick the pile with the maximum number of coins.
- You will pick the next pile with the maximum number of coins.
- Your friend Bob will pick the last pile.
- Repeat this process until there are no more piles of coins.
Given an array of integers
piles
where piles[i]
is the number of coins in the i-th
pile, return the maximum number of coins that you can have.## Example 1:
Input:
piles = [2,4,1,2,7,8]
Output: 9
Explanation:
- Choose the triplet (2, 7, 8), Alice picks the pile with 8 coins, you pick the pile with 7 coins, and Bob picks the last one.
- Choose the triplet (1, 2, 4), Alice picks the pile with 4 coins, you pick the pile with 2 coins, and Bob picks the last one.
- The maximum number of coins which you can have are: 7 + 2 = 9.
On the other hand, if we choose the arrangement (1, 2, 8), (2, 4, 7), you only get 2 + 4 = 6 coins, which is not optimal.
## Example 2:
Input:
piles = [2,4,5]
Output: 4
## Example 3:
Input:
piles = [9,8,7,6,5,1,2,3,4]
Output: 18
## Constraints:
-
3 <= piles.length <= 10^5
-
piles.length % 3 == 0
-
1 <= piles[i] <= 10^4
[Problem](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/?envType=daily-question&envId=2023-11-25)
You are given an integer array
Example 1:
Input:
Output:
Explanation: Assuming the arrays are 0-indexed, then
-
-
-
Example 2:
Input:
Output:
Constraints:
-
-
You are given an integer array
nums
sorted in non-decreasing order. Build and return an integer array result
with the same length as nums
such that result[i]
is equal to the summation of absolute differences between nums[i]
and all the other elements in the array. In other words, result[i]
is equal to sum(|nums[i]-nums[j]|)
where 0 <= j < nums.length
and j != i
(0-indexed).Example 1:
Input:
nums = [2,3,5]
Output:
[4,3,5]
Explanation: Assuming the arrays are 0-indexed, then
-
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4
-
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3
-
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5
Example 2:
Input:
nums = [1,4,6,8,10]
Output:
[24,15,13,15,21]
Constraints:
-
2 <= nums.length <= 10^5
-
1 <= nums[i] <= nums[i + 1] <= 10^4
#### Problem Statement
Given a binary matrix
#### Example 1:
Input:
Output:
Explanation: You can rearrange the columns as shown above. The largest submatrix of
#### Example 2:
Input:
Output:
Explanation: You can rearrange the columns as shown above. The largest submatrix of
#### Example 3:
Input:
Output:
Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of
#### Constraints:
-
-
-
-
Given a binary matrix
matrix
of size m x n
, you are allowed to rearrange the columns of the matrix in any order. Return the area of the largest submatrix within matrix
where every element of the submatrix is 1
after reordering the columns optimally.#### Example 1:
Input:
matrix = [[0,0,1],[1,1,1],[1,0,1]]
Output:
4
Explanation: You can rearrange the columns as shown above. The largest submatrix of
1s
, in bold, has an area of 4
.#### Example 2:
Input:
matrix = [[1,0,1,0,1]]
Output:
3
Explanation: You can rearrange the columns as shown above. The largest submatrix of
1s
, in bold, has an area of 3
.#### Example 3:
Input:
matrix = [[1,1,0],[1,0,1]]
Output:
2
Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of
1s
larger than an area of 2
.#### Constraints:
-
m == matrix.length
-
n == matrix[i].length
-
1 <= m * n <= 10^5
-
matrix[i][j]
is either 0
or 1
.