Skip to content

Commit 8170963

Browse files
authored
feat: add solutions to lc problems: No.2208,2209 (#2482)
1 parent 468a94b commit 8170963

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ It can be shown that we cannot reduce the sum by at least half in less than 3 op
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Greedy + Priority Queue (Max Heap)
57+
58+
According to the problem description, each operation will halve a number in the array. To minimize the number of operations that reduce the array sum by at least half, each operation should halve the current maximum value in the array.
59+
60+
Therefore, we first calculate the total sum $s$ that the array needs to reduce, and then use a priority queue (max heap) to maintain all the numbers in the array. Each time we take the maximum value $t$ from the priority queue, halve it, and then put the halved number back into the priority queue, while updating $s$, until $s \le 0$. The number of operations at this time is the answer.
61+
62+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
5763

5864
<!-- tabs:start -->
5965

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ Note that the carpets are able to overlap one another.
4949

5050
## Solutions
5151

52-
### Solution 1
52+
### Solution 1: Memoization Search
53+
54+
Design a function $dfs(i, j)$ to represent the minimum number of white bricks that are not covered starting from index $i$ using $j$ carpets. The answer is $dfs(0, numCarpets)$.
55+
56+
For index $i$, we discuss different cases:
57+
58+
- If $i \ge n$, it means that all bricks have been covered, return $0$;
59+
- If $floor[i] = 0$, there is no need to use a carpet, just skip it, that is, $dfs(i, j) = dfs(i + 1, j)$;
60+
- If $j = 0$, we can directly calculate the number of remaining white bricks that have not been covered using the prefix sum array $s$, that is, $dfs(i, j) = s[n] - s[i]$;
61+
- If $floor[i] = 1$, we can choose to use a carpet to cover it, or choose not to use a carpet to cover it, and take the minimum of the two, that is, $dfs(i, j) = min(dfs(i + 1, j), dfs(i + carpetLen, j - 1))$.
62+
63+
Use memoization search.
64+
65+
The time complexity is $O(n\times m)$, and the space complexity is $O(n\times m)$. Where $n$ and $m$ are the lengths of the string $floor$ and the value of $numCarpets$ respectively.
5366

5467
<!-- tabs:start -->
5568

solution/2200-2299/2214.Minimum Health to Beat Game/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
我们可以贪心地选择在伤害值最大的回合中使用一次护甲技能,假设伤害值最大为 $mx$,那么我们可以免受 $min(mx, armor)$ 的伤害,因此我们需要的最小生命值为 $sum(damage) - min(mx, armor) + 1$。
7474

75-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `damage` 的长度。
75+
时间复杂度 $O(n)$,其中 $n$ 为数组 `damage` 的长度。空间复杂度 $O(1)$
7676

7777
<!-- tabs:start -->
7878

solution/2200-2299/2214.Minimum Health to Beat Game/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Note that you did not use your armor ability.
6565

6666
## Solutions
6767

68-
### Solution 1
68+
### Solution 1: Greedy
69+
70+
We can greedily choose to use the armor skill in the round with the maximum damage. Suppose the maximum damage is $mx$, then we can avoid $min(mx, armor)$ damage, so the minimum life value we need is $sum(damage) - min(mx, armor) + 1$.
71+
72+
The time complexity is $O(n)$, where $n$ is the length of the `damage` array. The space complexity is $O(1)$.
6973

7074
<!-- tabs:start -->
7175

0 commit comments

Comments
 (0)