Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3401_3500.s3407_substring_matching_pattern

// #Easy #String #String_Matching #2025_01_07_Time_2_(100.00%)_Space_38.80_(35.14%)

class Solution {
fun hasMatch(s: String, p: String): Boolean {
var index = -1
for (i in 0..<p.length) {
if (p[i] == '*') {
index = i
break
}
}
val num1 = `fun`(s, p.substring(0, index))
if (num1 == -1) {
return false
}
val num2 = `fun`(s.substring(num1), p.substring(index + 1))
return num2 != -1
}

private fun `fun`(s: String, k: String): Int {
val n = s.length
val m = k.length
var j: Int
for (i in 0..n - m) {
j = 0
while (j < m) {
val ch1 = s[j + i]
val ch2 = k[j]
if (ch1 != ch2) {
break
}
j++
}
if (j == m) {
return i + j
}
}
return -1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3407\. Substring Matching Pattern

Easy

You are given a string `s` and a pattern string `p`, where `p` contains **exactly one** `'*'` character.

The `'*'` in `p` can be replaced with any sequence of zero or more characters.

Return `true` if `p` can be made a substring of `s`, and `false` otherwise.

A **substring** is a contiguous **non-empty** sequence of characters within a string.

**Example 1:**

**Input:** s = "leetcode", p = "ee\*e"

**Output:** true

**Explanation:**

By replacing the `'*'` with `"tcod"`, the substring `"eetcode"` matches the pattern.

**Example 2:**

**Input:** s = "car", p = "c\*v"

**Output:** false

**Explanation:**

There is no substring matching the pattern.

**Example 3:**

**Input:** s = "luck", p = "u\*"

**Output:** true

**Explanation:**

The substrings `"u"`, `"uc"`, and `"uck"` match the pattern.

**Constraints:**

* `1 <= s.length <= 50`
* `1 <= p.length <= 50`
* `s` contains only lowercase English letters.
* `p` contains only lowercase English letters and exactly one `'*'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package g3401_3500.s3408_design_task_manager

// #Medium #Hash_Table #Design #Heap_Priority_Queue #Ordered_Set
// #2025_01_07_Time_405_(88.24%)_Space_263.70_(5.88%)

import java.util.TreeSet

class TaskManager(tasks: List<List<Int>>) {
private val tasks: TreeSet<IntArray?>
private val taskMap: MutableMap<Int?, IntArray>

init {
this.tasks =
TreeSet<IntArray?>(
Comparator { a: IntArray?, b: IntArray? ->
if (b!![2] == a!![2]) b[1] - a[1] else b[2] - a[2]
},
)
this.taskMap = HashMap<Int?, IntArray>()
for (task in tasks) {
val t = intArrayOf(task[0], task[1], task[2])
this.tasks.add(t)
this.taskMap.put(task[1], t)
}
}

fun add(userId: Int, taskId: Int, priority: Int) {
val task = intArrayOf(userId, taskId, priority)
this.tasks.add(task)
this.taskMap.put(taskId, task)
}

fun edit(taskId: Int, newPriority: Int) {
val task: IntArray = taskMap[taskId]!!
tasks.remove(task)
taskMap.remove(taskId)
val newTask = intArrayOf(task[0], task[1], newPriority)
tasks.add(newTask)
taskMap.put(taskId, newTask)
}

fun rmv(taskId: Int) {
this.tasks.remove(this.taskMap[taskId])
this.taskMap.remove(taskId)
}

fun execTop(): Int {
if (this.tasks.isEmpty()) {
return -1
}
val task = this.tasks.pollFirst()
this.taskMap.remove(task!![1])
return task[0]
}
}

/*
* Your TaskManager object will be instantiated and called as such:
* TaskManager obj = new TaskManager(tasks);
* obj.add(userId,taskId,priority);
* obj.edit(taskId,newPriority);
* obj.rmv(taskId);
* int param_4 = obj.execTop();
*/
48 changes: 48 additions & 0 deletions src/main/kotlin/g3401_3500/s3408_design_task_manager/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3408\. Design Task Manager

Medium

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.

Implement the `TaskManager` class:

* `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.

* `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.

* `void edit(int taskId, int newPriority)` updates the priority of the existing `taskId` to `newPriority`. It is **guaranteed** that `taskId` _exists_ in the system.

* `void rmv(int taskId)` removes the task identified by `taskId` from the system. It is **guaranteed** that `taskId` _exists_ in the system.

* `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.


**Note** that a user may be assigned multiple tasks.

**Example 1:**

**Input:**
["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]

**Output:**
[null, null, null, 3, null, null, 5]

**Explanation**

TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
taskManager.execTop(); // return 3. Executes task 103 for User 3.
taskManager.rmv(101); // Removes task 101 from the system.
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
taskManager.execTop(); // return 5. Executes task 105 for User 5.

**Constraints:**

* <code>1 <= tasks.length <= 10<sup>5</sup></code>
* <code>0 <= userId <= 10<sup>5</sup></code>
* <code>0 <= taskId <= 10<sup>5</sup></code>
* <code>0 <= priority <= 10<sup>9</sup></code>
* <code>0 <= newPriority <= 10<sup>9</sup></code>
* At most <code>2 * 10<sup>5</sup></code> calls will be made in **total** to `add`, `edit`, `rmv`, and `execTop` methods.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g3401_3500.s3409_longest_subsequence_with_decreasing_adjacent_difference

// #Medium #Array #Dynamic_Programming #2025_01_07_Time_70_(100.00%)_Space_57.87_(85.71%)

import kotlin.math.max

class Solution {
fun longestSubsequence(nums: IntArray): Int {
var max = 0
for (n in nums) {
max = max(n, max)
}
max += 1
val dp: Array<IntArray> = Array<IntArray>(max) { IntArray(max) }
for (i in nums) {
var v = 1
for (diff in max - 1 downTo 0) {
if (i + diff < max) {
v = max(v, (dp[i + diff][diff] + 1))
}
if (i - diff >= 0) {
v = max(v, (dp[i - diff][diff] + 1))
}
dp[i][diff] = v
}
}
var res = 0
for (i in dp) {
for (j in i) {
res = max(res, j)
}
}
return res
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3409\. Longest Subsequence With Decreasing Adjacent Difference

Medium

You are given an array of integers `nums`.

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>.

Return the length of such a subsequence.

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.

**Example 1:**

**Input:** nums = [16,6,3]

**Output:** 3

**Explanation:**

The longest subsequence is `[16, 6, 3]` with the absolute adjacent differences `[10, 3]`.

**Example 2:**

**Input:** nums = [6,5,3,4,2,1]

**Output:** 4

**Explanation:**

The longest subsequence is `[6, 4, 2, 1]` with the absolute adjacent differences `[2, 2, 1]`.

**Example 3:**

**Input:** nums = [10,20,10,19,10,20]

**Output:** 5

**Explanation:**

The longest subsequence is `[10, 20, 10, 19, 10]` with the absolute adjacent differences `[10, 10, 9, 9]`.

**Constraints:**

* <code>2 <= nums.length <= 10<sup>4</sup></code>
* `1 <= nums[i] <= 300`
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g3401_3500.s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element

// #Hard #Array #Dynamic_Programming #Segment_Tree
// #2025_01_07_Time_80_(100.00%)_Space_68.87_(100.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun maxSubarraySum(nums: IntArray): Long {
val prefixMap: MutableMap<Long, Long> = HashMap<Long, Long>()
var result = nums[0].toLong()
var prefixSum: Long = 0
var minPrefix: Long = 0
prefixMap.put(0L, 0L)
for (num in nums) {
prefixSum += num.toLong()
result = max(result, (prefixSum - minPrefix))
if (num < 0) {
if (prefixMap.containsKey(num.toLong())) {
prefixMap.put(
num.toLong(),
min(prefixMap[num.toLong()]!!, prefixMap[0L]!!) + num,
)
} else {
prefixMap.put(num.toLong(), prefixMap[0L]!! + num)
}
minPrefix = min(minPrefix, prefixMap[num.toLong()]!!)
}
prefixMap.put(0L, min(prefixMap[0L]!!, prefixSum))
minPrefix = min(minPrefix, prefixMap[0L]!!)
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3410\. Maximize Subarray Sum After Removing All Occurrences of One Element

Hard

You are given an integer array `nums`.

You can do the following operation on the array **at most** once:

* Choose **any** integer `x` such that `nums` remains **non-empty** on removing all occurrences of `x`.
* Remove **all** occurrences of `x` from the array.

Return the **maximum** subarray sum across **all** possible resulting arrays.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [-3,2,-2,-1,3,-2,3]

**Output:** 7

**Explanation:**

We can have the following arrays after at most one operation:

* 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`.
* 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`.
* 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`.
* 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`.
* Deleting all occurences of `x = 3` results in <code>nums = [-3, <ins>**2**</ins>, -2, -1, -2]</code>. The maximum subarray sum is 2.

The output is `max(4, 4, 7, 4, 2) = 7`.

**Example 2:**

**Input:** nums = [1,2,3,4]

**Output:** 10

**Explanation:**

It is optimal to not perform any operations.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code>
Loading
Loading