File tree Expand file tree Collapse file tree 5 files changed +97
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +97
-0
lines changed Original file line number Diff line number Diff line change 1+ // found a pattern where the result is the addition of two previous elements,
2+ // but not sure if this is the expected answer
3+ var climbStairs = function ( n ) {
4+ let arr = new Array ( n ) . fill ( 1 ) ;
5+ for ( let i = 2 ; i <= n ; i ++ ) {
6+ arr [ i ] = arr [ i - 2 ] + arr [ i - 1 ] ;
7+ }
8+ return n === 1 ? 1 : arr [ n ] ;
9+ } ;
10+
11+ // time - O(n) iterate up to n times
12+ // space - O(n) creates an array upto n elements
Original file line number Diff line number Diff line change 1+ var coinChange = function ( coins , amount ) {
2+ // create a dp array that stores the minimum amount of coins used for each amount leading up to the target amount
3+ // fill with array with amount + 1 as a default
4+ const dp = [ 0 , ...new Array ( amount ) . fill ( amount + 1 ) ] ;
5+
6+ // loop through the coins
7+ for ( const coin of coins ) {
8+ // for each amount, assess how the current coin can modify the existing value within the dp array by comparing the min value
9+ for ( let i = 1 ; i <= amount ; i ++ ) {
10+ // only works if coin is less than or equal to the assessing amount
11+ if ( coin <= i ) {
12+ // min comparison
13+ dp [ i ] = Math . min ( dp [ i ] , dp [ i - coin ] + 1 ) ;
14+ }
15+ }
16+ }
17+ // check target amount in dp array to see if it's the default value
18+ // if it's default value, it means coin combination cannot lead up to target amount
19+ // if it's not default value, that is the minimum required coin change to lead up to target amount
20+ return dp [ amount ] === amount + 1 ? - 1 : dp [ amount ] ;
21+ } ;
22+
23+ // time - O(a * c) loops through amount * loops through coin
24+ // space - O(a) depends on the size of amount
Original file line number Diff line number Diff line change 1+ var combinationSum = function ( candidates , target ) {
2+ let results = [ ] ;
3+
4+ function helper ( idx , curr , total ) {
5+ // base case - when total = target push into results
6+ if ( total === target ) {
7+ results . push ( [ ...curr ] ) ;
8+ return ;
9+ }
10+ // base exit case - when the index is greater or equal to candidates or the total is greater than target, exit
11+ if ( idx >= candidates . length || total > target ) {
12+ return ;
13+ }
14+
15+ // recursive case
16+ // case where we include the current value without advancing the index
17+ curr . push ( candidates [ idx ] ) ;
18+ helper ( idx , curr , total + candidates [ idx ] ) ;
19+ curr . pop ( )
20+ // case where we advance the index
21+ helper ( idx + 1 , curr , total ) ;
22+ }
23+ helper ( 0 , [ ] , 0 ) ;
24+
25+ return results ;
26+ } ;
27+
28+ // time - O(2^n) backtracking
29+ // space - O(1)
Original file line number Diff line number Diff line change 1+ var productExceptSelf = function ( nums ) {
2+ let result = new Array ( nums . length ) . fill ( 0 ) ; // create a result array of length num
3+ let pre = 1 , post = 1 ; // default to 1
4+
5+ for ( let i = 0 ; i < nums . length ; i ++ ) { // fill the result array with prefix (multiplication of left values)
6+ result [ i ] = pre ;
7+ pre *= nums [ i ] ;
8+ }
9+ for ( let i = nums . length - 1 ; i >= 0 ; i -- ) { // multiply the postfix (multiplication of right values) to the result array in their corresponding index
10+ result [ i ] *= post ;
11+ post *= nums [ i ] ;
12+ }
13+
14+ return result ;
15+ } ;
16+
17+ // time - O(n) iterate through the nums array twice - 2n, remove constant which ends up to be n
18+ // space - O(1) result array not part of space complexity
Original file line number Diff line number Diff line change 1+ var twoSum = function ( nums , target ) {
2+ let map = new Map ( ) ; // create a map to store the number and index of each element
3+ for ( let i = 0 ; i < nums . length ; i ++ ) {
4+ let diff = target - nums [ i ] ;
5+ if ( map . has ( diff ) ) { // once the differnece is found, return both index
6+ return [ i , map . get ( diff ) ] ;
7+ } else { // otherwise add to map
8+ map . set ( nums [ i ] , i )
9+ }
10+ }
11+ } ;
12+
13+ // time - O(n) at worst, iterate through the entire nums array
14+ // space - O(n) at worst, map the entire nums array
You can’t perform that action at this time.
0 commit comments