-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_20220620.kt
149 lines (134 loc) · 4.39 KB
/
_20220620.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
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
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._06
import Testable
import utils.assertEqualTo
import utils.forEachPair
import utils.runTimedTests
import utils.toMatrixStr
// 54. Spiral Matrix
// https://leetcode.com/problems/spiral-matrix/
private interface Leetcode_54 {
fun spiralOrder(matrix: Array<IntArray>): List<Int>
companion object : Testable {
override fun test() {
val tests = listOf(
arrayOf(
intArrayOf(1,2,3),
intArrayOf(4,5,6),
intArrayOf(7,8,9),
) to listOf(1,2,3,6,9,8,7,4,5),
arrayOf(
intArrayOf(1,2,3,4),
intArrayOf(5,6,7,8),
intArrayOf(9,10,11,12),
intArrayOf(13,14,15,16)
) to listOf(1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10)
)
listOf(
M2()::spiralOrder,
).runTimedTests {
tests.forEachPair { first, second ->
invoke(first).assertEqualTo(second)
}
}
}
}
// not working
private class M1 : Leetcode_54 {
private fun tryAdd(
i: Int, k: Int, m: Int, n: Int,
list: MutableList<Int>,
matrix: Array<IntArray>,
map: Array<BooleanArray>
) : Boolean {
return if (i < m && k < n && i >= 0 && k >= 0 && !map[i][k]) {
list.add(matrix[i][k])
map[i][k] = true
true
} else false
}
override fun spiralOrder(matrix: Array<IntArray>): List<Int> {
matrix.toMatrixStr()
val m = matrix.size
val n = matrix[0].size
val map = Array<BooleanArray>(m){
BooleanArray(n){ false }
}
val list = ArrayList<Int>(m * n)
var i = 0
var k = -1
while (true) {
when{
tryAdd(i, k+1, m, n, list, matrix, map) -> {
k ++
}
tryAdd( i+1, k, m, n, list, matrix, map) -> {
i++
}
tryAdd(i, k-1, m, n, list, matrix, map) -> {
k--
}
tryAdd(i-1, k, m, n, list, matrix, map) -> {
i--
}
// else -> break
else -> return list
}
}
// return list
// 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 10, 11, 7, 6, 5
}
}
private class M2 : Leetcode_54 {
private lateinit var matrix: Array<IntArray>
private val position = intArrayOf(0, -1)
private lateinit var mn : Pair<Int, Int>
private lateinit var map: Array<BooleanArray>
private fun tryAdd(operation: Int, list: ArrayList<Int>) : Boolean {
var i: Int = position[0]
var k: Int = position[1]
val m = mn.first
val n = mn.second
when(operation) {
1 -> i++
2 -> i--
3 -> k++
0 -> k--
else -> throw Exception("unsupported operation:$operation")
}
return if (i < m && k < n && i >= 0 && k >= 0 && !map[i][k]) {
list.add(matrix[i][k])
map[i][k] = true
position[0] = i
position[1] = k
true
} else false
}
override fun spiralOrder(matrix: Array<IntArray>): List<Int> {
matrix.toMatrixStr()
this.matrix = matrix
mn = matrix.size to matrix[0].size
val size = mn.run { first * second }
map = Array<BooleanArray>(mn.first){
BooleanArray(mn.second){ false }
}
val list = ArrayList<Int>(size)
position[0] = 0
position[1] = -1
var ops = 0
for (i in 0 until size) {
while (true) {
ops = ops % 4
if (!tryAdd(ops, list)) {
ops++
} else break
}
}
return list
}
}
// https://labuladong.github.io/algo/2/18/24/
}
fun main() {
Leetcode_54.test()
}