Day 23

Assign Cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int cookieIndex = s.length - 1;
int res = 0;
for (int i = g.length - 1; i >= 0 && cookieIndex >= 0; i--) {
if (g[i] <= s[cookieIndex]) {
cookieIndex--;
res++;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
cookieIndex, res := len(s)-1, 0
for i := len(g) - 1; i >= 0 && cookieIndex >= 0; i-- {
if g[i] <= s[cookieIndex] {
cookieIndex--
res++
}
}
return res
}

Wiggle Subsequence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length == 1) {
return 1;
}
int prediff = 0;
int currdiff = 0;
int res = 1;
for (int i = 0; i < nums.length - 1; i++) {
currdiff = nums[i + 1] - nums[i];
if ((prediff >= 0 && currdiff < 0) || (prediff <= 0 && currdiff > 0)) {
res++;
prediff = currdiff;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func wiggleMaxLength(nums []int) int {
if len(nums) == 1 {
return 1
}
prediff, currdiff, res := 0, 0, 1
for i := 0; i < len(nums)-1; i++ {
currdiff = nums[i+1] - nums[i]
if (prediff >= 0 && currdiff < 0) || (prediff <= 0 && currdiff > 0) {
res++
prediff = currdiff
}
}
return res
}

Maximum Subarray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
int res = Integer.MIN_VALUE;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
res = Math.max(res, sum);
if (sum < 0) {
sum = 0;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
func maxSubArray(nums []int) int {
sum, res := 0, nums[0]
for i := 0; i < len(nums); i++ {
sum += nums[i]
if sum > res {
res = sum
}
if sum < 0 {
sum = 0
}
}
return res
}