forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2483-minimum-penalty-for-a-shop.kt
47 lines (40 loc) · 1.11 KB
/
2483-minimum-penalty-for-a-shop.kt
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
// Time O(n) and space O(n) with prefix/suffix sums
class Solution {
fun bestClosingTime(c: String): Int {
val n = c.length
val prefix = IntArray (n + 1)
val postfix = IntArray (n + 1)
for (i in 1..n) {
prefix[i] = prefix[i - 1] + if (c[i - 1] == 'N') 1 else 0
}
for (i in n - 1 downTo 0) {
postfix[i] = postfix[i + 1] + if (c[i] == 'Y') 1 else 0
}
var res = Integer.MAX_VALUE
var min = Integer.MAX_VALUE
for (i in 0..n) {
val pen = prefix[i] + postfix[i]
if (pen < min) {
min = pen
res = i
}
}
return res
}
}
// Time O(n) and space O(1) with Kadane's Algorithm
class Solution {
fun bestClosingTime(customers: String): Int {
var cur = 0
var max = 0
var closeTime = 0
for ((i, c) in customers.withIndex()) {
cur += if(c == 'Y') 1 else -1
if (cur > max) {
max = cur
closeTime = i + 1
}
}
return closeTime
}
}