Skip to content

Commit 7978675

Browse files
committed
feat: add solutions to lcp problem: No.33
1 parent d4362c6 commit 7978675

File tree

20 files changed

+402
-179
lines changed

20 files changed

+402
-179
lines changed

lcp/LCP 33. 蓄水/README.md

+119-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,140 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:贪心 + 枚举**
47+
48+
题目中涉及两个操作:升级水桶、蓄水。我们应该贪心地把升级水桶的操作放在前面,这样在蓄水时,每次能蓄水的量就会更多,操作次数就会更少。
49+
50+
首先,如果最低蓄水量 $vat$ 中所有元素都为 $0$,说明不需要蓄水,直接返回 $0$ 即可。
51+
52+
接下来,我们可以枚举蓄水的次数 $x$,其中 $x \in [1, \max(vat)]$,那么在开始蓄水前,每个水桶的容量至少应该为 $\lceil \frac{vat_i}{x} \rceil$,其中 $\lceil \rceil$ 表示向上取整。因此,每个水桶的升级次数为 $\max(0, \lceil \frac{vat_i}{x} \rceil - bucket_i)$,我们将所有水桶的升级次数累加,记为 $y$,再加上蓄水的次数 $x$,就是总的操作次数。答案为所有 $x + y$ 中的最小值。
53+
54+
时间复杂度 $O(n \times M)$,其中 $n$ 和 $M$ 分别为数组 $vat$ 的长度和数组 $vat$ 中的最大值。空间复杂度 $O(1)$。
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**
4959

5060
<!-- 这里可写当前语言的特殊实现逻辑 -->
5161

5262
```python
53-
63+
class Solution:
64+
def storeWater(self, bucket: List[int], vat: List[int]) -> int:
65+
mx = max(vat)
66+
if mx == 0:
67+
return 0
68+
ans = inf
69+
for x in range(1, mx + 1):
70+
y = sum(max(0, (v + x - 1) // x - b) for v, b in zip(vat, bucket))
71+
ans = min(ans, x + y)
72+
return ans
5473
```
5574

5675
### **Java**
5776

5877
<!-- 这里可写当前语言的特殊实现逻辑 -->
5978

6079
```java
80+
class Solution {
81+
public int storeWater(int[] bucket, int[] vat) {
82+
int mx = Arrays.stream(vat).max().getAsInt();
83+
if (mx == 0) {
84+
return 0;
85+
}
86+
int n = vat.length;
87+
int ans = 1 << 30;
88+
for (int x = 1; x <= mx; ++x) {
89+
int y = 0;
90+
for (int i = 0; i < n; ++i) {
91+
y += Math.max(0, (vat[i] + x - 1) / x - bucket[i]);
92+
}
93+
ans = Math.min(ans, x + y);
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int storeWater(vector<int>& bucket, vector<int>& vat) {
106+
int mx = *max_element(vat.begin(), vat.end());
107+
if (mx == 0) {
108+
return 0;
109+
}
110+
int ans = 1 << 30;
111+
int n = bucket.size();
112+
for (int x = 1; x <= mx; ++x) {
113+
int y = 0;
114+
for (int i = 0; i < n; ++i) {
115+
y += max(0, (vat[i] + x - 1) / x - bucket[i]);
116+
}
117+
ans = min(ans, x + y);
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func storeWater(bucket []int, vat []int) int {
128+
mx := 0
129+
for _, x := range vat {
130+
mx = max(mx, x)
131+
}
132+
if mx == 0 {
133+
return 0
134+
}
135+
ans := 1 << 30
136+
for x := 1; x <= mx; x++ {
137+
y := 0
138+
for i, v := range vat {
139+
y += max(0, (v+x-1)/x-bucket[i])
140+
}
141+
ans = min(ans, x+y)
142+
}
143+
return ans
144+
}
145+
146+
func max(a, b int) int {
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
152+
153+
func min(a, b int) int {
154+
if a < b {
155+
return a
156+
}
157+
return b
158+
}
159+
```
61160

161+
### **TypeScript**
162+
163+
```ts
164+
function storeWater(bucket: number[], vat: number[]): number {
165+
const mx = Math.max(...vat);
166+
if (mx === 0) {
167+
return 0;
168+
}
169+
const n = vat.length;
170+
let ans = 1 << 30;
171+
for (let x = 1; x <= mx; ++x) {
172+
let y = 0;
173+
for (let i = 0; i < n; ++i) {
174+
y += Math.max(0, Math.ceil(vat[i] / x) - bucket[i]);
175+
}
176+
ans = Math.min(ans, x + y);
177+
}
178+
return ans;
179+
}
62180
```
63181

64182
### **...**

lcp/LCP 33. 蓄水/Solution.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int storeWater(vector<int>& bucket, vector<int>& vat) {
4+
int mx = *max_element(vat.begin(), vat.end());
5+
if (mx == 0) {
6+
return 0;
7+
}
8+
int ans = 1 << 30;
9+
int n = bucket.size();
10+
for (int x = 1; x <= mx; ++x) {
11+
int y = 0;
12+
for (int i = 0; i < n; ++i) {
13+
y += max(0, (vat[i] + x - 1) / x - bucket[i]);
14+
}
15+
ans = min(ans, x + y);
16+
}
17+
return ans;
18+
}
19+
};

lcp/LCP 33. 蓄水/Solution.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func storeWater(bucket []int, vat []int) int {
2+
mx := 0
3+
for _, x := range vat {
4+
mx = max(mx, x)
5+
}
6+
if mx == 0 {
7+
return 0
8+
}
9+
ans := 1 << 30
10+
for x := 1; x <= mx; x++ {
11+
y := 0
12+
for i, v := range vat {
13+
y += max(0, (v+x-1)/x-bucket[i])
14+
}
15+
ans = min(ans, x+y)
16+
}
17+
return ans
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
26+
27+
func min(a, b int) int {
28+
if a < b {
29+
return a
30+
}
31+
return b
32+
}

lcp/LCP 33. 蓄水/Solution.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int storeWater(int[] bucket, int[] vat) {
3+
int mx = Arrays.stream(vat).max().getAsInt();
4+
if (mx == 0) {
5+
return 0;
6+
}
7+
int n = vat.length;
8+
int ans = 1 << 30;
9+
for (int x = 1; x <= mx; ++x) {
10+
int y = 0;
11+
for (int i = 0; i < n; ++i) {
12+
y += Math.max(0, (vat[i] + x - 1) / x - bucket[i]);
13+
}
14+
ans = Math.min(ans, x + y);
15+
}
16+
return ans;
17+
}
18+
}

lcp/LCP 33. 蓄水/Solution.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def storeWater(self, bucket: List[int], vat: List[int]) -> int:
3+
mx = max(vat)
4+
if mx == 0:
5+
return 0
6+
ans = inf
7+
for x in range(1, mx + 1):
8+
y = sum(max(0, (v + x - 1) // x - b) for v, b in zip(vat, bucket))
9+
ans = min(ans, x + y)
10+
return ans

lcp/LCP 33. 蓄水/Solutoin.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function storeWater(bucket: number[], vat: number[]): number {
2+
const mx = Math.max(...vat);
3+
if (mx === 0) {
4+
return 0;
5+
}
6+
const n = vat.length;
7+
let ans = 1 << 30;
8+
for (let x = 1; x <= mx; ++x) {
9+
let y = 0;
10+
for (let i = 0; i < n; ++i) {
11+
y += Math.max(0, Math.ceil(vat[i] / x) - bucket[i]);
12+
}
13+
ans = Math.min(ans, x + y);
14+
}
15+
return ans;
16+
}

solution/0000-0099/0050.Pow(x, n)/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111
<p>&nbsp;</p>
1212

13-
<p><strong>示例 1:</strong></p>
13+
<p><strong class="example">示例 1:</strong></p>
1414

1515
<pre>
1616
<strong>输入:</strong>x = 2.00000, n = 10
1717
<strong>输出:</strong>1024.00000
1818
</pre>
1919

20-
<p><strong>示例 2:</strong></p>
20+
<p><strong class="example">示例 2:</strong></p>
2121

2222
<pre>
2323
<strong>输入:</strong>x = 2.10000, n = 3
2424
<strong>输出:</strong>9.26100
2525
</pre>
2626

27-
<p><strong>示例 3:</strong></p>
27+
<p><strong class="example">示例 3:</strong></p>
2828

2929
<pre>
3030
<strong>输入:</strong>x = 2.00000, n = -2
@@ -40,6 +40,7 @@
4040
<li><code>-100.0 &lt; x &lt; 100.0</code></li>
4141
<li><code>-2<sup>31</sup> &lt;= n &lt;= 2<sup>31</sup>-1</code></li>
4242
<li><code>n</code>&nbsp;是一个整数</li>
43+
<li>要么 <code>x</code> 不为零,要么 <code>n &gt; 0</code> 。</li>
4344
<li><code>-10<sup>4</sup> &lt;= x<sup>n</sup> &lt;= 10<sup>4</sup></code></li>
4445
</ul>
4546

solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ul>
1212
<li><code>0 &lt;= i, j &lt; nums.length</code></li>
1313
<li><code>i != j</code></li>
14-
<li><code>nums[i] - nums[j] == k</code></li>
14+
<li><code>|nums[i] - nums[j]| == k</code></li>
1515
</ul>
1616

1717
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>

solution/1300-1399/1336.Number of Transactions per Visit/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Transactions table:
9797
+--------------------+--------------+
9898
<strong>Explanation:</strong> The chart drawn for this example is shown above.
9999
* For transactions_count = 0, The visits (1, &quot;2020-01-01&quot;), (2, &quot;2020-01-02&quot;), (12, &quot;2020-01-01&quot;) and (19, &quot;2020-01-03&quot;) did no transactions so visits_count = 4.
100-
* For transactions_count = 1, The visits (2, &quot;2020-01-03&quot;), (7, &amp;quo;2020-01-11&quot;), (8, &quot;2020-01-28&quot;), (1, &quot;2020-01-02&quot;) and (1, &quot;2020-01-04&quot;) did one transaction so visits_count = 5.
100+
* For transactions_count = 1, The visits (2, &quot;2020-01-03&quot;), (7, &quot;2020-01-11&quot;), (8, &quot;2020-01-28&quot;), (1, &quot;2020-01-02&quot;) and (1, &quot;2020-01-04&quot;) did one transaction so visits_count = 5.
101101
* For transactions_count = 2, No customers visited the bank and did two transactions so visits_count = 0.
102102
* For transactions_count = 3, The visit (9, &quot;2020-01-25&quot;) did three transactions so visits_count = 1.
103103
* For transactions_count &gt;= 4, No customers visited the bank and did more than three transactions so we will stop at transactions_count = 3

solution/1500-1599/1528.Shuffle String/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ var restoreString = function (s, indices) {
121121
for (let i = 0; i < s.length; i++) {
122122
rs[indices[i]] = s[i];
123123
}
124-
return rs.join("");
124+
return rs.join('');
125125
};
126126
```
127127

solution/1500-1599/1528.Shuffle String/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var restoreString = function (s, indices) {
107107
for (let i = 0; i < s.length; i++) {
108108
rs[indices[i]] = s[i];
109109
}
110-
return rs.join("");
110+
return rs.join('');
111111
};
112112
```
113113

solution/2600-2699/2689.Extract Kth Character From The Rope Tree/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public:
170170
string left = dfs(root->left);
171171
string right = dfs(root->right);
172172
return left + right;
173-
};
173+
};
174174
return dfs(root)[k - 1];
175175
}
176176
};

solution/2600-2699/2690.Infinite Method Object/README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
# [2690. Infinite Method Object](https://leetcode.cn/problems/infinite-method-object)
1+
# [2690. 无穷方法对象](https://leetcode.cn/problems/infinite-method-object)
22

33
[English Version](/solution/2600-2699/2690.Infinite%20Method%20Object/README_EN.md)
44

55
## 题目描述
66

77
<!-- 这里写题目描述 -->
88

9-
<p>Write a function that&nbsp;returns an&nbsp;<strong>infinite-method</strong><strong>&nbsp;object</strong>.</p>
9+
<p>请你编写一个函数,返回一个 <strong>无穷方法对象</strong></p>
1010

11-
<p>An&nbsp;<strong>infinite-method</strong><strong>&nbsp;object</strong>&nbsp;is defined as an object that allows you to call any method and it will always return the name of the method.</p>
11+
<p><strong>无穷方法对象</strong> 被定义为一个对象,它允许您调用任何方法,并始终返回方法的名称。</p>
1212

13-
<p>For example, if you execute&nbsp;<code>obj.abc123()</code>, it will return&nbsp;<code>&quot;abc123&quot;</code>.</p>
13+
<p>例如,如果执行 <code>obj.abc123()</code> ,它将返回 <code>"abc123"</code></p>
1414

1515
<p>&nbsp;</p>
16-
<p><strong class="example">Example 1:</strong></p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
1718

1819
<pre>
19-
<strong>Input:</strong> method = &quot;abc123&quot;
20-
<strong>Output:</strong> &quot;abc123&quot;
21-
<strong>Explanation:</strong>
20+
<b>输入:</b>method = "abc123"
21+
<b>输出:</b>"abc123"
22+
<strong>解释:</strong>
2223
const obj = createInfiniteObject();
23-
obj[&#39;abc123&#39;](); // &quot;abc123&quot;
24-
The returned string should always match the method name.</pre>
24+
obj['abc123'](); // "abc123"
25+
返回的字符串应始终与方法名称匹配。</pre>
2526

26-
<p><strong class="example">Example 2:</strong></p>
27+
<p><strong class="example">示例 2:</strong></p>
2728

2829
<pre>
29-
<strong>Input:</strong> method = &quot;.-qw73n|^2It&quot;
30-
<strong>Output:</strong> &quot;.-qw73n|^2It&quot;
31-
<strong>Explanation:</strong> The returned string should always match the method name.</pre>
30+
<b>输入:</b>method = ".-qw73n|^2It"
31+
<strong>输出:</strong>".-qw73n|^2It"
32+
<b>解释:</b>返回的字符串应始终与方法名称匹配。</pre>
3233

3334
<p>&nbsp;</p>
34-
<p><strong>Constraints:</strong></p>
35+
36+
<p><strong>提示:</strong></p>
3537

3638
<ul>
3739
<li><code>0 &lt;= method.length &lt;= 1000</code></li>

0 commit comments

Comments
 (0)