Skip to content

Commit 23f48fb

Browse files
author
Yi Gu
committed
Add a solution to Pour Water
1 parent fae043e commit 23f48fb

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Math/PourWater.swift

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/pour-water/
3+
* Primary idea: One pointer. Go left and then right to update the drop pointer.
4+
* Time Complexity: O(nk), Space Complexity: O(1)
5+
*
6+
*/
7+
8+
class PourWater {
9+
func pourWater(_ heights: [Int], _ volume: Int, _ k: Int) -> [Int] {
10+
var result = heights, idx = k, dropIdx = k
11+
12+
for _ in 0..<volume {
13+
dropIdx = k
14+
15+
// check left
16+
idx = k - 1
17+
while idx >= 0 {
18+
if result[idx] > result[dropIdx] {
19+
break
20+
}
21+
22+
if result[idx] < result[dropIdx] {
23+
dropIdx = idx
24+
}
25+
26+
idx -= 1
27+
}
28+
29+
// check right
30+
if dropIdx == k {
31+
idx = k + 1
32+
while idx < result.count {
33+
if result[idx] > result[dropIdx] {
34+
break
35+
}
36+
37+
if result[idx] < result[dropIdx] {
38+
dropIdx = idx
39+
}
40+
41+
idx += 1
42+
}
43+
}
44+
45+
result[dropIdx] += 1
46+
}
47+
48+
return result
49+
}
50+
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@
340340
[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)| [Swift](./Math/SparseMatrixMultiplication.swift)| Medium| O(n^3)| O(n^2)|
341341
[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [Swift](./Math/RectangleArea.swift)| Easy| O(1)| O(1)|
342342
[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| [Swift](./Math/MinimumMovesEqualArrayElements.swift)| Easy| O(n)| O(1)|
343-
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| O(n)| O(n)|
343+
[Pour Water](https://leetcode.com/problems/pour-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| O(n)| O(n)|
344+
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Medium| O(nk)| O(1)|
344345
[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Swift](./Math/ContainerMostWater.swift)| Medium| O(n)| O(1)|
345346
[Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)|
346347
[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)| [Swift](./Math/KthSmallestLexicographicalOrder.swift)| Hard| O(n)| O(1)|
@@ -683,7 +684,7 @@
683684
| [Swift](./Array/ProductExceptSelf.swift) | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | Medium |
684685
| | 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | Easy |
685686
| | 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | Medium |
686-
| | 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | Easy |
687+
| [Swift](./Tree/LowestCommonAncestorBinarySearchTree.swift) | 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | Easy |
687688
| [Swift](./LinkedList/PalindromeLinkedList.swift) | 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | Easy |
688689
| | 233 | [Number of Digit One](https://leetcode.com/problems/number-of-digit-one/) | Hard |
689690
| [Swift](./Queue/ImplementQueueUsingStacks.swift) | 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | Easy |

0 commit comments

Comments
 (0)