backtracking + misc Flashcards

1
Q

Given an integer array nums of unique elements, return all possible subsets.

A

standard backtracking; we proceed with both path 1 - include current num, and path 2 - don’t include current num. no for loop

time: O(n * 2^n)
2^(n - 1) subsets, and O(n) to copy each subset into output.

space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given an array of distinct ints and a target, return a list of all unique combinations of nums where the chosen nums sum to target. The same number may be chosen from candidates an unlimited number of times

A

remember to check for valid sum conditions. we pursue 2 paths: 1 - add current num again, 2 - don’t add current num and move onto the next num

time: we make 2 decisions at each step, and worst case, the height of our tree is == target, if all nums are 1. O(2 ^ target)

space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

https://leetcode.com/problems/combinations/
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

A

include the num or don’t include it

time: at most n decisions at each step (but most steps have less), height of tree is k, O(k) time to copy each combination. loose upper bound is O(k * n^k)

space: O(k)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given an array nums of distinct integers, return all the possible permutations.

A

use a set and start for-loop at 0 every time. skip num if it’s in the set. add to set, add to temp. after rec call, remove

time: O(n * n!); space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given an integer array nums that may contain duplicates, return all possible subsets.

A

same as subsets 1 but SORT and add this line: if i != idx and nums[i] == nums[i - 1], continue

time: O(n * 2^n)
space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in (candidates) where the candidate numbers sum to (target).

A

SORT the input array and do
i != idx and nums[i] == nums[i - 1], continue

time: O(n * 2^n)
space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.

A

instead of iterating thru input array, create map { num: count } and iterate thru its keyset instead.

if num’s count == 0, continue. and then go down the 2 paths, remember to update map each time

time: O(n * n!)
space: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

https://leetcode.com/problems/restore-ip-addresses/description/

A
  • impossible if len == 0 or len == 12
  • recursive function: return when there are too many chars left in string
  • for loop thru nums 1 to 3; these are the nums you add to sIdx to get the substring you’re currently considering. (return if sIdx + i > s.length()). must consider num == 0 separately since no leading 0s allowed (call helper and break)

TC: we need to separate the input string into 4 integers, each integer is at most 3 digits. O(4 * 3^4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

https://leetcode.com/problems/partition-to-k-equal-sum-subsets/

A

TC: O(k * 2^n). either we include the num, or we don’t include it; that’s 2^(n - 1). we must generate k subsets.

if k == 0 return true;
if currSum == goalSum, call helper(), but with idx = 0.

maintain a used[] array to keep track of which nums have been used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

A

O(n * 4^n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/description/

A

pass in a ‘prev’ variable to the helper function. handle the cases prev == null and prev == curr + 1 separately. also maintain a count variable; it must be at least 2

better than O(n ^ n) since we’ll be pruning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

https://leetcode.com/problems/palindrome-partitioning/description/

A

TC: there are n-1 places we can cut a string of length n. at each of those places, we can cut or not cut. it also costs O(n) to copy and add each substring into the output. so TC is O(n * 2^n)

use helper isPal() function to build a boolean[][] array of palindromicities. use a standard backtracking helper function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

https://leetcode.com/problems/matchsticks-to-square/

A

exact same concept as partition into k equal sum subsets

TC: O(4^n). height of tree is n, and at each level we make 4 decisions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/

A

either we include the string, or we don’t include it.

use hasUniqueChars helper function for the concatenated string.

TC: O(2^n * m), where m is avg length of each string, and n is the number of strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

https://leetcode.com/problems/n-queens/description/

A

placements[] array for rows and 3 sets for col, diag1 (r + c), diag2 (r - c).

if row == n, build board and add to output.

for loop (cols) 0 to n - 1. if value is in one of the 3 sets, continue. else, set placements[row] = col, add values to sets, and make a recursive call. then, backtrack by erasing the placement and deleting from all 3 sets

TC: with each row, we have a few less choices to place the queen in. upper bound O(n!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

https://leetcode.com/problems/restore-ip-addresses

A

for loop is [1, 3].
if idx + i > s.length(), break.

only make a recursive call if num < 255 AND the first char of num isn’t 0 or num is only 1 char long

17
Q

https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/

A

to guarantee this condition, each num’s neighbors must either be both greater than that num, or both less than that num.

  1. sort array
  2. l, r ptrs at ends of the array
  3. for loop thru array. if i is even, res[i] = arr[l++]. if i is odd, res[i] = arr[r–]
18
Q

https://leetcode.com/problems/longest-common-prefix/description/

A

no need to sort.

outer loop thru indices of first str.
inner loop thru indices of the input array.
immediately return the current prefix if OOB or chars aren’t equal

19
Q

https://leetcode.com/problems/evaluate-reverse-polish-notation/

don’t code; review logic

A

.

20
Q

https://leetcode.com/problems/baseball-game/

don’t code; review logic

A

.

21
Q

https://leetcode.com/problems/spiral-matrix/description/

A

include
&& res.size() < n * m
as a condition in each loop.

have startRow, startCol, endRow, endCol