Created
February 3, 2019 01:42
-
-
Save hendisantika/ddf0062f0736c126014b15aa66e6344e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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