-
Notifications
You must be signed in to change notification settings - Fork 29
/
Contents.swift
79 lines (69 loc) · 2.1 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//: Playground - noun: a place where people can play
import UIKit
/*
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
*/
//O(n*m) m - height
func rainDrops(in array: [Int]) -> Int {
var l = true
var r = true
var dropsCountTotal = 0
var layer = 1
let max = array.max() ?? 0
while layer <= max {
r = false
l = false
var dropsCount = 0
for i in array {
if i >= layer {
if !l { l = true }
else if !r { r = dropsCount == 0 ? false : true }
}
if l, r, dropsCount > 0 {
// print(l, r, dropsCount, "in layer -", layer, "position -", i)
dropsCountTotal += dropsCount
dropsCount = 0
r = false
}
else if i < layer, l || r {
dropsCount += 1
}
}
layer += 1
}
return dropsCountTotal
}
//O(n)
func rainDrops2(in array: [Int]) -> Int {
guard array.count > 0 else { return 0 }
var left = 0
var right = array.count - 1
var moveLeft = true
var level: (Int, Int) = (0, 0)
var count = 0
while left != right {
let value = moveLeft ? array[left] : array [right]
let oppositeLevel = moveLeft ? level.1 : level.0
let selfLevel = moveLeft ? level.0 : level.1
if value > oppositeLevel {
if moveLeft { level.0 = value } else { level.1 = value } //increment
moveLeft = !moveLeft //switch side
// print(value, "move left - ", !moveLeft)
continue
}
if value > selfLevel {
if moveLeft { level.0 = value } else { level.1 = value } //increment
}
else if value < min(level.0, level.1) {
count += min(level.0, level.1) - value
}
if moveLeft {
left += 1
}
else {
right -= 1
}
}
return count
}
rainDrops2(in: [0,1,0,2,1,0,1,3,2,1,2,1])