Skip to main content

Command Palette

Search for a command to run...

Day 40 of LeetCode Challenge

Published
2 min read
Day 40 of LeetCode Challenge
T

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.

Problem 1: Subsets

Link to the problem: https://leetcode.com/problems/subsets/

class Solution {  
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        ArrayList<Integer> arr = new ArrayList<>();
        takeElement(res, nums, 0, arr);
        return res;
    }
    private void takeElement(List<List<Integer>> res, int[] nums, int i, ArrayList<Integer> arr){
        if(i>=nums.length){
            res.add(new ArrayList<>(arr));
            return;
        }else{
            arr.add(nums[i]);
            takeElement(res, nums, i+1, arr);
            arr.remove(arr.size()-1);
            takeElement(res, nums, i+1, arr);
        }
    }
}

Problem 2: Find the Duplicate Number

Link to the problem: https://leetcode.com/problems/find-the-duplicate-number/

class Solution {
    public int findDuplicate(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i:nums){
            if(map.containsKey(i)) return i;
            map.put(i, 1);
        }
        return 0;
    }
}

Problem 3: Reverse Integer

Link to the problem: https://leetcode.com/problems/reverse-integer/

class Solution {
    public int reverse(int x) {
        if(Math.log10(x)==0 || x==0) return x;
        boolean negative=false;
        long y = (long) x;
        if(y<0){
            negative = true;
            y*=(-1);
        }
        long res = 0;
        while(Math.log10(y)+1>1){
            res*=10;
            res+=(y%10);
            y-=(y%10);
            y/=10;
        }
        if(y!=0){
            res*=10;
            res+=y;
        }
        if(negative){
            res*=(-1);
            return res<=((long) Integer.MIN_VALUE)?0:(int) res;
        }else return res>=((long) Integer.MAX_VALUE)?0:(int) res;
    }
}

Problem 4: K Closest Points to Origin

Link to the problem: https://leetcode.com/problems/k-closest-points-to-origin/

class Solution {
    public class Point {
        int x, y, dist;

        public Point(int x, int y, int dist) {
            this.x = x;
            this.y = y;
            this.dist = dist;
        }
    }

    public int[][] kClosest(int[][] points, int k) {
        PriorityQueue<Point> pq = new PriorityQueue<>((p1, p2) -> p1.dist == p2.dist ? p1.x - p2.x : p2.dist - p1.dist);
        for (int[] point : points) {
            int x = point[0];
            int y = point[1];
            int dist = x * x + y * y;
            Point p = new Point(x, y, dist);
            pq.add(p);
            if (pq.size() > k) {
                pq.poll();
            }
        }

        int[][] ans = new int[pq.size()][2];
        int i = 0;
        while (!pq.isEmpty()) {
            Point p = pq.poll();
            ans[i][0] = p.x;
            ans[i][1] = p.y;
            i++;
        }
        return ans;
    }
}

Problem 5: Delete Characters to Make Fancy String

Link to the problem: https://leetcode.com/problems/delete-characters-to-make-fancy-string/

class Solution {
    public String makeFancyString(String s) {

        char[] chars = s.toCharArray();
        char last = chars[0];
        int count = 1;
        int pos = 1;

        for (int i = 1; i < chars.length; i++) {
            if (chars[i] != last) {
                last = chars[i];
                count = 0;
            }

            if (++count > 2) continue;

            chars[pos++] = chars[i];
        }

        return new String(chars, 0, pos);
    }
}