-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
41 lines (40 loc) · 1.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
namespace BestTimeToBuyAndSellStock
{
class Program
{
static void Main(string[] args)
{
int[] prices = {1, 0, 2, 4, 6 };
Console.WriteLine(MaxProfit(prices));
}
static int MaxProfit(int[] prices)
{
int minPrice = int.MaxValue;
int maxProfit = 0;
for (int i = 0; i < prices.Length; i++)
{
if (prices[i] < minPrice)
minPrice = prices[i];
else if (prices[i] - minPrice > maxProfit)
maxProfit = prices[i] - minPrice;
}
return maxProfit;
}
static int MaxProfit_RegulateSolution(int[] prices)
{
if (prices.Length == 0) return 0;
var dp = new int[prices.Length][];
for (int i = 0; i < dp.Length; i++)
dp[i] = new int[2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < dp.Length; i++)
{
dp[i][0] = Math.Max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.Max(dp[i - 1][1], -prices[i]);
}
return dp[prices.Length - 1][0];
}
}
}