Skip to content

Commit e2c10b7

Browse files
committed
feat: add solutions to lc problems: No.2596~2606
1 parent 95ef8c7 commit e2c10b7

File tree

8 files changed

+99
-0
lines changed

8 files changed

+99
-0
lines changed

solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141

4242
## Solutions
4343

44+
**Approach 1: Simulation**
45+
46+
We first use the array $pos$ to record the coordinates of the grid visited by the knight, and then traverse the $pos$ array to check whether the difference between the adjacent two grid coordinates is $(1, 2)$ or $(2, 1)$. If not, return `false`.
47+
48+
Otherwise, return `true` after the traversal ends.
49+
50+
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the length of the chessboard.
51+
4452
<!-- tabs:start -->
4553

4654
### **Python3**

solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
5353

5454
## Solutions
5555

56+
**Approach 1: Count**
57+
58+
We use a hash table or array $cnt$ to count the number of times each remainder of $value$ is taken modulo in the array.
59+
60+
Then start from $0$ and traverse, for the current number $i$ traversed, if $cnt[i \bmod value]$ is $0$, it means that there is no number in the array that takes $i$ modulo $value$ as the remainder, then $i$ is the MEX of the array, and return directly. Otherwise, reduce $cnt[i \bmod value]$ by $1$ and continue to traverse.
61+
62+
The time complexity is $O(n)$ and the space complexity is $O(value)$. Where $n$ is the length of the array $nums$.
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**

solution/2500-2599/2599.Make the Prefix Sum Non-negative/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ The array after the operation is [3,-2,6,-5]. The prefix sum array is [3, 1, 7,
4343

4444
## Solutions
4545

46+
**Method 1: Greedy + Priority Queue (Min Heap)**
47+
48+
We use a variable $s$ to record the prefix sum of the current array.
49+
50+
Traverse the array $nums$, add the current element $x$ to the prefix sum $s$. If $x$ is a negative number, add $x$ to the min heap. If $s$ is negative at this time, greedily take out the smallest negative number and subtract it from $s$, and add one to the answer. Finally, return the answer.
51+
52+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**

solution/2600-2699/2601.Prime Subtraction Operation/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ After the second operation, nums is sorted in strictly increasing order, so the
5151

5252
## Solutions
5353

54+
**Approach 1: Preprocessing prime numbers + binary search**
55+
56+
We first preprocess all the primes within $1000$ and record them in the array $p$.
57+
58+
For each element $nums[i]$ in the array $nums$, we need to find a prime $p[j]$ such that $p[j] \gt nums[i] - nums[i + 1]$ and $p[j]$ is as small as possible. If there is no such prime, it means that it cannot be strictly increased by subtraction operations, return `false`. If there is such a prime, we will subtract $p[j]$ from $nums[i]$ and continue to process the next element.
59+
60+
If all the elements in $nums$ are processed, it means that it can be strictly increased by subtraction operations, return `true`.
61+
62+
The time complexity is $O(n \log n)$ and the space complexity is $O(n)$. where $n$ is the length of the array $nums$.
63+
5464
<!-- tabs:start -->
5565

5666
### **Python3**

solution/2600-2699/2603.Collect Coins in a Tree/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@
5050

5151
## Solutions
5252

53+
**Approach 1: Topological sorting**
54+
55+
We first convert the edges in $edges$ to the adjacency list $g$, where $g[i]$ represents all the adjacent nodes of node $i$, represented by a set.
56+
57+
Then we traverse all nodes and find the nodes where $coins[i]=0$ and $g[i]$ only has one node (that is, the leaf node where the coin is $0$), and add them to the queue $q$.
58+
59+
Then we continuously remove nodes from the queue and delete them from the adjacent list. Then we check whether the adjacent nodes meet the condition where $coins[j]=0$ and $g[j]$ only has one node. If it meets, we add it to the queue $q$. Loop until the queue is empty.
60+
61+
After the above operation, we get a new tree, and the leaf nodes of the tree are all nodes where the coin is $1$.
62+
63+
Then, we delete the remaining two layers of leaf nodes, and finally get a tree where all nodes need to be visited. We only need to count the number of edges and multiply it by $2$ to get the answer.
64+
65+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes
66+
5367
<!-- tabs:start -->
5468

5569
### **Python3**

solution/2600-2699/2604.Minimum Time to Eat All Grains/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ So, the maximum time needed is 1.
5050

5151
## Solutions
5252

53+
**Approach 1: Sorting + Binary Search**
54+
55+
First, sort the chickens and grains by their position from left to right. Then enumerate the time $t$ using binary search to find the smallest $t$ such that all the grains can be eaten up in $t$ seconds.
56+
57+
For each chicken, we use the pointer $j$ to point to the leftmost grain that has not been eaten, and the current position of the chicken is $x$ and the position of the grain is $y$. There are the following cases:
58+
59+
- If $y \leq x$, we note that $d = x - y$. If $d \gt t$, the current grain cannot be eaten, so directly return `false`. Otherwise, move the pointer $j$ to the right until $j=m$ or $grains[j] \gt x$. At this point, we need to check whether the chicken can eat the grain pointed to by $j$. If it can, continue to move the pointer $j$ to the right until $j=m$ or $min(d, grains[j] - x) + grains[j] - y \gt t$.
60+
- If $y \lt x$, move the pointer $j$ to the right until $j=m$ or $grains[j] - x \gt t$.
61+
62+
If $j=m$, it means that all the grains have been eaten, return `true`, otherwise return `false`.
63+
64+
Time complexity $O(n \times \log n + m \times \log m + (m + n) \times \log U)$, space complexity $O(\log m + \log n)$. $n$ and $m$ are the number of chickens and grains respectively, and $U$ is the maximum value of all the chicken and grain positions.
65+
5366
<!-- tabs:start -->
5467

5568
### **Python3**

solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>
3434

3535
## Solutions
3636

37+
**Approach 1: Enumeration**
38+
39+
We observe that if there are the same numbers in the arrays $nums1$ and $nums2$, then the minimum of the same numbers is the smallest number. Otherwise, we take the number $a$ in the array $nums1$ and the number $b$ in the array $nums2$, and concatenate the two numbers $a$ and $b$ into two numbers, and take the smaller number.
40+
41+
The time complexity is $O(m \times n)$, and the space complexity is $O(1)$, where $m$ and $n$ are the lengths of the arrays $nums1$ and $nums2$.
42+
43+
**Approach 2: Hash Table or Array + Enumeration**
44+
45+
We can use a hash table or array to record the numbers in the arrays $nums1$ and $nums2$, and then enumerate $1 \sim 9$. If $i$ appears in both arrays, then $i$ is the smallest number. Otherwise, we take the number $a$ in the array $nums1$ and the number $b$ in the array $nums2$, and concatenate the two numbers $a$ and $b$ into two numbers, and take the smaller number.
46+
47+
The time complexity is $(m + n)$, and the space complexity is $O(C)$. Where $m$ and $n$ are the lengths of the arrays $nums1$ and $nums2$ respectively; and $C$ is the range of the numbers in the arrays $nums1$ and $nums2$, and the range in this problem is $C = 10$.
48+
49+
**Approach 3: Bit Operation**
50+
51+
Since the range of the numbers is $1 \sim 9$, we can use a binary number with a length of $10$ to represent the numbers in the arrays $nums1$ and $nums2$. We use $mask1$ to represent the numbers in the array $nums1$, and use $mask2$ to represent the numbers in the array $nums2$.
52+
53+
If the number $mask$ obtained by performing a bitwise AND operation on $mask1$ and $mask2$ is not equal to $0$, then we extract the position of the last $1$ in the number $mask$, which is the smallest number.
54+
55+
Otherwise, we extract the position of the last $1$ in $mask1$ and $mask2$ respectively, and denote them as $a$ and $b$, respectively. Then the smallest number is $min(a \times 10 + b, b \times 10 + a)$.
56+
57+
The time complexity is $O(m + n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the lengths of the arrays $nums1$ and $nums2$ respectively.
58+
3759
<!-- tabs:start -->
3860

3961
### **Python3**

solution/2600-2699/2606.Find the Substring With Maximum Cost/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ It can be proven that 0 is the maximum cost.
5656

5757
## Solutions
5858

59+
**Approach 1: Prefix sum + Maintain the minimum prefix sum**
60+
61+
According to the description of the problem, we traverse each character $c$ in the string $s$, obtain its corresponding value $v$, and then update the current prefix sum $tot=tot+v$. Then, the cost of the maximum cost substring ending with $c$ is $tot$ minus the minimum prefix sum $mi$, that is, $tot-mi$. We update the answer $ans=max(ans,tot-mi)$ and maintain the minimum prefix sum $mi=min(mi,tot)$.
62+
63+
After the traversal is over, return the answer $ans$.
64+
65+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Where $n$ is the length of the string $s$; and $C$ is the size of the character set, which is $26$ in this problem.
66+
67+
**Approach 2: Convert to the maximum subarray sum problem**
68+
69+
We can consider the value $v$ of each character $c$ as an integer, so the actual problem is to solve the maximum subarray sum problem.
70+
71+
We use the variable $f$ to maintain the cost of the maximum cost substring ending with the current character $c$. Each time we traverse to a character $c$, we update $f=max(f, 0) + v$. Then we update the answer $ans=max(ans,f)$.
72+
73+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Where $n$ is the length of the string $s$; and $C$ is the size of the character set, which is $26$ in this problem.
74+
5975
<!-- tabs:start -->
6076

6177
### **Python3**

0 commit comments

Comments
 (0)