Skip to content

Commit 3a86f28

Browse files
committed
Added tasks 3582-3585
1 parent 975b81e commit 3a86f28

File tree

12 files changed

+549
-0
lines changed

12 files changed

+549
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3501_3600.s3582_generate_tag_for_video_caption;
2+
3+
// #Easy #2025_06_16_Time_12_ms_(100.00%)_Space_45.47_MB_(100.00%)
4+
5+
public class Solution {
6+
public String generateTag(String caption) {
7+
if (caption.trim().isEmpty()) {
8+
return "#";
9+
}
10+
String[] arr = caption.trim().split("\\s+");
11+
StringBuilder res = new StringBuilder("#");
12+
String firstWord = arr[0];
13+
firstWord =
14+
firstWord.substring(0, 1).toLowerCase()
15+
+ (firstWord.length() > 1 ? firstWord.substring(1).toLowerCase() : "");
16+
res.append(firstWord);
17+
for (int i = 1; i < arr.length; i++) {
18+
String w = arr[i];
19+
if (w.isEmpty()) {
20+
continue;
21+
}
22+
w =
23+
w.substring(0, 1).toUpperCase()
24+
+ (w.length() > 1 ? w.substring(1).toLowerCase() : "");
25+
res.append(w);
26+
if (res.length() >= 100) {
27+
break;
28+
}
29+
}
30+
return res.length() > 100 ? res.substring(0, 100) : res.toString();
31+
}
32+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3582\. Generate Tag for Video Caption
2+
3+
Easy
4+
5+
You are given a string `caption` representing the caption for a video.
6+
7+
The following actions must be performed **in order** to generate a **valid tag** for the video:
8+
9+
1. **Combine all words** in the string into a single _camelCase string_ prefixed with `'#'`. A _camelCase string_ is one where the first letter of all words _except_ the first one is capitalized. All characters after the first character in **each** word must be lowercase.
10+
11+
2. **Remove** all characters that are not an English letter, **except** the first `'#'`.
12+
13+
3. **Truncate** the result to a maximum of 100 characters.
14+
15+
16+
Return the **tag** after performing the actions on `caption`.
17+
18+
**Example 1:**
19+
20+
**Input:** caption = "Leetcode daily streak achieved"
21+
22+
**Output:** "#leetcodeDailyStreakAchieved"
23+
24+
**Explanation:**
25+
26+
The first letter for all words except `"leetcode"` should be capitalized.
27+
28+
**Example 2:**
29+
30+
**Input:** caption = "can I Go There"
31+
32+
**Output:** "#canIGoThere"
33+
34+
**Explanation:**
35+
36+
The first letter for all words except `"can"` should be capitalized.
37+
38+
**Example 3:**
39+
40+
**Input:** caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
41+
42+
**Output:** "#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
43+
44+
**Explanation:**
45+
46+
Since the first word has length 101, we need to truncate the last two letters from the word.
47+
48+
**Constraints:**
49+
50+
* `1 <= caption.length <= 150`
51+
* `caption` consists only of English letters and `' '`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3501_3600.s3583_count_special_triplets;
2+
3+
// #Medium #2025_06_16_Time_250_ms_(100.00%)_Space_62.22_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int specialTriplets(int[] nums) {
10+
int mod = 1_000_000_007;
11+
int res = 0;
12+
Map<Integer, Integer> left = new HashMap<>();
13+
Map<Integer, Integer> right = new HashMap<>();
14+
for (int num : nums) {
15+
right.put(num, right.getOrDefault(num, 0) + 1);
16+
}
17+
for (int num : nums) {
18+
right.put(num, right.get(num) - 1);
19+
int ci = left.getOrDefault(num * 2, 0);
20+
int ck = right.getOrDefault(num * 2, 0);
21+
res = (res + ci * ck) % mod;
22+
left.put(num, left.getOrDefault(num, 0) + 1);
23+
}
24+
return res;
25+
}
26+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
3583\. Count Special Triplets
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
A **special triplet** is defined as a triplet of indices `(i, j, k)` such that:
8+
9+
* `0 <= i < j < k < n`, where `n = nums.length`
10+
* `nums[i] == nums[j] * 2`
11+
* `nums[k] == nums[j] * 2`
12+
13+
Return the total number of **special triplets** in the array.
14+
15+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [6,3,6]
20+
21+
**Output:** 1
22+
23+
**Explanation:**
24+
25+
The only special triplet is `(i, j, k) = (0, 1, 2)`, where:
26+
27+
* `nums[0] = 6`, `nums[1] = 3`, `nums[2] = 6`
28+
* `nums[0] = nums[1] * 2 = 3 * 2 = 6`
29+
* `nums[2] = nums[1] * 2 = 3 * 2 = 6`
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [0,1,0,0]
34+
35+
**Output:** 1
36+
37+
**Explanation:**
38+
39+
The only special triplet is `(i, j, k) = (0, 2, 3)`, where:
40+
41+
* `nums[0] = 0`, `nums[2] = 0`, `nums[3] = 0`
42+
* `nums[0] = nums[2] * 2 = 0 * 2 = 0`
43+
* `nums[3] = nums[2] * 2 = 0 * 2 = 0`
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [8,4,2,8,4]
48+
49+
**Output:** 2
50+
51+
**Explanation:**
52+
53+
There are exactly two special triplets:
54+
55+
* `(i, j, k) = (0, 1, 3)`
56+
* `nums[0] = 8`, `nums[1] = 4`, `nums[3] = 8`
57+
* `nums[0] = nums[1] * 2 = 4 * 2 = 8`
58+
* `nums[3] = nums[1] * 2 = 4 * 2 = 8`
59+
* `(i, j, k) = (1, 2, 4)`
60+
* `nums[1] = 4`, `nums[2] = 2`, `nums[4] = 4`
61+
* `nums[1] = nums[2] * 2 = 2 * 2 = 4`
62+
* `nums[4] = nums[2] * 2 = 2 * 2 = 4`
63+
64+
**Constraints:**
65+
66+
* <code>3 <= n == nums.length <= 10<sup>5</sup></code>
67+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3501_3600.s3584_maximum_product_of_first_and_last_elements_of_a_subsequence;
2+
3+
// #Medium #2025_06_16_Time_4_ms_(87.17%)_Space_61.31_MB_(100.00%)
4+
5+
public class Solution {
6+
public long maximumProduct(int[] nums, int m) {
7+
long ma = nums[0];
8+
long mi = nums[0];
9+
long res = (long) nums[0] * nums[m - 1];
10+
for (int i = m - 1; i < nums.length; ++i) {
11+
ma = Math.max(ma, nums[i - m + 1]);
12+
mi = Math.min(mi, nums[i - m + 1]);
13+
res = Math.max(res, Math.max(mi * nums[i], ma * nums[i]));
14+
}
15+
return res;
16+
}
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3584\. Maximum Product of First and Last Elements of a Subsequence
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `m`.
6+
7+
Return the **maximum** product of the first and last elements of any ****subsequences**** of `nums` of size `m`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-1,-9,2,3,-2,-3,1], m = 1
12+
13+
**Output:** 81
14+
15+
**Explanation:**
16+
17+
The subsequence `[-9]` has the largest product of the first and last elements: `-9 * -9 = 81`. Therefore, the answer is 81.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,3,-5,5,6,-4], m = 3
22+
23+
**Output:** 20
24+
25+
**Explanation:**
26+
27+
The subsequence `[-5, 6, -4]` has the largest product of the first and last elements.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [2,-1,2,-6,5,2,-5,7], m = 2
32+
33+
**Output:** 35
34+
35+
**Explanation:**
36+
37+
The subsequence `[5, 7]` has the largest product of the first and last elements.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
43+
* `1 <= m <= nums.length`
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package g3501_3600.s3585_find_weighted_median_node_in_tree;
2+
3+
// #Hard #2025_06_16_Time_162_ms_(100.00%)_Space_141.58_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+
private int log;
11+
private long[] dist;
12+
private int[] depth;
13+
private int[][] up;
14+
15+
public int[] findMedian(int n, int[][] edges, int[][] queries) {
16+
List<List<int[]>> adj = new ArrayList<>();
17+
for (int i = 0; i < n; i++) {
18+
adj.add(new ArrayList<>());
19+
}
20+
for (int[] edge : edges) {
21+
adj.get(edge[0]).add(new int[] {edge[1], edge[2]});
22+
adj.get(edge[1]).add(new int[] {edge[0], edge[2]});
23+
}
24+
dist = new long[n];
25+
depth = new int[n];
26+
log = 0;
27+
while (1 << log < n) {
28+
log++;
29+
}
30+
up = new int[n][log];
31+
for (int[] u : up) {
32+
Arrays.fill(u, -1);
33+
}
34+
dfs(0, -1, adj, 0, 0);
35+
int[] ans = new int[queries.length];
36+
for (int i = 0; i < queries.length; i++) {
37+
int[] query = queries[i];
38+
int first = query[0];
39+
int second = query[1];
40+
long distance = getDistance(first, second);
41+
long needed = (distance + 1) / 2;
42+
int mid = lca(first, second);
43+
if (getDistance(first, mid) < needed) {
44+
needed -= getDistance(first, mid);
45+
first = mid;
46+
} else {
47+
second = mid;
48+
}
49+
if (depth[first] <= depth[second]) {
50+
long curDistance = getDistance(first, second);
51+
for (int j = log - 1; j >= 0; j--) {
52+
if (up[second][j] == -1
53+
|| curDistance - getDistance(up[second][j], second) < needed) {
54+
continue;
55+
}
56+
curDistance -= getDistance(up[second][j], second);
57+
second = up[second][j];
58+
}
59+
ans[i] = second;
60+
} else {
61+
long curDistance = 0;
62+
for (int j = log - 1; j >= 0; j--) {
63+
if (up[first][j] == -1
64+
|| curDistance + getDistance(up[first][j], first) >= needed) {
65+
continue;
66+
}
67+
curDistance += getDistance(up[first][j], first);
68+
first = up[first][j];
69+
}
70+
ans[i] = up[first][0];
71+
}
72+
}
73+
return ans;
74+
}
75+
76+
private long getDistance(int from, int to) {
77+
if (from == to) {
78+
return 0;
79+
}
80+
int ancesor = lca(from, to);
81+
return dist[from] + dist[to] - 2 * dist[ancesor];
82+
}
83+
84+
private int lca(int first, int second) {
85+
if (depth[first] < depth[second]) {
86+
return lca(second, first);
87+
}
88+
for (int i = log - 1; i >= 0; i--) {
89+
if (depth[first] - (1 << i) >= depth[second]) {
90+
first = up[first][i];
91+
}
92+
}
93+
if (first == second) {
94+
return second;
95+
}
96+
for (int i = log - 1; i >= 0; i--) {
97+
if (depth[first] != -1 && up[first][i] != up[second][i]) {
98+
first = up[first][i];
99+
second = up[second][i];
100+
}
101+
}
102+
first = up[first][0];
103+
return first;
104+
}
105+
106+
private void dfs(int current, int parent, List<List<int[]>> adj, long curDist, int curDepth) {
107+
up[current][0] = parent;
108+
for (int i = 1; i < log; i++) {
109+
if (up[current][i - 1] != -1) {
110+
up[current][i] = up[up[current][i - 1]][i - 1];
111+
}
112+
}
113+
dist[current] = curDist;
114+
depth[current] = curDepth;
115+
for (int[] next : adj.get(current)) {
116+
if (next[0] == parent) {
117+
continue;
118+
}
119+
dfs(next[0], current, adj, curDist + next[1], curDepth + 1);
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)