Skip to content

Commit bb868f4

Browse files
committed
Added tasks 3289-3292
1 parent 51bd13e commit bb868f4

File tree

12 files changed

+444
-0
lines changed

12 files changed

+444
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3201_3300.s3289_the_two_sneaky_numbers_of_digitville;
2+
3+
// #Easy #2024_09_16_Time_3_ms_(100.00%)_Space_45_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int[] getSneakyNumbers(int[] nums) {
10+
Map<Integer, Integer> countMap = new HashMap<>();
11+
// Populate the HashMap with the frequency of each number
12+
for (int num : nums) {
13+
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
14+
}
15+
// Array to store the result
16+
int[] result = new int[2];
17+
int index = 0;
18+
// Find the numbers that appear exactly twice
19+
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
20+
if (entry.getValue() == 2) {
21+
result[index++] = entry.getKey();
22+
// Break if we have found both sneaky numbers
23+
if (index == 2) {
24+
break;
25+
}
26+
}
27+
}
28+
return result;
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3289\. The Two Sneaky Numbers of Digitville
2+
3+
Easy
4+
5+
In the town of Digitville, there was a list of numbers called `nums` containing integers from `0` to `n - 1`. Each number was supposed to appear **exactly once** in the list, however, **two** mischievous numbers sneaked in an _additional time_, making the list longer than usual.
6+
7+
As the town detective, your task is to find these two sneaky numbers. Return an array of size **two** containing the two numbers (in _any order_), so peace can return to Digitville.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [0,1,1,0]
12+
13+
**Output:** [0,1]
14+
15+
**Explanation:**
16+
17+
The numbers 0 and 1 each appear twice in the array.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [0,3,2,1,3,2]
22+
23+
**Output:** [2,3]
24+
25+
**Explanation:**
26+
27+
The numbers 2 and 3 each appear twice in the array.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [7,1,5,4,3,4,6,0,9,5,8,2]
32+
33+
**Output:** [4,5]
34+
35+
**Explanation:**
36+
37+
The numbers 4 and 5 each appear twice in the array.
38+
39+
**Constraints:**
40+
41+
* `2 <= n <= 100`
42+
* `nums.length == n + 2`
43+
* `0 <= nums[i] < n`
44+
* The input is generated such that `nums` contains **exactly** two repeated elements.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3201_3300.s3290_maximum_multiplication_score;
2+
3+
// #Medium #2024_09_16_Time_8_ms_(100.00%)_Space_62.5_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long maxScore(int[] a, int[] b) {
9+
long[] dp = new long[4];
10+
Arrays.fill(dp, (long) -1e11);
11+
for (int bi : b) {
12+
for (int i = 3; i >= 0; i--) {
13+
dp[i] = Math.max(dp[i], (i > 0 ? dp[i - 1] : 0) + (long) a[i] * bi);
14+
}
15+
}
16+
return dp[3];
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
3290\. Maximum Multiplication Score
2+
3+
Medium
4+
5+
You are given an integer array `a` of size 4 and another integer array `b` of size **at least** 4.
6+
7+
You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array `b` such that <code>i<sub>0</sub> < i<sub>1</sub> < i<sub>2</sub> < i<sub>3</sub></code>. Your score will be equal to the value <code>a[0] * b[i<sub>0</sub>] + a[1] * b[i<sub>1</sub>] + a[2] * b[i<sub>2</sub>] + a[3] * b[i<sub>3</sub>]</code>.
8+
9+
Return the **maximum** score you can achieve.
10+
11+
**Example 1:**
12+
13+
**Input:** a = [3,2,5,6], b = [2,-6,4,-5,-3,2,-7]
14+
15+
**Output:** 26
16+
17+
**Explanation:**
18+
We can choose the indices 0, 1, 2, and 5. The score will be `3 * 2 + 2 * (-6) + 5 * 4 + 6 * 2 = 26`.
19+
20+
**Example 2:**
21+
22+
**Input:** a = [-1,4,5,-2], b = [-5,-1,-3,-2,-4]
23+
24+
**Output:** \-1
25+
26+
**Explanation:**
27+
We can choose the indices 0, 1, 3, and 4. The score will be `(-1) * (-5) + 4 * (-1) + 5 * (-2) + (-2) * (-4) = -1`.
28+
29+
**Constraints:**
30+
31+
* `a.length == 4`
32+
* <code>4 <= b.length <= 10<sup>5</sup></code>
33+
* <code>-10<sup>5</sup> <= a[i], b[i] <= 10<sup>5</sup></code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3201_3300.s3291_minimum_number_of_valid_strings_to_form_target_i;
2+
3+
// #Medium #2024_09_16_Time_263_ms_(60.00%)_Space_56.9_MB_(20.00%)
4+
5+
public class Solution {
6+
public int minValidStrings(String[] words, String target) {
7+
TrieNode root = new TrieNode();
8+
for (String word : words) {
9+
insert(root, word);
10+
}
11+
int n = target.length();
12+
int[] dp = new int[n];
13+
for (int i = n - 1; i >= 0; i--) {
14+
dp[i] = Integer.MAX_VALUE;
15+
TrieNode node = root;
16+
for (int j = i; j < n; j++) {
17+
int idx = target.charAt(j) - 'a';
18+
if (node.children[idx] == null) {
19+
break;
20+
}
21+
if (j == n - 1) {
22+
dp[i] = 1;
23+
} else if (dp[j + 1] >= 0) {
24+
dp[i] = Math.min(dp[i], 1 + dp[j + 1]);
25+
}
26+
node = node.children[idx];
27+
}
28+
if (dp[i] == Integer.MAX_VALUE) {
29+
dp[i] = -1;
30+
}
31+
}
32+
return dp[0];
33+
}
34+
35+
private void insert(TrieNode root, String word) {
36+
TrieNode node = root;
37+
for (char c : word.toCharArray()) {
38+
if (node.children[c - 'a'] == null) {
39+
node.children[c - 'a'] = new TrieNode();
40+
}
41+
node = node.children[c - 'a'];
42+
}
43+
}
44+
45+
private static class TrieNode {
46+
TrieNode[] children = new TrieNode[26];
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3291\. Minimum Number of Valid Strings to Form Target I
2+
3+
Medium
4+
5+
You are given an array of strings `words` and a string `target`.
6+
7+
A string `x` is called **valid** if `x` is a prefix of **any** string in `words`.
8+
9+
Return the **minimum** number of **valid** strings that can be _concatenated_ to form `target`. If it is **not** possible to form `target`, return `-1`.
10+
11+
A prefix of a string is a substring that starts from the beginning of the string and extends to any point within it.
12+
13+
**Example 1:**
14+
15+
**Input:** words = ["abc","aaaaa","bcdef"], target = "aabcdabc"
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
The target string can be formed by concatenating:
22+
23+
* Prefix of length 2 of `words[1]`, i.e. `"aa"`.
24+
* Prefix of length 3 of `words[2]`, i.e. `"bcd"`.
25+
* Prefix of length 3 of `words[0]`, i.e. `"abc"`.
26+
27+
**Example 2:**
28+
29+
**Input:** words = ["abababab","ab"], target = "ababaababa"
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
The target string can be formed by concatenating:
36+
37+
* Prefix of length 5 of `words[0]`, i.e. `"ababa"`.
38+
* Prefix of length 5 of `words[0]`, i.e. `"ababa"`.
39+
40+
**Example 3:**
41+
42+
**Input:** words = ["abcdef"], target = "xyz"
43+
44+
**Output:** \-1
45+
46+
**Constraints:**
47+
48+
* `1 <= words.length <= 100`
49+
* <code>1 <= words[i].length <= 5 * 10<sup>3</sup></code>
50+
* The input is generated such that <code>sum(words[i].length) <= 10<sup>5</sup></code>.
51+
* `words[i]` consists only of lowercase English letters.
52+
* <code>1 <= target.length <= 5 * 10<sup>3</sup></code>
53+
* `target` consists only of lowercase English letters.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g3201_3300.s3292_minimum_number_of_valid_strings_to_form_target_ii;
2+
3+
// #Hard #2024_09_16_Time_103_ms_(100.00%)_Space_94.7_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int minValidStrings(String[] words, String target) {
11+
int n = target.length();
12+
int[] dp = new int[n + 1];
13+
Arrays.fill(dp, Integer.MAX_VALUE);
14+
dp[0] = 0;
15+
List<List<Integer>> matches = new ArrayList<>(n);
16+
for (int i = 0; i < n; i++) {
17+
matches.add(new ArrayList<>());
18+
}
19+
char[] targetChars = target.toCharArray();
20+
for (String word : words) {
21+
char[] wordChars = word.toCharArray();
22+
int m = wordChars.length;
23+
int[] pi = new int[m];
24+
for (int i = 1, j = 0; i < m; i++) {
25+
while (j > 0 && wordChars[i] != wordChars[j]) {
26+
j = pi[j - 1];
27+
}
28+
if (wordChars[i] == wordChars[j]) {
29+
j++;
30+
}
31+
pi[i] = j;
32+
}
33+
for (int i = 0, j = 0; i < n; i++) {
34+
while (j > 0 && targetChars[i] != wordChars[j]) {
35+
j = pi[j - 1];
36+
}
37+
if (targetChars[i] == wordChars[j]) {
38+
j++;
39+
}
40+
if (j > 0) {
41+
matches.get(i - j + 1).add(j);
42+
if (j == m) {
43+
j = pi[j - 1];
44+
}
45+
}
46+
}
47+
}
48+
for (int i = 0; i < n; i++) {
49+
if (dp[i] == Integer.MAX_VALUE) {
50+
continue;
51+
}
52+
for (int len : matches.get(i)) {
53+
if (i + len <= n) {
54+
dp[i + len] = Math.min(dp[i + len], dp[i] + 1);
55+
}
56+
}
57+
}
58+
return dp[n] == Integer.MAX_VALUE ? -1 : dp[n];
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3292\. Minimum Number of Valid Strings to Form Target II
2+
3+
Hard
4+
5+
You are given an array of strings `words` and a string `target`.
6+
7+
A string `x` is called **valid** if `x` is a prefix of **any** string in `words`.
8+
9+
Return the **minimum** number of **valid** strings that can be _concatenated_ to form `target`. If it is **not** possible to form `target`, return `-1`.
10+
11+
A prefix of a string is a substring that starts from the beginning of the string and extends to any point within it.
12+
13+
**Example 1:**
14+
15+
**Input:** words = ["abc","aaaaa","bcdef"], target = "aabcdabc"
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
The target string can be formed by concatenating:
22+
23+
* Prefix of length 2 of `words[1]`, i.e. `"aa"`.
24+
* Prefix of length 3 of `words[2]`, i.e. `"bcd"`.
25+
* Prefix of length 3 of `words[0]`, i.e. `"abc"`.
26+
27+
**Example 2:**
28+
29+
**Input:** words = ["abababab","ab"], target = "ababaababa"
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
The target string can be formed by concatenating:
36+
37+
* Prefix of length 5 of `words[0]`, i.e. `"ababa"`.
38+
* Prefix of length 5 of `words[0]`, i.e. `"ababa"`.
39+
40+
**Example 3:**
41+
42+
**Input:** words = ["abcdef"], target = "xyz"
43+
44+
**Output:** \-1
45+
46+
**Constraints:**
47+
48+
* `1 <= words.length <= 100`
49+
* <code>1 <= words[i].length <= 5 * 10<sup>4</sup></code>
50+
* The input is generated such that <code>sum(words[i].length) <= 10<sup>5</sup></code>.
51+
* `words[i]` consists only of lowercase English letters.
52+
* <code>1 <= target.length <= 5 * 10<sup>4</sup></code>
53+
* `target` consists only of lowercase English letters.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3201_3300.s3289_the_two_sneaky_numbers_of_digitville;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void getSneakyNumbers() {
11+
assertThat(
12+
new Solution().getSneakyNumbers(new int[] {0, 1, 1, 0}), equalTo(new int[] {0, 1}));
13+
}
14+
15+
@Test
16+
void getSneakyNumbers2() {
17+
assertThat(
18+
new Solution().getSneakyNumbers(new int[] {0, 3, 2, 1, 3, 2}),
19+
equalTo(new int[] {2, 3}));
20+
}
21+
22+
@Test
23+
void getSneakyNumbers3() {
24+
assertThat(
25+
new Solution().getSneakyNumbers(new int[] {7, 1, 5, 4, 3, 4, 6, 0, 9, 5, 8, 2}),
26+
equalTo(new int[] {4, 5}));
27+
}
28+
}

0 commit comments

Comments
 (0)