Skip to content

Commit fa61042

Browse files
authored
Added tasks 3407-3414
1 parent fbc3266 commit fa61042

File tree

24 files changed

+965
-0
lines changed

24 files changed

+965
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3401_3500.s3407_substring_matching_pattern
2+
3+
// #Easy #String #String_Matching #2025_01_07_Time_2_(100.00%)_Space_38.80_(35.14%)
4+
5+
class Solution {
6+
fun hasMatch(s: String, p: String): Boolean {
7+
var index = -1
8+
for (i in 0..<p.length) {
9+
if (p[i] == '*') {
10+
index = i
11+
break
12+
}
13+
}
14+
val num1 = `fun`(s, p.substring(0, index))
15+
if (num1 == -1) {
16+
return false
17+
}
18+
val num2 = `fun`(s.substring(num1), p.substring(index + 1))
19+
return num2 != -1
20+
}
21+
22+
private fun `fun`(s: String, k: String): Int {
23+
val n = s.length
24+
val m = k.length
25+
var j: Int
26+
for (i in 0..n - m) {
27+
j = 0
28+
while (j < m) {
29+
val ch1 = s[j + i]
30+
val ch2 = k[j]
31+
if (ch1 != ch2) {
32+
break
33+
}
34+
j++
35+
}
36+
if (j == m) {
37+
return i + j
38+
}
39+
}
40+
return -1
41+
}
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3407\. Substring Matching Pattern
2+
3+
Easy
4+
5+
You are given a string `s` and a pattern string `p`, where `p` contains **exactly one** `'*'` character.
6+
7+
The `'*'` in `p` can be replaced with any sequence of zero or more characters.
8+
9+
Return `true` if `p` can be made a substring of `s`, and `false` otherwise.
10+
11+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "leetcode", p = "ee\*e"
16+
17+
**Output:** true
18+
19+
**Explanation:**
20+
21+
By replacing the `'*'` with `"tcod"`, the substring `"eetcode"` matches the pattern.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "car", p = "c\*v"
26+
27+
**Output:** false
28+
29+
**Explanation:**
30+
31+
There is no substring matching the pattern.
32+
33+
**Example 3:**
34+
35+
**Input:** s = "luck", p = "u\*"
36+
37+
**Output:** true
38+
39+
**Explanation:**
40+
41+
The substrings `"u"`, `"uc"`, and `"uck"` match the pattern.
42+
43+
**Constraints:**
44+
45+
* `1 <= s.length <= 50`
46+
* `1 <= p.length <= 50`
47+
* `s` contains only lowercase English letters.
48+
* `p` contains only lowercase English letters and exactly one `'*'`
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g3401_3500.s3408_design_task_manager
2+
3+
// #Medium #Hash_Table #Design #Heap_Priority_Queue #Ordered_Set
4+
// #2025_01_07_Time_405_(88.24%)_Space_263.70_(5.88%)
5+
6+
import java.util.TreeSet
7+
8+
class TaskManager(tasks: List<List<Int>>) {
9+
private val tasks: TreeSet<IntArray?>
10+
private val taskMap: MutableMap<Int?, IntArray>
11+
12+
init {
13+
this.tasks =
14+
TreeSet<IntArray?>(
15+
Comparator { a: IntArray?, b: IntArray? ->
16+
if (b!![2] == a!![2]) b[1] - a[1] else b[2] - a[2]
17+
},
18+
)
19+
this.taskMap = HashMap<Int?, IntArray>()
20+
for (task in tasks) {
21+
val t = intArrayOf(task[0], task[1], task[2])
22+
this.tasks.add(t)
23+
this.taskMap.put(task[1], t)
24+
}
25+
}
26+
27+
fun add(userId: Int, taskId: Int, priority: Int) {
28+
val task = intArrayOf(userId, taskId, priority)
29+
this.tasks.add(task)
30+
this.taskMap.put(taskId, task)
31+
}
32+
33+
fun edit(taskId: Int, newPriority: Int) {
34+
val task: IntArray = taskMap[taskId]!!
35+
tasks.remove(task)
36+
taskMap.remove(taskId)
37+
val newTask = intArrayOf(task[0], task[1], newPriority)
38+
tasks.add(newTask)
39+
taskMap.put(taskId, newTask)
40+
}
41+
42+
fun rmv(taskId: Int) {
43+
this.tasks.remove(this.taskMap[taskId])
44+
this.taskMap.remove(taskId)
45+
}
46+
47+
fun execTop(): Int {
48+
if (this.tasks.isEmpty()) {
49+
return -1
50+
}
51+
val task = this.tasks.pollFirst()
52+
this.taskMap.remove(task!![1])
53+
return task[0]
54+
}
55+
}
56+
57+
/*
58+
* Your TaskManager object will be instantiated and called as such:
59+
* TaskManager obj = new TaskManager(tasks);
60+
* obj.add(userId,taskId,priority);
61+
* obj.edit(taskId,newPriority);
62+
* obj.rmv(taskId);
63+
* int param_4 = obj.execTop();
64+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3408\. Design Task Manager
2+
3+
Medium
4+
5+
There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks.
6+
7+
Implement the `TaskManager` class:
8+
9+
* `TaskManager(vector<vector<int>>& tasks)` initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form `[userId, taskId, priority]`, which adds a task to the specified user with the given priority.
10+
11+
* `void add(int userId, int taskId, int priority)` adds a task with the specified `taskId` and `priority` to the user with `userId`. It is **guaranteed** that `taskId` does not _exist_ in the system.
12+
13+
* `void edit(int taskId, int newPriority)` updates the priority of the existing `taskId` to `newPriority`. It is **guaranteed** that `taskId` _exists_ in the system.
14+
15+
* `void rmv(int taskId)` removes the task identified by `taskId` from the system. It is **guaranteed** that `taskId` _exists_ in the system.
16+
17+
* `int execTop()` executes the task with the **highest** priority across all users. If there are multiple tasks with the same **highest** priority, execute the one with the highest `taskId`. After executing, the `taskId` is **removed** from the system. Return the `userId` associated with the executed task. If no tasks are available, return -1.
18+
19+
20+
**Note** that a user may be assigned multiple tasks.
21+
22+
**Example 1:**
23+
24+
**Input:**
25+
["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]
26+
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]
27+
28+
**Output:**
29+
[null, null, null, 3, null, null, 5]
30+
31+
**Explanation**
32+
33+
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.
34+
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
35+
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
36+
taskManager.execTop(); // return 3. Executes task 103 for User 3.
37+
taskManager.rmv(101); // Removes task 101 from the system.
38+
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
39+
taskManager.execTop(); // return 5. Executes task 105 for User 5.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= tasks.length <= 10<sup>5</sup></code>
44+
* <code>0 <= userId <= 10<sup>5</sup></code>
45+
* <code>0 <= taskId <= 10<sup>5</sup></code>
46+
* <code>0 <= priority <= 10<sup>9</sup></code>
47+
* <code>0 <= newPriority <= 10<sup>9</sup></code>
48+
* At most <code>2 * 10<sup>5</sup></code> calls will be made in **total** to `add`, `edit`, `rmv`, and `execTop` methods.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3401_3500.s3409_longest_subsequence_with_decreasing_adjacent_difference
2+
3+
// #Medium #Array #Dynamic_Programming #2025_01_07_Time_70_(100.00%)_Space_57.87_(85.71%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun longestSubsequence(nums: IntArray): Int {
9+
var max = 0
10+
for (n in nums) {
11+
max = max(n, max)
12+
}
13+
max += 1
14+
val dp: Array<IntArray> = Array<IntArray>(max) { IntArray(max) }
15+
for (i in nums) {
16+
var v = 1
17+
for (diff in max - 1 downTo 0) {
18+
if (i + diff < max) {
19+
v = max(v, (dp[i + diff][diff] + 1))
20+
}
21+
if (i - diff >= 0) {
22+
v = max(v, (dp[i - diff][diff] + 1))
23+
}
24+
dp[i][diff] = v
25+
}
26+
}
27+
var res = 0
28+
for (i in dp) {
29+
for (j in i) {
30+
res = max(res, j)
31+
}
32+
}
33+
return res
34+
}
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3409\. Longest Subsequence With Decreasing Adjacent Difference
2+
3+
Medium
4+
5+
You are given an array of integers `nums`.
6+
7+
Your task is to find the length of the **longest subsequence** `seq` of `nums`, such that the **absolute differences** between _consecutive_ elements form a **non-increasing sequence** of integers. In other words, for a subsequence <code>seq<sub>0</sub></code>, <code>seq<sub>1</sub></code>, <code>seq<sub>2</sub></code>, ..., <code>seq<sub>m</sub></code> of `nums`, <code>|seq<sub>1</sub> - seq<sub>0</sub>| >= |seq<sub>2</sub> - seq<sub>1</sub>| >= ... >= |seq<sub>m</sub> - seq<sub>m - 1</sub>|</code>.
8+
9+
Return the length of such a subsequence.
10+
11+
A **subsequence** is an **non-empty** array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [16,6,3]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
The longest subsequence is `[16, 6, 3]` with the absolute adjacent differences `[10, 3]`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [6,5,3,4,2,1]
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
The longest subsequence is `[6, 4, 2, 1]` with the absolute adjacent differences `[2, 2, 1]`.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [10,20,10,19,10,20]
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
The longest subsequence is `[10, 20, 10, 19, 10]` with the absolute adjacent differences `[10, 10, 9, 9]`.
42+
43+
**Constraints:**
44+
45+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
46+
* `1 <= nums[i] <= 300`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3401_3500.s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element
2+
3+
// #Hard #Array #Dynamic_Programming #Segment_Tree
4+
// #2025_01_07_Time_80_(100.00%)_Space_68.87_(100.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun maxSubarraySum(nums: IntArray): Long {
11+
val prefixMap: MutableMap<Long, Long> = HashMap<Long, Long>()
12+
var result = nums[0].toLong()
13+
var prefixSum: Long = 0
14+
var minPrefix: Long = 0
15+
prefixMap.put(0L, 0L)
16+
for (num in nums) {
17+
prefixSum += num.toLong()
18+
result = max(result, (prefixSum - minPrefix))
19+
if (num < 0) {
20+
if (prefixMap.containsKey(num.toLong())) {
21+
prefixMap.put(
22+
num.toLong(),
23+
min(prefixMap[num.toLong()]!!, prefixMap[0L]!!) + num,
24+
)
25+
} else {
26+
prefixMap.put(num.toLong(), prefixMap[0L]!! + num)
27+
}
28+
minPrefix = min(minPrefix, prefixMap[num.toLong()]!!)
29+
}
30+
prefixMap.put(0L, min(prefixMap[0L]!!, prefixSum))
31+
minPrefix = min(minPrefix, prefixMap[0L]!!)
32+
}
33+
return result
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3410\. Maximize Subarray Sum After Removing All Occurrences of One Element
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
You can do the following operation on the array **at most** once:
8+
9+
* Choose **any** integer `x` such that `nums` remains **non-empty** on removing all occurrences of `x`.
10+
* Remove **all** occurrences of `x` from the array.
11+
12+
Return the **maximum** subarray sum across **all** possible resulting arrays.
13+
14+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [-3,2,-2,-1,3,-2,3]
19+
20+
**Output:** 7
21+
22+
**Explanation:**
23+
24+
We can have the following arrays after at most one operation:
25+
26+
* The original array is <code>nums = [-3, 2, -2, -1, <ins>**3, -2, 3**</ins>]</code>. The maximum subarray sum is `3 + (-2) + 3 = 4`.
27+
* Deleting all occurences of `x = -3` results in <code>nums = [2, -2, -1, **<ins>3, -2, 3</ins>**]</code>. The maximum subarray sum is `3 + (-2) + 3 = 4`.
28+
* Deleting all occurences of `x = -2` results in <code>nums = [-3, **<ins>2, -1, 3, 3</ins>**]</code>. The maximum subarray sum is `2 + (-1) + 3 + 3 = 7`.
29+
* Deleting all occurences of `x = -1` results in <code>nums = [-3, 2, -2, **<ins>3, -2, 3</ins>**]</code>. The maximum subarray sum is `3 + (-2) + 3 = 4`.
30+
* Deleting all occurences of `x = 3` results in <code>nums = [-3, <ins>**2**</ins>, -2, -1, -2]</code>. The maximum subarray sum is 2.
31+
32+
The output is `max(4, 4, 7, 4, 2) = 7`.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [1,2,3,4]
37+
38+
**Output:** 10
39+
40+
**Explanation:**
41+
42+
It is optimal to not perform any operations.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
47+
* <code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)