Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day22q3 #467

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Day 22/aishwarya--J.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```
class Solution {
public int minOperations(int[] nums) {
var counter = new HashMap<Integer, Integer>();
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;
}
}
```
29 changes: 29 additions & 0 deletions Day 22/q1.md
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions Day 22/q3.md
Original file line number Diff line number Diff line change
@@ -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: []
51 changes: 51 additions & 0 deletions Day 22/q3_aishwarya--J.md
Original file line number Diff line number Diff line change
@@ -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;
}

}
```