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
.Problem URL: [Knight Dialer](https://leetcode.com/problems/knight-dialer/?envType=daily-question&envId=2023-11-27)
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 diagram.
A chess knight can move as indicated in the chess diagram below.
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).
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 109 + 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 numbers 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 <= 5000
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 diagram.
A chess knight can move as indicated in the chess diagram below.
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).
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 109 + 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 numbers 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 <= 5000
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 109 + 7. If there is no way, return 0.
Example 1:
- 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:
- 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:
- 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'.
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 109 + 7. If there is no way, return 0.
Example 1:
- 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:
- 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:
- 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'.
### Problem Description
Problem url: [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/?envType=daily-question&envId=2023-11-29)
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.
### Examples
#### Example 1:
#### Example 2:
#### Example 3:
### 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?
Problem url: [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/?envType=daily-question&envId=2023-11-29)
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.
### Examples
#### 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?
[Problem URL](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/?envType=daily-question&envId=2023-11-30)
Given an integer n, transform it into 0 using the following operations:
- Change the rightmost (0th) bit in the binary representation of n.
- Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th 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 2nd operation since the 0th bit is 1.
- "01" -> "00" with the 1st operation.
#### Example 2:
Input: n = 6
Output: 4
Explanation:
The binary representation of 6 is "110".
- "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
- "010" -> "011" with the 1st operation.
- "011" -> "001" with the 2nd operation since the 0th bit is 1.
- "001" -> "000" with the 1st operation.
#### Constraints:
0 <= n <= 10^9
Given an integer n, transform it into 0 using the following operations:
- Change the rightmost (0th) bit in the binary representation of n.
- Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th 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 2nd operation since the 0th bit is 1.
- "01" -> "00" with the 1st operation.
#### Example 2:
Input: n = 6
Output: 4
Explanation:
The binary representation of 6 is "110".
- "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
- "010" -> "011" with the 1st operation.
- "011" -> "001" with the 2nd operation since the 0th bit is 1.
- "001" -> "000" with the 1st operation.
#### Constraints:
0 <= n <= 10^9
Problem URL: [Check if Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/?envType=daily-question&envId=2023-12-01)
Text:
Given two string arrays
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input:
-
-
Output:
Explanation:
-
-
- The strings are the same, so return
Example 2:
Input:
-
-
Output:
Example 3:
Input:
-
-
Output:
Constraints:
- 1 <=
- 1 <=
- 1 <= sum(
-
Text:
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.Today is a first day of https://adventofcode.com/2023/day/1, great opportunity to learn some new language. I will be solving it in Rust https://github.com/samoylenkodmitry/AdventOfCode_2023
GitHub
GitHub - samoylenkodmitry/AdventOfCode_2023: my advent of code 2023 solutions
my advent of code 2023 solutions. Contribute to samoylenkodmitry/AdventOfCode_2023 development by creating an account on GitHub.
#### Problem
We are given an array of strings
#### Example 1
- Input:
-
-
- Output:
- Explanation: The "good" strings that can be formed are "cat" and "hat", so the answer is
#### Example 2
- Input:
-
-
- Output:
- Explanation: The "good" strings that can be formed are "hello" and "world", so the answer is
#### Constraints
-
-
-
#### Problem URL
[Problem URL](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/?envType=daily-question&envId=2023-12-02)
We are given an array of strings
words
and a string chars
. A string is considered "good" if it can be formed using characters from chars
, with each character used only once. We need to find the sum of the lengths of all "good" strings in words
.#### Example 1
- Input:
-
words = ["cat","bt","hat","tree"]
-
chars = "atach"
- Output:
6
- Explanation: The "good" 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 "good" 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.#### Problem URL
[Problem URL](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/?envType=daily-question&envId=2023-12-02)