2023-11-20
2391. Minimum Amount of Time to Collect Garbage
Topic: Array, String, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed array of strings
You are also given a 0-indexed integer array
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at 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.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2391. Minimum Amount of Time to Collect Garbage
Topic: Array, String, Prefix Sum
Difficulty: Medium
Problem:
You are given a 0-indexed array of strings
garbage where garbage[i] represents the assortment of garbage at the i^th house. garbage[i] consists only of the 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 a 0-indexed integer array
travel where travel[i] is the number of minutes needed to go from house i to house i + 1.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.
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] <= 1002023-11-21
1814. Count Nice Pairs in an Array
Topic: Array, Hash Table, Math, Counting
Difficulty: Medium
Problem:
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:
Example 2:
Constraints:
•
•
1814. Count Nice Pairs in an Array
Topic: Array, Hash Table, Math, Counting
Difficulty: Medium
Problem:
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^92023-11-22
1424. Diagonal Traverse II
Topic: Array, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
Given a 2D integer array
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png
Constraints:
•
•
•
•
1424. Diagonal Traverse II
Topic: Array, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
Given a 2D integer array
nums, return all elements of nums in diagonal order as shown in the below images.Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png
Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,4,2,7,5,3,8,6,9]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png
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^52023-11-23
1630. Arithmetic Subarrays
Topic: Array, Sorting
Difficulty: Medium
Problem:
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
For example, these are arithmetic sequences:
The following sequence is not arithmetic:
You are given an array of
Return a list of
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
•
1630. Arithmetic Subarrays
Topic: Array, Sorting
Difficulty: Medium
Problem:
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 i^th 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 0^th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1^st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2^nd 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•
-10^5 <= nums[i] <= 10^52023-11-24
1561. Maximum Number of Coins You Can Get
Topic: Array, Math, Greedy, Sorting, Game Theory
Difficulty: Medium
Problem:
There are
• In each step, you will choose any
• 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 until there are no more piles of coins.
Given an array of integers
Return the maximum number of coins that you can have.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1561. Maximum Number of Coins You Can Get
Topic: Array, Math, Greedy, Sorting, Game Theory
Difficulty: Medium
Problem:
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 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 Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one.
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one.
The maximum number of coins which you can have are: 7 + 2 = 9.
On the other hand if we choose this 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^42023-11-25
1685. Sum of Absolute Differences in a Sorted Array
Topic: Array, Math, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
Build and return an integer array
In other words,
Example 1:
Example 2:
Constraints:
•
•
1685. Sum of Absolute Differences in a Sorted Array
Topic: Array, Math, Prefix Sum
Difficulty: Medium
Problem:
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^42023-11-26
1727. Largest Submatrix With Rearrangements
Topic: Array, Greedy, Sorting, Matrix
Difficulty: Medium
Problem:
You are given a binary matrix
Return the area of the largest submatrix within
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40536-pm.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40852-pm.png
Example 3:
Constraints:
•
•
•
•
1727. Largest Submatrix With Rearrangements
Topic: Array, Greedy, Sorting, Matrix
Difficulty: Medium
Problem:
You are given a binary matrix
matrix of size m x n, and 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:
Image: https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40536-pm.png
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:
Image: https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40852-pm.png
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.2023-11-27
935. Knight Dialer
Topic: Dynamic Programming
Difficulty: Medium
Problem:
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
Image: https://assets.leetcode.com/uploads/2020/08/18/chess.jpg
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Image: https://assets.leetcode.com/uploads/2020/08/18/phone.jpg
Given an integer
You are allowed to place the knight on any numeric cell initially and then you should perform
As the answer may be very large, return the answer modulo
Example 1:
Example 2:
Example 3:
Constraints:
•
935. Knight Dialer
Topic: Dynamic Programming
Difficulty: Medium
Problem:
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
Image: https://assets.leetcode.com/uploads/2020/08/18/chess.jpg
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Image: https://assets.leetcode.com/uploads/2020/08/18/phone.jpg
Given an integer
n, return how many distinct phone numbers of length n we can dial.You are allowed to place the knight on any numeric cell initially and then you should perform
n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.As the answer may be very large, return the answer modulo
10^9 + 7.Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
•
1 <= n <= 50002023-11-28
2147. Number of Ways to Divide a Long Corridor
Topic: Math, String, Dynamic Programming
Difficulty: Hard
Problem:
Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string
One room divider has already been installed to the left of index
Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.
Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/04/1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/04/2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/12/3.png
Constraints:
•
•
•
2147. Number of Ways to Divide a Long Corridor
Topic: Math, String, Dynamic Programming
Difficulty: Hard
Problem:
Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string
corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.One room divider has already been installed to the left of index
0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.
Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo
10^9 + 7. If there is no way, return 0.Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/04/1.png
Input: corridor = "SSPPSPS"
Output: 3
Explanation: There are 3 different ways to divide the corridor.
The black bars in the above image indicate the two room dividers already installed.
Note that in each of the ways, each section has exactly two seats.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/04/2.png
Input: corridor = "PPSPSP"
Output: 1
Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
Installing any would create some section that does not have exactly two seats.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/12/12/3.png
Input: corridor = "S"
Output: 0
Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
Constraints:
•
n == corridor.length•
1 <= n <= 10^5•
corridor[i] is either 'S' or 'P'.2023-11-29
191. Number of 1 Bits
Topic: Divide and Conquer, Bit Manipulation
Difficulty: Easy
Problem:
Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Note:
• Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
• In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer.
Example 1:
Example 2:
Example 3:
Constraints:
• The input must be a binary string of length
Follow up: If this function is called many times, how would you optimize it?
191. Number of 1 Bits
Topic: Divide and Conquer, Bit Manipulation
Difficulty: Easy
Problem:
Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Note:
• Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
• In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer.
-3.Example 1:
Input: n = 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
Input: n = 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
Input: n = 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
Constraints:
• The input must be a binary string of length
32.Follow up: If this function is called many times, how would you optimize it?
2023-11-30
1611. Minimum One Bit Operations to Make Integers Zero
Topic: Dynamic Programming, Bit Manipulation, Memoization
Difficulty: Hard
Problem:
Given an integer
• Change the rightmost (
• Change the
Return the minimum number of operations to transform
Example 1:
Example 2:
Constraints:
•
1611. Minimum One Bit Operations to Make Integers Zero
Topic: Dynamic Programming, Bit Manipulation, Memoization
Difficulty: Hard
Problem:
Given an integer
n, you must transform it into 0 using the following operations any number of times:• Change the rightmost (
0^th) bit in the binary representation of n.• Change the
i^th bit in the binary representation of n if the (i-1)^th bit is set to 1 and the (i-2)^th through 0^th bits are set to 0.Return the minimum number of operations to transform
n into 0.Example 1:
Input: n = 3
Output: 2
Explanation: The binary representation of 3 is "11".
"11" -> "01" with the 2^nd operation since the 0^th bit is 1.
"01" -> "00" with the 1^st operation.
Example 2:
Input: n = 6
Output: 4
Explanation: The binary representation of 6 is "110".
"110" -> "010" with the 2^nd operation since the 1^st bit is 1 and 0^th through 0^th bits are 0.
"010" -> "011" with the 1^st operation.
"011" -> "001" with the 2^nd operation since the 0^th bit is 1.
"001" -> "000" with the 1^st operation.
Constraints:
•
0 <= n <= 10^92023-12-01
1662. Check If Two String Arrays are Equivalent
Topic: Array, String
Difficulty: Easy
Problem:
Given two string arrays
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1662. Check If Two String Arrays are Equivalent
Topic: Array, String
Difficulty: Easy
Problem:
Given two string arrays
word1 and word2, return true if the two arrays represent the same string, and false otherwise.A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.
Example 2:
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false
Example 3:
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true
Constraints:
•
1 <= word1.length, word2.length <= 10^3•
1 <= word1[i].length, word2[i].length <= 10^3•
1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3•
word1[i] and word2[i] consist of lowercase letters.2023-12-02
1160. Find Words That Can Be Formed by Characters
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
You are given an array of strings
A string is good if it can be formed by characters from chars (each character can only be used once).
Return the sum of lengths of all good strings in words.
Example 1:
Example 2:
Constraints:
•
•
•
1160. Find Words That Can Be Formed by Characters
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
You are given an array of strings
words and a string chars.A string is good if it can be formed by characters from chars (each character can only be used once).
Return the sum of lengths of all good strings in words.
Example 1:
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
Example 2:
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
Constraints:
•
1 <= words.length <= 1000•
1 <= words[i].length, chars.length <= 100•
words[i] and chars consist of lowercase English letters.2023-12-03
1266. Minimum Time Visiting All Points
Topic: Array, Math, Geometry
Difficulty: Easy
Problem:
On a 2D plane, there are
You can move according to these rules:
• In
• move vertically by one unit,
• move horizontally by one unit, or
• move diagonally
• You have to visit the points in the same order as they appear in the array.
• You are allowed to pass through points that appear later in the order, but these do not count as visits.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/14/1626_example_1.PNG
Example 2:
Constraints:
•
•
•
•
1266. Minimum Time Visiting All Points
Topic: Array, Math, Geometry
Difficulty: Easy
Problem:
On a 2D plane, there are
n points with integer coordinates points[i] = [x_i, y_i]. Return the minimum time in seconds to visit all the points in the order given by points.You can move according to these rules:
• In
1 second, you can either:• move vertically by one unit,
• move horizontally by one unit, or
• move diagonally
sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).• You have to visit the points in the same order as they appear in the array.
• You are allowed to pass through points that appear later in the order, but these do not count as visits.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/14/1626_example_1.PNG
Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds
Example 2:
Input: points = [[3,2],[-2,2]]
Output: 5
Constraints:
•
points.length == n•
1 <= n <= 100•
points[i].length == 2•
-1000 <= points[i][0], points[i][1] <= 10002023-12-04
2264. Largest 3-Same-Digit Number in String
Topic: String
Difficulty: Easy
Problem:
You are given a string
• It is a substring of
• It consists of only one unique digit.
Return the maximum good integer as a string or an empty string
Note:
• A substring is a contiguous sequence of characters within a string.
• There may be leading zeroes in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2264. Largest 3-Same-Digit Number in String
Topic: String
Difficulty: Easy
Problem:
You are given a string
num representing a large integer. An integer is good if it meets the following conditions:• It is a substring of
num with length 3.• It consists of only one unique digit.
Return the maximum good integer as a string or an empty string
"" if no such integer exists.Note:
• A substring is a contiguous sequence of characters within a string.
• There may be leading zeroes in
num or a good integer.Example 1:
Input: num = "6777133339"
Output: "777"
Explanation: There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
Example 2:
Input: num = "2300019"
Output: "000"
Explanation: "000" is the only good integer.
Example 3:
Input: num = "42352338"
Output: ""
Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
Constraints:
•
3 <= num.length <= 1000•
num only consists of digits.2023-12-05
1688. Count of Matches in Tournament
Topic: Math, Simulation
Difficulty: Easy
Problem:
You are given an integer
• If the current number of teams is even, each team gets paired with another team. A total of
• If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of
Return the number of matches played in the tournament until a winner is decided.
Example 1:
Example 2:
Constraints:
•
1688. Count of Matches in Tournament
Topic: Math, Simulation
Difficulty: Easy
Problem:
You are given an integer
n, the number of teams in a tournament that has strange rules:• If the current number of teams is even, each team gets paired with another team. A total of
n / 2 matches are played, and n / 2 teams advance to the next round.• If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of
(n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.Return the number of matches played in the tournament until a winner is decided.
Example 1:
Input: n = 7
Output: 6
Explanation: Details of the tournament:
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 3 + 2 + 1 = 6.
Example 2:
Input: n = 14
Output: 13
Explanation: Details of the tournament:
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 7 + 3 + 2 + 1 = 13.
Constraints:
•
1 <= n <= 200Leetcode Question of Today pinned «End of Maintenance Notice Dear subscribers, I regret to inform you that I have made the decision to discontinue the maintenance of this bot. This means that I will no longer provide updates on LeetCode's "Question of Today". The decision is primarily based…»
2023-12-06
1716. Calculate Money in Leetcode Bank
Topic: Math
Difficulty: Easy
Problem:
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
He starts by putting in
Given
Example 1:
Example 2:
Example 3:
Constraints:
•
1716. Calculate Money in Leetcode Bank
Topic: Math
Difficulty: Easy
Problem:
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
He starts by putting in
$1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday. Given
n, return the total amount of money he will have in the Leetcode bank at the end of the n^th day.Example 1:
Input: n = 4
Output: 10
Explanation: After the 4^th day, the total is 1 + 2 + 3 + 4 = 10.
Example 2:
Input: n = 10
Output: 37
Explanation: After the 10^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2^nd Monday, Hercy only puts in $2.
Example 3:
Input: n = 20
Output: 96
Explanation: After the 20^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
Constraints:
•
1 <= n <= 10002023-12-07
1903. Largest Odd Number in String
Topic: Math, String, Greedy
Difficulty: Easy
Problem:
You are given a string
A substring is a contiguous sequence of characters within a string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1903. Largest Odd Number in String
Topic: Math, String, Greedy
Difficulty: Easy
Problem:
You are given a string
num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.A substring is a contiguous sequence of characters within a string.
Example 1:
Input: num = "52"
Output: "5"
Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
Example 2:
Input: num = "4206"
Output: ""
Explanation: There are no odd numbers in "4206".
Example 3:
Input: num = "35427"
Output: "35427"
Explanation: "35427" is already an odd number.
Constraints:
•
1 <= num.length <= 10^5•
num only consists of digits and does not contain any leading zeros.2023-12-08
606. Construct String from Binary Tree
Topic: String, Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/cons1-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/03/cons2-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
•
606. Construct String from Binary Tree
Topic: String, Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/cons1-tree.jpg
Input: root = [1,2,3,4]
Output: "1(2(4))(3)"
Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/03/cons2-tree.jpg
Input: root = [1,2,3,null,4]
Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-1000 <= Node.val <= 1000