Skip to content

Commit bb9c4e9

Browse files
authored
Added tasks 3582-3585
1 parent 4dab810 commit bb9c4e9

File tree

13 files changed

+610
-1
lines changed

13 files changed

+610
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
2-
org.gradle.jvmargs=-Xms256m -Xmx1024m
2+
org.gradle.jvmargs=-Xms512m -Xmx2048m
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3501_3600.s3582_generate_tag_for_video_caption
2+
3+
// #Easy #String #Simulation #2025_06_17_Time_3_ms_(100.00%)_Space_45.13_MB_(85.00%)
4+
5+
class Solution {
6+
fun generateTag(caption: String): String? {
7+
var caption = caption
8+
val sb = StringBuilder()
9+
sb.append('#')
10+
var space = false
11+
caption = caption.trim { it <= ' ' }
12+
for (i in 0..<caption.length) {
13+
val c = caption[i]
14+
if (c == ' ') {
15+
space = true
16+
}
17+
if (c >= 'A' && c <= 'Z') {
18+
if (space) {
19+
space = !space
20+
sb.append(c)
21+
} else {
22+
sb.append(c.lowercaseChar())
23+
}
24+
}
25+
if (c >= 'a' && c <= 'z') {
26+
if (space) {
27+
space = !space
28+
sb.append(c.uppercaseChar())
29+
} else {
30+
sb.append(c)
31+
}
32+
}
33+
}
34+
if (sb.length > 100) {
35+
return sb.substring(0, 100)
36+
}
37+
return sb.toString()
38+
}
39+
}
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 #Array #Hash_Table #Counting #2025_06_17_Time_238_ms_(55.56%)_Space_83.48_MB_(77.78%)
4+
5+
class Solution {
6+
fun specialTriplets(nums: IntArray): Int {
7+
val mod = 1_000_000_007
8+
var res = 0
9+
val left = mutableMapOf<Int, Int>()
10+
val right = mutableMapOf<Int, Int>()
11+
for (num in nums) {
12+
right[num] = right.getOrDefault(num, 0) + 1
13+
}
14+
for (num in nums) {
15+
right[num] = right[num]!! - 1
16+
val ci = left.getOrDefault(num * 2, 0)
17+
val ck = right.getOrDefault(num * 2, 0)
18+
res = ((res + 1L * ci * ck) % mod).toInt()
19+
left[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 #Array #Two_Pointers #2025_06_17_Time_8_ms_(100.00%)_Space_80.95_MB_(50.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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package g3501_3600.s3585_find_weighted_median_node_in_tree
2+
3+
// #Hard #Array #Dynamic_Programming #Tree #Binary_Search #Depth_First_Search
4+
// #2025_06_17_Time_123_ms_(100.00%)_Space_184.68_MB_(100.00%)
5+
6+
import kotlin.math.ceil
7+
import kotlin.math.ln
8+
9+
class Solution {
10+
private lateinit var adj: MutableList<MutableList<IntArray>>
11+
private lateinit var depth: IntArray
12+
private lateinit var dist: LongArray
13+
private lateinit var parent: Array<IntArray>
14+
private var longMax = 0
15+
private var nodes = 0
16+
17+
fun findMedian(n: Int, edges: Array<IntArray>, queries: Array<IntArray>): IntArray {
18+
nodes = n
19+
if (n > 1) {
20+
longMax = ceil(ln(n.toDouble()) / ln(2.0)).toInt()
21+
} else {
22+
longMax = 1
23+
}
24+
adj = ArrayList()
25+
for (i in 0..<n) {
26+
adj.add(ArrayList())
27+
}
28+
for (edge in edges) {
29+
val u = edge[0]
30+
val v = edge[1]
31+
val w = edge[2]
32+
adj[u].add(intArrayOf(v, w))
33+
adj[v].add(intArrayOf(u, w))
34+
}
35+
depth = IntArray(n)
36+
dist = LongArray(n)
37+
parent = Array(longMax) { IntArray(n) }
38+
for (i in 0..<longMax) {
39+
parent[i].fill(-1)
40+
}
41+
dfs(0, -1, 0, 0L)
42+
buildLcaTable()
43+
val ans = IntArray(queries.size)
44+
var sabrelonta: IntArray
45+
for (qIdx in queries.indices) {
46+
sabrelonta = queries[qIdx]
47+
val u = sabrelonta[0]
48+
val v = sabrelonta[1]
49+
ans[qIdx] = findMedianNode(u, v)
50+
}
51+
52+
return ans
53+
}
54+
55+
private fun dfs(u: Int, p: Int, d: Int, currentDist: Long) {
56+
depth[u] = d
57+
parent[0][u] = p
58+
dist[u] = currentDist
59+
for (edge in adj[u]) {
60+
val v = edge[0]
61+
val w = edge[1]
62+
if (v == p) {
63+
continue
64+
}
65+
dfs(v, u, d + 1, currentDist + w)
66+
}
67+
}
68+
69+
private fun buildLcaTable() {
70+
for (k in 1..<longMax) {
71+
for (node in 0..<nodes) {
72+
if (parent[k - 1][node] != -1) {
73+
parent[k][node] = parent[k - 1][parent[k - 1][node]]
74+
}
75+
}
76+
}
77+
}
78+
79+
private fun getKthAncestor(u: Int, k: Int): Int {
80+
var u = u
81+
for (p in longMax - 1 downTo 0) {
82+
if (u == -1) {
83+
break
84+
}
85+
if (((k shr p) and 1) == 1) {
86+
u = parent[p][u]
87+
}
88+
}
89+
return u
90+
}
91+
92+
private fun getLCA(u: Int, v: Int): Int {
93+
var u = u
94+
var v = v
95+
if (depth[u] < depth[v]) {
96+
val temp = u
97+
u = v
98+
v = temp
99+
}
100+
u = getKthAncestor(u, depth[u] - depth[v])
101+
if (u == v) {
102+
return u
103+
}
104+
for (p in longMax - 1 downTo 0) {
105+
if (parent[p][u] != -1 && parent[p][u] != parent[p][v]) {
106+
u = parent[p][u]
107+
v = parent[p][v]
108+
}
109+
}
110+
return parent[0][u]
111+
}
112+
113+
private fun findMedianNode(u: Int, v: Int): Int {
114+
if (u == v) {
115+
return u
116+
}
117+
val lca = getLCA(u, v)
118+
val totalPathWeight = dist[u] + dist[v] - 2 * dist[lca]
119+
val halfWeight = (totalPathWeight + 1) / 2L
120+
return if (dist[u] - dist[lca] >= halfWeight) {
121+
var curr = u
122+
for (p in longMax - 1 downTo 0) {
123+
val nextNode = parent[p][curr]
124+
if (nextNode != -1 && (dist[u] - dist[nextNode] < halfWeight)) {
125+
curr = nextNode
126+
}
127+
}
128+
parent[0][curr]
129+
} else {
130+
val remainingWeightFromLCA = halfWeight - (dist[u] - dist[lca])
131+
var curr = v
132+
for (p in longMax - 1 downTo 0) {
133+
val nextNode = parent[p][curr]
134+
if (nextNode != -1 && depth[nextNode] >= depth[lca] &&
135+
(dist[nextNode] - dist[lca]) >= remainingWeightFromLCA
136+
) {
137+
curr = nextNode
138+
}
139+
}
140+
curr
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)