-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_1363.kt
29 lines (28 loc) · 1.25 KB
/
LC_1363.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
class LC1363 {
fun largestMultipleOfThree(digits: IntArray): String? {
Arrays.sort(digits)
val remain1Indices: MutableList<Int> = ArrayList(2)
val remain2Indices: MutableList<Int> = ArrayList(2)
for (i in digits.indices) {
if (digits[i] % 3 == 1 && remain1Indices.size < 2) remain1Indices.add(i)
else if (digits[i] % 3 == 2 && remain2Indices.size < 2) remain2Indices.add(i)
}
val remainSum = Arrays.stream(digits).sum() % 3
if (remainSum == 1) {
return if (remain1Indices.size >= 1) getResult(digits, remain1Indices[0], -1)
else getResult(digits, remain2Indices[0], remain2Indices[1])
} else if (remainSum == 2) {
return if (remain2Indices.size >= 1) getResult(digits, remain2Indices[0], -1)
else getResult(digits, remain1Indices[0], remain1Indices[1])
}
return getResult(digits, -1, -1)
}
private fun getResult(digits: IntArray, ban1: Int, ban2: Int): String {
val sb = StringBuilder()
for (i in digits.indices.reversed()) {
if (i == ban1 || i == ban2) continue
sb.append(digits[i])
}
return if (sb.isNotEmpty() && sb[0] == '0') "0" else sb.toString()
}
}