Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SE103 - Week 10, Session 2

Caren
August 08, 2020

SE103 - Week 10, Session 2

Caren

August 08, 2020
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Partition to K Equal Sum Subsets
  2. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: ?? Partition to K Equal Sum Subsets
  3. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: false Partition to K Equal Sum Subsets
  4. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: false Input: nums = [1, 1, 1], k = 3
 Output: ?? Partition to K Equal Sum Subsets
  5. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: false Input: nums = [1, 1, 1], k = 3
 Output: true Partition to K Equal Sum Subsets
  6. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: false Input: nums = [1, 1, 1], k = 3
 Output: true Input: nums = [1, 2, 3], k = 2
 Output: ?? Partition to K Equal Sum Subsets
  7. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
 Output: True 
 (5), (1, 4), (2,3), (2,3) have equal sums Input: nums = [1], k = 3 
 Output: false Input: nums = [1, 1, 1], k = 3
 Output: true Input: nums = [1, 2, 3], k = 2
 Output: true Partition to K Equal Sum Subsets
  8. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 
 
 Partition to K Equal Sum Subsets
  9. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 
 
 Partition to K Equal Sum Subsets
  10. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 If k = 1, we’re done!
 
 
 
 Partition to K Equal Sum Subsets
  11. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 If k = 1, we’re done!
 
 nums = [1, 2], k = 2 -> returns false
 
 
 
 
 Partition to K Equal Sum Subsets
  12. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 If k = 1, we’re done!
 
 nums = [1, 2], k = 2 -> returns false
 If sum(nums) % k != 0 , we can return false
 
 
 
 Partition to K Equal Sum Subsets
  13. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 If k = 1, we’re done!
 
 nums = [1, 2], k = 2 -> returns false
 If sum(nums) % k != 0 , we can return false
 
 nums = [1, 2, 3], k = 2 -> returns true
 
 
 Partition to K Equal Sum Subsets
  14. Given an array of integers nums and a positive integer

    k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Let’s think about some base cases to get us started:
 nums = [1, 2] , k = 1 -> returns true
 If k = 1, we’re done!
 
 nums = [1, 2], k = 2 -> returns false
 If sum(nums) % k != 0 , we can return false
 
 nums = [1, 2, 3], k = 2 -> returns true
 Each bucket needs sum(nums) / k => 3
 
 Partition to K Equal Sum Subsets
  15. Backtracking template: 1. Search - what are we searching /

    solving for? 2. GetCandidates - get all the possible potential solutions (local solutions) 3. IsSolution - is this candidate a solution? 4. Backtrack - Undo any state that was caused with local guesses Partition to K Equal Sum Subsets
  16. create k-groups
 
 search:
 for number in nums:
 if k

    group’s sum doesn’t exceed target
 - add it to k group
 - recursively search with one less number
 
 if every number is in a group, search was successful
 Partition to K Equal Sum Subsets
  17. nums = [1, 2, 3, 4] , k = 2

    Partition to K Equal Sum Subsets
  18. nums = [1, 2, 3, 4] , k = 2

    Partition to K Equal Sum Subsets
  19. nums = [1, 2, 3, 4] , k = 2

    sum = ?? sum = ?? Partition to K Equal Sum Subsets
  20. nums = [1, 2, 3, 4] , k = 2

    sum = 5 sum = 5 Partition to K Equal Sum Subsets
  21. nums = [1, 2, 3, 4] , k = 2

    sum = 5 sum = 5 Partition to K Equal Sum Subsets
  22. nums = [1, 2, 3, 4] , k = 2

    Partition to K Equal Sum Subsets sum = 5 sum = 5 1 1
  23. nums = [1, 2, 3, 4] , k = 2

    Partition to K Equal Sum Subsets sum = 5 sum = 5 1 1 1 1 recurse
  24. nums = [1, 2, 3, 4] , k = 2

    Partition to K Equal Sum Subsets sum = 5 sum = 5 1 1 1 1 recurse recurse 3 2 2 2 2 2 2 3
  25. Dynamic Programming:
 
 use ‘mask’ to determine the current state.

    
 -> tells us which numbers have been used already. It also allows us to set the ith bit, unset the ith bit, check if ith bit is set in one step Partition to K Equal Sum Subsets
  26. Dynamic Programming:
 
 use ‘mask’ to determine the current state.

    
 -> tells us which numbers have been used already. It also allows us to set the ith bit, unset the ith bit, check if ith bit is set in one step For example: nums[] = [2, 1, 4, 3, 5, 6, 2], mask = (1100101), which means that { 2, 1, 5, 2 } have been used in this state Partition to K Equal Sum Subsets
  27. 2 problems 30 minutes for each problem Pick the person

    that has their birthday coming up next! Mock Interviews
  28. Practice more Permutations, Combinations, DP -> take HackerRank Look more

    into Partition to K equal subsets with DP if you’re super advanced. Otherwise, simple backtracking approach with recursion would be fine 99.99% of the time. Review Week! Coming Up
  29. Dynamic Programming:
 
 - target = sum/k - only start

    a new subset when we have a subset who’s sum = target - don’t allow any state where sum > target Partition to K Equal Sum Subsets
  30. Dynamic Programming:
 
 use ‘mask’ to determine the current state.

    
 -> tells us which numbers have been used already. It also allows us to set the ith bit, unset the ith bit, check if ith bit in one step For example: nums[] = [2, 1, 4, 3, 5, 6, 2], mask = (1100101), which means that { 2, 1, 5, 2 } have been used in this state 
 Partition to K Equal Sum Subsets
  31. Dynamic Programming:
 
 use ‘mask’ to determine the current state.

    
 -> tells us which numbers have been used already. It also allows us to set the ith bit, unset the ith bit, check if ith bit in one step For example: nums[] = [2, 1, 4, 3, 5, 6, 2], mask = (1100101), which means that { 2, 1, 5, 2 } have been used in this state Let’s say our target = 8,
 dp[1100101] = (2 + 1 + 5 + 2) % 8 = 2
 -> We need to find an unused number to satisfy the next state
 Partition to K Equal Sum Subsets
  32. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first
  33. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want
  34. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want - Overpromising
  35. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want - Overpromising - Trying to change too much within the first 3 months
  36. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want - Overpromising - Trying to change too much within the first 3 months - Not getting iterative feedback
  37. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want - Overpromising - Trying to change too much within the first 3 months - Not getting iterative feedback - Not learning from my own interview mistakes
  38. Mistakes I made early in my career - Asking too

    many questions without trying to look for the answers first - Not asking for the projects / tasks I want. Not telling my manager what I want - Overpromising - Trying to change too much within the first 3 months - Not getting iterative feedback - Not learning from my own interview mistakes - Trying to learn all the cool new techlogies and frameworks