Skip to content

Commit 29a5ed7

Browse files
authored
Added task 2801
1 parent 806a282 commit 29a5ed7

File tree

4 files changed

+111
-9
lines changed
  • src/main/kotlin
    • g2801_2900/s2801_count_stepping_numbers_in_range
    • g2901_3000
      • s2973_find_number_of_coins_to_place_in_tree_nodes
      • s2983_palindrome_rearrangement_queries

4 files changed

+111
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,7 @@
19681968
| 2808 |[Minimum Seconds to Equalize a Circular Array](src/main/kotlin/g2801_2900/s2808_minimum_seconds_to_equalize_a_circular_array)| Medium | Array, Hash_Table, Greedy | 847 | 50.00
19691969
| 2807 |[Insert Greatest Common Divisors in Linked List](src/main/kotlin/g2801_2900/s2807_insert_greatest_common_divisors_in_linked_list)| Medium | Array, Math, Linked_List | 225 | 67.65
19701970
| 2806 |[Account Balance After Rounded Purchase](src/main/kotlin/g2801_2900/s2806_account_balance_after_rounded_purchase)| Easy | Math | 108 | 100.00
1971+
| 2801 |[Count Stepping Numbers in Range](src/main/kotlin/g2801_2900/s2801_count_stepping_numbers_in_range)| Hard | String, Dynamic_Programming | 288 | 100.00
19711972
| 2800 |[Shortest String That Contains Three Strings](src/main/kotlin/g2701_2800/s2800_shortest_string_that_contains_three_strings)| Medium | String, Greedy, Enumeration | 259 | 100.00
19721973
| 2799 |[Count Complete Subarrays in an Array](src/main/kotlin/g2701_2800/s2799_count_complete_subarrays_in_an_array)| Medium | Array, Hash_Table, Sliding_Window | 206 | 96.97
19731974
| 2798 |[Number of Employees Who Met the Target](src/main/kotlin/g2701_2800/s2798_number_of_employees_who_met_the_target)| Easy | Array, Enumeration | 153 | 92.50
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2801\. Count Stepping Numbers in Range
5+
6+
Hard
7+
8+
Given two positive integers `low` and `high` represented as strings, find the count of **stepping numbers** in the inclusive range `[low, high]`.
9+
10+
A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of **exactly** `1`.
11+
12+
Return _an integer denoting the count of stepping numbers in the inclusive range_ `[low, high]`_._
13+
14+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
15+
16+
**Note:** A stepping number should not have a leading zero.
17+
18+
**Example 1:**
19+
20+
**Input:** low = "1", high = "11"
21+
22+
**Output:** 10
23+
24+
**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.
25+
26+
**Example 2:**
27+
28+
**Input:** low = "90", high = "101"
29+
30+
**Output:** 2
31+
32+
**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.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= int(low) <= int(high) < 10<sup>100</sup></code>
37+
* `1 <= low.length, high.length <= 100`
38+
* `low` and `high` consist of only digits.
39+
* `low` and `high` don't have any leading zeros.
40+
41+
## Solution
42+
43+
```kotlin
44+
import kotlin.math.abs
45+
46+
class Solution {
47+
private lateinit var dp: Array<Array<Array<Array<Int?>>>>
48+
49+
fun countSteppingNumbers(low: String, high: String): Int {
50+
dp = Array(low.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } }
51+
val count1 = solve(low, 0, 0, 1, 1)
52+
dp = Array(high.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } }
53+
val count2 = solve(high, 0, 0, 1, 1)
54+
return (count2!! - count1!! + isStep(low) + MOD) % MOD
55+
}
56+
57+
private fun solve(s: String, i: Int, prevDigit: Int, hasBound: Int, curIsZero: Int): Int? {
58+
if (i >= s.length) {
59+
if (curIsZero == 1) {
60+
return 0
61+
}
62+
return 1
63+
}
64+
if (dp[i][prevDigit][hasBound][curIsZero] != null) {
65+
return dp[i][prevDigit][hasBound][curIsZero]
66+
}
67+
var count = 0
68+
var limit = 9
69+
if (hasBound == 1) {
70+
limit = s[i].code - '0'.code
71+
}
72+
for (digit in 0..limit) {
73+
val nextIsZero = if ((curIsZero == 1 && digit == 0)) 1 else 0
74+
val nextHasBound = if ((hasBound == 1 && digit == limit)) 1 else 0
75+
if (curIsZero == 1 || abs(digit - prevDigit) == 1) {
76+
count = (count + solve(s, i + 1, digit, nextHasBound, nextIsZero)!!) % MOD
77+
}
78+
}
79+
dp[i][prevDigit][hasBound][curIsZero] = count
80+
return dp[i][prevDigit][hasBound][curIsZero]
81+
}
82+
83+
private fun isStep(s: String): Int {
84+
var isValid = true
85+
for (i in 0 until s.length - 1) {
86+
if (abs((s[i + 1].code - s[i].code)) != 1) {
87+
isValid = false
88+
break
89+
}
90+
}
91+
if (isValid) {
92+
return 1
93+
}
94+
return 0
95+
}
96+
97+
companion object {
98+
private const val MOD = (1e9 + 7).toInt()
99+
}
100+
}
101+
```

src/main/kotlin/g2901_3000/s2973_find_number_of_coins_to_place_in_tree_nodes/readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ class Solution {
103103
for (ne in next) {
104104
if (ne != p) {
105105
val r = dp(g, cost, ne, i)
106-
while (!r.min!!.isEmpty()) {
106+
while (r.min!!.isNotEmpty()) {
107107
val a = r.min!!.poll()
108108
pq2.add(a)
109109
}
110-
while (!r.max!!.isEmpty()) {
110+
while (r.max!!.isNotEmpty()) {
111111
val a = r.max!!.poll()
112112
pq.add(a)
113113
}
@@ -116,11 +116,11 @@ class Solution {
116116
if (pq.size + pq2.size < 3) {
117117
result[i] = 1
118118
} else {
119-
val a = if (!pq.isEmpty()) pq.poll() else 0
120-
val b = if (!pq.isEmpty()) pq.poll() else 0
121-
val c = if (!pq.isEmpty()) pq.poll() else 0
122-
val aa = if (!pq2.isEmpty()) pq2.poll() else 0
123-
val bb = if (!pq2.isEmpty()) pq2.poll() else 0
119+
val a = if (pq.isNotEmpty()) pq.poll() else 0
120+
val b = if (pq.isNotEmpty()) pq.poll() else 0
121+
val c = if (pq.isNotEmpty()) pq.poll() else 0
122+
val aa = if (pq2.isNotEmpty()) pq2.poll() else 0
123+
val bb = if (pq2.isNotEmpty()) pq2.poll() else 0
124124
result[i] = max(0, (a.toLong() * b * c))
125125
result[i] = max(result[i], max(0, (a.toLong() * aa * bb)))
126126
.toLong()

src/main/kotlin/g2901_3000/s2983_palindrome_rearrangement_queries/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Solution {
163163
// in the left half to match the right end of the right interval, this
164164
// means we do not need the right end of the right interval to go too far
165165
while (mp.containsKey(s[rptr]) ||
166-
(rptr >= n / 2 && s[rptr] == s[opp(rptr)] && mp.size == 0)
166+
(rptr >= n / 2 && s[rptr] == s[opp(rptr)] && mp.isEmpty())
167167
) {
168168
mp.computeIfPresent(s[rptr]) { _: Char?, v: Int -> if (v == 1) null else v - 1 }
169169
rptr--
@@ -177,7 +177,7 @@ class Solution {
177177
for (i in opp(problemPoint) downTo n / 2) {
178178
mp.compute(s[i]) { _: Char?, v: Int? -> if (v == null) 1 else v + 1 }
179179
while (mp.containsKey(s[lptr]) ||
180-
(lptr < n / 2 && s[lptr] == s[opp(lptr)] && mp.size == 0)
180+
(lptr < n / 2 && s[lptr] == s[opp(lptr)] && mp.isEmpty())
181181
) {
182182
mp.computeIfPresent(s[lptr]) { _: Char?, v: Int -> if (v == 1) null else v - 1 }
183183
lptr++

0 commit comments

Comments
 (0)