Skip to content

Commit 25bbea3

Browse files
authored
Added tasks 3483-3490
1 parent 7ed6245 commit 25bbea3

File tree

24 files changed

+1004
-0
lines changed

24 files changed

+1004
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3401_3500.s3483_unique_3_digit_even_numbers;
2+
3+
// #Easy #Array #Hash_Table #Recursion #Enumeration
4+
// #2025_03_17_Time_5_ms_(100.00%)_Space_44.59_MB_(100.00%)
5+
6+
import java.util.HashSet;
7+
8+
public class Solution {
9+
public int totalNumbers(int[] digits) {
10+
HashSet<Integer> set = new HashSet<>();
11+
int n = digits.length;
12+
for (int i = 0; i < n; i++) {
13+
if (digits[i] == 0) {
14+
continue;
15+
}
16+
for (int j = 0; j < n; j++) {
17+
if (j == i) {
18+
continue;
19+
}
20+
for (int k = 0; k < n; k++) {
21+
if (k == i || k == j) {
22+
continue;
23+
}
24+
if (digits[k] % 2 == 0) {
25+
int number = digits[i] * 100 + digits[j] * 10 + digits[k];
26+
set.add(number);
27+
}
28+
}
29+
}
30+
}
31+
return set.size();
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3483\. Unique 3-Digit Even Numbers
2+
3+
Easy
4+
5+
You are given an array of digits called `digits`. Your task is to determine the number of **distinct** three-digit even numbers that can be formed using these digits.
6+
7+
**Note**: Each _copy_ of a digit can only be used **once per number**, and there may **not** be leading zeros.
8+
9+
**Example 1:**
10+
11+
**Input:** digits = [1,2,3,4]
12+
13+
**Output:** 12
14+
15+
**Explanation:** The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2.
16+
17+
**Example 2:**
18+
19+
**Input:** digits = [0,2,2]
20+
21+
**Output:** 2
22+
23+
**Explanation:** The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array.
24+
25+
**Example 3:**
26+
27+
**Input:** digits = [6,6,6]
28+
29+
**Output:** 1
30+
31+
**Explanation:** Only 666 can be formed.
32+
33+
**Example 4:**
34+
35+
**Input:** digits = [1,3,5]
36+
37+
**Output:** 0
38+
39+
**Explanation:** No even 3-digit numbers can be formed.
40+
41+
**Constraints:**
42+
43+
* `3 <= digits.length <= 10`
44+
* `0 <= digits[i] <= 9`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3401_3500.s3484_design_spreadsheet;
2+
3+
// #Medium #Array #String #Hash_Table #Matrix #Design
4+
// #2025_03_17_Time_117_ms_(100.00%)_Space_56.71_MB_(100.00%)
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
@SuppressWarnings("unused")
10+
public class Spreadsheet {
11+
private final Map<String, Integer> data = new HashMap<>();
12+
13+
public Spreadsheet(int rows) {}
14+
15+
public void setCell(String cell, int value) {
16+
data.put(cell, value);
17+
}
18+
19+
public void resetCell(String cell) {
20+
data.put(cell, 0);
21+
}
22+
23+
public int getValue(String formula) {
24+
int index = formula.indexOf('+');
25+
String left = formula.substring(1, index);
26+
String right = formula.substring(index + 1);
27+
int x =
28+
Character.isLetter(left.charAt(0))
29+
? data.getOrDefault(left, 0)
30+
: Integer.parseInt(left);
31+
int y =
32+
Character.isLetter(right.charAt(0))
33+
? data.getOrDefault(right, 0)
34+
: Integer.parseInt(right);
35+
return x + y;
36+
}
37+
}
38+
39+
/*
40+
* Your Spreadsheet object will be instantiated and called as such:
41+
* Spreadsheet obj = new Spreadsheet(rows);
42+
* obj.setCell(cell,value);
43+
* obj.resetCell(cell);
44+
* int param_3 = obj.getValue(formula);
45+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3484\. Design Spreadsheet
2+
3+
Medium
4+
5+
A spreadsheet is a grid with 26 columns (labeled from `'A'` to `'Z'`) and a given number of `rows`. Each cell in the spreadsheet can hold an integer value between 0 and 10<sup>5</sup>.
6+
7+
Implement the `Spreadsheet` class:
8+
9+
* `Spreadsheet(int rows)` Initializes a spreadsheet with 26 columns (labeled `'A'` to `'Z'`) and the specified number of rows. All cells are initially set to 0.
10+
* `void setCell(String cell, int value)` Sets the value of the specified `cell`. The cell reference is provided in the format `"AX"` (e.g., `"A1"`, `"B10"`), where the letter represents the column (from `'A'` to `'Z'`) and the number represents a **1-indexed** row.
11+
* `void resetCell(String cell)` Resets the specified cell to 0.
12+
* `int getValue(String formula)` Evaluates a formula of the form `"=X+Y"`, where `X` and `Y` are **either** cell references or non-negative integers, and returns the computed sum.
13+
14+
**Note:** If `getValue` references a cell that has not been explicitly set using `setCell`, its value is considered 0.
15+
16+
**Example 1:**
17+
18+
**Input:**
19+
["Spreadsheet", "getValue", "setCell", "getValue", "setCell", "getValue", "resetCell", "getValue"]
20+
[[3], ["=5+7"], ["A1", 10], ["=A1+6"], ["B2", 15], ["=A1+B2"], ["A1"], ["=A1+B2"]]
21+
22+
**Output:**
23+
[null, 12, null, 16, null, 25, null, 15]
24+
25+
**Explanation**
26+
27+
```java
28+
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns
29+
spreadsheet.getValue("=5+7"); // returns 12 (5+7)
30+
spreadsheet.setCell("A1", 10); // sets A1 to 10
31+
spreadsheet.getValue("=A1+6"); // returns 16 (10+6)
32+
spreadsheet.setCell("B2", 15); // sets B2 to 15
33+
spreadsheet.getValue("=A1+B2"); // returns 25 (10+15)
34+
spreadsheet.resetCell("A1"); // resets A1 to 0
35+
spreadsheet.getValue("=A1+B2"); // returns 15 (0+15)
36+
```
37+
38+
**Constraints:**
39+
40+
* <code>1 <= rows <= 10<sup>3</sup></code>
41+
* <code>0 <= value <= 10<sup>5</sup></code>
42+
* The formula is always in the format `"=X+Y"`, where `X` and `Y` are either valid cell references or **non-negative** integers with values less than or equal to <code>10<sup>5</sup></code>.
43+
* Each cell reference consists of a capital letter from `'A'` to `'Z'` followed by a row number between `1` and `rows`.
44+
* At most <code>10<sup>4</sup></code> calls will be made in **total** to `setCell`, `resetCell`, and `getValue`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g3401_3500.s3485_longest_common_prefix_of_k_strings_after_removal;
2+
3+
// #Hard #Array #String #Trie #2025_03_17_Time_333_ms_(100.00%)_Space_64.12_MB_(100.00%)
4+
5+
public class Solution {
6+
private static class TrieNode {
7+
TrieNode[] children = new TrieNode[26];
8+
int count = 0;
9+
int bestPrefixLength = -1;
10+
}
11+
12+
private TrieNode root;
13+
14+
public int[] longestCommonPrefix(String[] words, int k) {
15+
int totalWords = words.length;
16+
int[] result = new int[totalWords];
17+
if (totalWords - 1 < k) {
18+
return result;
19+
}
20+
root = new TrieNode();
21+
for (String word : words) {
22+
// insert the word with increasing the count by 1 (prefix count)
23+
updateTrie(word, 1, k);
24+
}
25+
for (int i = 0; i < totalWords; i++) {
26+
// temp deletion of the word with count decreased by 1
27+
updateTrie(words[i], -1, k);
28+
result[i] = root.bestPrefixLength;
29+
// re-insertion of the word
30+
updateTrie(words[i], 1, k);
31+
}
32+
return result;
33+
}
34+
35+
private void updateTrie(String word, int count, int k) {
36+
int wordLength = word.length();
37+
// used to store the path used during trie travesal to update the count and use the count
38+
TrieNode[] nodePath = new TrieNode[wordLength + 1];
39+
int[] depths = new int[wordLength + 1];
40+
nodePath[0] = root;
41+
depths[0] = 0;
42+
// trie insertion
43+
for (int i = 0; i < wordLength; i++) {
44+
int letterIndex = word.charAt(i) - 'a';
45+
if (nodePath[i].children[letterIndex] == null) {
46+
nodePath[i].children[letterIndex] = new TrieNode();
47+
}
48+
nodePath[i + 1] = nodePath[i].children[letterIndex];
49+
depths[i + 1] = depths[i] + 1;
50+
}
51+
// increase the count of each prefix
52+
for (TrieNode node : nodePath) {
53+
node.count += count;
54+
}
55+
// find the best prefix
56+
for (int i = nodePath.length - 1; i >= 0; i--) {
57+
TrieNode currentNode = nodePath[i];
58+
int candidate = (currentNode.count >= k ? depths[i] : -1);
59+
for (TrieNode childNode : currentNode.children) {
60+
if (childNode != null) {
61+
candidate = Math.max(candidate, childNode.bestPrefixLength);
62+
}
63+
}
64+
currentNode.bestPrefixLength = candidate;
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3485\. Longest Common Prefix of K Strings After Removal
2+
3+
Hard
4+
5+
You are given an array of strings `words` and an integer `k`.
6+
7+
For each index `i` in the range `[0, words.length - 1]`, find the **length** of the **longest common prefix** among any `k` strings (selected at **distinct indices**) from the remaining array after removing the <code>i<sup>th</sup></code> element.
8+
9+
Return an array `answer`, where `answer[i]` is the answer for <code>i<sup>th</sup></code> element. If removing the <code>i<sup>th</sup></code> element leaves the array with fewer than `k` strings, `answer[i]` is 0.
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["jump","run","run","jump","run"], k = 2
14+
15+
**Output:** [3,4,4,3,4]
16+
17+
**Explanation:**
18+
19+
* Removing index 0 (`"jump"`):
20+
* `words` becomes: `["run", "run", "jump", "run"]`. `"run"` occurs 3 times. Choosing any two gives the longest common prefix `"run"` (length 3).
21+
* Removing index 1 (`"run"`):
22+
* `words` becomes: `["jump", "run", "jump", "run"]`. `"jump"` occurs twice. Choosing these two gives the longest common prefix `"jump"` (length 4).
23+
* Removing index 2 (`"run"`):
24+
* `words` becomes: `["jump", "run", "jump", "run"]`. `"jump"` occurs twice. Choosing these two gives the longest common prefix `"jump"` (length 4).
25+
* Removing index 3 (`"jump"`):
26+
* `words` becomes: `["jump", "run", "run", "run"]`. `"run"` occurs 3 times. Choosing any two gives the longest common prefix `"run"` (length 3).
27+
* Removing index 4 ("run"):
28+
* `words` becomes: `["jump", "run", "run", "jump"]`. `"jump"` occurs twice. Choosing these two gives the longest common prefix `"jump"` (length 4).
29+
30+
**Example 2:**
31+
32+
**Input:** words = ["dog","racer","car"], k = 2
33+
34+
**Output:** [0,0,0]
35+
36+
**Explanation:**
37+
38+
* Removing any index results in an answer of 0.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= k <= words.length <= 10<sup>5</sup></code>
43+
* <code>1 <= words[i].length <= 10<sup>4</sup></code>
44+
* `words[i]` consists of lowercase English letters.
45+
* The sum of `words[i].length` is smaller than or equal <code>10<sup>5</sup></code>.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g3401_3500.s3486_longest_special_path_ii;
2+
3+
// #Hard #Array #Hash_Table #Tree #Prefix_Sum #Depth_First_Search
4+
// #2025_03_17_Time_166_ms_(100.00%)_Space_105.50_MB_(100.00%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Comparator;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
@SuppressWarnings("java:S107")
14+
public class Solution {
15+
public int[] longestSpecialPath(int[][] edges, int[] nums) {
16+
int[] ans = {0, 1};
17+
Map<Integer, List<int[]>> graph = new HashMap<>();
18+
for (int[] edge : edges) {
19+
int a = edge[0];
20+
int b = edge[1];
21+
int c = edge[2];
22+
graph.computeIfAbsent(a, k -> new ArrayList<>()).add(new int[] {b, c});
23+
graph.computeIfAbsent(b, k -> new ArrayList<>()).add(new int[] {a, c});
24+
}
25+
List<Integer> costs = new ArrayList<>();
26+
Map<Integer, Integer> last = new HashMap<>();
27+
dfs(0, 0, -1, new ArrayList<>(Arrays.asList(0, 0)), nums, graph, costs, last, ans);
28+
return ans;
29+
}
30+
31+
private void dfs(
32+
int node,
33+
int currCost,
34+
int prev,
35+
List<Integer> left,
36+
int[] nums,
37+
Map<Integer, List<int[]>> graph,
38+
List<Integer> costs,
39+
Map<Integer, Integer> last,
40+
int[] ans) {
41+
int nodeColorIndexPrev = last.getOrDefault(nums[node], -1);
42+
last.put(nums[node], costs.size());
43+
costs.add(currCost);
44+
int diff = currCost - costs.get(left.get(0));
45+
int length = costs.size() - left.get(0);
46+
if (diff > ans[0] || (diff == ans[0] && length < ans[1])) {
47+
ans[0] = diff;
48+
ans[1] = length;
49+
}
50+
for (int[] next : graph.getOrDefault(node, new ArrayList<>())) {
51+
int nextNode = next[0];
52+
int nextCost = next[1];
53+
if (nextNode == prev) {
54+
continue;
55+
}
56+
List<Integer> nextLeft = new ArrayList<>(left);
57+
if (last.containsKey(nums[nextNode])) {
58+
nextLeft.add(last.get(nums[nextNode]) + 1);
59+
}
60+
nextLeft.sort(Comparator.naturalOrder());
61+
while (nextLeft.size() > 2) {
62+
nextLeft.remove(0);
63+
}
64+
dfs(nextNode, currCost + nextCost, node, nextLeft, nums, graph, costs, last, ans);
65+
}
66+
last.put(nums[node], nodeColorIndexPrev);
67+
costs.remove(costs.size() - 1);
68+
}
69+
}

0 commit comments

Comments
 (0)