Numbers With Same Consecutive Differences
Given two integers n and k, return an array of all the integers of length n
where the difference between every two consecutive digits is k
. You may return the answer in any order.
Note that the integers should not have leading zeros. Integers as 02
and 043
are not allowed.
Example 1:
Input: n = 3, k = 7
Output: [181,292,707,818,929]
Explanation: Note that 070
is not a valid number, because it has leading zeroes.
Example 2:
Input: n = 2, k = 1
Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Constraints:
2 <= n <= 9
0 <= k <= 9
Solution
We want to build up the numbers from the leftmost digit to the rightmost.
Since n
is >= 2 and there should not be leading zeros, we will start from one digit (1-9) and call our backtracking helper to construct the next digits.
Here, our path
(num
in the implementation) will store an integer instead of a list to save space and time.
We want to apply backtracking1 template. We fill in the logic:
is_leaf
:start_index == n
, when the constucted number containsn
digits.get_edges
: the next digit can becur_digit +- k
is_valid
:cur_digit + k <= 9
andcur_digit - k >= 0
to avoid the next digit being out of bound. Note that in the implementation, ifk=0
thencur_digit+k == cur_digit - k
, we must avoid the duplicate by checking whetherk=0
.
Implementation
1def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
2 def dfs(start_index, num):
3 if start_index == n: # num is n digits, add to ans
4 ans.append(num)
5 return
6 cur_digit = num % 10 # current digit
7 if (cur_digit - k >= 0):
8 dfs(start_index + 1, num * 10 + (cur_digit - k))
9 if (cur_digit + k <= 9 and k != 0): # avoid duplicate when k = 0
10 dfs(start_index + 1, num * 10 + (cur_digit + k))
11 ans = []
12 for i in range(1, 10): # first digit can be 1-9
13 dfs(1, i)
14 return ans
Which of the following problems can be solved with backtracking (select multiple)
Depth first search can be used to find whether two components in a graph are connected.
Solution Implementation
Given a sorted array of integers and an integer called target, find the element that
equals to the target and return its index. Select the correct code that fills the
___
in the given code snippet.
1def binary_search(arr, target):
2 left, right = 0, len(arr) - 1
3 while left ___ right:
4 mid = (left + right) // 2
5 if arr[mid] == target:
6 return mid
7 if arr[mid] < target:
8 ___ = mid + 1
9 else:
10 ___ = mid - 1
11 return -1
12
1public static int binarySearch(int[] arr, int target) {
2 int left = 0;
3 int right = arr.length - 1;
4
5 while (left ___ right) {
6 int mid = left + (right - left) / 2;
7 if (arr[mid] == target) return mid;
8 if (arr[mid] < target) {
9 ___ = mid + 1;
10 } else {
11 ___ = mid - 1;
12 }
13 }
14 return -1;
15}
16
1function binarySearch(arr, target) {
2 let left = 0;
3 let right = arr.length - 1;
4
5 while (left ___ right) {
6 let mid = left + Math.trunc((right - left) / 2);
7 if (arr[mid] == target) return mid;
8 if (arr[mid] < target) {
9 ___ = mid + 1;
10 } else {
11 ___ = mid - 1;
12 }
13 }
14 return -1;
15}
16
How does merge sort divide the problem into subproblems?
Recommended Readings
Top Patterns to Conquer the Technical Coding Interview Should the written word bore you fear not A delightful video alternative awaits iframe width 560 height 315 src https www youtube com embed LW8Io6IPYHw title YouTube video player frameborder 0 allow accelerometer autoplay clipboard write encrypted media gyroscope picture in picture
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Got a question? Ask the Teaching Assistant anything you don't understand.
Still not clear? Ask in the Forum, Discord or Submit the part you don't understand to our editors.