diff --git a/Day 22/aishwarya--J.md b/Day 22/aishwarya--J.md new file mode 100644 index 00000000..3f52e98c --- /dev/null +++ b/Day 22/aishwarya--J.md @@ -0,0 +1,18 @@ +``` +class Solution { + public int minOperations(int[] nums) { + var counter = new HashMap(); + for (int num: nums) { + counter.put(num, counter.getOrDefault(num, 0) + 1); + } + int ans = 0; + for (int c: counter.values()) { + if (c == 1) { + return -1; + } + ans += Math.ceil((double) c / 3); + } + return ans; + } +} +``` \ No newline at end of file diff --git a/Day 22/q1.md b/Day 22/q1.md new file mode 100644 index 00000000..acd2c656 --- /dev/null +++ b/Day 22/q1.md @@ -0,0 +1,29 @@ +You are given a 0-indexed array nums consisting of positive integers. + +There are two types of operations that you can apply on the array any number of times: + +Choose two elements with equal values and delete them from the array. +Choose three elements with equal values and delete them from the array. +Return the minimum number of operations required to make the array empty, or -1 if it is not possible. + +Example 1: + +Input: nums = [2,3,3,2,2,4,2,3,4] +Output: 4 +Explanation: We can apply the following operations to make the array empty: + +Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. +Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. +Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. +Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. +It can be shown that we cannot make the array empty in less than 4 operations. +Example 2: + +Input: nums = [2,1,2,2,3,3] +Output: -1 +Explanation: It is impossible to empty the array. + +Constraints: + +2 <= nums.length <= 105 +1 <= nums[i] <= 106 \ No newline at end of file diff --git a/Day 22/q3.md b/Day 22/q3.md new file mode 100644 index 00000000..fedee417 --- /dev/null +++ b/Day 22/q3.md @@ -0,0 +1,14 @@ +## sort list +Given the head of a linked list, return the list after sorting it in ascending order. + +Example 1: +Input: head = [4,2,1,3] +Output: [1,2,3,4] + +Example 2: +Input: head = [-1,5,3,4,0] +Output: [-1,0,3,4,5] + +Example 3: +Input: head = [] +Output: [] \ No newline at end of file diff --git a/Day 22/q3_aishwarya--J.md b/Day 22/q3_aishwarya--J.md new file mode 100644 index 00000000..0580cf70 --- /dev/null +++ b/Day 22/q3_aishwarya--J.md @@ -0,0 +1,51 @@ +``` +public class Solution { + + public ListNode sortList(ListNode head) { + if (head == null || head.next == null) + return head; + + // step 1. cut the list to two halves + ListNode prev = null, slow = head, fast = head; + + while (fast != null && fast.next != null) { + prev = slow; + slow = slow.next; + fast = fast.next.next; + } + + prev.next = null; + + // step 2. sort each half + ListNode l1 = sortList(head); + ListNode l2 = sortList(slow); + + // step 3. merge l1 and l2 + return merge(l1, l2); + } + + ListNode merge(ListNode l1, ListNode l2) { + ListNode l = new ListNode(0), p = l; + + while (l1 != null && l2 != null) { + if (l1.val < l2.val) { + p.next = l1; + l1 = l1.next; + } else { + p.next = l2; + l2 = l2.next; + } + p = p.next; + } + + if (l1 != null) + p.next = l1; + + if (l2 != null) + p.next = l2; + + return l.next; + } + +} +``` \ No newline at end of file