Skip to content

Commit 4634b99

Browse files
authored
Added task 2801
1 parent f70a79b commit 4634b99

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g2801_2900.s2801_count_stepping_numbers_in_range
2+
3+
// #Hard #String #Dynamic_Programming #2024_01_19_Time_288_ms_(100.00%)_Space_38.2_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
private lateinit var dp: Array<Array<Array<Array<Int?>>>>
9+
10+
fun countSteppingNumbers(low: String, high: String): Int {
11+
dp = Array(low.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } }
12+
val count1 = solve(low, 0, 0, 1, 1)
13+
dp = Array(high.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } }
14+
val count2 = solve(high, 0, 0, 1, 1)
15+
return (count2!! - count1!! + isStep(low) + MOD) % MOD
16+
}
17+
18+
private fun solve(s: String, i: Int, prevDigit: Int, hasBound: Int, curIsZero: Int): Int? {
19+
if (i >= s.length) {
20+
if (curIsZero == 1) {
21+
return 0
22+
}
23+
return 1
24+
}
25+
if (dp[i][prevDigit][hasBound][curIsZero] != null) {
26+
return dp[i][prevDigit][hasBound][curIsZero]
27+
}
28+
var count = 0
29+
var limit = 9
30+
if (hasBound == 1) {
31+
limit = s[i].code - '0'.code
32+
}
33+
for (digit in 0..limit) {
34+
val nextIsZero = if ((curIsZero == 1 && digit == 0)) 1 else 0
35+
val nextHasBound = if ((hasBound == 1 && digit == limit)) 1 else 0
36+
if (curIsZero == 1 || abs(digit - prevDigit) == 1) {
37+
count = (count + solve(s, i + 1, digit, nextHasBound, nextIsZero)!!) % MOD
38+
}
39+
}
40+
dp[i][prevDigit][hasBound][curIsZero] = count
41+
return dp[i][prevDigit][hasBound][curIsZero]
42+
}
43+
44+
private fun isStep(s: String): Int {
45+
var isValid = true
46+
for (i in 0 until s.length - 1) {
47+
if (abs((s[i + 1].code - s[i].code)) != 1) {
48+
isValid = false
49+
break
50+
}
51+
}
52+
if (isValid) {
53+
return 1
54+
}
55+
return 0
56+
}
57+
58+
companion object {
59+
private const val MOD = (1e9 + 7).toInt()
60+
}
61+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2801\. Count Stepping Numbers in Range
2+
3+
Hard
4+
5+
Given two positive integers `low` and `high` represented as strings, find the count of **stepping numbers** in the inclusive range `[low, high]`.
6+
7+
A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of **exactly** `1`.
8+
9+
Return _an integer denoting the count of stepping numbers in the inclusive range_ `[low, high]`_._
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Note:** A stepping number should not have a leading zero.
14+
15+
**Example 1:**
16+
17+
**Input:** low = "1", high = "11"
18+
19+
**Output:** 10
20+
21+
**Explanation:** The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10.
22+
23+
**Example 2:**
24+
25+
**Input:** low = "90", high = "101"
26+
27+
**Output:** 2
28+
29+
**Explanation:** The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= int(low) <= int(high) < 10<sup>100</sup></code>
34+
* `1 <= low.length, high.length <= 100`
35+
* `low` and `high` consist of only digits.
36+
* `low` and `high` don't have any leading zeros.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2801_2900.s2801_count_stepping_numbers_in_range
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun countSteppingNumbers() {
10+
assertThat(Solution().countSteppingNumbers("1", "11"), equalTo(10))
11+
}
12+
13+
@Test
14+
fun countSteppingNumbers2() {
15+
assertThat(Solution().countSteppingNumbers("90", "101"), equalTo(2))
16+
}
17+
}

0 commit comments

Comments
 (0)