Skip to main content

Command Palette

Search for a command to run...

Day 12 of LeetCode Challenge

Published
3 min readView as Markdown
Day 12 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: Merge 2 Sorted Lists

Link to the problem: https://leetcode.com/problems/merge-two-sorted-lists/description/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode ans = new ListNode();
        ListNode temp = ans;
        while(list1!=null && list2!=null){
            if(list1.val<=list2.val){
                temp.next = list1;
                list1 = list1.next;
            }else{
                temp.next = list2;
                list2 = list2.next;
            }
            temp = temp.next;
        }
        while(list1!=null){
            temp.next = list1;
            list1 = list1.next;
            temp = temp.next;
        }
        while(list2!=null){
            temp.next = list2;
            list2 = list2.next;
            temp = temp.next;
        }
        return ans.next;
    }
}

Problem 2: Minimum Pair Removal to Sort Array I

Link to the problem: https://leetcode.com/problems/minimum-pair-removal-to-sort-array-i/description/

class Solution {
    public int minimumPairRemoval(int[] nums) {
        if(nums.length==0 || nums.length==1)
            return 0;
        int ans = 0;
        ArrayList<Integer> arr = new ArrayList<>(nums.length);
        for(int i:nums)
            arr.add(i);
        while(!isSorted(arr)){
            int min = Integer.MAX_VALUE;
            int index = -1;
            int len = arr.size();
            for(int i=0; i<len-1; i++){
                if(min>arr.get(i)+arr.get(i+1)){
                    min = arr.get(i) + arr.get(i+1);
                    index = i;
                }
            }
            if(index!=-1){
                arr.remove(index+1);
                arr.remove(index);
                arr.add(index, min);
                System.out.println(arr);
                ans++;
            }
        }
        return ans;
    }
    private boolean isSorted(ArrayList<Integer> arr){
        if(arr.size() == 0 || arr.size() == 1)
            return true;
        for(int i=0; i<arr.size()-1; i++){
            if(arr.get(i)>arr.get(i+1))
                return false;
        }
        return true;
    }
}

Problem 3: String to Integer

Link to the problem: https://leetcode.com/problems/string-to-integer-atoi/submissions/1659011209/

class Solution {
    public int myAtoi(String s) {
        int index = 0;
        int n = s.length();
        int sign = 1;
        int result = 0;

        while (index < n && s.charAt(index) == ' ') {
            index++;
        }
        if (index == n) return 0;

        if (s.charAt(index) == '+' || s.charAt(index) == '-') {
            sign = s.charAt(index) == '-' ? -1 : 1;
            index++;
        }

        while (index < n && Character.isDigit(s.charAt(index))) {
            int digit = s.charAt(index) - '0';

            if (result > Integer.MAX_VALUE / 10 || 
                (result == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }

            result = result * 10 + digit;
            index++;
        }

        return result * sign;
    }
}

Problem 4: Make the String Great

Link to the problem: https://leetcode.com/problems/make-the-string-great/description/

class Solution {
    public String makeGood(String s) {
        if(s.length() == 0 || s.length() == 1)
            return s;
        while(!isGood(s)){
            for(int i=0; i<s.length()-1; i++){
                if(s.charAt(i)-s.charAt(i+1)==32 || s.charAt(i)-s.charAt(i+1)==-32)
                    s = s.substring(0, i).concat(s.substring(i+2));
            }
        }
        return s;
    }
    private boolean isGood(String s){
        for(int i=0; i<s.length()-1; i++){
            if(s.charAt(i)-s.charAt(i+1)==32 || s.charAt(i)-s.charAt(i+1)==-32)
                return false;
        }
        return true;
    }
}

Problem 5: Palindrome Linked List

Link to the problem: https://leetcode.com/problems/palindrome-linked-list/description/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while(head!=null){
            stack1.push(head.val);
            head = head.next;
        }
        int len = stack1.size();
        if(len==0 || len==1)
            return true;
        if(len%2==0){
            len/=2;
            while(len-->0)
                stack2.push(stack1.pop());
        }else{
            len/=2;
            while(len-->0)
                stack2.push(stack1.pop());
            stack1.pop();
        }
        while(!stack1.empty() && stack1.peek()==stack2.peek()){
            stack1.pop();
            stack2.pop();
        }
        if(stack1.size()>0)
            return false;
        return true;
    }
}

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.