Day 3 of LeetCode Challenge

Tushar PantTushar Pant
2 min read

Problem 1: Minimum Suffix Flips

link to the problem

class Solution {
    public int minFlips(String target) {
        char curr = '0';
        int ans=0;
        for(int i=0; i<target.length(); i++){
            if(target.charAt(i) != curr){
                curr=(curr=='0')?'1':'0';
                ans++;
            }
        }
        return ans;
    }
}

Problem 2: Positions of Large Groups

link to the problem

class Solution {
    public List<List<Integer>> largeGroupPositions(String S) {
        List<List<Integer>> ans = new ArrayList();
        int i = 0, N = S.length(); 
        for (int j = 0; j < N; ++j) {
            if (j == N-1 || S.charAt(j) != S.charAt(j+1)) {
                if (j-i+1 >= 3)
                    ans.add(Arrays.asList(new Integer[]{i, j}));
                i = j + 1;
            }
        }

        return ans;
    }
}

Problem 3: Find Minimum Operations to Make All Elements Divisible by Three

link to the problem

class Solution {
    public int minimumOperations(int[] nums) {
        int ans = 0;
        for(int i : nums){
            if(i%3!=0)
                ans++;
        }
        return ans;
    }
}

Problem 4: Can You Eat Your Favorite Candy on Your Favorite Day?

link to the problem

class Solution {
    public boolean[] canEat(int[] candiesCount, int[][] queries) {

        long[] prefix = new long[candiesCount.length+1];
        boolean[] res = new boolean[queries.length];
        prefix[0] = 0;

        for(int i=1; i< prefix.length; i++)
            prefix[i] = prefix[i-1]+candiesCount[i-1];

        for(int i=0; i< res.length; i++) {
            int type = queries[i][0];
            int day  = queries[i][1];
            int cap  = queries[i][2];

            long maxDay = prefix[type+1]-1; 
            long minDay = prefix[type]/cap; 

            res[i] = (minDay <= day && day <= maxDay);            
        }

        return res;
    }
}

Problem 5: Minimum Cost for Cutting Cake I

link to the problem

class Solution {
    public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
        int res = Arrays.stream(horizontalCut).sum() + Arrays.stream(verticalCut).sum();
        for (int a : horizontalCut)
            for (int b : verticalCut)
                res += Math.min(a, b);
        return res;      
    }
}int res = Arrays.stream(h).sum() + Arrays.stream(v).sum();
        for (int a : h)
            for (int b : v)
                res += Math.min(a, b);
        return res;
0
Subscribe to my newsletter

Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Pant
Tushar Pant