Skip to content

Commit c61860e

Browse files
committed
feat: add solutions to lc problems: No.2578~2582
1 parent 8ff920e commit c61860e

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

solution/2500-2599/2578.Split With Minimum Sum/README_EN.md

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

5151
## Solutions
5252

53+
**Approach 1: Count + Greedy**
54+
55+
We first use a hash table or array `cnt` to count the number of times each digit appears in `num`, and use the variable `n` to record the number of digits in `num`.
56+
57+
Then, enumerate the number of digits $i$ of `nums`, and assign the numbers in `cnt` in ascending order alternately to `num1` and `num2`, and record it in an array of length $2$ `$ans`. Finally, return the sum of the two numbers in `ans`.
58+
59+
The time complexity is $O(n)$ and the space complexity is $O(C)$. Where $n$ is the number of digits in `num`; and $C$ is the number of different numbers in `num`, which is $C \leq 10$ in this problem.
60+
61+
**Approach 2: Sorting + Greedy**
62+
63+
We can convert `num` to a string or character array and sort it. Then assign the numbers in the sorted array in ascending order alternately to `num1` and `num2`, and finally return the sum of `num1` and `num2`.
64+
65+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$. Where $n$ is the number of digits in `num`.
66+
5367
<!-- tabs:start -->
5468

5569
### **Python3**

solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md

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

4242
## Solutions
4343

44+
**Approach 1: Mathematics**
45+
46+
We find that after the $n$th minute, there are a total of $2 \times n - 1$ columns in the grid, and the numbers on each column are respectively $1, 3, 5, \cdots, 2 \times n - 1, 2 \times n - 3, \cdots, 3, 1$. The left and right parts are both arithmetic progressions, and the sum can be obtained by $2 \times n \times (n - 1) + 1$.
47+
48+
Time complexity $O(1)$, space complexity $O(1)$.
49+
4450
<!-- tabs:start -->
4551

4652
### **Python3**

solution/2500-2599/2581.Count Number of Possible Root Nodes/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ Considering any node as root will give at least 1 correct guess.
7272

7373
## Solutions
7474

75+
76+
**Approach 1: Tree DP (change root)**
77+
78+
First, we traverse the given edge set $edges$ and convert it to an adjacency list $g$ where $g[i]$ represents the adjacent nodes of node $i$. Use a hash map $gs$ to record the given guess set $guesses$.
79+
80+
Then, we start from node $0$ and perform a DFS to count the number of edges in $guesses$ among all the nodes that can be reached from node $0$. We use the variable $cnt$ to record this number.
81+
82+
Next, we start from node $0$ and perform a DFS to count the number of edges in $guesses$ in each tree with $0$ as the root. If the number is greater than or equal to $k$, it means that this node is a possible root node, and we add $1$ to the answer.
83+
84+
Therefore, the problem becomes to count the number of edges in $guesses$ in each tree with each node as the root. We already know that there are $cnt$ edges in $guesses$ among all the nodes that can be reached from node $0$. We can maintain this value by adding or subtracting the current edge in $guesses$ in DFS.
85+
86+
Assume that we are currently traversing node $i$ and $cnt$ represents the number of edges in $guesses$ with $i$ as the root node. Then, for each adjacent node $j$ of $i$, we need to calculate the number of edges in $guesses$ with $j$ as the root node. If $(i, j)$ is in $guesses$, then there is no edge $(i, j)$ in the tree with $j$ as the root node, so $cnt$ should decrease by $1$. If $(j, i)$ is in $guesses$, then there is an extra edge $(i, j)$ in the tree with $j$ as the root node, so $cnt$ should increase by $1$. That is, $f[j] = f[i] + (j, i) \in guesses - (i, j) \in guesses$. Where $f[i]$ represents the number of edges in $guesses$ with $i$ as the root node.
87+
88+
The time complexity is $O(n + m)$ and the space complexity is $O(n + m)$, where $n$ and $m$ are the lengths of $edges$ and $guesses$ respectively.
89+
7590
<!-- tabs:start -->
7691

7792
### **Python3**

solution/2500-2599/2582.Pass the Pillow/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ Afer two seconds, the pillow is given to the 3<sup>r</sup><sup>d</sup> person.
4040

4141
## Solutions
4242

43+
**Approach 1: Simulation**
44+
45+
We can simulate the process of passing the pillow, and each time the pillow is passed, if the pillow reaches the front or the end of the queue, the direction of the pillow will change, and the queue will continue to pass the pillow along the opposite direction.
46+
47+
The time complexity is $O(time)$ and the space complexity is $O(1)$, where $time$ is the given time.
48+
49+
**Approach 2: Math**
50+
51+
We notice that there are $n - 1$ passes in each round. Therefore, we can divide $time$ by $n - 1$ to get the number of rounds $k$ that the pillow is passed, and then take the remainder of $time$ modulo $n - 1$ to get the remaining passes $mod$ in the current round.
52+
53+
Then we judge the current round $k$:
54+
55+
- If $k$ is odd, then the current direction of the pillow is from the end of the queue to the front, so the pillow will be passed to the person with the number $n - mod$.
56+
- If $k$ is even, then the current direction of the pillow is from the front of the queue to the back, so the pillow will be passed to the person with the number $mod + 1$.
57+
58+
The time complexity is $O(1)$ and the space complexity is $O(1)$.
59+
4360
<!-- tabs:start -->
4461

4562
### **Python3**

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

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

4242
## Solutions
4343

44-
# Approach 1: Counting + Backtracking
44+
**Approach 1: Counting + Backtracking**
4545

4646
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.
4747

0 commit comments

Comments
 (0)