Count Good Integers on a Grid Path Problem
Description
LeetCode Problem 3906.
You are given two integers l and r, and a string directions consisting of exactly three 'D' characters and three 'R' characters.
For each integer x in the range [l, r] (inclusive), perform the following steps:
- If
xhas fewer than 16 digits, pad it on the left with leading zeros to obtain a 16-digit string. - Place the 16 digits into a
4 × 4grid in row-major order (the first 4 digits form the first row from left to right, the next 4 digits form the second row, and so on). - Starting at the top-left cell (
row = 0,column = 0), apply the 6 characters ofdirectionsin order:'D'increments the row by 1.'R'increments the column by 1.
- Record the sequence of digits visited along the path (including the starting cell), producing a sequence of length 7.
The integer x is considered good if the recorded sequence is non-decreasing.
Return an integer representing the number of good integers in the range [l, r].
Example 1:
Input: l = 8, r = 10, directions = "DDDRRR"
Output: 2
Explanation:
The grid for x = 8:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 8
- Path: (0,0) → (1,0) → (2,0) → (3,0) → (3,1) → (3,2) → (3,3)
- The sequence of digits visited is [0, 0, 0, 0, 0, 0, 8].
- As the sequence of digits visited is non-decreasing, 8 is a good integer.
The grid for x = 9:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 9
- The sequence of digits visited is [0, 0, 0, 0, 0, 0, 9].
- As the sequence of digits visited is non-decreasing, 9 is a good integer.
The grid for x = 10:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 1 0
- The sequence of digits visited is [0, 0, 0, 0, 0, 1, 0].
- As the sequence of digits visited is not non-decreasing, 10 is not a good integer.
- Hence, only 8 and 9 are good, giving a total of 2 good integers in the range.
Example 2:
Input: l = 123456789, r = 123456790, directions = "DDRRDR"
Output: 1
Explanation:
The grid for x = 123456789:
0 0 0 0
0 0 0 1
2 3 4 5
6 7 8 9
- Path: (0,0) → (1,0) → (2,0) → (2,1) → (2,2) → (3,2) → (3,3)
- The sequence of digits visited is [0, 0, 2, 3, 4, 8, 9].
- As the sequence of digits visited is non-decreasing, 123456789 is a good integer.
The grid for x = 123456790:
0 0 0 0
0 0 0 1
2 3 4 5
6 7 9 0
- The sequence of digits visited is [0, 0, 2, 3, 4, 9, 0].
- As the sequence of digits visited is not non-decreasing, 123456790 is not a good integer.
- Hence, only 123456789 is good, giving a total of 1 good integer in the range.
Example 3:
Input: l = 1288561398769758, r = 1288561398769758, directions = "RRRDDD"
Output: 0
Explanation:
The grid for x = 1288561398769758:
1 2 8 8
5 6 1 3
9 8 7 6
9 7 5 8
- Path: (0,0) → (0,1) → (0,2) → (0,3) → (1,3) → (2,3) → (3,3)
- The sequence of digits visited is [1, 2, 8, 8, 3, 6, 8].
- As the sequence of digits visited is not non-decreasing, 1288561398769758 is not a good integer.
- No numbers are good, giving a total of 0 good integers in the range.
Constraints:
1 <= l <= r <= 9 × 10^15directions.length == 6directionsconsists of exactly three'D'characters and three'R'characters.
Solution
Approach: Digit DP
The 6 moves in directions fix which 7 of the 16 cells are on the path. Build a boolean array key of length 16, where key[i] says whether cell i (in row-major order, so cell i is the i-th digit of the padded number) is on the path.
Count good integers in [0, x] with digit DP and answer with count(r) - count(l - 1). Pad x to 16 digits as the string s and define dfs(pos, last, lim):
posis the digit position being chosen,lastis the digit at the previous path cell,limtells whether the digits chosen so far equal the prefix ofs(so the next digit is capped bys[pos]).
When pos == 16, one valid number was built, so return 1. Otherwise, the digit ranges from last (if key[pos] is true, since path digits must not decrease) or from 0 (if the cell is off the path) up to s[pos] if lim is true, else 9. For each digit, recurse with last updated when the cell is on the path, and lim kept only if the digit equals the cap. Memoize the states where lim is false.
Complexity: time O(D^2 log r) and space O(D log r), where D = 10 is the number of digit values and log r is the number of digits (16).
from functools import lru_cache
class Solution:
def countGoodIntegersOnPath(self, l: int, r: int, directions: str) -> int:
key = [False] * 16
row, col = 0, 0
key[0] = True
for c in directions:
if c == "D":
row += 1
else:
col += 1
key[row * 4 + col] = True
s = ""
@lru_cache(None)
def dfs(pos, last, lim):
if pos == 16:
return 1
res = 0
start = last if key[pos] else 0
end = int(s[pos]) if lim else 9
for i in range(start, end + 1):
res += dfs(pos + 1, i if key[pos] else last, lim and (i == end))
return res
def calc(x):
nonlocal s
if x < 0:
return 0
s = str(x).zfill(16)
dfs.cache_clear()
return dfs(0, 0, True)
return calc(r) - calc(l - 1)