DEV Community

Cover image for LeetCode Daily Problem - 967. Numbers With Same Consecutive Differences
Upamanyu Das
Upamanyu Das

Posted on

1 1

LeetCode Daily Problem - 967. Numbers With Same Consecutive Differences

Problem

Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k.

Note that every number in the answer must not have leading zeros. For example, 01 has one leading zero and is invalid.

You may return the answer in any order.

Idea

The way to solve this is to construct the number digit by digit. We can pick any digit x from 1 through 9 as the first digit. Once we have picked the digit we have at most two valid choices x + k and x - k i.e. they both lie in [0, 9]. The problem then reduces to solving a smaller problem with similar structure, i.e. with one less digit at every step. When n digits have been picked we can add the number to our answer array.

Solution

class Solution {
    public void dfs (int NumOfDigits, int currNum, int k, List<Integer> ans) {
        if (NumOfDigits == 0) {
            ans.add(currNum);
            return;
        }

        List<Integer> nextDigits = new ArrayList<>();

        int tailDigit = currNum % 10;
        nextDigits.add(tailDigit + k);
        if(k != 0) {
            nextDigits.add(tailDigit - k);
        }

        for(int nextDigit : nextDigits) {
            if(0 <= nextDigit && nextDigit < 10) {
                int num = currNum * 10 + nextDigit;
                dfs(NumOfDigits - 1, num, k, ans);
            }
        }
    }

    public int[] numsSameConsecDiff(int n, int k) {
        if (n == 1) {
            return new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        }

        List<Integer> ans = new LinkedList<Integer>();

        for (int i = 1; i < 10; i++) {
            dfs(n - 1, i, k, ans);
        }

        return ans.stream().mapToInt(i -> i).toArray();

    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 9 ms, faster than 21.14% of Java online submissions for Numbers With Same Consecutive Differences.

Memory Usage: 44.2 MB, less than 6.62% of Java online submissions for Numbers With Same Consecutive Differences.

Note - The BFS solution of this problem will be much more memory efficient as the recursion stack will not have to be dealt with.

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay