-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppStockProblem.cs
More file actions
41 lines (37 loc) · 1.07 KB
/
AppStockProblem.cs
File metadata and controls
41 lines (37 loc) · 1.07 KB
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 Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.InterviewCake
{
[TestClass]
public class AppStockProblem
{
public int FindBestStock(int[] stockPrices)
{
int max_Profit_sofar = int.MinValue;
int j = 0;
for (int i = 1; i < stockPrices.Length; i++)
{
if (max_Profit_sofar < stockPrices[i] - stockPrices[j])
{
max_Profit_sofar = stockPrices[i] - stockPrices[j];
}
else
{
j = i;
}
}
return max_Profit_sofar;
}
[TestMethod]
public void TestStockProblem()
{
int expectedProfit = -1;
int bestProfit = this.FindBestStock(new[] { 9, 7, 4, 1, 6 });
Assert.AreEqual(expectedProfit, bestProfit, "Expected and Actual are wrong!");
}
}
}