Skip to content

Commit 8ff920e

Browse files
committed
feat: add solutions to lc problems: No.2585~2597,2602
1 parent e2c10b7 commit 8ff920e

File tree

12 files changed

+147
-0
lines changed

12 files changed

+147
-0
lines changed

solution/2500-2599/2585.Number of Ways to Earn Points/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@
6666

6767
## Solutions
6868

69+
**Approach 1: Dynamic Programming**
70+
71+
We define $f[i][j]$ to represent the number of methods to get $j$ points exactly from the first $i$ types of questions. Initially, $f[0][0] = 1$, and the rest $f[i][j] = 0$. The answer is $f[n][target]$.
72+
73+
We can enumerate the $i$th type of questions, suppose the number of questions of this type is $count$, and the score is $marks$. Then we can get the following state transition equation:
74+
75+
$$
76+
f[i][j] = \sum_{k=0}^{count} f[i-1][j-k \times marks]
77+
$$
78+
79+
where $k$ represents the number of questions of the $i$th type.
80+
81+
The final answer is $f[n][target]$. Note that the answer may be very large and needs to be modulo $10^9 + 7$.
82+
83+
The time complexity is $O(n \times target \times count)$ and the space complexity is $O(n \times target)$. $n$ is the number of types of questions, and $target$ and $count$ are the target score and the number of questions of each type, respectively.
84+
6985
<!-- tabs:start -->
7086

7187
### **Python3**

solution/2500-2599/2586.Count the Number of Vowel Strings in Range/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ The number of vowel strings in the mentioned range is 3.
4848

4949
## Solutions
5050

51+
**Approach 1: Simulation**
52+
53+
We just need to traverse the string in the interval $[left,.. right]$, and check if it starts and ends with a vowel. If so, the answer plus one.
54+
55+
After the traversal, return the answer.
56+
57+
The time complexity is $O(m)$, and the space complexity is $O(1)$. Where $m = right - left + 1$.
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**

solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@
5252

5353
## Solutions
5454

55+
**Approach 1: Prefix XOR + Hash Table**
56+
57+
We observe that a subarray can become an array of all $0$ s if and only if the number of $1$s in each bit of all the elements in the subarray is even.
58+
59+
If there are indices $i$ and $j$ such that $i \lt j$ and the number of $1$s in each bit of the subarray $nums[0,..,i]$ and $nums[0,..,j]$ is the same, then we can make the subarray $nums[i + 1,..,j]$ an array of all $0$ s.
60+
61+
Therefore, we can use the prefix XOR method and use the hash table $cnt$ to count the number of occurrences of each prefix XOR value. Traverse the array, for each element $x$, calculate the prefix XOR value $mask$, then add the number of occurrences of $mask$ to the answer. Then, add $1$ to the number of occurrences of $mask$.
62+
63+
Finally, return the answer.
64+
65+
Time complexity $O(n)$, space complexity $O(n)$, where $n$ is the length of the array $nums$.
66+
5567
<!-- tabs:start -->
5668

5769
### **Python3**

solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ The computer will be on for a total of 4 seconds.
4747

4848
## Solutions
4949

50+
**Approach 1: Greedy + Sort**
51+
52+
We find that the problem is equivalent to choosing $duration$ integer time points in each interval $[start,..,end]$ so that the total number of integer time points selected is the smallest.
53+
54+
Therefore, we can sort the $tasks$ by the end time $end$ from small to large. Then greedily select. For each task, we start from the end time $end$ and choose as many points as possible from back to front, so that these points are more likely to be reused by later tasks.
55+
56+
In implementation, we can use a length of $2010$ array $vis$ to record whether each time point has been selected. Then for each task, we first count the number of points that have been selected in the $[start,..,end]$ interval $cnt$, then choose $duration - cnt$ points from back to front, and record the number of points selected $ans$ and update the $vis$ array.
57+
58+
Finally, we return $ans$.
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**

solution/2500-2599/2590.Design a Todo List/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ todoList.getAllTasks(1); // return [&quot;Task3&quot;, &quot;Task1&quot;]. User
5757

5858
## Solutions
5959

60+
**Approach 1: Hash Table + Sorted Set**
61+
62+
We use a hash table $tasks$ to record the set of tasks for each user, where the key is the user ID and the value is a sorted set sorted by the deadline of the task. In addition, we use a variable $i$ to record the current task ID.
63+
64+
When calling the `addTask` method, we add the task to the task set of the corresponding user and return the task ID. The time complexity of this operation is $O(\log n)$.
65+
66+
When calling the `getAllTasks` method, we traverse the task set of the corresponding user and add the description of the unfinished task to the result list, and then return the result list. The time complexity of this operation is $O(n)$.
67+
68+
When calling the `getTasksForTag` method, we traverse the task set of the corresponding user and add the description of the unfinished task to the result list, and then return the result list. The time complexity of this operation is $O(n)$.
69+
70+
When calling the `completeTask` method, we traverse the task set of the corresponding user and mark the task whose task ID is $taskId$ as completed. The time complexity of this operation is $(n)$.
71+
72+
The space complexity is $O(n)$. Where $n$ is the number of all tasks.
73+
6074
<!-- tabs:start -->
6175

6276
### **Python3**

solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ It can be proven that no distribution exists such that number of children gettin
4848

4949
## Solutions
5050

51+
**Approach 1: Case analysis**
52+
53+
If $money \lt children$, then there must be a child who did not receive money, return $-1$.
54+
55+
If $money \gt 8 \times children$, then there are $children-1$ children who received $8$ dollars, and the remaining child received $money - 8 \times (children-1)$ dollars, return $children-1$.
56+
57+
If $money = 8 \times children - 4$, then there are $children-2$ children who received $8$ dollars, and the remaining two children shared the remaining $12$ dollars (as long as it is not $4$, $8$ dollars is fine), return $children-2$.
58+
59+
If we assume that there are $x$ children who received $8$ dollars, then the remaining money is $money- 8 \times x$, as long as it is greater than or equal to the number of remaining children $children-x$, it can meet the requirements. Therefore, we only need to find the maximum value of $x$, which is the answer.
60+
61+
Time complexity $O(1)$, space complexity $O(1)$.
62+
5163
<!-- tabs:start -->
5264

5365
### **Python3**

solution/2500-2599/2592.Maximize Greatness of an Array/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ At indices = 0, 1, and 2, perm[i] &gt; nums[i]. Hence, we return 3.
3838

3939
## Solutions
4040

41+
**Approach 1: Greedy**
42+
43+
We can sort the array $nums$ first.
44+
45+
Then we define a pointer $i$ pointing to the first element of the array $nums$. We traverse the array $nums$, and for each element $x$ we encounter, if $x$ is greater than $nums[i]$, then we move the pointer $i$ to the right.
46+
47+
Finally, we return the value of the pointer $i$.
48+
49+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $nums$.
50+
4151
<!-- tabs:start -->
4252

4353
### **Python3**

solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ Our score is 1 + 2 + 2 = 5.
5252

5353
## Solutions
5454

55+
**Approach 1: Priority Queue (Min Heap)**
56+
57+
We use a priority queue to maintain the unmarked elements in the array, and each item in the queue is a tuple $(x, i)$, where $x$ and $i$ represent the element value and index of the array respectively. An array $vis$ is used to record whether the element in the array is marked.
58+
59+
Each time we take out the smallest element $(x, i)$ from the queue, we add $x$ to the answer, and then mark the element at the $i$ position, and the left and right adjacent elements at the $i$ position, that is, the elements at the $i-1$ and $i+1$ positions. Then we determine whether the top element of the heap is marked. If it is marked, pop the top element of the heap until the top element is unmarked or the heap is empty.
60+
61+
Finally, return the answer.
62+
63+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array.
64+
65+
**Approach 2: Sorting**
66+
67+
We can create an index array $idx$ where $idx[i]=i$, and then we sort the index array $idx$ according to the element values in the array $nums$. If the element values are the same, then sort them according to the index values.
68+
69+
Next, create an array $vis$ of length $n+2$ where $vis[i]=false$, which means whether the element in the array is marked.
70+
71+
We traverse the index array $idx$, and for each index $i$ in the array, if $vis[i+1]$ is $false$, that is, the element at position $i$ is not marked, we add $nums[i]$ to the answer, and then mark the element at position $i$, and the left and right adjacent elements at position $i$, that is, the elements at positions $i-1$ and $i+1$. Continue to traverse the index array $idx$ until the end.
72+
73+
Finally, return the answer.
74+
75+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array.
76+
5577
<!-- tabs:start -->
5678

5779
### **Python3**

solution/2500-2599/2594.Minimum Time to Repair Cars/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ It can be proved that the cars cannot be repaired in less than 16 minutes.​​
4949

5050
## Solutions
5151

52+
**Approach 1: Binary Search**
53+
54+
We notice that the longer the repair time, the more repaired cars. Therefore, we can use binary search to find the minimum repair time.
55+
56+
We define the left and right boundaries of binary search as $left=0$, $right=ranks[0] \times cars \times cars$. Next, we enumerate the repair time $mid$ in binary search. The number of cars that each mechanic can repair is $\lfloor \sqrt{\frac{mid}{r}} \rfloor$, where $\lfloor x \rfloor$ represents the floor function. If the number of cars repaired is greater than or equal to $cars$, then the repair time $mid$ is feasible, and we shrink the right boundary to $mid$, otherwise we increase the left boundary to $mid+1$.
57+
58+
Finally, we return the left boundary.
59+
60+
Time complexity $(n \times \log n)$, space complexity $O(1)$. Where $n$ is the number of mechanics.
61+
5262
<!-- tabs:start -->
5363

5464
### **Python3**

solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ There are 0 even and 1 odd indices.
4242

4343
## Solutions
4444

45+
**Approach 1: Enumerate**
46+
47+
According to the problem description, enumerate the binary representation of $n$ from the low bit to the high bit. If the bit is $1$, add $1$ to the corresponding counter according to whether the index of the bit is odd or even.
48+
49+
The time complexity is $O(\log n)$ and the space complexity is $O(1)$. Where $n$ is the given integer.
50+
4551
<!-- tabs:start -->
4652

4753
### **Python3**

solution/2500-2599/2597.The Number of Beautiful Subsets/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ It can be proved that there is only 1 beautiful subset in the array [1].
4141

4242
## Solutions
4343

44+
# Approach 1: Counting + Backtracking
45+
46+
We use a hash table or an array $cnt$ to record the currently selected numbers and their counts, and use $ans$ to record the number of beautiful subsets, initially $ans = -1$, indicating that the empty set is excluded.
47+
48+
For each number $x$ in the array $nums$, we have two choices:
49+
50+
- Do not choose $x$, and then directly recurse to the next number;
51+
- Choose $x$, then we need to check whether $x + k$ and $x - k$ have appeared in $cnt$ before, if neither has appeared before, then we can choose $x$, at this time we add one to the number of $x$, and then recurse to the next number, and finally subtract one from the number of $x$.
52+
53+
Finally, we return $ans$.
54+
55+
Time complexity $O(2^n)$, space complexity $O(n)$, where $n$ is the length of the array $nums$.
56+
4457
<!-- tabs:start -->
4558

4659
### **Python3**

solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
5555

5656
## Solutions
5757

58+
**Approach 1: sort + prefix sum + binary search**
59+
60+
First, we sort the array $nums$ and calculate the prefix sum array $s$ with a length of $n+1$, where $s[i]$ represents the sum of the first $i$ elements in the array $nums$.
61+
62+
Then, we traverse each query $queries[i]$, we need to reduce all elements greater than $queries[i]$ to $queries[i]$, and increase all elements less than $queries[i]$ to $queries[i]$.
63+
64+
We can use binary search to find the index $i$ of the first element in the array $nums$ that is greater than $queries[i]$. There are $n-i$ elements that need to be reduced to $queries[i]$, and the sum of these elements is $s[n]-s[i]$. These elements need to be reduced by $n-i$ $queries[i]$, so the total number of operations to reduce these elements to $queries[i]$ is $s[n]-s[i]-(n-i)\times queries[i]$.
65+
66+
Similarly, we can find the index $i$ of the first element in the array $nums$ that is greater than or equal to $queries[i]$. There are $i$ elements that need to be increased to $queries[i]$, and the sum of these elements is $s[i]$. Therefore, the total number of operations to increase these elements to $queries[i]$ is $queries[i]\times i-s[i]$.
67+
68+
Finally, add these two total operation counts together to get the minimum number of operations to change all elements in the array $nums$ to $queries[i]$, that is, $ans[i]=s[n]-s[i]-(n-i)\times queries[i]+queries[i]\times i-s[i]$.
69+
70+
Time complexity $O(n \times \log n)$, space complexity $O(n)$, where $n$ is the length of the array $nums$.
71+
5872
<!-- tabs:start -->
5973

6074
### **Python3**

0 commit comments

Comments
 (0)