LeetCode Entry
1291. Sequential Digits
Increasing digit numbers in a range
1291. Sequential Digits medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1419
Problem TLDR
Increasing digit numbers in a range
Intuition
The possible set of numbers is very small. Generate all then filter and sort.
Approach
- iterate over lengths, sliding window over string in an inner loop
Complexity
-
Time complexity: \(O(log^2(n))\)
-
Space complexity: \(O(log^2(n))\)
Code
fun sequentialDigits(l: Int, h: Int) =
(2..9).flatMap{"123456789".windowed(it)}.map{it.toInt()}.filter{it in l..h}
pub fn sequential_digits(l: i32, h: i32) -> Vec<i32> {
(2..10).flat_map(|w|(1..11-w).map(move |x| (x..x+w).fold(0,|r,t|r*10+t)))
.filter(|&x|l<=x&&x<=h).collect()
}
Comments