-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
49 lines (44 loc) · 1.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
/**
* Created by Inno Fang on 2018/3/3.
*/
/**
* 302 / 302 test cases passed.
* Status: Accepted
* Runtime: 520 ms
*/
class Solution {
fun monotoneIncreasingDigits(N: Int): Int {
val src = N.toString()
var res = ""
src.toCharArray().forEachIndexed { idx, c ->
(c..'9').forEach { num ->
if (src < res + Array(src.length - idx) { num }.joinToString("")) {
res += (num - 1)
return@forEachIndexed
}
}
res += '9'
}
return res.toInt()
}
}
/**
* 302 / 302 test cases passed.
* Status: Accepted
* Runtime: 228 ms
*/
class Solution2 {
fun monotoneIncreasingDigits(N: Int): Int {
val nums = N.toString().toCharArray()
var i = 1
while (i < nums.size && nums[i-1] <= nums[i]) i++
while (i > 0 && i < nums.size && nums[i-1] > nums[i]) nums[--i]--
(i+1..nums.lastIndex).forEach { nums[it] = '9' }
return nums.joinToString("").toInt()
}
}
fun main(args: Array<String>) {
Solution2().monotoneIncreasingDigits(332).let(::println)
Solution2().monotoneIncreasingDigits(1234).let(::println)
Solution2().monotoneIncreasingDigits(10).let(::println)
}