LeetCode Biweekly Contest 171 Discussion and Solution
Priyanshu Chaurasiya
11 days ago
Priyanshu Chaurasiya
11 days 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 | 6 | Unsolved |
class Solution {
public boolean isPrime(long n){
if(n <= 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i * i <= n; i += 2){
if(n % i == 0) return false;
}
return true;
}
public boolean completePrime(int num) {
long org = num;
long rev = 0;
while(num > 0){
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
long n = 0;
long m = 0;
int power = 0;
while(org > 0){
long digit = org % 10;
n = digit * (int) Math.pow(10, power) + n;
power++;
org /= 10;
if(!isPrime(n)) return false;
}
while(rev > 0){
long digit = rev % 10;
m = m * 10 + digit;
rev /= 10;
if(!isPrime(m)) return false;
}
return true;
}
}class Solution {
public boolean checkPalin(int n){
StringBuilder sb = new StringBuilder();
while(n > 0){
if(n % 2 == 0) sb.append('0');
else sb.append('1');
n /= 2;
}
int i = 0;
int j = sb.length() - 1;
while(i <= j){
if(sb.charAt(i) != sb.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
public int countIncOpr(int n){
int count = 0;
while(!checkPalin(n)){
count++;
n++;
}
return count;
}
public int countDecOpr(int n){
int count = 0;
while(!checkPalin(n)){
count++;
n--;
}
return count;
}
public int[] minOperations(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
for(int i = 0; i < n; i++){
int inc = countIncOpr(nums[i]);
int dec = countDecOpr(nums[i]);
ans[i] = Math.min(inc, dec);
}
return ans;
}
}class Solution {
public class TaskPoint{
int point;
int idx;
public TaskPoint(int point, int idx){
this.point = point;
this.idx = idx;
}
}
public long maxPoints(int[] t1, int[] t2, int k) {
int n = t1.length;
PriorityQueue<TaskPoint> pq = new PriorityQueue<>((a, b) -> b.point - a.point);
boolean[] completed = new boolean[n];
for(int i = 0; i < n; i++){
int point = t1[i] - t2[i];
TaskPoint tp = new TaskPoint(point, i);
pq.add(tp);
}
long ans = 0;
while(k > 0 && !pq.isEmpty()){
TaskPoint tp = pq.poll();
completed[tp.idx] = true;
ans += t1[tp.idx];
k--;
}
for(int i = 0; i < n; i++){
if(!completed[i]){
ans += Math.max(t1[i], t2[i]);
}
}
return ans;
}
}No comments yet, Start the conversation!