-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220619.kt
More file actions
216 lines (184 loc) · 5.96 KB
/
_20220619.kt
File metadata and controls
216 lines (184 loc) · 5.96 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
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._06
import Testable
import utils.*
// Leetcode 370: Range Addition
// https://leetcode.ca/all/370.html
// https://baihuqian.github.io/2018-08-16-range-addition/
/**
* Assume you have an array of length n initialized with all 0’s and are given k update operations.
*
* Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex … endIndex] (startIndex and endIndex inclusive) with inc.
*
* Return the modified array after all k operations were executed.
*
* Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
* Output: [-2,0,3,5,3]
*
* Initial state:
* [ 0, 0, 0, 0, 0 ]
* After applying operation [1, 3, 2]:
* [ 0, 2, 2, 2, 0 ]
* After applying operation [2, 4, 3]:
* [ 0, 2, 5, 5, 3 ]
* After applying operation [0, 2, -2]:
* [-2, 0, 3, 5, 3 ]
*
*
* */
private interface Leetcode_370 {
fun getModifiedArray(length: Int, updates: Array<IntArray>) : IntArray
companion object : Testable {
override fun test() {
listOf(
tupleOf(
arrayOf(
intArrayOf(1, 3, 2),
intArrayOf(2, 4, 3),
intArrayOf(0, 2, -2),
),
5,
intArrayOf(-2, 0, 3, 5, 3)
)
).runTimedTests {
}
val tests = listOf(
tupleOf(
arrayOf(
intArrayOf(1, 3, 2),
intArrayOf(2, 4, 3),
intArrayOf(0, 2, -2),
),
5,
intArrayOf(-2, 0, 3, 5, 3)
)
)
listOf(
M1()::getModifiedArray,
).runTimedTests {
tests.forEachTuple { first, second, third ->
val array = first.clone()
invoke(second, array).assertEqualTo(third)
}
}
}
}
private class M1 : Leetcode_370 {
override fun getModifiedArray(length: Int, updates: Array<IntArray>): IntArray {
TODO("Not yet implemented")
}
}
}
private interface Leetcode_1094 {
fun carPooling(trips: Array<IntArray>, capacity: Int): Boolean
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(
arrayOf(intArrayOf(2,1,5), intArrayOf(3,3,7),),
4,
false
),
tupleOf(
arrayOf(intArrayOf(2,1,5), intArrayOf(3,3,7),),
5,
true
),
)
listOf(
M1()::carPooling,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third)
}
}
}
}
private class M1 : Leetcode_1094 {
override fun carPooling(trips: Array<IntArray>, capacity: Int): Boolean {
val tripDiff = IntArray(1001)
for (trip in trips) {
tripDiff[trip[1]] += trip[0]
tripDiff[trip[2]] -= trip[0]
}
val result = IntArray(tripDiff.size)
var counter = 0
for (i in tripDiff.indices) {
counter += tripDiff[i]
result[i] = counter
if (counter > capacity) return false
}
return true
}
}
}
// 1109. Corporate Flight Bookings
// https://leetcode.com/problems/corporate-flight-bookings/
// 48. Rotate Image
// https://leetcode.com/problems/rotate-image/
// 先把二维矩阵沿对角线反转,然后反转矩阵的每一行,结果就是顺时针反转整个矩阵。
private interface Leetcode_48 {
fun rotate(matrix: Array<IntArray>): Unit
companion object : Testable {
override fun test() {
val tests = listOf<Pair<Matrix, Matrix>>(
arrayOf(
intArrayOf(1,2,3),
intArrayOf(4,5,6),
intArrayOf(7,8,9),
) to arrayOf(
intArrayOf(7,4,1),
intArrayOf(8,5,2),
intArrayOf(9,6,3),
),
arrayOf(
intArrayOf(5,1,9,11),
intArrayOf(2,4,8,10),
intArrayOf(13,3,6,7),
intArrayOf(15,14,12,16),
) to arrayOf(
intArrayOf(15,13,2,5),
intArrayOf(14,3,4,1),
intArrayOf(12,6,8,9),
intArrayOf(16,7,10,11),
),
)
with(M1()) {
tests.forEachPair { first, second ->
rotate(first)
}
}
listOf(
M1()::rotate,
).runTimedTests {
tests.forEachPair { first, second ->
first.deepClone().also {
invoke(it)
}.assertEqualTo(second, toStr = { toMatrixStr(false) })
}
}
}
}
private class M1 : Leetcode_48 {
override fun rotate(matrix: Array<IntArray>) {
println("original matrix: ${matrix.toMatrixStr()}")
val size = matrix.size
for (i in matrix.indices) {
for (j in i until size) {
val temp = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = temp
}
}
for (n in matrix) {
n.reverse()
}
println("rotated matrix: ${matrix.toMatrixStr()}")
}
}
}
fun main() {
Leetcode_370.test()
Leetcode_1094.test()
// Leetcode_48.test()
}