Skip to main content

Command Palette

Search for a command to run...

Day 28 of LeetCode Challenge

Published
2 min readView as Markdown
Day 28 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: Product of Array Except Self

Link to the problem: https://leetcode.com/problems/product-of-array-except-self/

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int zeroes = 0;
        boolean numbers = false;
        int product = 1;
        for(int i:nums){
            if(i==0)
                zeroes++;
            else{
                numbers = true;
                product*=i;
            }
        }
        for(int i=0; i<nums.length; i++){
            if(zeroes>1)
                nums[i]=0;
            else if(zeroes==1 && nums[i]==0)
                nums[i]=product;
            else if(zeroes==1)
                nums[i]=0;
            else
                nums[i]=product/nums[i];
        }
        return nums;
    }
}

Problem 2: Combination Sum

Link to the problem: https://leetcode.com/problems/combination-sum/

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();

        makeCombination(candidates, target, 0, new ArrayList<>(), 0, res);
        return res;
    }

    private void makeCombination(int[] candidates, int target, int idx, List<Integer> comb, int List<List<Integer>> res) {
        if (total == target) {
            res.add(new ArrayList<>(comb));
            return;
        }

        if (total > target || idx >= candidates.length) {
            return;
        }

        comb.add(candidates[idx]);
        makeCombination(candidates, target, idx, comb, total + candidates[idx], res);
        comb.remove(comb.size() - 1);
        makeCombination(candidates, target, idx + 1, comb, total, res);
    }
}

Problem 3: Find the kth Character in String Game I

Link to the problem: https://leetcode.com/problems/find-the-k-th-character-in-string-game-i/

class Solution {
    public char kthCharacter(int k) {
        String str = "a";
        while(str.length()<k){
            String str2 = "";
            for(char ch:str.toCharArray()){
                if(ch=='z')
                    ch='a';
                else
                    ch++;
                str2 = str2.concat(Character.toString(ch));
            }
            str = str.concat(str2);
        }
        return str.charAt(k-1);
    }
}

Problem 4: Sort Colors

Link to the problem: https://leetcode.com/problems/sort-colors/

class Solution {
    public void sortColors(int[] nums) {
        int red=0;
        int white=0;
        int blue=0;
        for(int i:nums){
            if(i==0)
                red++;
            else if(i==1)
                white++;
            else
                blue++;
        }
        int index = 0;
        while(red-->0)
            nums[index++]=0;
        while(white-->0)
            nums[index++]=1;
        while(blue-->0)
            nums[index++]=2;
    }
}

Problem 5: Container with Most Water

Link to the problem: https://leetcode.com/problems/container-with-most-water/

class Solution {
    public int maxArea(int[] height) {

        int maxArea = 0;
        int left = 0;
        int right = height.length - 1;

        while (left < right) {
            maxArea = Math.max(maxArea, (right - left) * Math.min(height[left], height[right]));

            if (height[left] < height[right])
                left++;
            else
                right--;
        }
        return maxArea;        
    }
}

More from this blog

T

Tushar Pant's Blog

178 posts

Welcome to Tushar's Blog! Here, I share my journey in tech, covering DevOps and cloud computing. Explore tutorials, tips, and insights to fuel your learning and growth in technology.