-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220703.kt
More file actions
310 lines (271 loc) · 9.07 KB
/
_20220703.kt
File metadata and controls
310 lines (271 loc) · 9.07 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._07
import Testable
import utils.*
// 128. Longest Consecutive Sequence
// https://leetcode.com/problems/longest-consecutive-sequence/
private interface Leetcode_128 {
fun longestConsecutive(nums: IntArray): Int
companion object : Testable {
override fun test() {
val tests = listOf(
intArrayOf(100,4,200,1,3,2) to 4,
intArrayOf(100,4,200,1,3,2,6) to 4,
intArrayOf(0,3,7,2,5,8,4,6,0,1) to 9,
)
listOf(
M1()::longestConsecutive,
).runTimedTests {
tests.forEachPair { first, second ->
invoke(first).assertEqualTo(second)
}
}
}
}
// https://leetcode.com/submissions/detail/737828338/
// Runtime: 647 ms, faster than 86.18% of Kotlin online submissions for Longest Consecutive Sequence.
// Memory Usage: 73 MB, less than 71.43% of Kotlin online submissions for Longest Consecutive Sequence.
private class M1 : Leetcode_128 {
override fun longestConsecutive(nums: IntArray): Int {
val map = HashSet<Int>()
nums.forEach { map.add(it) }
var max = 0
map.forEach {
if (!map.contains(it - 1)) {
var counter = 0
while (map.contains(counter+it)) {
counter++
}
max = Math.max(max, counter)
}
}
return max
}
}
}
// 121. Best Time to Buy and Sell Stock
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
private interface Leetcode_121 {
fun maxProfit(prices: IntArray): Int
companion object : Testable {
override fun test() {
val tests = listOf(
intArrayOf(7,1,5,3,6,4) to 5,
intArrayOf(7,6,4,3,1) to 0,
)
listOf(
M1()::maxProfit,
S1()::maxProfit,
).runTimedTests {
tests.forEachPair { first, second ->
invoke(first).assertEqualTo(second)
}
}
}
}
// https://leetcode.com/submissions/detail/737024419/
// Runtime: 586 ms, faster than 96.21% of Kotlin online submissions for Best Time to Buy and Sell Stock.
// Memory Usage: 54.2 MB, less than 98.11% of Kotlin online submissions for Best Time to Buy and Sell Stock.
private class M1 : Leetcode_121 {
override fun maxProfit(prices: IntArray): Int {
if (prices.size <= 1) return 0
val maxPrices = IntArray(prices.size)
val minPrices = IntArray(prices.size)
var max = prices.last()
for(i in prices.size - 1 downTo 0) {
max = Math.max(max, prices[i])
maxPrices[i] = max
}
var min = prices.first()
for (i in prices.indices) {
min = Math.min(min, prices[i])
minPrices[i] = min
}
var res = 0
for (i in prices.indices){
val profit = maxPrices[i] - minPrices[i]
res = Math.max(profit, res)
}
return res
}
}
// Two pointers
private class S1 : Leetcode_121 {
override fun maxProfit(prices: IntArray): Int {
if (prices.size <= 1) return 0
var l = 0
var r = 1
var res = 0
while (r < prices.size) {
if (prices[r] > prices[l]) {
val prof = prices[r] - prices[l]
if (prof > res) { res = prof }
} else {
l = r
}
r++
}
return res
}
}
}
// 424. Longest Repeating Character Replacement
// https://leetcode.com/problems/longest-repeating-character-replacement/
private interface Leetcode_424 {
fun characterReplacement(s: String, k: Int): Int
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf("ABAB", 2, 4),
tupleOf("AABABBA", 1, 4),
)
listOf(
// M1()::characterReplacement,
S1()::characterReplacement,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third)
}
}
}
}
// wrong
private class M1 : Leetcode_424 {
private fun isValid(array: IntArray, k: Int) : Boolean {
var freq = -1
fun mostFreq(array: IntArray) : Int {
var index = 0
for (i in array.indices) {
if (freq < array[i]) {
freq = array[i]
index = i
}
}
return index
}
val mostFreq = mostFreq(array)
var others = 0
for (i in array.indices) {
if (i != mostFreq) {
others += array[i]
}
}
return others <= k
}
override fun characterReplacement(s: String, k: Int): Int {
if (s.length <= 1) return s.length
var left = 0
var right = 0
val array = IntArray(26){ 0 }
var res = 0
while (right < s.length) {
val c = s[right]
array[c - 'A'] += 1
if (isValid(array, k)) {
right++
res = Math.max(res, (right - left + 1))
} else {
left++
}
}
return res
}
}
private class S1 : Leetcode_424{
override fun characterReplacement(s: String, k: Int): Int {
if (s.length <= 1) return s.length
val count = IntArray(26){ 0 }
var res = 0
var l = 0
var maxF = 0
for (r in s.indices) {
val c = s[r]
count[c - 'A']++
maxF = Math.max(maxF, count[c - 'A'])
// check if valid
if ((r - l + 1) - maxF > k) {
count[s[l] - 'A']--
l++
}
res = Math.max(res, (r - l + 1))
}
return res
}
}
}
// 567. Permutation in String
// https://leetcode.com/problems/permutation-in-string/
private interface Leetcode_567 {
fun checkInclusion(s1: String, s2: String): Boolean
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf("ab", "eidbaooo", true),
tupleOf("ab", "eidboaoo", false),
tupleOf("ab", "ab", true),
tupleOf("ab", "ba", true),
tupleOf("adc", "dcda", true),
)
listOf(
M2()::checkInclusion,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third)
}
}
}
}
// misunderstood the question, thought "permutation" was reversing order
@Deprecated("not working", level = DeprecationLevel.ERROR)
private class M1 : Leetcode_567 {
override fun checkInclusion(s1: String, s2: String): Boolean {
var p1 = s1.length - 1
var p2 = 0
while (p2 < s2.length) {
while (p1 >= 0 && p2 < s2.length && s1[p1] == s2[p2]) {
p1--
p2++
}
if (p1 == -1) return true
p1 = s1.length - 1
p2++
}
return false
}
}
// https://leetcode.com/submissions/detail/737850787/
// Runtime: 206 ms, faster than 96.21% of Kotlin online submissions for Permutation in String.
// Memory Usage: 35.1 MB, less than 99.53% of Kotlin online submissions for Permutation in String.
// O(26*n)
private class M2 : Leetcode_567 {
override fun checkInclusion(s1: String, s2: String): Boolean {
if (s1.length > s2.length) return false
val s1Map = IntArray(26)
s1.forEach {
s1Map[it - 'a'] ++
}
val s2Map = IntArray(26)
var left = 0
var right = 0
while (right < s2.length) {
val c = s2[right]
s2Map[c - 'a']++
if (s1.length == (right - left + 1)) {
if (s1Map.contentEquals(s2Map)) return true
}
right++
if ((right - left + 1) > s1.length) {
s2Map[s2[left] - 'a']--
left++
}
}
return false
}
}
}
fun main() {
// Leetcode_128.test()
// Leetcode_121.test()
// Leetcode_424.test()
Leetcode_567.test()
}