-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16f6ec9
commit b5461d7
Showing
6 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
/** | ||
* Functional Approach | ||
*/ | ||
|
This file contains 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
This file contains 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,41 @@ | ||
// A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1. | ||
// Your task is to write a program that will be used in a vending machine to return change. | ||
// Assume that the vending machine will always want to return the least number of coins or notes. | ||
// Devise a function getChange(M, P) where M is how much money was inserted into the machine | ||
// and P the price of the item selected, that returns an array of integers representing | ||
// the number of each denomination to return. | ||
|
||
// Example: | ||
// getChange(5, 0.99) // should return [1,0,0,0,0,4] | ||
|
||
// getChange(3.14, 1.99) // should return [0,1,1,0,0,1] | ||
// getChange(3, 0.01) // should return [4,0,2,1,1,2] | ||
// getChange(4, 3.14) // should return [1,0,1,1,1,0] | ||
// getChange(0.45, 0.34) // should return [1,0,1,0,0,0] | ||
|
||
const coinChange_I = (money, price) => { | ||
const coins = [1, 5, 10, 25, 50, 100]; | ||
|
||
let change = Math.round((money - price) * 100); | ||
console.log(change); | ||
|
||
for (let i = coins.length - 1; i >= 0; i--) { | ||
let val = coins[i]; | ||
if (change >= val) { | ||
coins[i] = Math.floor(change / val); | ||
} else { | ||
coins[i] = 0; | ||
} | ||
change = change - val * coins[i]; | ||
} | ||
|
||
console.log(coins); | ||
//return coins; | ||
}; | ||
|
||
coinChange_I(5, 0.99); // should return [1,0,0,0,0,4] | ||
|
||
coinChange_I(3.14, 1.99); // should return [0,1,1,0,0,1] | ||
coinChange_I(3, 0.01); // should return [4,0,2,1,1,2] | ||
coinChange_I(4, 3.14); // should return [1,0,1,1,1,0] | ||
coinChange_I(0.45, 0.34); // should return [1,0,1,0,0,0] |
This file contains 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
This file contains 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,44 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
const lengthOfLIS = nums => { | ||
if (nums.length === 0) return 0; | ||
if (nums.length === 1) return 1; | ||
|
||
const stack = [nums[0]]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
// if item is grater than push it to the array | ||
if (nums[i] > stack[stack.length - 1]) stack.push(nums[i]); | ||
// else binary search and place the item in the correct position | ||
else { | ||
let pos = binarySearch(stack, nums[i]); | ||
stack[pos] = nums[i]; | ||
} | ||
console.log(nums[i], stack); | ||
} | ||
return stack.length; | ||
}; | ||
|
||
const binarySearch = (stack, value, left = 0, right = stack.length - 1) => { | ||
while (right - left > 1) { | ||
let mid = left + Math.floor((right - left) / 2); | ||
|
||
if (stack[mid] >= value) right = mid; | ||
else left = mid; | ||
} | ||
return right; | ||
|
||
// if ( left > right ) return -1; | ||
// if ( left === right ) return left; | ||
|
||
// let mid = left + Math.floor( ( right - left ) / 2 ); | ||
|
||
// if ( stack[mid] <= value && stack[mid + 1] > value ) return mid; | ||
// else if ( value > stack[mid] ) return binarySearch( stack, value, mid + 1, right ); | ||
// else return binarySearch( stack, value, left, mid - 1 ); | ||
}; | ||
|
||
const length = lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]); | ||
console.log(length); |
This file contains 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