From 4d538bf008928dc98a55f43cfdfa1da860940ece Mon Sep 17 00:00:00 2001 From: Openset Date: Fri, 25 Jan 2019 10:11:51 +0800 Subject: [PATCH] Add: Longest Continuous Increasing Subsequence --- ...ngest_continuous_increasing_subsequence.go | 15 ++++++++ ..._continuous_increasing_subsequence_test.go | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence.go b/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence.go index 68d83d63b..e7bd206a3 100644 --- a/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence.go +++ b/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence.go @@ -1 +1,16 @@ package longest_continuous_increasing_subsequence + +func findLengthOfLCIS(nums []int) int { + max, cur := 0, 1 + for i, v := range nums { + if i >= 1 && v > nums[i-1] { + cur++ + } else { + cur = 1 + } + if cur > max { + max = cur + } + } + return max +} diff --git a/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence_test.go b/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence_test.go index 68d83d63b..c6063ff2a 100644 --- a/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence_test.go +++ b/problems/longest-continuous-increasing-subsequence/longest_continuous_increasing_subsequence_test.go @@ -1 +1,35 @@ package longest_continuous_increasing_subsequence + +import "testing" + +type caseType struct { + input []int + expected int +} + +func TestFindLengthOfLCIS(t *testing.T) { + tests := [...]caseType{ + { + input: []int{1, 3, 5, 4, 7}, + expected: 3, + }, + { + input: []int{2, 2, 2, 2, 2}, + expected: 1, + }, + { + input: []int{}, + expected: 0, + }, + { + input: []int{1, 3, 5, 7}, + expected: 4, + }, + } + for _, tc := range tests { + output := findLengthOfLCIS(tc.input) + if output != tc.expected { + t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected) + } + } +}