Skip to content

Commit ab80f73

Browse files
committed
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
1 parent be26462 commit ab80f73

File tree

4 files changed

+62
-74
lines changed

4 files changed

+62
-74
lines changed

EasyProblems/Best Time to Buy and Sell Stock II.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
namespace LeetCode.EasyProblems
22
{
3-
/// <summary>
4-
/// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
5-
/// </summary>
6-
public class Best_Time_to_Buy_and_Sell_Stock
3+
public class BestTimeToBuyAndSellStock
74
{
85
public int MaxProfit(int[] prices)
96
{
107
int profit = 0;
8+
int bestPriceToBuy = prices[0];
119

12-
if (prices != null && prices.Length > 1)
10+
for (int i = 1; i < prices.Length; i++)
1311
{
14-
int bestPriceToBuy = prices[0];
15-
16-
for (int i = 1; i < prices.Length; i++)
17-
{
18-
profit = Math.Max(profit, prices[i] - bestPriceToBuy);
19-
bestPriceToBuy = Math.Min(bestPriceToBuy, prices[i]);
20-
}
12+
profit = Math.Max(profit, prices[i] - bestPriceToBuy);
13+
bestPriceToBuy = Math.Min(bestPriceToBuy, prices[i]);
2114
}
2215

2316
return profit;
2417
}
18+
19+
[Test(Description = "https://leetcode.com/problems/best-time-to-buy-and-sell-stock/")]
20+
[Category("Easy")]
21+
[Category("LeetCode")]
22+
[Category("Best Time to Buy and Sell Stock")]
23+
[TestCaseSource(nameof(Input))]
24+
public void Test1((int Output, int[] Input) item)
25+
{
26+
var response = MaxProfit(item.Input);
27+
Assert.That(response, Is.EqualTo(item.Output));
28+
}
29+
30+
public static IEnumerable<(int Output, int[] Input)> Input =>
31+
new List<(int Output, int[] Input)>()
32+
{
33+
(5, ( [7,1,5,3,6,4])),
34+
};
2535
}
2636
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace LeetCode.MediumProblems
2+
{
3+
class BestTimeToBuyAndSellStockIi
4+
{
5+
public int MaxProfit(int[] prices) {
6+
7+
int profit = 0;
8+
9+
for(int i = 1; i < prices.Length; i++)
10+
{
11+
if(prices[i] > prices[i - 1])
12+
profit += prices[i] - prices[i - 1];
13+
}
14+
15+
return profit;
16+
17+
}
18+
19+
[Test(Description = "https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/")]
20+
[Category("Medium")]
21+
[Category("LeetCode")]
22+
[Category("Best Time to Buy and Sell Stock II")]
23+
[TestCaseSource(nameof(Input))]
24+
public void Test1((int Output, int[] Input) item)
25+
{
26+
var response = MaxProfit(item.Input);
27+
Assert.That(response, Is.EqualTo(item.Output));
28+
}
29+
30+
public static IEnumerable<(int Output, int[] Input)> Input =>
31+
new List<(int Output, int[] Input)>()
32+
{
33+
34+
(8, ( [3,3,5,0,0,3,1,4])),
35+
(7, ( [7,1,5,3,6,4])),
36+
(4, ( [1,2,3,4,5])),
37+
(0, ( [7,6,4,3,1])),
38+
};
39+
}
40+
}

MediumProblems/Kth Largest Element in an Array.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)