One line to save them all!

This problem, given the constraints, could be solved via brute-force since we're dealing with at the most 9! permutations, which is a tiny number. However, it was leading to TLE. The one liner which saved them all was the one here. Basically, I just needed to know if the current number was already larger than the smallest one that I have already found. If so, it is pointless to continue. Code is down below, cheers, ACC.

Construct Smallest Number From DI String - LeetCode

2375. Construct Smallest Number From DI String
Medium

You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.

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'.

public string SmallestNumber(string pattern)
{
    long retVal = Int64.MaxValue;

    SmallestNumber(pattern, 0L, 1, new Hashtable(), ref retVal);
    return retVal.ToString();
}

private void SmallestNumber(string pattern,
                            long current,
                            long power,
                            Hashtable usedDigit,
                            ref long smallestVal)
{
    if (usedDigit.Count == pattern.Length + 1)
    {
        if (ValidStringPattern(pattern, current.ToString()))
        {
            smallestVal = Math.Min(smallestVal, current);
        }
        return;
    }

    if (smallestVal != Int64.MaxValue && smallestVal % power < current) return;

    for (int i = 1; i <= 9; i++)
    {
        if (!usedDigit.ContainsKey(i))
        {
            usedDigit.Add(i, true);
            SmallestNumber(pattern, 10 * current + i, power * 10, usedDigit, ref smallestVal);
            usedDigit.Remove(i);
        }
    }
}

private bool ValidStringPattern(string pattern, string str)
{
    for (int i = 0; i < str.Length - 1; i++)
    {
        if (pattern[i] == 'I' && ((int)(str[i] - '0') >= (int)(str[i + 1] - '0'))) return false;
        if (pattern[i] == 'D' && ((int)(str[i] - '0') <= (int)(str[i + 1] - '0'))) return false;
    }

    return true;
}

Comments

Popular posts from this blog

Changing the root of a binary tree

Prompt Engineering and LeetCode

ProjectEuler Problem 719 (some hints, but no spoilers)