From d4bf288e649ec22aa6d63660b388e6d9fb100878 Mon Sep 17 00:00:00 2001 From: Abinash Karmakar Date: Wed, 8 May 2024 16:40:53 +0530 Subject: [PATCH] Add some code --- leetcode/problems/java/add-two-numbers.java | 25 ++++++ .../java/copy-list-with-random-pointer.java | 37 ++++++++ .../java/find-the-duplicate-number.java | 24 ++++++ leetcode/problems/java/linked-list-cycle.java | 17 ++++ leetcode/problems/java/lru-cache.java | 84 +++++++++++++++++++ .../problems/java/merge-k-sorted-lists.java | 58 +++++++++++++ leetcode/problems/java/relative-ranks.java | 57 +++++++++++++ .../java/reverse-nodes-in-k-group.java | 32 +++++++ 8 files changed, 334 insertions(+) create mode 100644 leetcode/problems/java/add-two-numbers.java create mode 100644 leetcode/problems/java/copy-list-with-random-pointer.java create mode 100644 leetcode/problems/java/find-the-duplicate-number.java create mode 100644 leetcode/problems/java/linked-list-cycle.java create mode 100644 leetcode/problems/java/lru-cache.java create mode 100644 leetcode/problems/java/merge-k-sorted-lists.java create mode 100644 leetcode/problems/java/relative-ranks.java create mode 100644 leetcode/problems/java/reverse-nodes-in-k-group.java diff --git a/leetcode/problems/java/add-two-numbers.java b/leetcode/problems/java/add-two-numbers.java new file mode 100644 index 0000000..1881abc --- /dev/null +++ b/leetcode/problems/java/add-two-numbers.java @@ -0,0 +1,25 @@ +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode sumNode = new ListNode(0); + ListNode curr = sumNode; + int carry = 0, sum; + + while (l1 != null || l2 != null || carry > 0) { + sum = carry; + if (l1 != null) { + sum += l1.val; + l1 = l1.next; + } + if (l2 != null) { + sum += l2.val; + l2 = l2.next; + } + + carry = sum / 10; + curr.next = new ListNode(sum % 10); + curr = curr.next; + } + + return sumNode.next; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/copy-list-with-random-pointer.java b/leetcode/problems/java/copy-list-with-random-pointer.java new file mode 100644 index 0000000..2f91e59 --- /dev/null +++ b/leetcode/problems/java/copy-list-with-random-pointer.java @@ -0,0 +1,37 @@ +import java.util.HashMap; + +class Node { + int val; + Node next; + Node random; + + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; + } +} + +class Solution { + public Node copyRandomList(Node head) { + if (head == null) + return null; + + HashMap map = new HashMap<>(); + Node curr = head; + + while (curr != null) { + map.put(curr, new Node(curr.val)); + curr = curr.next; + } + + curr = head; + while (curr != null) { + map.get(curr).next = map.get(curr.next); + map.get(curr).random = map.get(curr.random); + curr = curr.next; + } + + return map.get(head); + } +} \ No newline at end of file diff --git a/leetcode/problems/java/find-the-duplicate-number.java b/leetcode/problems/java/find-the-duplicate-number.java new file mode 100644 index 0000000..1c38c94 --- /dev/null +++ b/leetcode/problems/java/find-the-duplicate-number.java @@ -0,0 +1,24 @@ +class Solution { + public int findDuplicate(int[] nums) { + int slow = nums[0]; + int fast = nums[0]; + + // Find the intersection point of the two runners. + while (true) { + slow = nums[slow]; + fast = nums[nums[fast]]; + if (slow == fast) + break; + } + + // Find the "entrance" to the cycle. + int slow2 = nums[0]; + while (slow2 != slow) { + slow = nums[slow]; + slow2 = nums[slow2]; + } + + return slow; + + } +} \ No newline at end of file diff --git a/leetcode/problems/java/linked-list-cycle.java b/leetcode/problems/java/linked-list-cycle.java new file mode 100644 index 0000000..a6a6a38 --- /dev/null +++ b/leetcode/problems/java/linked-list-cycle.java @@ -0,0 +1,17 @@ +class Solution { + public boolean hasCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + + if (slow == fast) + return true; + } + + return false; + + } +} \ No newline at end of file diff --git a/leetcode/problems/java/lru-cache.java b/leetcode/problems/java/lru-cache.java new file mode 100644 index 0000000..766a4ed --- /dev/null +++ b/leetcode/problems/java/lru-cache.java @@ -0,0 +1,84 @@ +class LRUCache { + + class Node { + int key; + int value; + Node prev; + Node next; + + Node(int key, int value) { + this.key = key; + this.value = value; + } + } + + Node[] cache; + int capacity, size; + Node head, tail; + + public LRUCache(int capacity) { + this.capacity = capacity; + this.size = 0; + this.cache = new Node[10001]; + + head = new Node(0, 0); + tail = new Node(0, 0); + + head.next = tail; + tail.prev = head; + head.prev = null; + tail.next = null; + } + + void deleteNode(Node node) { + node.prev.next = node.next; + node.next.prev = node.prev; + } + + void addNode(Node node) { + node.next = head.next; + node.next.prev = node; + node.prev = head; + head.next = node; + } + + public int get(int key) { + if (cache[key] != null) { + Node node = cache[key]; + int val = node.value; + deleteNode(node); + addNode(node); + return val; + } else { + return -1; + } + } + + public void put(int key, int value) { + if (cache[key] != null) { + Node node = cache[key]; + node.value = value; + deleteNode(node); + addNode(node); + } else { + Node node = new Node(key, value); + cache[key] = node; + + if (size < capacity) { + size++; + addNode(node); + } else { + cache[tail.prev.key] = null; + deleteNode(tail.prev); + addNode(node); + } + } + } +} + +/** + * Your LRUCache object will be instantiated and called as such: + * LRUCache obj = new LRUCache(capacity); + * int param_1 = obj.get(key); + * obj.put(key,value); + */ \ No newline at end of file diff --git a/leetcode/problems/java/merge-k-sorted-lists.java b/leetcode/problems/java/merge-k-sorted-lists.java new file mode 100644 index 0000000..e9aa6e3 --- /dev/null +++ b/leetcode/problems/java/merge-k-sorted-lists.java @@ -0,0 +1,58 @@ +import java.util.PriorityQueue; + +class Solution { + public ListNode mergeKLists(ListNode[] lists) { + if (lists == null || lists.length == 0) + return null; + PriorityQueue queue = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val); + + ListNode dummy = new ListNode(0); + ListNode tail = dummy; + + for (ListNode node : lists) { + if (node != null) + queue.add(node); + } + + while (!queue.isEmpty()) { + tail.next = queue.poll(); + tail = tail.next; + + if (tail.next != null) + queue.add(tail.next); + } + + return dummy.next; + } +} + +class Solution2 { + public ListNode mergeKLists(ListNode[] lists) { + if (lists == null || lists.length == 0) + return null; + return merge(lists, 0, lists.length - 1); + } + + ListNode merge(ListNode[] lists, int start, int end) { + if (start == end) + return lists[start]; + int mid = (start + end) / 2; + ListNode left = merge(lists, start, mid); + ListNode right = merge(lists, mid + 1, end); + return mergeTwoLists(left, right); + } + + ListNode mergeTwoLists(ListNode l1, ListNode l2) { + if (l1 == null) + return l2; + if (l2 == null) + return l1; + if (l1.val < l2.val) { + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } else { + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } + } +} \ No newline at end of file diff --git a/leetcode/problems/java/relative-ranks.java b/leetcode/problems/java/relative-ranks.java new file mode 100644 index 0000000..7d8ce89 --- /dev/null +++ b/leetcode/problems/java/relative-ranks.java @@ -0,0 +1,57 @@ +import java.util.Arrays; +import java.util.PriorityQueue; + +class Solution { + public String[] findRelativeRanks(int[] score) { + Integer[] sort = new Integer[score.length]; + for (int i = 0; i < score.length; i++) { + sort[i] = i; + } + + Arrays.sort(sort, (a, b) -> (score[b] - score[a])); + + String[] result = new String[score.length]; + + for (int i = 0; i < score.length; i++) { + if (i == 0) { + result[sort[i]] = "Gold Medal"; + } else if (i == 1) { + result[sort[i]] = "Silver Medal"; + } else if (i == 2) { + result[sort[i]] = "Bronze Medal"; + } else { + result[sort[i]] = String.valueOf(i + 1); + } + } + + return result; + } +} + +class Solution2 { + // Using priority queue + public String[] findRelativeRanks(int[] score) { + int n = score.length; + String[] result = new String[n]; + PriorityQueue pq = new PriorityQueue<>(n, (a, b) -> score[b] - score[a]); + + for (int i = 0; i < n; i++) { + pq.add(i); + } + + for (int i = 0; i < n; i++) { + int index = pq.poll(); + if (i == 0) { + result[index] = "Gold Medal"; + } else if (i == 1) { + result[index] = "Silver Medal"; + } else if (i == 2) { + result[index] = "Bronze Medal"; + } else { + result[index] = String.valueOf(i + 1); + } + } + + return result; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/reverse-nodes-in-k-group.java b/leetcode/problems/java/reverse-nodes-in-k-group.java new file mode 100644 index 0000000..aaa65bb --- /dev/null +++ b/leetcode/problems/java/reverse-nodes-in-k-group.java @@ -0,0 +1,32 @@ + +class Solution { + public ListNode reverseKGroup(ListNode head, int k) { + ListNode dummy = new ListNode(0); + dummy.next = head; + ListNode prev = dummy; + ListNode curr = head; + int count = 0; + while (curr != null) { + count++; + if (count % k == 0) { + prev = reverse(prev, curr.next); + curr = prev.next; + } else { + curr = curr.next; + } + } + return dummy.next; + } + + private ListNode reverse(ListNode prev, ListNode next) { + ListNode last = prev.next; + ListNode curr = last.next; + while (curr != next) { + last.next = curr.next; + curr.next = prev.next; + prev.next = curr; + curr = last.next; + } + return last; + } +} \ No newline at end of file