Skip to main content

Command Palette

Search for a command to run...

Day 11 of LeetCode Challenge

Published
3 min readView as Markdown
Day 11 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: Wildest Vertical Area Between Two Points Containing No Points

Link to the problem: https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/description/

class Solution {
    public int maxWidthOfVerticalArea(int[][] points) {
        int nums[] = new int[points.length];
        for(int i=0; i<points.length; i++)
            nums[i] = points[i][0];

        nums = MergeSort(nums, 0, nums.length-1);
        int ans = 0;
        for(int i=0; i<nums.length-1; i++)
            ans = ans>nums[i+1]-nums[i]?ans:nums[i+1]-nums[i];
        return ans;
    }

    private static int[] MergeSort(int[] arr, int left, int right){
        if(left<right){
            int mid = (left+right)/2;
            MergeSort(arr, left, mid);
            MergeSort(arr, mid+1, right);
            Merge(arr, left, mid, right);
        }
        return arr;
    }

    private static int[] Merge(int[] arr, int left, int mid, int right){
        int n1 = mid-left+1;
        int n2 = right-mid;

        int[] leftarr = new int[n1];
        int[] rightarr = new int[n2];

        for(int i=0; i<n1; ++i)
            leftarr[i] = arr[left+i];
        for(int j=0; j<n2; ++j)
            rightarr[j] = arr[mid+j+1];

        int i=0; int j=0; int k=left;

        while(i<n1 && j<n2){
            if(leftarr[i]<=rightarr[j])
                arr[k++] = leftarr[i++];
            else
                arr[k++] = rightarr[j++];
        }
        while(i<n1)
            arr[k++] = leftarr[i++];
        while(j<n2)
            arr[k++] = rightarr[j++];

        return arr;
    }
}

Problem 2: Remove Linked List Elements

Link to the problem: https://leetcode.com/problems/remove-linked-list-elements/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 removeElements(ListNode head, int val) {
        ListNode start = head;
        while(start != null && start.val == val)
            start = start.next;
        head = start;
        if(start==null)
            return null;
        while(start.next!=null){
            if(start.next.val==val)
                start.next = start.next.next;
            else
                start = start.next;
        }
        return head;
    }
}

Problem 3: Intersection of 2 Linked Lists

Link to the problem: https://leetcode.com/problems/intersection-of-two-linked-lists/description/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 import java.util.HashMap;

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode start1 = headA;
        ListNode start2 = headB;
        HashMap<ListNode, Integer> map = new HashMap<>();
        while(start1!=null){
            int value = start1.val;
            map.put(start1, value);
            start1 = start1.next;
        }
        System.out.println(map);
        while(start2!=null){
            if(map.containsKey(start2))
                return start2;
            start2 = start2.next;
        }
        return null;
    }
}

Problem 4: Reverse Linked List

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

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode node = null;

        while (head != null) {
            ListNode temp = head.next;
            head.next = node;
            node = head;
            head = temp;
        }

        return node;        
    }
}

Problem 5: Remove Duplicates from Sorted Linked List

Link to the problem: https://leetcode.com/problems/remove-duplicates-from-sorted-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 ListNode deleteDuplicates(ListNode head) {
        ListNode start = head;
        if(head==null)
            return null;
        while(start.next!=null){
            if(start.val == start.next.val)
                start.next = start.next.next;
            else
                start = start.next;
        }
        return head;
    }
}

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.