Skip to content

Commit f082f1e

Browse files
committed
Added tasks 3507-3510
1 parent 478dc6c commit f082f1e

File tree

12 files changed

+911
-0
lines changed

12 files changed

+911
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3501_3600.s3507_minimum_pair_removal_to_sort_array_i;
2+
3+
// #Easy #2025_04_06_Time_1_ms_(100.00%)_Space_43.18_MB_(31.56%)
4+
5+
public class Solution {
6+
public int minimumPairRemoval(int[] nums) {
7+
int operations = 0;
8+
while (!isNonDecreasing(nums)) {
9+
int minSum = Integer.MAX_VALUE;
10+
int index = 0;
11+
// Find the leftmost pair with minimum sum
12+
for (int i = 0; i < nums.length - 1; i++) {
13+
int sum = nums[i] + nums[i + 1];
14+
if (sum < minSum) {
15+
minSum = sum;
16+
index = i;
17+
}
18+
}
19+
// Merge the pair at index
20+
int[] newNums = new int[nums.length - 1];
21+
int j = 0;
22+
for (int i = 0; i < nums.length; i++) {
23+
if (i == index) {
24+
newNums[j++] = nums[i] + nums[i + 1];
25+
// Skip the next one since it's merged
26+
i++;
27+
} else {
28+
newNums[j++] = nums[i];
29+
}
30+
}
31+
nums = newNums;
32+
operations++;
33+
}
34+
return operations;
35+
}
36+
37+
private boolean isNonDecreasing(int[] nums) {
38+
for (int i = 1; i < nums.length; i++) {
39+
if (nums[i] < nums[i - 1]) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
}
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,105 @@
1+
package g3501_3600.s3508_implement_router;
2+
3+
// #Medium #2025_04_06_Time_145_ms_(100.00%)_Space_115.44_MB_(94.12%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
10+
public class Router {
11+
private final int size;
12+
private int cur;
13+
private final Queue<int[]> q;
14+
private final HashMap<Integer, ArrayList<int[]>> map;
15+
16+
public Router(int memoryLimit) {
17+
q = new LinkedList<>();
18+
map = new HashMap<>();
19+
size = memoryLimit;
20+
cur = 0;
21+
}
22+
23+
public boolean addPacket(int source, int destination, int timestamp) {
24+
if (map.containsKey(destination)) {
25+
boolean found = false;
26+
ArrayList<int[]> list = map.get(destination);
27+
for (int i = list.size() - 1; i >= 0; i--) {
28+
if (list.get(i)[1] < timestamp) {
29+
break;
30+
} else if (list.get(i)[0] == source) {
31+
found = true;
32+
break;
33+
}
34+
}
35+
if (found) {
36+
return false;
37+
}
38+
}
39+
if (map.containsKey(destination)) {
40+
ArrayList<int[]> list = map.get(destination);
41+
list.add(new int[] {source, timestamp});
42+
cur++;
43+
q.offer(new int[] {source, destination, timestamp});
44+
} else {
45+
ArrayList<int[]> temp = new ArrayList<>();
46+
temp.add(new int[] {source, timestamp});
47+
cur++;
48+
map.put(destination, temp);
49+
q.offer(new int[] {source, destination, timestamp});
50+
}
51+
if (cur > size) {
52+
forwardPacket();
53+
}
54+
return true;
55+
}
56+
57+
public int[] forwardPacket() {
58+
if (q.isEmpty()) {
59+
return new int[] {};
60+
}
61+
int[] temp = q.poll();
62+
ArrayList<int[]> list = map.get(temp[1]);
63+
list.remove(0);
64+
if (list.isEmpty()) {
65+
map.remove(temp[1]);
66+
}
67+
cur--;
68+
return temp;
69+
}
70+
71+
public int getCount(int destination, int startTime, int endTime) {
72+
if (map.containsKey(destination)) {
73+
ArrayList<int[]> list = map.get(destination);
74+
int lower = -1;
75+
int higher = -1;
76+
for (int i = 0; i < list.size(); i++) {
77+
if (list.get(i)[1] >= startTime) {
78+
lower = i;
79+
break;
80+
}
81+
}
82+
for (int i = list.size() - 1; i >= 0; i--) {
83+
if (list.get(i)[1] <= endTime) {
84+
higher = i;
85+
break;
86+
}
87+
}
88+
if (lower == -1 || higher == -1) {
89+
return 0;
90+
} else {
91+
return Math.max(0, higher - lower + 1);
92+
}
93+
} else {
94+
return 0;
95+
}
96+
}
97+
}
98+
99+
/*
100+
* Your Router object will be instantiated and called as such:
101+
* Router obj = new Router(memoryLimit);
102+
* boolean param_1 = obj.addPacket(source,destination,timestamp);
103+
* int[] param_2 = obj.forwardPacket();
104+
* int param_3 = obj.getCount(destination,startTime,endTime);
105+
*/
+79
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)