Skip to content

Commit a8906e4

Browse files
Added Best time to buy and sell stock II
1 parent bcc7390 commit a8906e4

7 files changed

+77
-20
lines changed
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)