|
43 | 43 |
|
44 | 44 | <!-- 这里可写通用的实现逻辑 -->
|
45 | 45 |
|
| 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 | + |
46 | 56 | <!-- tabs:start -->
|
47 | 57 |
|
48 | 58 | ### **Python3**
|
49 | 59 |
|
50 | 60 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
51 | 61 |
|
52 | 62 | ```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 |
54 | 73 | ```
|
55 | 74 |
|
56 | 75 | ### **Java**
|
57 | 76 |
|
58 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
59 | 78 |
|
60 | 79 | ```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 | +``` |
61 | 160 |
|
| 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 | +} |
62 | 180 | ```
|
63 | 181 |
|
64 | 182 | ### **...**
|
|
0 commit comments