Skip to content

Commit 5d01592

Browse files
authored
Added tasks 3507-3510
1 parent 384135c commit 5d01592

File tree

12 files changed

+771
-0
lines changed

12 files changed

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