Skip to content

Instantly share code, notes, and snippets.

@hendisantika
Created February 3, 2019 01:42
Show Gist options
  • Save hendisantika/ddf0062f0736c126014b15aa66e6344e to your computer and use it in GitHub Desktop.
Save hendisantika/ddf0062f0736c126014b15aa66e6344e to your computer and use it in GitHub Desktop.
public class Solution {
public int countHillNValley(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return 1;
int count = 1;
int i = 0, j = i + 1;
while(i < nums.length && j < nums.length){
if(nums[j] == nums[i]){
++i;
++j;
}else if(nums[j] > nums[i]){
++count;
int k = j + 1;
while(k < nums.length && nums[k] >= nums[k - 1]){
++k;
}
if(k == nums.length)
return count;
i = k - 1;
j = k;
}else{
++count;
int k = j + 1;
while(k < nums.length && nums[k] <= nums[k - 1]){
++k;
}
if(k == nums.length)
return count;
i = k - 1;
j = k;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment