Unverified Commit 3e8d2a29 authored by ASAHI OCEAN's avatar ASAHI OCEAN Committed by GitHub
Browse files

Merge pull request #228 from asahiocean/develop

42. Trapping Rain Water
parents a230d0ee aaf2376a
No preview for this file type
import Foundation
// 42. Trapping Rain Water
// https://leetcode.com/problems/trapping-rain-water/
// Discuss: https://vk.cc/c4e16C
class Solution {
func trap(_ height: [Int]) -> Int {
var stack = [Int](), result = 0
height.indices.forEach {
while !stack.isEmpty && height[$0] > height[stack.last!] {
let top = stack.removeLast()
guard !stack.isEmpty else { break }
let l = stack.last!
result += ($0-l-1) * (min(height[$0], height[l]) - height[top])
}
stack.append($0)
}
return result
}
}
import XCTest
// Executed 2 tests, with 0 failures (0 unexpected) in 0.024 (0.026) seconds
class Tests: XCTestCase {
private let s = Solution()
func test0() {
let res = s.trap([0,1,0,2,1,0,1,3,2,1,2,1])
XCTAssertEqual(res, 6)
}
func test1() {
let res = s.trap([4,2,0,3,2,5])
XCTAssertEqual(res, 9)
}
}
Tests.defaultTestSuite.run()
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment