Skip to main content

Command Palette

Search for a command to run...

Day 43 of LeetCode Challenge

Published
2 min read
Day 43 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: Find All K-Distant Indices in an Array

Link to the problem: https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/

class Solution {
    public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        Map<Integer, Integer> res = new HashMap<>();
        for(int i=0; i<nums.length; i++) if(nums[i]==key) map.put(i, 0);
        for(int i=0; i<nums.length; i++){
            for(int j=0; j<nums.length; j++){
                if(Math.abs(i-j)<=k && map.containsKey(j) && !res.containsKey(i)) res.put(i, 0);
            }
        }
        Set<Integer> set = res.keySet();
        List<Integer> ans = new ArrayList<>(set);
        Collections.sort(ans);
        return ans;
    }
}

Problem 2: Longest Increasing Subsequence

Link to the problem: https://leetcode.com/problems/longest-increasing-subsequence/

class Solution {
    public int lengthOfLIS(int[] nums) {
        int n = nums.length;
        int dp[] = new int[n];
        Arrays.fill(dp, 1);
        for(int i=1; i<n; i++){
            for(int j=0; j<i; j++){
                if(nums[i]>nums[j]) dp[i] = Math.max(dp[i], dp[j]+1);
            }
        }
        int num = 0;
        for(int i:dp) num = Math.max(num, i);
        return num;
    }
}

Problem 3: Maximum Product Subarray

Link to the problem: https://leetcode.com/problems/maximum-product-subarray/

class Solution {
    public int maxProduct(int[] nums) {
        int n = nums.length;
        if(n==1) return nums[0];
        int lProd = 1;
        int rProd = 1;
        int res = Integer.MIN_VALUE;
        for(int i=0; i<n; i++){
            int j = n-i-1;
            if(nums[i]==0){
                res = Math.max(res, 0);
                lProd = 1;
            }else{
                lProd *= nums[i];
                res = Math.max(lProd, res);
            }
            if(nums[j]==0){
                res = Math.max(0, res);
                rProd = 1;
            }else{
                rProd *= nums[j];
                res = Math.max(rProd, res);
            }
        }
        return res;
    }
}

Problem 4: Find Pivot Index

Link to the problem: https://leetcode.com/problems/find-pivot-index/

class Solution {
    public int pivotIndex(int[] nums) {
        int suffixSum = 0;
        for(int i:nums) suffixSum+=i;
        int prefixSum = 0;
        for(int i=0; i<nums.length; i++){
            suffixSum -= nums[i];
            if(prefixSum==suffixSum) return i;
            prefixSum += nums[i];
        }
        return -1;
    }
}

Problem 5: Replace Elements with Greatest Element on the Right Side

Link to the problem: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

class Solution {
    public int[] replaceElements(int[] arr) {
        int n = arr.length;
        int rMax = arr[n-1];
        arr[n-1] = -1;
        for(int i=n-2; i>=0; i--){
            int temp = arr[i];
            arr[i] = rMax;
            rMax = Math.max(temp, rMax);
        }
        return arr;
    }
}