Skip to main content

Command Palette

Search for a command to run...

Day 17 of LeetCode Challenge

Published
3 min readView as Markdown
Day 17 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: Calculate Score after Performing Instructions

Link to the problem: https://leetcode.com/problems/calculate-score-after-performing-instructions/

class Solution {
    public long calculateScore(String[] instructions, int[] values) {
        long ans = 0;
        int i=0;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 0);
        while(i>=0 && i<instructions.length){
            if(instructions[i].equals("add")){
                ans += values[i];
                i++;
                if(map.containsKey(i))
                    return ans;
                map.put(i, i);
            }
            else if(instructions[i].equals("jump")){
                i=i+values[i];
                if(map.containsKey(i))
                    return ans;
                map.put(i, i);
            }
        }
        return ans;
    }
}

Problem 2: Partition Array into Disjoint Intervals

Link to the problem: https://leetcode.com/problems/partition-array-into-disjoint-intervals/

class Solution {
    public int partitionDisjoint(int[] nums) {
        int N = nums.length;
        int[] minRight = new int[N];
        minRight[N - 1] = nums[N - 1];

        for (int i = N - 2; i >= 0; --i) {
            minRight[i] = Math.min(minRight[i + 1], nums[i]);
        }

        int currMax = nums[0];
        for (int i = 1; i < N; ++i) {
            currMax = Math.max(currMax, nums[i - 1]);
            if (currMax <= minRight[i]) {
                return i;
            }
        }
        return -1;
    }
}

Problem 3: Longest Sub String without Repeating Characters

Link to the problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0)
            return 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int ans = 0;
        for(int i=0; i<s.length(); i++){
            for(int j=i; j<s.length(); j++){
                if(map.containsKey(s.charAt(j)))
                    break;
                if(ans<j-i)
                    ans = j-i;
                map.put(s.charAt(j), 1);
            }
            map.clear();
        }
        return ans+1;
    }
}

Problem 4: Vertical Order Traversal of a Binary Tree

Link to the problem: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
import java.util.Collection;

class Solution {
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root==null)
            return ans;
        List<List<Integer>> list = new ArrayList<>();
        CreateList(root, list, 0, 0);
        List<Integer> arraylist = new ArrayList<>();
        for(List<Integer> l:list)
            arraylist.add(l.get(1));
        Collections.sort(arraylist);
        arraylist = arraylist.stream().distinct().collect(Collectors.toList());
        for(int i:arraylist){
            List<Integer> sublist = new ArrayList<>();
            List<List<Integer>> temp1 = new ArrayList<>();
            List<Integer> temp2 = new ArrayList<>();
            for(List<Integer> l:list){
                if(i==l.get(1)){
                    List<Integer> temp = new ArrayList<>();
                    temp.add(l.get(0));
                    temp.add(l.get(2));
                    temp1.add(temp);
                    temp2.add(l.get(2));
                }
            }
            temp2 = temp2.stream().distinct().collect(Collectors.toList());
            Collections.sort(temp2);
            for(int j:temp2){
                List<Integer> temp3 = new ArrayList<>();
                for(int k=0; k<temp1.size(); k++){
                    if((temp1.get(k)).get(1)==j){
                        int num = (temp1.get(k)).get(0);
                        temp3.add(num);
                    }
                }
                Collections.sort(temp3);
                sublist.addAll(temp3);
            }
            ans.add(sublist);
        }
        return ans;
    }
    private TreeNode CreateList(TreeNode root, List<List<Integer>> list, int val, int depth){
        if(root==null)
            return null;
        CreateList(root.left, list, val-1, depth+1);
        List<Integer> temp = new ArrayList<>();
        temp.add(root.val);
        temp.add(val);
        temp.add(depth);
        list.add(temp);
        CreateList(root.right, list, val+1, depth+1);
        return root;
    }
}

Problem 5: Valid Parenthesis

Link to the problem: https://leetcode.com/problems/valid-parentheses/?envType=problem-list-v2&envId=eeudwo2i

    class Solution {
        public boolean isValid(String s) {
            if(s.length()%2!=0)
                return false;
            Stack<Character> stack = new Stack<>();
            for(char ch:s.toCharArray()){
                if(ch=='(' || ch=='{' || ch=='[' || stack.isEmpty())
                    stack.push(ch);
                else if(stack.peek()=='(' && ch==')')
                    stack.pop();
                else if(stack.peek()=='{' && ch=='}')
                    stack.pop();
                else if(stack.peek()=='[' && ch==']')
                    stack.pop();
                else 
                    return false;
            }
            return stack.isEmpty()?true:false;
        }
    }

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.