Skip to content

Fixes #32

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions LeetcodeProblems/Award_Budget_Cuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Award Budget Cuts
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem they’re facing. Originally,
the committee planned to give N research grants this year. However, due to spending cutbacks,
the budget was reduced to newBudget dollars and now they need to reallocate the grants.
The committee made a decision that they’d like to impact as few grant recipients as possible by applying a maximum cap on all grants.
Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, won’t be impacted.

Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).

Analyze the time and space complexities of your solution.

Example:

input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190

output: 47 # and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
Constraints:

[time limit] 5000ms

[input] array.double grantsArray

0 ≤ grantsArray.length ≤ 20
0 ≤ grantsArray[i]
[input] double newBudget

[output] double
*/

var cutAwardBadges = function(nums, newBadge) {
var currentBadge = 0;
for(var i = 0; i < nums.length; i++)
currentBadge += nums[i];

if(currentBadge < newBadge)
return;

const cap = findCap(nums, currentBadge, newBadge);

var iter = 0;
while(iter >= 0 && nums[iter] > cap) {
nums[iter] = cap;
iter++;
}

return nums;
}

var findCap = function(nums, currentBadge, newBadge) {
nums.sort(function(a, b) { return b - a });
if(nums[nums.length - 1] * nums.length > newBadge)
return newBadge / nums.length;

var diff = currentBadge - newBadge;
var iter = 0;
while(iter < nums.length - 1 && diff > 0) {
const substraction = nums[iter] - nums[iter + 1]
diff -= (iter + 1) * substraction;
iter++;
}

return nums[iter] + (-diff) / iter;
}

var main = function() {
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 190));
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 5));
}

module.exports.main = main;
63 changes: 63 additions & 0 deletions LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
*/

/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var profit = 0;
var iter = 0;

while(iter < prices.length) {
while(iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
iter++;
}
const buy = prices[iter];
iter++;
while(iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
iter++;
}

if(iter < prices.length) {
const sell = prices[iter];
profit = profit + sell - buy;
}
}

return profit;
};

var main = function() {
console.log(maxProfit([7,1,5,3,6,4]));
console.log(maxProfit([7,1,5,3320,6,4]));
}

module.exports.main = main;
3 changes: 2 additions & 1 deletion LeetcodeProblems/Number_of_Islands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Number of Islands
https://leetcode.com/problems/number-of-islands/

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and
is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Expand Down
17 changes: 7 additions & 10 deletions LeetcodeProblems/Permutations_II.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ var permuteUniqueAux = function(n, map, currentSol) {
}

var ret = [];
var set = new Set();
for(var num in map) {
if(!set.has(num)) {
const occ = map[num];
if(occ === 1) {
delete map[num];
} else {
map[num] = occ -1 ;
}
ret = [...ret, ...permuteUniqueAux(n, map, currentSol.concat(num))];
map[num] = occ;
const occ = map[num];
if(occ === 1) {
delete map[num];
} else {
map[num] = occ -1 ;
}
ret = [...ret, ...permuteUniqueAux(n, map, currentSol.concat(num))];
map[num] = occ;
}

return ret;
Expand Down
5 changes: 2 additions & 3 deletions LeetcodeProblems/Permutations_Without_Duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ var subsetWithDuplicatesAux = function(nums, current, sol) {

for(var i = 0; i < nums.length; i++) {
var newCurrent = [...current, nums[i]]

var newNum = nums.filter(function(num, index) { return index !== i});
subsetWithDuplicatesAux(newNum, newCurrent, sol);
var newNums = nums.filter(function(num, index) { return index !== i });
subsetWithDuplicatesAux(newNums, newCurrent, sol);
}
}

Expand Down
4 changes: 1 addition & 3 deletions LeetcodeProblems/Subsets.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ Output:
]
*/


var subsets = function(nums) {
var ret = [];

subsetByPosition = function (nums, position, current) {
console.log(current);
if(position == nums.length) {
return [current];
return [current];
}
var currentRight = current.slice().concat([nums[position]]);
return subsetByPosition(nums, position + 1, currentRight).concat(subsetByPosition(nums, position + 1, current));
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ Solutions of algorithm problems using Javascript
| [Valid Parentheses ](/LeetcodeProblems/Valid_Parentheses.js) | Easy | https://leetcode.com/problems/valid-parentheses/ |
| [Backspace String Compare ](/LeetcodeProblems/Backspace_String_Compare.js) | Easy | https://leetcode.com/problems/backspace-string-compare/ |
| [Binary Gap ](/LeetcodeProblems/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
| [Best Time to Buy and Sell Stock II ](/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js) | Easy | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ |
| [Tic Tac Toe ](/LeetcodeProblems/Tic_Tac_Toe.js) | | |
| [Permutations With Duplicates ](/LeetcodeProblems/Permutations_With_Duplicates.js) | | |
| [Deletion Distance](/LeetcodeProblems/Deletion_Distance.js) | | |
| [Award Budget Cuts](/LeetcodeProblems/Award_Budget_Cuts.js) | | |

### Sorting Algorithms
| Algoritmhs |
Expand Down
3 changes: 0 additions & 3 deletions SortingAlgorithms/heapSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ console.log(heapSort([1]));
console.log(heapSort([2, 1]));
console.log(heapSort([1,7,2,3,4,1,10,2,3,4,5]))