You are given a 0-indexed string pattern of length n consisting of the characters ‘I’ meaning increasing and ‘D’ meaning decreasing.
A 0-indexed string num of length n + 1 is created using the following conditions:
num consists of the digits ‘1’ to ‘9’, where each digit is used at most once.
If pattern[i] == ‘I’, then num[i] < num[i + 1].
If pattern[i] == ‘D’, then num[i] > num[i + 1].
Return the lexicographically smallest possible string num that meets the conditions.
Example 1:
Input: pattern = “IIIDIDDD”
Output: “123549876”
Explanation:
At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
Some possible values of num are “245639871”, “135749862”, and “123849765”.
It can be proven that “123549876” is the smallest possible num that meets the conditions.
Note that “123414321” is not possible because the digit ‘1’ is used more than once.
Example 2:
Input: pattern = “DDD”
Output: “4321”
Explanation:
Some possible values of num are “9876”, “7321”, and “8742”.
It can be proven that “4321” is the smallest possible num that meets the conditions.
Constraints:
- 1 <= pattern.length <= 8
- pattern consists of only the letters ‘I’ and ‘D’.
用 back tracing 的方法检查下一步是否可行, 如果可行则直接返回,都不可行的话则记录当前路径,防止重复检查,然后返回。注意无论是’I’还是’D’, 我们要从小到大遍历可能值, 这样才能保证最终的输出结果是最小的。
use std::collections::HashSet;
impl Solution {
fn dp(
pattern: &Vec<char>,
i: usize,
curr: &mut i32,
contained: &mut HashSet<i32>,
cache: &mut HashSet<(usize, i32)>,
) -> bool {
if i == pattern.len() {
return true;
}
let trend = pattern[i];
let last_digit = *curr % 10;
if trend == 'I' {
for d in last_digit + 1..10 {
if contained.contains(&d) {
continue;
}
contained.insert(d);
*curr *= 10;
*curr += d;
if cache.contains(&(i, *curr)) {
*curr -= d;
*curr /= 10;
contained.remove(&d);
continue;
}
if Solution::dp(pattern, i + 1, curr, contained, cache) {
return true;
}
*curr -= d;
*curr /= 10;
contained.remove(&d);
}
cache.insert((i, *curr));
return false;
}
for d in 1..last_digit {
if contained.contains(&d) {
continue;
}
contained.insert(d);
*curr *= 10;
*curr += d;
if cache.contains(&(i, *curr)) {
*curr -= d;
*curr /= 10;
contained.remove(&d);
continue;
}
if Solution::dp(pattern, i + 1, curr, contained, cache) {
return true;
}
*curr -= d;
*curr /= 10;
contained.remove(&d);
}
cache.insert((i, *curr));
return false;
}
pub fn smallest_number(pattern: String) -> String {
for s in 1..10 {
let mut contained = HashSet::new();
contained.insert(s);
let mut ans = s;
if Solution::dp(
&pattern.chars().collect(),
0,
&mut ans,
&mut contained,
&mut HashSet::new(),
) {
return ans.to_string();
}
}
unreachable!()
}
}

给定一个由'I'(递增)和'D'(递减)组成的字符串模式,构造一个长度为n+1的字符串num,使得num遵循模式中的递增或递减条件,并且数字1到9最多使用一次。返回满足条件的字典序最小的num。例如,对于模式'IIIDIDDD',输出'123549876'。问题可以通过回溯法解决,确保始终从小到大尝试数字,以找到最小的可能结果。
1446

被折叠的 条评论
为什么被折叠?



