Page cover

Leet Code Intutions

#
Problem
Intuition
Solution Links

1

Recursive:

  1. Start with 0, -1.

  2. If an element in the index is greater than the previous one, include it or skip the element.

  3. Find max(include and exclude).

  4. Base: index == len(nums), return 0.

Tabulation DP: (Optimized)

  1. Start with each element as a subsequence of length 1 in a [].

  2. For each element in the sequence, look for pre elements and if the current element is greater than prev ele then update [].

  3. The Max element in list is Longest subsequence.

Memoization DP:

  1. Avoid recalculation and initialize a Dict.

  2. Fill the dict with the Longest sequence up to the point.

2

Recursive:

  1. start with current_sum =0

  2. For each number in the list:

    1. you can either ADD(+)

    2. Or Subtract (-)

  3. When Curr_Sum == Target then return 1 else, return 0

  4. return the total of ADD+Subtract; this would be the "total number of Targeted sum."

Solution

Memoization DP:

  1. Avoid recalculation and initialize a Dict.

  2. Fill the dict with the (index,curr_sum) = ADD+Subtract.

  3. Now return the Dict[ (index,curr_sum)]

Tabulation DP:

3

Recursive:

  1. Base case:

    1. when the index is 0 you will return nums[index] (only one house to rob)

    2. when the index is less than 0 return 0 (Invalid case, No house)

  2. Start with the end of the list. When you go for an index you can include or exclude

    1. Include = element at the index + f(index -2) (skip the adjacent house)

    2. Exclude = 0 + f(index -1) (Skip the current house and check the previous one)

  3. return the maximum(include, exclude)

Memoization DP:

  1. To avoid re-calculation, Use an array/dict to keep track of the maximum at that index.

  2. When the index is in Dict, return the memo[index] value.

Tabulation DP:

  1. We fill a DP array

  2. Dp[i] stores a max money robbed until that house.

  3. At each step, we can decide to include or exclude the current house.

    1. Include: adding money from i-2 since adjacent houses can't be robbed.

    2. Exclude : taking the value from dp[i-1] .

  4. return last element from Dp array.

Space optimized :

  1. Let's use

    1. prev as the first element in the list.

    2. prev2 as 0

  2. with the same bottom-up approach but without storing results in an array.

    1. include = curr_element and if index is greater than 1 add prev2.

    2. exclude = prev

    3. curr_max = max(include, exclude)

    4. Swap prev to prev2, curr_max to prev.

  3. Return prev.

4

House Robber II Same as House Robber

Recursive Memoization DP Tabulation DP Space optimized :

  1. Just divide the array into

    1. array[1:] ( 0 element not included)

    2. array[:-1] (Last element not included)

5

Space Optimized :

  1. start with initializing buy , sell to zero

  2. Start from last element :

    1. buy if max(buy, sell - day_price )

    2. sell if max(sell, buy + price -fee)

  3. return buy -> holds the maximum profit without holding a stock at the end.

Last updated