Skip to content

Added tasks 3076-3081 #1738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 16, 2024
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,77 @@
package g3001_3100.s3076_shortest_uncommon_substring_in_an_array;

// #Medium #Array #String #Hash_Table #Trie #2024_04_16_Time_9_ms_(99.97%)_Space_45.8_MB_(39.57%)

public class Solution {
private final Trie root = new Trie();

public String[] shortestSubstrings(String[] arr) {
int n = arr.length;
for (int k = 0; k < n; ++k) {
String s = arr[k];
char[] cs = s.toCharArray();
int m = cs.length;
for (int i = 0; i < m; ++i) {
insert(cs, i, m, k);
}
}
String[] ans = new String[n];
for (int k = 0; k < n; ++k) {
String s = arr[k];
char[] cs = s.toCharArray();
int m = cs.length;
String result = "";
int resultLen = m + 1;
for (int i = 0; i < m; ++i) {
int curLen = search(cs, i, Math.min(m, i + resultLen), k);
if (curLen != -1) {
String sub = new String(cs, i, curLen);
if (curLen < resultLen || result.compareTo(sub) > 0) {
result = sub;
resultLen = curLen;
}
}
}
ans[k] = result;
}
return ans;
}

private void insert(char[] cs, int start, int end, int wordIndex) {
Trie curr = root;
for (int i = start; i < end; ++i) {
int index = cs[i] - 'a';
if (curr.children[index] == null) {
curr.children[index] = new Trie();
}
curr = curr.children[index];
if (curr.wordIndex == -1 || curr.wordIndex == wordIndex) {
curr.wordIndex = wordIndex;
} else {
curr.wordIndex = -2;
}
}
}

private int search(char[] cs, int start, int end, int wordIndex) {
Trie cur = root;
for (int i = start; i < end; i++) {
int index = cs[i] - 'a';
cur = cur.children[index];
if (cur.wordIndex == wordIndex) {
return i - start + 1;
}
}
return -1;
}

private static class Trie {
Trie[] children;
int wordIndex;

public Trie() {
children = new Trie[26];
wordIndex = -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3076\. Shortest Uncommon Substring in an Array

Medium

You are given an array `arr` of size `n` consisting of **non-empty** strings.

Find a string array `answer` of size `n` such that:

* `answer[i]` is the **shortest** substring of `arr[i]` that does **not** occur as a substring in any other string in `arr`. If multiple such substrings exist, `answer[i]` should be the lexicographically smallest. And if no such substring exists, `answer[i]` should be an empty string.

Return _the array_ `answer`.

**Example 1:**

**Input:** arr = ["cab","ad","bad","c"]

**Output:** ["ab","","ba",""]

**Explanation:** We have the following:
- For the string "cab", the shortest substring that does not occur in any other string is either "ca" or "ab", we choose the lexicographically smaller substring, which is "ab".
- For the string "ad", there is no substring that does not occur in any other string.
- For the string "bad", the shortest substring that does not occur in any other string is "ba".
- For the string "c", there is no substring that does not occur in any other string.

**Example 2:**

**Input:** arr = ["abc","bcd","abcd"]

**Output:** ["","","abcd"]

**Explanation:** We have the following:
- For the string "abc", there is no substring that does not occur in any other string.
- For the string "bcd", there is no substring that does not occur in any other string.
- For the string "abcd", the shortest substring that does not occur in any other string is "abcd".

**Constraints:**

* `n == arr.length`
* `2 <= n <= 100`
* `1 <= arr[i].length <= 20`
* `arr[i]` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package g3001_3100.s3077_maximum_strength_of_k_disjoint_subarrays;

// #Hard #Array #Dynamic_Programming #Prefix_Sum
// #2024_04_16_Time_20_ms_(97.16%)_Space_56.3_MB_(71.00%)

public class Solution {
public long maximumStrength(int[] n, int k) {
if (n.length == 1) {
return n[0];
}
long[][] dp = new long[n.length][k];
dp[0][0] = (long) k * n[0];
for (int i = 1; i < k; i++) {
long pm = -1;
dp[i][0] = Math.max(0L, dp[i - 1][0]) + (long) k * n[i];
for (int j = 1; j < i; j++) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1]) + ((long) k - j) * n[i] * pm;
pm = -pm;
}
dp[i][i] = dp[i - 1][i - 1] + ((long) k - i) * n[i] * pm;
}
long max = dp[k - 1][k - 1];
for (int i = k; i < n.length; i++) {
long pm = 1;
dp[i][0] = Math.max(0L, dp[i - 1][0]) + (long) k * n[i];
for (int j = 1; j < k; j++) {
pm = -pm;
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1]) + ((long) k - j) * n[i] * pm;
}
if (max < dp[i][k - 1]) {
max = dp[i][k - 1];
}
}
return max;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3077\. Maximum Strength of K Disjoint Subarrays

Hard

You are given a **0-indexed** array of integers `nums` of length `n`, and a **positive** **odd** integer `k`.

The strength of `x` subarrays is defined as `strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1` where `sum[i]` is the sum of the elements in the <code>i<sup>th</sup></code> subarray. Formally, strength is sum of <code>(-1)<sup>i+1</sup> * sum[i] * (x - i + 1)</code> over all `i`'s such that `1 <= i <= x`.

You need to select `k` **disjoint subarrays** from `nums`, such that their **strength** is **maximum**.

Return _the **maximum** possible **strength** that can be obtained_.

**Note** that the selected subarrays **don't** need to cover the entire array.

**Example 1:**

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

**Output:** 22

**Explanation:** The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is (1 + 2 + 3) \* 3 - (-1) \* 2 + 2 \* 1 = 22.

**Example 2:**

**Input:** nums = [12,-2,-2,-2,-2], k = 5

**Output:** 64

**Explanation:** The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is 12 \* 5 - (-2) \* 4 + (-2) \* 3 - (-2) \* 2 + (-2) \* 1 = 64.

**Example 3:**

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

**Output:** -1

**Explanation:** The best possible way to select 1 subarray is: nums[0..0]. The strength is -1.

**Constraints:**

* <code>1 <= n <= 10<sup>4</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
* `1 <= k <= n`
* <code>1 <= n * k <= 10<sup>6</sup></code>
* `k` is odd.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3001_3100.s3079_find_the_sum_of_encrypted_integers;

// #Easy #Array #Math #2024_04_16_Time_1_ms_(99.95%)_Space_42.7_MB_(75.97%)

public class Solution {
private int encrypt(int x) {
int nDigits = 0;
int max = 0;
while (x > 0) {
max = Math.max(max, x % 10);
x /= 10;
nDigits++;
}
int ans = 0;
for (int i = 0; i < nDigits; i++) {
ans = ans * 10 + max;
}
return ans;
}

public int sumOfEncryptedInt(int[] nums) {
int ret = 0;
for (int num : nums) {
ret += encrypt(num);
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
3079\. Find the Sum of Encrypted Integers

Easy

You are given an integer array `nums` containing **positive** integers. We define a function `encrypt` such that `encrypt(x)` replaces **every** digit in `x` with the **largest** digit in `x`. For example, `encrypt(523) = 555` and `encrypt(213) = 333`.

Return _the **sum** of encrypted elements_.

**Example 1:**

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

**Output:** 6

**Explanation:** The encrypted elements are `[1,2,3]`. The sum of encrypted elements is `1 + 2 + 3 == 6`.

**Example 2:**

**Input:** nums = [10,21,31]

**Output:** 66

**Explanation:** The encrypted elements are `[11,22,33]`. The sum of encrypted elements is `11 + 22 + 33 == 66`.

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package g3001_3100.s3080_mark_elements_on_array_by_performing_queries;

// #Medium #Array #Hash_Table #Sorting #Heap_Priority_Queue #Simulation
// #2024_04_16_Time_50_ms_(99.96%)_Space_77.2_MB_(15.35%)

@SuppressWarnings({"java:S1871", "java:S6541"})
public class Solution {
public long[] unmarkedSumArray(int[] nums, int[][] queries) {
int l = nums.length;
int[] orig = new int[l];
for (int i = 0; i < l; i++) {
orig[i] = i;
}
int x = 1;
while (x < l) {
int[] temp = new int[l];
int[] teor = new int[l];
int y = 0;
while (y < l) {
int s1 = 0;
int s2 = 0;
while (s1 + s2 < 2 * x && y + s1 + s2 < l) {
if (s2 >= x || y + x + s2 >= l) {
temp[y + s1 + s2] = nums[y + s1];
teor[y + s1 + s2] = orig[y + s1];
s1++;
} else if (s1 >= x) {
temp[y + s1 + s2] = nums[y + x + s2];
teor[y + s1 + s2] = orig[y + x + s2];
s2++;
} else if (nums[y + s1] <= nums[y + x + s2]) {
temp[y + s1 + s2] = nums[y + s1];
teor[y + s1 + s2] = orig[y + s1];
s1++;
} else {
temp[y + s1 + s2] = nums[y + x + s2];
teor[y + s1 + s2] = orig[y + x + s2];
s2++;
}
}
y += 2 * x;
}
for (int i = 0; i < l; i++) {
nums[i] = temp[i];
orig[i] = teor[i];
}
x *= 2;
}
int[] change = new int[l];
for (int i = 0; i < l; i++) {
change[orig[i]] = i;
}
boolean[] mark = new boolean[l];
int m = queries.length;
int st = 0;
long sum = 0;
for (int num : nums) {
sum += num;
}
long[] out = new long[m];
for (int i = 0; i < m; i++) {
int a = queries[i][0];
if (!mark[change[a]]) {
mark[change[a]] = true;
sum -= nums[change[a]];
}
int b = queries[i][1];
int many = 0;
while (many < b) {
if (st == l) {
out[i] = sum;
break;
}
if (!mark[st]) {
mark[st] = true;
sum -= nums[st];
many++;
}
st++;
}
out[i] = sum;
}
return out;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3080\. Mark Elements on Array by Performing Queries

Medium

You are given a **0-indexed** array `nums` of size `n` consisting of positive integers.

You are also given a 2D array `queries` of size `m` where <code>queries[i] = [index<sub>i</sub>, k<sub>i</sub>]</code>.

Initially all elements of the array are **unmarked**.

You need to apply `m` queries on the array in order, where on the <code>i<sup>th</sup></code> query you do the following:

* Mark the element at index <code>index<sub>i</sub></code> if it is not already marked.
* Then mark <code>k<sub>i</sub></code> unmarked elements in the array with the **smallest** values. If multiple such elements exist, mark the ones with the smallest indices. And if less than <code>k<sub>i</sub></code> unmarked elements exist, then mark all of them.

Return _an array answer of size_ `m` _where_ `answer[i]` _is the **sum** of unmarked elements in the array after the_ <code>i<sup>th</sup></code> _query_.

**Example 1:**

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

**Output:** [8,3,0]

**Explanation:**

We do the following queries on the array:

* Mark the element at index `1`, and `2` of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = [**<ins>1</ins>**,<ins>**2**</ins>,2,<ins>**1**</ins>,2,3,1]</code>. The sum of unmarked elements is `2 + 2 + 3 + 1 = 8`.
* Mark the element at index `3`, since it is already marked we skip it. Then we mark `3` of the smallest unmarked elements with the smallest indices, the marked elements now are <code>nums = [**<ins>1</ins>**,<ins>**2**</ins>,<ins>**2**</ins>,<ins>**1**</ins>,<ins>**2**</ins>,3,**<ins>1</ins>**]</code>. The sum of unmarked elements is `3`.
* Mark the element at index `4`, since it is already marked we skip it. Then we mark `2` of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = [**<ins>1</ins>**,<ins>**2**</ins>,<ins>**2**</ins>,<ins>**1**</ins>,<ins>**2**</ins>,**<ins>3</ins>**,<ins>**1**</ins>]</code>. The sum of unmarked elements is `0`.

**Example 2:**

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

**Output:** [7]

**Explanation:** We do one query which is mark the element at index `0` and mark the smallest element among unmarked elements. The marked elements will be <code>nums = [**<ins>1</ins>**,4,<ins>**2**</ins>,3]</code>, and the sum of unmarked elements is `4 + 3 = 7`.

**Constraints:**

* `n == nums.length`
* `m == queries.length`
* <code>1 <= m <= n <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* <code>0 <= index<sub>i</sub>, k<sub>i</sub> <= n - 1</code>
Loading
Loading