-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220812.kt
More file actions
260 lines (222 loc) · 7.49 KB
/
_20220812.kt
File metadata and controls
260 lines (222 loc) · 7.49 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
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import utils.assertEqualTo
import utils.runTimedTests
import utils.tupleOf
// 846. Hand of Straights
// https://leetcode.com/problems/hand-of-straights/
private interface Leetcode_846 {
fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(
intArrayOf(1,2,3,6,2,3,4,7,8), 3, true
),
tupleOf(
intArrayOf(1,2,3,4,5), 4, false
),
)
listOf(
M1()::isNStraightHand,
).runTimedTests(tests) { a, b, c ->
invoke(a, b).assertEqualTo(c)
}
}
}
// Runtime: 526 ms, faster than 82.35% of Kotlin online submissions for Hand of Straights.
// Memory Usage: 57.6 MB, less than 94.12% of Kotlin online submissions for Hand of Straights.\
// https://leetcode.com/submissions/detail/771548657/
private class M1 : Leetcode_846 {
override fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {
if (hand.size % groupSize != 0) return false
val groupsNo = hand.size / groupSize
hand.sort()
var start = 0
for (x in 0 until groupsNo) {
val startValue = hand[start]
val endValue = startValue + groupSize - 1
var i = start
var newStart = -1
for (v in startValue .. endValue) {
while(i<hand.size&&hand[i] != v) {
if (newStart == -1 && hand[i] != -1) { newStart = i }
i++
}
if (i > hand.lastIndex) return false
// println("hand[$i](${hand[i]}) -> -1")
hand[i] = -1
i++
}
start = if (newStart != -1) newStart else i
}
return true
}
}
}
// 57. Insert Interval
// https://leetcode.com/problems/insert-interval/
private interface Leetcode_57 {
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray>
companion object : Testable {
override fun test() {
/*
val tests = listOf(
)
listOf(
M1()::,
).runTimedTests(tests) { a, b ->
invoke(a).assertEqualTo(b)
}
*/
}
}
private class M1 : Leetcode_57 {
private val res = ArrayList<IntArray>()
override fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
if (intervals.isEmpty()) return arrayOf(newInterval)
val _start = newInterval[0]
val _end = newInterval[1]
if (intervals[0][0] > _end) return Array(intervals.size+1) {
when(it) {
0 -> newInterval
else -> {
intervals[it-1]
}
}
}
if (intervals.last()[1] < _start) return Array(intervals.size+1) {
when(it) {
intervals.size -> newInterval
else -> {
intervals[it]
}
}
}
// index
var start = -1
var end = -1
fun end() {
if (end != -1) {
res.add(intArrayOf(start, end))
start = -1
}
end = -1
}
fun setStart(v: Int) {
end()
if (start > v || start == -1) {
start = v
}
}
fun setEnd(v: Int) {
if (end == -1 || end < v) {
end = v
}
}
for (i in intervals) {
val _s = i[0]
val _e = i[1]
if (end != -1 && _start>end && _start<_e) {
setStart(_start)
end = -1
}
setStart(_s)
if (_start>_s&&_start<_e) {
setStart(_start)
}
if (_end>_s&&_end<_e) {
setStart(_end)
}
setEnd(_e)
}
end()
return res.toTypedArray()
}
}
private class M2 : Leetcode_57 {
private val res = ArrayList<IntArray>()
override fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
if (intervals.isEmpty()) return arrayOf(newInterval)
val _start = newInterval[0]
val _end = newInterval[1]
if (intervals[0][0] > _end) return Array(intervals.size+1) {
when(it) {
0 -> newInterval
else -> {
intervals[it-1]
}
}
}
if (intervals.last()[1] < _start) return Array(intervals.size+1) {
when(it) {
intervals.size -> newInterval
else -> {
intervals[it]
}
}
}
var open = 0
var p = -1 // previous
var index = 0
fun submit(v: Int, start: Boolean) {
if (start) {
open++
} else {
open--
}
if (open > 0) {
p = Math.min(p, v)
} else {
res += intArrayOf(p, v)
}
p = v
}
while(index < intervals.size) {
val temp = intervals[index]
val _s = temp[0]
val _e = temp[1]
if (p != -1 &&_start>p && _start<=_s) {
submit(_start, true)
}
if (_end>p && _end<=_s) {
submit(_end, false)
}
submit(_s, true)
if (_start>p && _start<=_e) {
submit(_start, true)
}
if (_end>p && _end<=_e) {
submit(_end, false)
}
submit(_e, false)
index++
}
return res.toTypedArray()
}
}
private class S1 : Leetcode_57 {
override fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
var newInterval : IntArray? = newInterval
val res: MutableList<IntArray> = ArrayList()
for (interval in intervals) {
if (newInterval == null || interval[1] < newInterval[0]) res.add(
interval
) else if (interval[0] > newInterval[1]) {
res.add(newInterval)
res.add(interval)
newInterval = null
} else {
newInterval[0] = Math.min(interval[0], newInterval[0])
newInterval[1] = Math.max(interval[1], newInterval[1])
}
}
if (newInterval != null) res.add(newInterval)
return res.toTypedArray()
}
}
}
val _20220812 = listOf<Testable>(
Leetcode_846,
)