Skip to content

Commit

Permalink
Added tasks 3407-3414
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Jan 7, 2025
1 parent 7fe8bd1 commit c2fddc5
Show file tree
Hide file tree
Showing 24 changed files with 893 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g3401_3500.s3407_substring_matching_pattern;

// #Easy #String #String_Matching #2025_01_06_Time_1_(100.00%)_Space_42.63_(100.00%)

public class Solution {
public boolean hasMatch(String s, String p) {
int index = -1;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
index = i;
break;
}
}
int num1 = fun(s, p.substring(0, index));
if (num1 == -1) {
return false;
}
int num2 = fun(s.substring(num1), p.substring(index + 1));
return num2 != -1;
}

private int fun(String s, String k) {
int n = s.length();
int m = k.length();
int j = 0;
for (int i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
char ch1 = s.charAt(j + i);
char ch2 = k.charAt(j);
if (ch1 != ch2) break;
}
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,63 @@
package g3401_3500.s3408_design_task_manager;

// #Medium #Hash_Table #Design #Ordered_Set #Heap_Priority_Queue
// #2025_01_06_Time_349_(100.00%)_Space_152.40_(100.00%)

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

public class TaskManager {

private TreeSet<int[]> tasks;
private Map<Integer, int[]> taskMap;

public TaskManager(List<List<Integer>> tasks) {
this.tasks = new TreeSet<>((a, b) -> b[2] == a[2] ? b[1] - a[1] : b[2] - a[2]);
this.taskMap = new HashMap<>();
for (List<Integer> task : tasks) {
int[] t = new int[] {task.get(0), task.get(1), task.get(2)};
this.tasks.add(t);
this.taskMap.put(task.get(1), t);
}
}

public void add(int userId, int taskId, int priority) {
int[] task = new int[] {userId, taskId, priority};
this.tasks.add(task);
this.taskMap.put(taskId, task);
}

public void edit(int taskId, int newPriority) {
int[] task = taskMap.get(taskId);
tasks.remove(task);
taskMap.remove(taskId);
int[] newTask = new int[] {task[0], task[1], newPriority};
tasks.add(newTask);
taskMap.put(taskId, newTask);
}

public void rmv(int taskId) {
this.tasks.remove(this.taskMap.get(taskId));
this.taskMap.remove(taskId);
}

public int execTop() {
if (this.tasks.isEmpty()) {
return -1;
}
int[] 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/java/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,33 @@
package g3401_3500.s3409_longest_subsequence_with_decreasing_adjacent_difference;

// #Medium #Array #Dynamic_Programming #2025_01_07_Time_68_(99.55%)_Space_45.74_(78.73%)

public class Solution {
public int longestSubsequence(int[] nums) {
int max = 0;
for (int n : nums) {
max = Math.max(n, max);
}
max += 1;
int[][] dp = new int[max][max];
for (int i : nums) {
int v = 1;
for (int diff = max - 1; diff >= 0; diff--) {
if (i + diff < max) {
v = Math.max(v, dp[i + diff][diff] + 1);
}
if (i - diff >= 0) {
v = Math.max(v, dp[i - diff][diff] + 1);
}
dp[i][diff] = v;
}
}
int res = 0;
for (int[] i : dp) {
for (int j : i) {
res = Math.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,34 @@
package g3401_3500.s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element;

// #Hard #Array #Dynamic_Programming #Segment_Tree #2025_01_07_Time_51_(97.96%)_Space_57.22_(85.71%)

import java.util.HashMap;
import java.util.Map;

public class Solution {
public long maxSubarraySum(int[] nums) {
Map<Long, Long> prefixMap = new HashMap<>();
long result = nums[0];
long prefixSum = 0;
long minPrefix = 0;
prefixMap.put(0L, 0L);
for (int num : nums) {
prefixSum += num;
result = Math.max(result, prefixSum - minPrefix);
if (num < 0) {
if (prefixMap.containsKey((long) num)) {
prefixMap.put(
(long) num,
Math.min(prefixMap.get((long) num), prefixMap.get(0L)) + num);
} else {
prefixMap.put((long) num, prefixMap.get(0L) + num);
}
minPrefix = Math.min(minPrefix, prefixMap.get((long) num));
}
prefixMap.put(0L, Math.min(prefixMap.get(0L), prefixSum));
minPrefix = Math.min(minPrefix, prefixMap.get(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

0 comments on commit c2fddc5

Please sign in to comment.