Created
June 28, 2021 15:12
-
-
Save terencechain/21beecb1dead5693e344a2ccbefa06a8 to your computer and use it in GitHub Desktop.
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
// Problem: COMPUTE THE LONGEST CONTIGUOUS INCREASING SUBARRAY | |
// | |
// An array is increasing if each element is less than its succeeding element except for the last element. | |
// Implement an algorithm that takes as input an array A of n elements, | |
// and returns the beginning and ending indices of a longest increasing subarray of A. | |
// | |
// For example, if A =(2,11,3,5,13,7,19,17,23), | |
// the longest increasing subarray is (3,5,13), and you should return (2,4) | |
package main | |
import ( | |
"reflect" | |
"testing" | |
) | |
func TestLongestIncreasingSubarray(t *testing.T) { | |
type tc struct { | |
input []int | |
output []int | |
} | |
tests := []tc{ | |
tc{[]int{1,2,3,4}, []int{0,3}}, | |
tc{[]int{1,2,3,1}, []int{0,2}}, | |
tc{[]int{1,2,2,1}, []int{0,1}}, | |
tc{[]int{4,3,2,1}, []int{0,0}}, | |
tc{[]int{2,11,3,5,13,7,19,17,23}, []int{2,4}}, | |
} | |
for _, tc := range tests { | |
if actual := longestIncreasingSubarray(tc.input); !reflect.DeepEqual(actual, tc.output) { | |
t.Errorf("%v failed, wanted %v, got %v", tc.input, tc.output, actual) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment