Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 47
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it.

For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars and sell it at 10 dollars.

Pull request description:
Day47
Resolves #105 .

Patch:
diff --git a/README.md b/README.md
index e01fd7f..158fb9b 100644
--- a/README.md
+++ b/README.md
@@ -59,3 +59,4 @@ problems from
 * [Day 44](https://github.com/vaskoz/dailycodingproblem-go/issues/99)
 * [Day 45](https://github.com/vaskoz/dailycodingproblem-go/issues/101)
 * [Day 46](https://github.com/vaskoz/dailycodingproblem-go/issues/103)
+* [Day 47](https://github.com/vaskoz/dailycodingproblem-go/issues/105)
diff --git a/day47/problem.go b/day47/problem.go
new file mode 100644
index 0000000..322422d
--- /dev/null
+++ b/day47/problem.go
@@ -0,0 +1,35 @@
+package day47
+
+// MaxProfitBrute calculates the maximum profit from these chronological stock prices.
+// Runs in O(N^2) and O(1) space.
+func MaxProfitBrute(stockPrices []int) int {
+	var maxProfit int
+	for i := range stockPrices {
+		for j := i + 1; j < len(stockPrices); j++ {
+			if profit := stockPrices[j] - stockPrices[i]; profit > maxProfit {
+				maxProfit = profit
+			}
+		}
+	}
+	return maxProfit
+}
+
+// MaxProfit calculates the maximum profit from these chronological stock prices.
+// Runs in O(N) time and O(1) space.
+func MaxProfit(stockPrices []int) int {
+	if len(stockPrices) == 0 {
+		return 0
+	}
+	var maxProfit, lowestPrice int
+	lowestPrice = stockPrices[0]
+	for _, price := range stockPrices {
+		if price < lowestPrice {
+			lowestPrice = price
+		} else {
+			if profit := price - lowestPrice; profit > maxProfit {
+				maxProfit = profit
+			}
+		}
+	}
+	return maxProfit
+}


Test patch:
diff --git a/day47/problem_test.go b/day47/problem_test.go
new file mode 100644
index 0000000..41a0f40
--- /dev/null
+++ b/day47/problem_test.go
@@ -0,0 +1,46 @@
+package day47
+
+import "testing"
+
+var testcases = []struct {
+	prices    []int
+	maxProfit int
+}{
+	{[]int{9, 11, 8, 5, 7, 10}, 5},
+	{[]int{}, 0},
+	{nil, 0},
+}
+
+func TestMaxProfitBrute(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if profit := MaxProfitBrute(tc.prices); tc.maxProfit != profit {
+			t.Errorf("Expected profit %d but got %d", tc.maxProfit, profit)
+		}
+	}
+}
+
+func BenchmarkMaxProfitBrute(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			MaxProfitBrute(tc.prices)
+		}
+	}
+}
+
+func TestMaxProfit(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if profit := MaxProfit(tc.prices); tc.maxProfit != profit {
+			t.Errorf("Expected profit %d but got %d", tc.maxProfit, profit)
+		}
+	}
+}
+
+func BenchmarkMaxProfit(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			MaxProfit(tc.prices)
+		}
+	}
+}