Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
42 slides
-
- Backtracing: Efficient searching with constraints - Dynamic Programming: Recursively
build most optimal solution Backtracking vs DP -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
nums = [1, 2, 3, 4] , k = 2
Partition to K Equal Sum Subsets -
nums = [1, 2, 3, 4] , k = 2
Partition to K Equal Sum Subsets -
nums = [1, 2, 3, 4] , k = 2
sum = ?? sum = ?? Partition to K Equal Sum Subsets -
nums = [1, 2, 3, 4] , k = 2
sum = 5 sum = 5 Partition to K Equal Sum Subsets -
nums = [1, 2, 3, 4] , k = 2
sum = 5 sum = 5 Partition to K Equal Sum Subsets -
nums = [1, 2, 3, 4] , k = 2
Partition to K Equal Sum Subsets sum = 5 sum = 5 1 1 -
nums = [1, 2, 3, 4] , k = 2
Partition to K Equal Sum Subsets sum = 5 sum = 5 1 1 1 1 recurse -
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 -
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 -
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 -
2 problems 30 minutes for each problem Pick the person
that has their birthday coming up next! Mock Interviews -
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 -
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 -
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 -
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 -
Mistakes I made early in my career - Asking too
many questions without trying to look for the answers first -
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 -
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 -
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 -
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 -
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 -
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