Day 6 of LeetCode Challenge

Tushar PantTushar Pant
2 min read

Problem 1: Flip String to Monotone Increasing

Link to the problem: https://leetcode.com/problems/flip-string-to-monotone-increasing/description/

class Solution {
    public int minFlipsMonoIncr(String s) {
        int dp0 = 0;
        int dp1 = 0;
        for(char ch : s.toCharArray()){
            if(ch=='1')
                dp0++;
            else if(ch=='0')
                dp1 = dp0<dp1+1?dp0: dp1+1;
        }
        return dp0<dp1?dp0:dp1;
    }
}

Problem 2: Slowest Key

Link to the problem: https://leetcode.com/problems/slowest-key/description/

class Solution {
    public char slowestKey(int[] releaseTimes, String keysPressed) {
        int min_dur = releaseTimes[0];
        char ans = ' ';
        for(int i=releaseTimes.length-1; i>0; i--){
            releaseTimes[i] -= releaseTimes[i-1];
            min_dur = releaseTimes[i]>min_dur?releaseTimes[i]:min_dur;
        }
        for(int i=0; i<releaseTimes.length; i++){
            if(releaseTimes[i] == min_dur)
                ans = ans<keysPressed.charAt(i)?keysPressed.charAt(i):ans;
        }
        return ans;
    }
}

Problem 3: Kth distinct String in an Array

Link to the problem: https://leetcode.com/problems/kth-distinct-string-in-an-array/description/

class Solution {
    public String kthDistinct(String[] arr, int k) {
        int count = 0;
        for(int i=0; i<arr.length; i++){
            if(isDistinct(arr, arr[i])) 
                count++;
            if(count == k) 
                return arr[i];
        }
        return "";
    }
    private boolean isDistinct(String[] arr, String str){
        int count = 0;
        for(int i=0; i<arr.length; i++){
            if(arr[i].equals(str)) count++;
        }
        return count!=1?false:true;
    }
}

Problem 4: Last Stone Weight

Link to the problem: https://leetcode.com/problems/last-stone-weight/

class Solution {
    public int lastStoneWeight(int[] stones) {
        int t=1;
        int ans = 0;
        if(stones.length == 1)
            return stones[0];
        else if(stones.length == 2)
            return stones[1]-stones[0]>0?stones[1]-stones[0]:stones[0]-stones[1];
        while(t++<stones.length){
            int x = stones[0]>stones[1]?0:1;
            int y = 1-x;
            for(int i=2; i<stones.length; i++){
                if(stones[i]!=0){
                    if(stones[x]<=stones[i]){
                        y=x;
                        x=i;
                    }else if(stones[y]<stones[i])
                        y=i;
                }
            }
            stones[x] = stones[x]-stones[y];
            stones[y] = 0;
            ans = stones[x];
        }
        return ans;
    }
}

Problem 5: Maximum Unique Subarray Sum after Deletion

Link to the problem: https://leetcode.com/problems/maximum-unique-subarray-sum-after-deletion/

class Solution {
    public int maxSum(int[] nums) {
        if(nums.length==1)
            return nums[0];
        int arr[] = new int[nums.length];
        int len = 0;
        int ans = 0;
        for(int i : nums){
            if(i>0){
                boolean bool = true;
                for(int j=0; j<len; j++){
                    if(i == arr[j])
                        bool = false;
                }
                if(bool){
                    arr[len] = i;
                    len++; 
                    ans+=arr[len-1];
                }
            }
        }
        if(len==0){
            ans = Integer.MIN_VALUE;
            for(int i:nums)
                ans = ans<i?i:ans;
        }
        return ans;
    }
}

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