-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_33.java
More file actions
75 lines (67 loc) · 1.63 KB
/
Copy pathDay_33.java
File metadata and controls
75 lines (67 loc) · 1.63 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
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
70
71
72
73
74
75
// Lemonade Change
class Solution {
public boolean lemonadeChange(int[] bills) {
int n = bills.length;
int cntf = 0, cntt = 0;
for(int i=0;i<n;i++){
int bill = bills[i];
if(bill == 5) cntf++;
else if(bill == 10){
if(cntf == 0) return false;
cntf--;
cntt++;
}
else{
if(cntf > 0 && cntt > 0){
cntf--;
cntt--;
}
else if(cntf >= 3) cntf-=3;
else return false;
}
}
return true;
}
}
// Shortest Job First
class Solution {
static int solve(int bt[]) {
int n = bt.length;
Arrays.sort(bt);
int twt = 0, wt = 0;
for(int i=1;i<n;i++){
wt+=bt[i-1];
twt+=wt;
}
return (twt/n);
}
}
// Longest Harmonious Subsequence
class Solution {
public int findLHS(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
int l = 0,maxlen = 0,min=nums[0],max=nums[0];
for(int r=1;r<n;r++){
max=nums[r];
if(min == max) continue;
while((l<r) && (max-min) != 1){
l++;
min = nums[l];
}
if(max-min == 1)maxlen = Math.max(maxlen,r-l+1);
}
return maxlen;
}
}
// class Solution {
public int arrayPairSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
int sum = 0;
for(int i=0;i<n;i+=2){
sum+=nums[i];
}
return sum;
}
}