LeetCode Biweekly Contest 172 Discussion and Solution
Priyanshu Chaurasiya
2 months ago
Priyanshu Chaurasiya
2 months ago

Join the conversation by signing in below!
| Problem | Difficulty | Points | Status |
|---|---|---|---|
| 1 | Medium | 4 | Solved |
| 2 | Medium | 4 | Solved |
| 3 | Medium | 5 | Unsolved |
| 4 | Hard | 6 | Unsolved |
class Solution {
public int minOperations(int[] nums) {
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
int i = 0;
int ans = 0;
int duplicateCount = 0;
for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
if(map.get(num) == 2){
duplicateCount++;
}
}
while(i < n && duplicateCount > 0){
for(int j = 0; j < 3 && i < n; j++, i++){
map.put(nums[i], map.get(nums[i]) - 1);
if(map.get(nums[i]) == 1) duplicateCount--;
}
ans++;
}
return ans;
}
}class Solution {
public int maximumSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int[] zero = new int[3];
int[] one = new int[3];
int[] two = new int[3];
int i = 2;
int j = 2;
int k = 2;
for(int l = n - 1; l >= 0; l--){
int rem = nums[l] % 3;
if(rem == 0 && i >= 0){
zero[i] = nums[l];
i--;
}
else if(rem == 1 && j >= 0){
one[j] = nums[l];
j--;
}
else if(rem == 2 && k >= 0){
two[k] = nums[l];
k--;
}
}
int max = 0;
max = i >= 0 ? max : Math.max(max, zero[0] + zero[1] + zero[2]);
max = j >= 0 ? max : Math.max(max, one[0] + one[1] + one[2]);
max = k >= 0 ? max : Math.max(max, two[0] + two[1] + two[2]);
if(i < 2 && j < 2 && k < 2){
max = Math.max(max, zero[2] + one[2] + two[2]);
}
return max;
}
}class Solution {
public long maximumScore(int[] nums, String s) {
int n = nums.length;
PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
long ans = 0;
for(int i = 0; i < n; i++){
queue.add(nums[i]);
if(s.charAt(i) == '1') ans += queue.poll();
}
return ans;
}
}No comments yet, Start the conversation!