Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions solution/0700-0799/0755.Pour Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tags:
# w #
## # ###
#########
0123456
0123456

当向左或向右移动时,水可以移动到相同或更低的高度。When moving left or right, the water can only move to the same level or a lower level.
(从水平上看,意思是该列的地形高度加上水的高度)
Expand All @@ -61,15 +61,15 @@ tags:
# #
## w# ###
#########
0123456
0123456

由于向左移动不会使其降落,所以停在该位置上。下一个水滴下落:

# #
# w #
## w# ###
#########
0123456
0123456


由于新水滴向左移动可以最终下落,因此向左移动。
Expand All @@ -81,13 +81,13 @@ tags:
# w #
## w# ###
#########
0123456
0123456

# #
# #
##ww# ###
#########
0123456
0123456

经过刚才的阶段后,第三个水滴下落。
由于向左移动不会最终下落,因此尝试向右移动。
Expand All @@ -98,13 +98,13 @@ tags:
# w #
##ww# ###
#########
0123456
0123456

# #
# #
##ww#w###
#########
0123456
0123456

最终,第四个水滴下落。
由于向左移动不会最终下落,因此尝试向右移动。
Expand All @@ -114,14 +114,14 @@ tags:
# w #
##ww#w###
#########
0123456
0123456

最终的答案为 [2,2,2,3,2,2,2]:

#
#######
#######
0123456
#
#######
#######
0123456
</pre>

<p><strong>示例 2:</strong></p>
Expand Down Expand Up @@ -160,7 +160,7 @@ tags:

我们可以模拟每一单位的水滴下落的过程,每次下落时,我们首先尝试向左移动,如果可以移动到更低的高度,则移动到最低的高度处;如果不能移动到更低的高度,则尝试向右移动,如果可以移动到更低的高度,则移动到最低的高度处;如果不能移动到更低的高度,则在当前位置上升。

时间复杂度 $O(v \times n),空间复杂度 O(1)$。其中 $v$ 和 $n$ 分别是水滴的数量和高度数组的长度。
时间复杂度 $O(v \times n),其中 $v$ 和 $n$ 分别是水滴的数量和高度数组的长度。空间复杂度 O(1)$

<!-- tabs:start -->

Expand Down Expand Up @@ -271,6 +271,34 @@ func pourWater(heights []int, volume int, k int) []int {
}
```

#### TypeScript

```ts
function pourWater(heights: number[], volume: number, k: number): number[] {
while (volume-- > 0) {
let find = false;
for (let d = -1; d < 2 && !find; d += 2) {
let i = k,
j = k;
while (i + d >= 0 && i + d < heights.length && heights[i + d] <= heights[i]) {
if (heights[i + d] < heights[i]) {
j = i + d;
}
i += d;
}
if (j !== k) {
find = true;
++heights[j];
}
}
if (!find) {
++heights[k];
}
}
return heights;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
28 changes: 28 additions & 0 deletions solution/0700-0799/0755.Pour Water/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,34 @@ func pourWater(heights []int, volume int, k int) []int {
}
```

#### TypeScript

```ts
function pourWater(heights: number[], volume: number, k: number): number[] {
while (volume-- > 0) {
let find = false;
for (let d = -1; d < 2 && !find; d += 2) {
let i = k,
j = k;
while (i + d >= 0 && i + d < heights.length && heights[i + d] <= heights[i]) {
if (heights[i + d] < heights[i]) {
j = i + d;
}
i += d;
}
if (j !== k) {
find = true;
++heights[j];
}
}
if (!find) {
++heights[k];
}
}
return heights;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
23 changes: 23 additions & 0 deletions solution/0700-0799/0755.Pour Water/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function pourWater(heights: number[], volume: number, k: number): number[] {
while (volume-- > 0) {
let find = false;
for (let d = -1; d < 2 && !find; d += 2) {
let i = k,
j = k;
while (i + d >= 0 && i + d < heights.length && heights[i + d] <= heights[i]) {
if (heights[i + d] < heights[i]) {
j = i + d;
}
i += d;
}
if (j !== k) {
find = true;
++heights[j];
}
}
if (!find) {
++heights[k];
}
}
return heights;
}