Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 285
Good morning! Here's your coding interview problem for today.

This problem was asked by Mailchimp.

You are given an array representing the heights of neighboring buildings on a city street, from east to west. The city assessor would like you to write an algorithm that returns how many of these buildings have a view of the setting sun, in order to properly value the street.

For example, given the array `[3, 7, 8, 3, 6, 1]`, you should return 3, since the top floors of the buildings with heights `8, 6, and 1` all have an unobstructed view to the west.

Can you do this using just one forward pass through the array?

Pull request description:
Day285
Resolves #583.

Patch:
diff --git a/README.md b/README.md
index 5014d04..e9c79db 100644
--- a/README.md
+++ b/README.md
@@ -293,3 +293,4 @@ problems from
 * [Day 282](https://github.com/vaskoz/dailycodingproblem-go/issues/575)
 * [Day 283](https://github.com/vaskoz/dailycodingproblem-go/issues/577)
 * [Day 284](https://github.com/vaskoz/dailycodingproblem-go/issues/578)
+* [Day 285](https://github.com/vaskoz/dailycodingproblem-go/issues/583)
diff --git a/day285/problem.go b/day285/problem.go
new file mode 100644
index 0000000..eb4e811
--- /dev/null
+++ b/day285/problem.go
@@ -0,0 +1,20 @@
+package day285
+
+// SunsetViews returns the number of building with some amount
+// of sunset views.
+// Buildings are ordered from east to west.
+// Runs in O(N) time and O(1) space.
+func SunsetViews(buildingHeights []int) int {
+	if len(buildingHeights) == 0 {
+		return 0
+	}
+	sunsetViews := 1
+	shadow := buildingHeights[len(buildingHeights)-1]
+	for i := len(buildingHeights) - 2; i >= 0; i-- {
+		if buildingHeights[i] > shadow {
+			sunsetViews++
+			shadow = buildingHeights[i]
+		}
+	}
+	return sunsetViews
+}


Test patch:
diff --git a/day285/problem_test.go b/day285/problem_test.go
new file mode 100644
index 0000000..bbd03fd
--- /dev/null
+++ b/day285/problem_test.go
@@ -0,0 +1,30 @@
+package day285
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	buildingHeights []int
+	sunsetViews     int
+}{
+	{[]int{3, 7, 8, 3, 6, 1}, 3},
+	{nil, 0},
+	{[]int{}, 0},
+}
+
+func TestSunsetViews(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if sunsets := SunsetViews(tc.buildingHeights); sunsets != tc.sunsetViews {
+			t.Errorf("Expected %v, got %v", tc.sunsetViews, sunsets)
+		}
+	}
+}
+
+func BenchmarkSunsetViews(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			SunsetViews(tc.buildingHeights)
+		}
+	}
+}