-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0531
69 lines (64 loc) · 2.13 KB
/
AlgoPlan0531
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Minimum Subsequence in Non-Increasing Order
##key idea:
- select the largest value from the nums array
- add them till it satisfies the requirement
- boom
class Solution {
public List<Integer> minSubsequence(int[] nums) {
Arrays.sort(nums);
int subsum = 0;
int remainsum = 0;
for (int i =0;i<nums.length;i++){
remainsum+=nums[i];
}
List<Integer> result = new ArrayList();
int cursor = nums.length-1;
while (subsum <= remainsum){
result.add(nums[cursor]);
subsum+=nums[cursor];
remainsum -=nums[cursor];
cursor--;
}
return result;
}
}
#count hills and valleys in an array
##key idea:
- to find nearest non-equal nodes
-if the nearest left node is not equal to index, assign it to be the left one; otherwise, no assignment
-if the nearest right node is not equal to index, assign it to be the right one; otherwise, assign index to the right one
-initiate left and right to be num[0] and num[1]
class Solution {
public int countHillValley(int[] nums) {
int count =0;
int indx = 0;
int nearleft = nums[0];
int nearright =nums[1];
while (indx< nums.length){
if (indx ==0 || indx ==nums.length-1){
//pass
}else{
if (nums[indx-1] !=nums[indx]){
nearleft = nums[indx-1];
}else{
//no movement
}
if ( nums[indx+1] != nums[indx]){
nearright = nums[indx+1];
}else{
nearright = nums[indx];
}
if (nearleft < nums[indx] && nums[indx] > nearright){
count++;
System.out.print(" there is a hill at "+ indx);
}
if (nearleft >nums[indx] && nums[indx]< nearright){
count++;
System.out.print(" there is a valley at "+ indx);
}
}
indx++;
}
return count;
}
}