diff --git a/LeetcodeProblems/Award_Budget_Cuts.js b/LeetcodeProblems/Award_Budget_Cuts.js new file mode 100644 index 0000000..d26e25b --- /dev/null +++ b/LeetcodeProblems/Award_Budget_Cuts.js @@ -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; diff --git a/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js b/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js new file mode 100644 index 0000000..4e4133e --- /dev/null +++ b/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js @@ -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; \ No newline at end of file diff --git a/LeetcodeProblems/Number_of_Islands.js b/LeetcodeProblems/Number_of_Islands.js index bdbc6ac..609d91a 100644 --- a/LeetcodeProblems/Number_of_Islands.js +++ b/LeetcodeProblems/Number_of_Islands.js @@ -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: diff --git a/LeetcodeProblems/Permutations_II.js b/LeetcodeProblems/Permutations_II.js index 09fbb6b..5fef9b8 100644 --- a/LeetcodeProblems/Permutations_II.js +++ b/LeetcodeProblems/Permutations_II.js @@ -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; diff --git a/LeetcodeProblems/Permutations_Without_Duplicates.js b/LeetcodeProblems/Permutations_Without_Duplicates.js index dafa0d9..dadc798 100644 --- a/LeetcodeProblems/Permutations_Without_Duplicates.js +++ b/LeetcodeProblems/Permutations_Without_Duplicates.js @@ -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); } } diff --git a/LeetcodeProblems/Subsets.js b/LeetcodeProblems/Subsets.js index bfce14e..0e65d00 100644 --- a/LeetcodeProblems/Subsets.js +++ b/LeetcodeProblems/Subsets.js @@ -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)); diff --git a/README.md b/README.md index f3a6bbc..2ffaf6a 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/SortingAlgorithms/heapSort.js b/SortingAlgorithms/heapSort.js index 32a3975..bae4570 100644 --- a/SortingAlgorithms/heapSort.js +++ b/SortingAlgorithms/heapSort.js @@ -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])) - - -