-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_59.java
More file actions
44 lines (40 loc) · 1.03 KB
/
Copy pathDay_59.java
File metadata and controls
44 lines (40 loc) · 1.03 KB
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
// Apple Redistribution into Boxes
class Solution {
public int minimumBoxes(int[] apple, int[] capacity) {
int n = apple.length, m = capacity.length;
int sum = 0;
Arrays.sort(capacity);
for(int i=0;i<n;i++){
sum+=apple[i];
}
int cnt = 0;
for(int i=m-1;i>=0;i--){
sum-= capacity[i];
cnt++;
if(sum <= 0) break;
}
return cnt;
}
}
// Maximize Sum Of Array After K Negations
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
int n = nums.length;
Arrays.sort(nums);
for(int i=0;i<n;i++){
if(k == 0) break;
if(nums[i] < 0){
nums[i] = - nums[i];
k--;
}
}
int sum = 0, min = Integer.MAX_VALUE;
for(int i=0;i<n;i++){
sum+=nums[i];
min = Math.min(min,nums[i]);
}
if(k % 2 == 0) return sum;
sum = sum - (2*min);
return sum;
}
}