Skip to content

Commit 73c53a8

Browse files
Merge pull request #32 from ignacio-chiazzo/fixes
Fixes
2 parents ec462ec + a8906e4 commit 73c53a8

8 files changed

+150
-20
lines changed

LeetcodeProblems/Award_Budget_Cuts.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Award Budget Cuts
3+
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,
4+
the committee planned to give N research grants this year. However, due to spending cutbacks,
5+
the budget was reduced to newBudget dollars and now they need to reallocate the grants.
6+
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.
7+
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.
8+
9+
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).
10+
11+
Analyze the time and space complexities of your solution.
12+
13+
Example:
14+
15+
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
16+
17+
output: 47 # and given this cap the new grants array would be
18+
# [2, 47, 47, 47, 47]. Notice that the sum of the
19+
# new grants is indeed 190
20+
Constraints:
21+
22+
[time limit] 5000ms
23+
24+
[input] array.double grantsArray
25+
26+
0 ≤ grantsArray.length ≤ 20
27+
0 ≤ grantsArray[i]
28+
[input] double newBudget
29+
30+
[output] double
31+
*/
32+
33+
var cutAwardBadges = function(nums, newBadge) {
34+
var currentBadge = 0;
35+
for(var i = 0; i < nums.length; i++)
36+
currentBadge += nums[i];
37+
38+
if(currentBadge < newBadge)
39+
return;
40+
41+
const cap = findCap(nums, currentBadge, newBadge);
42+
43+
var iter = 0;
44+
while(iter >= 0 && nums[iter] > cap) {
45+
nums[iter] = cap;
46+
iter++;
47+
}
48+
49+
return nums;
50+
}
51+
52+
var findCap = function(nums, currentBadge, newBadge) {
53+
nums.sort(function(a, b) { return b - a });
54+
if(nums[nums.length - 1] * nums.length > newBadge)
55+
return newBadge / nums.length;
56+
57+
var diff = currentBadge - newBadge;
58+
var iter = 0;
59+
while(iter < nums.length - 1 && diff > 0) {
60+
const substraction = nums[iter] - nums[iter + 1]
61+
diff -= (iter + 1) * substraction;
62+
iter++;
63+
}
64+
65+
return nums[iter] + (-diff) / iter;
66+
}
67+
68+
var main = function() {
69+
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 190));
70+
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 5));
71+
}
72+
73+
module.exports.main = main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
3+
Best Time to Buy and Sell Stock II
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
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).
8+
9+
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 7
15+
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
16+
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
17+
Example 2:
18+
19+
Input: [1,2,3,4,5]
20+
Output: 4
21+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
22+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
23+
engaging multiple transactions at the same time. You must sell before buying again.
24+
Example 3:
25+
26+
Input: [7,6,4,3,1]
27+
Output: 0
28+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
29+
*/
30+
31+
/**
32+
* @param {number[]} prices
33+
* @return {number}
34+
*/
35+
var maxProfit = function(prices) {
36+
var profit = 0;
37+
var iter = 0;
38+
39+
while(iter < prices.length) {
40+
while(iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
41+
iter++;
42+
}
43+
const buy = prices[iter];
44+
iter++;
45+
while(iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
46+
iter++;
47+
}
48+
49+
if(iter < prices.length) {
50+
const sell = prices[iter];
51+
profit = profit + sell - buy;
52+
}
53+
}
54+
55+
return profit;
56+
};
57+
58+
var main = function() {
59+
console.log(maxProfit([7,1,5,3,6,4]));
60+
console.log(maxProfit([7,1,5,3320,6,4]));
61+
}
62+
63+
module.exports.main = main;

LeetcodeProblems/Number_of_Islands.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Number of Islands
33
https://leetcode.com/problems/number-of-islands/
44
5-
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.
5+
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
6+
is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
67
78
Example 1:
89

LeetcodeProblems/Permutations_II.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ var permuteUniqueAux = function(n, map, currentSol) {
3131
}
3232

3333
var ret = [];
34-
var set = new Set();
3534
for(var num in map) {
36-
if(!set.has(num)) {
37-
const occ = map[num];
38-
if(occ === 1) {
39-
delete map[num];
40-
} else {
41-
map[num] = occ -1 ;
42-
}
43-
ret = [...ret, ...permuteUniqueAux(n, map, currentSol.concat(num))];
44-
map[num] = occ;
35+
const occ = map[num];
36+
if(occ === 1) {
37+
delete map[num];
38+
} else {
39+
map[num] = occ -1 ;
4540
}
41+
ret = [...ret, ...permuteUniqueAux(n, map, currentSol.concat(num))];
42+
map[num] = occ;
4643
}
4744

4845
return ret;

LeetcodeProblems/Permutations_Without_Duplicates.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ var subsetWithDuplicatesAux = function(nums, current, sol) {
3535

3636
for(var i = 0; i < nums.length; i++) {
3737
var newCurrent = [...current, nums[i]]
38-
39-
var newNum = nums.filter(function(num, index) { return index !== i});
40-
subsetWithDuplicatesAux(newNum, newCurrent, sol);
38+
var newNums = nums.filter(function(num, index) { return index !== i });
39+
subsetWithDuplicatesAux(newNums, newCurrent, sol);
4140
}
4241
}
4342

LeetcodeProblems/Subsets.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ Output:
2222
]
2323
*/
2424

25-
2625
var subsets = function(nums) {
2726
var ret = [];
2827

2928
subsetByPosition = function (nums, position, current) {
30-
console.log(current);
3129
if(position == nums.length) {
32-
return [current];
30+
return [current];
3331
}
3432
var currentRight = current.slice().concat([nums[position]]);
3533
return subsetByPosition(nums, position + 1, currentRight).concat(subsetByPosition(nums, position + 1, current));

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ Solutions of algorithm problems using Javascript
5656
| [Valid Parentheses ](/LeetcodeProblems/Valid_Parentheses.js) | Easy | https://leetcode.com/problems/valid-parentheses/ |
5757
| [Backspace String Compare ](/LeetcodeProblems/Backspace_String_Compare.js) | Easy | https://leetcode.com/problems/backspace-string-compare/ |
5858
| [Binary Gap ](/LeetcodeProblems/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
59+
| [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/ |
5960
| [Tic Tac Toe ](/LeetcodeProblems/Tic_Tac_Toe.js) | | |
6061
| [Permutations With Duplicates ](/LeetcodeProblems/Permutations_With_Duplicates.js) | | |
6162
| [Deletion Distance](/LeetcodeProblems/Deletion_Distance.js) | | |
63+
| [Award Budget Cuts](/LeetcodeProblems/Award_Budget_Cuts.js) | | |
6264

6365
### Sorting Algorithms
6466
| Algoritmhs |

SortingAlgorithms/heapSort.js

-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,3 @@ console.log(heapSort([1]));
4646
console.log(heapSort([2, 1]));
4747
console.log(heapSort([1,7,2,3,4,1,10,2,3,4,5]))
4848

49-
50-
51-

0 commit comments

Comments
 (0)