LeetCode Weekly Contest 482 Discussion and Solution
Priyanshu Chaurasiya
about 1 month ago
Priyanshu Chaurasiya
about 1 month ago

Join the conversation by signing in below!
| Problem | Difficulty | Points | Status |
|---|---|---|---|
| 1 | Medium | 4 | Solved |
| 2 | Medium | 5 | Solved |
| 3 | Medium | 5 | Solved |
| 4 | Hard | 7 | Unsolved |
class Solution {
public long maximumScore(int[] nums) {
int n = nums.length;
int[] minVal = new int[n];
long prefix = 0;
long ans = Long.MIN_VALUE;
minVal[n - 1] = nums[n - 1];
for(int i = n - 2; i >= 0; i--){
minVal[i] = Math.min(minVal[i + 1], nums[i]);
}
for(int i = 0; i < n - 1; i++){
prefix += nums[i];
int suffix = minVal[i + 1];
ans = Math.max(prefix - suffix, ans);
}
return ans;
}
}class Solution {
public long minimumCost(int cost1, int cost2, int costBoth, int need1, int need2) {
long ans = 0;
if(need1 == 0 && need2 == 0){
return 0;
}
int commonItems = Math.min(need1, need2);
need1 -= commonItems;
need2 -= commonItems;
if(commonItems > 0){
ans += 1L * commonItems * Math.min(cost1 + cost2, costBoth);
}
if(need1 == 0){
ans += 1L * need2 * Math.min(cost2, costBoth);
}
if(need2 == 0){
ans += 1L * need1 * Math.min(cost1, costBoth);
}
return ans;
}
}class Solution {
public int minAllOneMultiple(int k) {
Set<Integer> remSet = new HashSet<>();
int rem = 1 % k;
int count = 1;
remSet.add(rem);
while(rem != 0){
rem = (rem * 10 + 1) % k;
count++;
if(remSet.contains(rem)) return -1;
else remSet.add(rem);
}
return count;
}
}No comments yet, Start the conversation!