Skip to content

Commit 67007ce

Browse files
committed
Added tasks 3582-3585
1 parent 4dab810 commit 67007ce

File tree

12 files changed

+587
-0
lines changed

12 files changed

+587
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import java.util.Locale
6+
7+
class Solution {
8+
fun generateTag(caption: String): String? {
9+
if (caption.trim { it <= ' ' }.isEmpty()) {
10+
return "#"
11+
}
12+
val arr = caption.trim { it <= ' ' }.split("\\s+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
13+
val res = StringBuilder("#")
14+
var firstWord = arr[0]
15+
firstWord =
16+
(
17+
firstWord.substring(0, 1).lowercase(Locale.getDefault()) +
18+
(if (firstWord.length > 1) firstWord.substring(1).lowercase(Locale.getDefault()) else "")
19+
)
20+
res.append(firstWord)
21+
for (i in 1..<arr.size) {
22+
var w = arr[i]
23+
if (w.isEmpty()) {
24+
continue
25+
}
26+
w =
27+
(
28+
w.substring(0, 1).uppercase(Locale.getDefault()) +
29+
(if (w.length > 1) w.substring(1).lowercase(Locale.getDefault()) else "")
30+
)
31+
res.append(w)
32+
if (res.length >= 100) {
33+
break
34+
}
35+
}
36+
return if (res.length > 100) res.substring(0, 100) else res.toString()
37+
}
38+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
class Solution {
6+
fun specialTriplets(nums: IntArray): Int {
7+
val mod = 1000000007
8+
var res = 0
9+
val left: MutableMap<Int, Int> = HashMap()
10+
val right: MutableMap<Int, Int> = HashMap()
11+
for (num in nums) {
12+
right.put(num, right.getOrDefault(num, 0) + 1)
13+
}
14+
for (num in nums) {
15+
right.put(num, right[num]!! - 1)
16+
val ci: Int = left.getOrDefault(num * 2, 0)
17+
val ck: Int = right.getOrDefault(num * 2, 0)
18+
res = (res + ci * ck) % mod
19+
left.put(num, left.getOrDefault(num, 0) + 1)
20+
}
21+
return res
22+
}
23+
}
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,20 @@
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+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun maximumProduct(nums: IntArray, m: Int): Long {
10+
var ma = nums[0].toLong()
11+
var mi = nums[0].toLong()
12+
var res = nums[0].toLong() * nums[m - 1]
13+
for (i in m - 1..<nums.size) {
14+
ma = max(ma, nums[i - m + 1].toLong())
15+
mi = min(mi, nums[i - m + 1].toLong())
16+
res = max(res, max(mi * nums[i], ma * nums[i]))
17+
}
18+
return res
19+
}
20+
}
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+
class Solution {
6+
private var log = 0
7+
private lateinit var dist: LongArray
8+
private lateinit var depth: IntArray
9+
private lateinit var up: Array<IntArray>
10+
11+
fun findMedian(n: Int, edges: Array<IntArray>, queries: Array<IntArray>): IntArray {
12+
val adj: MutableList<MutableList<IntArray>?> = ArrayList<MutableList<IntArray>?>()
13+
for (i in 0..<n) {
14+
adj.add(ArrayList<IntArray>())
15+
}
16+
for (edge in edges) {
17+
adj[edge[0]]!!.add(intArrayOf(edge[1], edge[2]))
18+
adj[edge[1]]!!.add(intArrayOf(edge[0], edge[2]))
19+
}
20+
dist = LongArray(n)
21+
depth = IntArray(n)
22+
log = 0
23+
while (1 shl log < n) {
24+
log++
25+
}
26+
up = Array(n) { IntArray(log) }
27+
for (u in up) {
28+
u.fill(-1)
29+
}
30+
dfs(0, -1, adj, 0, 0)
31+
val ans = IntArray(queries.size)
32+
for (i in queries.indices) {
33+
val query = queries[i]
34+
var first = query[0]
35+
var second = query[1]
36+
val distance = getDistance(first, second)
37+
var needed = (distance + 1) / 2
38+
val mid = lca(first, second)
39+
if (getDistance(first, mid) < needed) {
40+
needed -= getDistance(first, mid)
41+
first = mid
42+
} else {
43+
second = mid
44+
}
45+
if (depth[first] <= depth[second]) {
46+
var curDistance = getDistance(first, second)
47+
for (j in log - 1 downTo 0) {
48+
if (up[second][j] == -1 ||
49+
curDistance - getDistance(up[second][j], second) < needed
50+
) {
51+
continue
52+
}
53+
curDistance -= getDistance(up[second][j], second)
54+
second = up[second][j]
55+
}
56+
ans[i] = second
57+
} else {
58+
var curDistance: Long = 0
59+
for (j in log - 1 downTo 0) {
60+
if (up[first][j] == -1 ||
61+
curDistance + getDistance(up[first][j], first) >= needed
62+
) {
63+
continue
64+
}
65+
curDistance += getDistance(up[first][j], first)
66+
first = up[first][j]
67+
}
68+
ans[i] = up[first][0]
69+
}
70+
}
71+
return ans
72+
}
73+
74+
private fun getDistance(from: Int, to: Int): Long {
75+
if (from == to) {
76+
return 0
77+
}
78+
val ancesor = lca(from, to)
79+
return dist[from] + dist[to] - 2 * dist[ancesor]
80+
}
81+
82+
private fun lca(first: Int, second: Int): Int {
83+
var first = first
84+
var second = second
85+
if (depth[first] < depth[second]) {
86+
return lca(second, first)
87+
}
88+
for (i in log - 1 downTo 0) {
89+
if (depth[first] - (1 shl i) >= depth[second]) {
90+
first = up[first][i]
91+
}
92+
}
93+
if (first == second) {
94+
return second
95+
}
96+
for (i in log - 1 downTo 0) {
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 fun dfs(current: Int, parent: Int, adj: MutableList<MutableList<IntArray>?>, curDist: Long, curDepth: Int) {
107+
up[current][0] = parent
108+
for (i in 1..<log) {
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 (next in adj[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)