- Notifications
You must be signed in to change notification settings - Fork2.4k
Create: 84-Largest-Rectangle-in-Histogram.ts, 4-Median-of-Two-Sorted-Arrays.ts, 25-Reverse-Nodes-in-k-Group.ts, 1899-Merge-Triplets-to-Form-Target-Triplet.ts, 10-Regular-Expression-Matching.ts, 134-Gas-Station.ts, 55-Jump-Game.ts, 45-Jump-Game-II.ts, 199-Binary-Tree-Right-Side-View.ts, 124-Binary-Tree-Maximum-Path-Sum.ts, 98-Validate-Binary-Search-Tree.ts, 678-Valid-Parenthesis-String.ts, 518-Coin-Change-II.ts, 97-Interleaving-String.ts, 115-Distinct-Subsequences.ts, 312-Burst-Balloons.ts, 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.ts, 215-Kth-Largest-Element-in-an-Array.ts, 621-Task-Scheduler.ts, 994-Rotting-Oranges.ts, 210-Course-Schedule-II.ts, 207-Course-Schedule.ts, 684-Redundant-Connection.ts, 127-Word-Ladder.ts, 787-Cheapest-Flights-Within-K-Stops.ts, 494-Target-Sum.ts, 846-Hand-of-Straights.ts#1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
29 commits Select commitHold shift + click to select a range
1ad6706
Create: 84-Largest-Rectangle-in-Histogram.ts
Ykhan79907a3d4a
Create: 4-Median-of-Two-Sorted-Arrays.ts
Ykhan7991616f50
Add: 25-Reverse-Nodes-in-k-Group.ts
Ykhan799f9e95be
Create: 1899-Merge-Triplets-to-Form-Target-Triplet.ts
Ykhan79904066fa
Create: 10-Regular-Expression-Matching.ts
Ykhan7999ae6057
Merge branch 'neetcode-gh:main' into main
Ykhan799156bccb
Create: 134-Gas-Station.ts
Ykhan7997c67774
Create: 55-Jump-Game.ts
Ykhan79907753d0
Create: 45-Jump-Game-II.ts
Ykhan799ac3cef1
Create: 199-Binary-Tree-Right-Side-View.ts
Ykhan799c661398
Create: 124-Binary-Tree-Maximum-Path-Sum.ts
Ykhan79922ec063
Create: 98-Validate-Binary-Search-Tree.ts
Ykhan799e6a0eb7
Create: 678-Valid-Parenthesis-String.ts
Ykhan7999634ba8
Create: 518-Coin-Change-II.ts
Ykhan7994509f59
Create: 97-Interleaving-String.ts
Ykhan799b1a0f76
Create: 115-Distinct-Subsequences.ts
Ykhan799db4585a
Create: 312-Burst-Balloons.ts
Ykhan79962962a4
Create: 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.ts
Ykhan799b740823
Create: 215-Kth-Largest-Element-in-an-Array.ts
Ykhan799aec310a
Create: 621-Task-Scheduler.ts
Ykhan79918e2450
Create: 994-Rotting-Oranges.ts
Ykhan79970a7c8e
Create: 210-Course-Schedule-II.ts
Ykhan79975af916
Create: 207-Course-Schedule.ts
Ykhan79903c5b10
Merge branch 'neetcode-gh:main' into main
Ykhan7991f08383
Create: 684-Redundant-Connection.ts
Ykhan799ba94b3f
Create: 127-Word-Ladder.ts
Ykhan79980e215d
Create: 787-Cheapest-Flights-Within-K-Stops.ts
Ykhan799c82a5cb
Create: 494-Target-Sum.ts
Ykhan799560f835
Create: 846-Hand-of-Straights.ts
Ykhan799File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Create: 494-Target-Sum.ts
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitc82a5cbe3e19075c3b871e8e47c4a75f55e686dc
There are no files selected for viewing
29 changes: 29 additions & 0 deletionstypescript/494-Target-Sum.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function findTargetSumWays(nums: number[], target: number): number { | ||
// key: index, sum till index element, value: number of ways to get to that sum | ||
const cache = new Map(); | ||
const backTrack = (i, sum) => { | ||
/* if we're at the last element + 1 we compare the sum to our target | ||
if it's true, we've found a way to our sum! | ||
*/ | ||
if (i === nums.length) { | ||
if (sum === target) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
// if already index, sum pair exist in our HashMap, we return the memoized result | ||
if (cache.has(`${i},${sum}`)) { | ||
return cache.get(`${i},${sum}`); | ||
} | ||
// DP: we memoize number of ways of each pair index, sum | ||
cache.set(`${i},${sum}`, backTrack(i + 1, sum + nums[i]) + backTrack(i + 1, sum - nums[i])); | ||
return cache.get(`${i},${sum}`); | ||
}; | ||
return backTrack(0, 0); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.