Created
June 8, 2017 21:23
-
-
Save jianminchen/3cb1adfdccc79da4991d79f651340530 to your computer and use it in GitHub Desktop.
Leetcode 122 - buy and sell stock - easy level algorithm - work on loops - check previous / next, play with boundary and duplicate numbers in a row.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leetcode122_buyAndSellStock | |
{ | |
/// <summary> | |
/// Leetcode 122: | |
/// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/description | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
public int MaxProfit(int[] prices) | |
{ | |
if (prices == null || prices.Length <= 1) | |
{ | |
return 0; | |
} | |
var filtered = removeDuplicate(prices); | |
if (filtered.Length <= 1) | |
{ | |
return 0; | |
} | |
// work on filtered array | |
int length = filtered.Length; | |
int buyPrice = 0; | |
bool cycleStart = false; | |
int profit = 0; | |
for (int i = 0; i < length; i++) | |
{ | |
var current = filtered[i]; | |
bool isLastItem = i == (length - 1); | |
bool isPeak1 = i == 0 && current > filtered[i + 1]; | |
bool isPeak2 = i > 0 && current > filtered[i - 1] && | |
(isLastItem || current > filtered[i + 1]); | |
if (cycleStart && ( isPeak1 || isPeak2 )) // skip first peak | |
{ | |
profit += current - buyPrice; | |
cycleStart = false; | |
} | |
bool isBottom1 = i == 0 && current < filtered[i + 1]; | |
bool isBottom2 = i > 0 && current < filtered[i - 1] | |
&& (i + 1 < length) && current < filtered[i + 1]; | |
if (!cycleStart && (isBottom1 || isBottom2)) | |
{ | |
buyPrice = current; | |
cycleStart = true; | |
} | |
} | |
return profit; | |
} | |
private static int[] removeDuplicate(int[] prices) | |
{ | |
var filtered = new List<int>(); | |
for (int i = 0; i < prices.Length; i++) | |
{ | |
if (i == 0 || prices[i] != prices[i - 1]) | |
{ | |
filtered.Add(prices[i]); | |
} | |
else if (prices[i] == prices[i - 1]) | |
{ | |
continue; | |
} | |
} | |
return filtered.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment