Skip to content

Commit 04723b5

Browse files
authored
Added tasks 3507-3510
1 parent ec417ab commit 04723b5

File tree

12 files changed

+755
-0
lines changed

12 files changed

+755
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3501_3600.s3507_minimum_pair_removal_to_sort_array_i;
2+
3+
// #Easy #Array #Hash_Table #Heap_Priority_Queue #Simulation #Linked_List #Ordered_Set
4+
// #Doubly_Linked_List #2025_04_09_Time_1_ms_(100.00%)_Space_42.96_MB_(53.67%)
5+
6+
public class Solution {
7+
public int minimumPairRemoval(int[] nums) {
8+
int operations = 0;
9+
while (!isNonDecreasing(nums)) {
10+
int minSum = Integer.MAX_VALUE;
11+
int index = 0;
12+
// Find the leftmost pair with minimum sum
13+
for (int i = 0; i < nums.length - 1; i++) {
14+
int sum = nums[i] + nums[i + 1];
15+
if (sum < minSum) {
16+
minSum = sum;
17+
index = i;
18+
}
19+
}
20+
// Merge the pair at index
21+
int[] newNums = new int[nums.length - 1];
22+
int j = 0;
23+
int i = 0;
24+
while (i < nums.length) {
25+
if (i == index) {
26+
newNums[j++] = nums[i] + nums[i + 1];
27+
// Skip the next one since it's merged
28+
i++;
29+
} else {
30+
newNums[j++] = nums[i];
31+
}
32+
i++;
33+
}
34+
nums = newNums;
35+
operations++;
36+
}
37+
return operations;
38+
}
39+
40+
private boolean isNonDecreasing(int[] nums) {
41+
for (int i = 1; i < nums.length; i++) {
42+
if (nums[i] < nums[i - 1]) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3507\. Minimum Pair Removal to Sort Array I
2+
3+
Easy
4+
5+
Given an array `nums`, you can perform the following operation any number of times:
6+
7+
* Select the **adjacent** pair with the **minimum** sum in `nums`. If multiple such pairs exist, choose the leftmost one.
8+
* Replace the pair with their sum.
9+
10+
Return the **minimum number of operations** needed to make the array **non-decreasing**.
11+
12+
An array is said to be **non-decreasing** if each element is greater than or equal to its previous element (if it exists).
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [5,2,3,1]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
* The pair `(3,1)` has the minimum sum of 4. After replacement, `nums = [5,2,4]`.
23+
* The pair `(2,4)` has the minimum sum of 6. After replacement, `nums = [5,6]`.
24+
25+
The array `nums` became non-decreasing in two operations.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,2,2]
30+
31+
**Output:** 0
32+
33+
**Explanation:**
34+
35+
The array `nums` is already sorted.
36+
37+
**Constraints:**
38+
39+
* `1 <= nums.length <= 50`
40+
* `-1000 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package g3501_3600.s3508_implement_router;
2+
3+
// #Medium #Array #Hash_Table #Binary_Search #Design #Ordered_Set #Queue
4+
// #2025_04_09_Time_137_ms_(100.00%)_Space_116.63_MB_(91.98%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
11+
@SuppressWarnings("java:S135")
12+
public class Router {
13+
private final int size;
14+
private int cur;
15+
private final Queue<int[]> q;
16+
private final HashMap<Integer, ArrayList<int[]>> map;
17+
18+
public Router(int memoryLimit) {
19+
q = new LinkedList<>();
20+
map = new HashMap<>();
21+
size = memoryLimit;
22+
cur = 0;
23+
}
24+
25+
public boolean addPacket(int source, int destination, int timestamp) {
26+
if (map.containsKey(destination)) {
27+
boolean found = false;
28+
ArrayList<int[]> list = map.get(destination);
29+
for (int i = list.size() - 1; i >= 0; i--) {
30+
if (list.get(i)[1] < timestamp) {
31+
break;
32+
} else if (list.get(i)[0] == source) {
33+
found = true;
34+
break;
35+
}
36+
}
37+
if (found) {
38+
return false;
39+
}
40+
}
41+
if (map.containsKey(destination)) {
42+
ArrayList<int[]> list = map.get(destination);
43+
list.add(new int[] {source, timestamp});
44+
cur++;
45+
q.offer(new int[] {source, destination, timestamp});
46+
} else {
47+
ArrayList<int[]> temp = new ArrayList<>();
48+
temp.add(new int[] {source, timestamp});
49+
cur++;
50+
map.put(destination, temp);
51+
q.offer(new int[] {source, destination, timestamp});
52+
}
53+
if (cur > size) {
54+
forwardPacket();
55+
}
56+
return true;
57+
}
58+
59+
public int[] forwardPacket() {
60+
if (q.isEmpty()) {
61+
return new int[] {};
62+
}
63+
int[] temp = q.poll();
64+
ArrayList<int[]> list = map.get(temp[1]);
65+
list.remove(0);
66+
if (list.isEmpty()) {
67+
map.remove(temp[1]);
68+
}
69+
cur--;
70+
return temp;
71+
}
72+
73+
public int getCount(int destination, int startTime, int endTime) {
74+
if (map.containsKey(destination)) {
75+
ArrayList<int[]> list = map.get(destination);
76+
int lower = -1;
77+
int higher = -1;
78+
for (int i = 0; i < list.size(); i++) {
79+
if (list.get(i)[1] >= startTime) {
80+
lower = i;
81+
break;
82+
}
83+
}
84+
for (int i = list.size() - 1; i >= 0; i--) {
85+
if (list.get(i)[1] <= endTime) {
86+
higher = i;
87+
break;
88+
}
89+
}
90+
if (lower == -1 || higher == -1) {
91+
return 0;
92+
} else {
93+
return Math.max(0, higher - lower + 1);
94+
}
95+
} else {
96+
return 0;
97+
}
98+
}
99+
}
100+
101+
/*
102+
* Your Router object will be instantiated and called as such:
103+
* Router obj = new Router(memoryLimit);
104+
* boolean param_1 = obj.addPacket(source,destination,timestamp);
105+
* int[] param_2 = obj.forwardPacket();
106+
* int param_3 = obj.getCount(destination,startTime,endTime);
107+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
3508\. Implement Router
2+
3+
Medium
4+
5+
Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:
6+
7+
* `source`: A unique identifier for the machine that generated the packet.
8+
* `destination`: A unique identifier for the target machine.
9+
* `timestamp`: The time at which the packet arrived at the router.
10+
11+
Implement the `Router` class:
12+
13+
`Router(int memoryLimit)`: Initializes the Router object with a fixed memory limit.
14+
15+
* `memoryLimit` is the **maximum** number of packets the router can store at any given time.
16+
* If adding a new packet would exceed this limit, the **oldest** packet must be removed to free up space.
17+
18+
`bool addPacket(int source, int destination, int timestamp)`: Adds a packet with the given attributes to the router.
19+
20+
* A packet is considered a duplicate if another packet with the same `source`, `destination`, and `timestamp` already exists in the router.
21+
* Return `true` if the packet is successfully added (i.e., it is not a duplicate); otherwise return `false`.
22+
23+
`int[] forwardPacket()`: Forwards the next packet in FIFO (First In First Out) order.
24+
25+
* Remove the packet from storage.
26+
* Return the packet as an array `[source, destination, timestamp]`.
27+
* If there are no packets to forward, return an empty array.
28+
29+
`int getCount(int destination, int startTime, int endTime)`:
30+
31+
* Returns the number of packets currently stored in the router (i.e., not yet forwarded) that have the specified destination and have timestamps in the inclusive range `[startTime, endTime]`.
32+
33+
**Note** that queries for `addPacket` will be made in increasing order of `timestamp`.
34+
35+
**Example 1:**
36+
37+
**Input:**
38+
["Router", "addPacket", "addPacket", "addPacket", "addPacket", "addPacket", "forwardPacket", "addPacket", "getCount"]
39+
[[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]]
40+
41+
**Output:**
42+
[null, true, true, false, true, true, [2, 5, 90], true, 1]
43+
44+
**Explanation**
45+
46+
Router router = new Router(3); // Initialize Router with memoryLimit of 3.
47+
router.addPacket(1, 4, 90); // Packet is added. Return True.
48+
router.addPacket(2, 5, 90); // Packet is added. Return True.
49+
router.addPacket(1, 4, 90); // This is a duplicate packet. Return False.
50+
router.addPacket(3, 5, 95); // Packet is added. Return True
51+
router.addPacket(4, 5, 105); // Packet is added, `[1, 4, 90]` is removed as number of packets exceeds memoryLimit. Return True.
52+
router.forwardPacket(); // Return `[2, 5, 90]` and remove it from router.
53+
router.addPacket(5, 2, 110); // Packet is added. Return True.
54+
router.getCount(5, 100, 110); // The only packet with destination 5 and timestamp in the inclusive range `[100, 110]` is `[4, 5, 105]`. Return 1.
55+
56+
**Example 2:**
57+
58+
**Input:**
59+
["Router", "addPacket", "forwardPacket", "forwardPacket"]
60+
[[2], [7, 4, 90], [], []]
61+
62+
**Output:**
63+
[null, true, [7, 4, 90], []]
64+
65+
**Explanation**
66+
67+
Router router = new Router(2); // Initialize `Router` with `memoryLimit` of 2.
68+
router.addPacket(7, 4, 90); // Return True.
69+
router.forwardPacket(); // Return `[7, 4, 90]`.
70+
router.forwardPacket(); // There are no packets left, return `[]`.
71+
72+
**Constraints:**
73+
74+
* <code>2 <= memoryLimit <= 10<sup>5</sup></code>
75+
* <code>1 <= source, destination <= 2 * 10<sup>5</sup></code>
76+
* <code>1 <= timestamp <= 10<sup>9</sup></code>
77+
* <code>1 <= startTime <= endTime <= 10<sup>9</sup></code>
78+
* At most <code>10<sup>5</sup></code> calls will be made to `addPacket`, `forwardPacket`, and `getCount` methods altogether.
79+
* queries for `addPacket` will be made in increasing order of `timestamp`.

0 commit comments

Comments
 (0)