We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c5fcd3a commit 3d3127dCopy full SHA for 3d3127d
Algorithms Basics/top-interview-questions/121. Best Time to Buy and Sell Stock.cs
@@ -0,0 +1,18 @@
1
+// mantain two value: minPrice, maxProfit
2
+public class Solution {
3
+ public int MaxProfit(int[] prices) {
4
+ if (prices == null || prices.Length < 2) {
5
+ return 0;
6
+ }
7
+ int minPrice = prices[0];
8
+ int maxProfit = 0;
9
+ for (int i = 1; i < prices.Length; i++) {
10
+ if (prices[i] < minPrice) {
11
+ minPrice = prices[i];
12
+ } else if (prices[i] - minPrice > maxProfit) {
13
+ maxProfit = prices[i] - minPrice;
14
15
16
+ return maxProfit;
17
18
+}
0 commit comments