forked from igorwojda/kotlin-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
69 lines (57 loc) · 2.24 KB
/
solution.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
package com.igorwojda.matrix.findrectangle
// Time complexity: O(n*m)
private object Solution1 {
private fun findRectangle(image: List<List<Int>>): List<Int>? {
// This helper function may be used with challange variation where multiple rectangles exists in the image
fun getRectangleFromTopLeftCorner(rowIndex: Int, columnIndex: Int): List<Int> {
// find right x coordinate
var rightColumnIndex = columnIndex
while (image.getOrNull(rowIndex)?.getOrNull(rightColumnIndex + 1) == 0) {
rightColumnIndex++
}
// find bottom y coordinate
var bottomRowIndex = rowIndex
while (image.getOrNull(bottomRowIndex + 1)?.getOrNull(columnIndex) == 0) {
bottomRowIndex++
}
return listOf(rowIndex, columnIndex, bottomRowIndex, rightColumnIndex)
}
image.forEachIndexed { rowIndex, rowItem ->
rowItem.forEachIndexed { columnIndex, pixelValue ->
if (pixelValue == 0) {
return getRectangleFromTopLeftCorner(rowIndex, columnIndex)
}
}
}
return null
}
}
// Time complexity: O(n*m)
private object Solution2 {
private fun findRectangle(image: List<List<Int>>): List<Int>? {
var top: Pair<Int, Int>? = null
var bottom: Pair<Int, Int>? = null
image.forEachIndexed { rowIndex, rowItem ->
rowItem.forEachIndexed { columnIndex, pixelValue ->
if (pixelValue == 0) {
if (top == null) {
top = rowIndex to columnIndex
} else {
bottom = rowIndex to columnIndex
}
}
}
}
// handle edge case where rectangle has 1 pixel
if (bottom == null) {
bottom = top
}
return if (top == null || bottom == null) {
null
} else {
// Smart cast does not work with inlined lambdas so we have to use non-null assertion operator
// More: https://youtrack.jetbrains.com/issue/KT-7186
listOf(top!!.first, top!!.second, bottom!!.first, bottom!!.second)
}
}
}