Skip to content

Added tasks 3582-3585 #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
org.gradle.jvmargs=-Xms256m -Xmx1024m
org.gradle.jvmargs=-Xms512m -Xmx2048m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g3501_3600.s3582_generate_tag_for_video_caption

// #Easy #String #Simulation #2025_06_17_Time_3_ms_(100.00%)_Space_45.13_MB_(85.00%)

class Solution {
fun generateTag(caption: String): String? {
var caption = caption
val sb = StringBuilder()
sb.append('#')
var space = false
caption = caption.trim { it <= ' ' }
for (i in 0..<caption.length) {
val c = caption[i]
if (c == ' ') {
space = true
}
if (c >= 'A' && c <= 'Z') {
if (space) {
space = !space
sb.append(c)
} else {
sb.append(c.lowercaseChar())
}
}
if (c >= 'a' && c <= 'z') {
if (space) {
space = !space
sb.append(c.uppercaseChar())
} else {
sb.append(c)
}
}
}
if (sb.length > 100) {
return sb.substring(0, 100)
}
return sb.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3582\. Generate Tag for Video Caption

Easy

You are given a string `caption` representing the caption for a video.

The following actions must be performed **in order** to generate a **valid tag** for the video:

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.

2. **Remove** all characters that are not an English letter, **except** the first `'#'`.

3. **Truncate** the result to a maximum of 100 characters.


Return the **tag** after performing the actions on `caption`.

**Example 1:**

**Input:** caption = "Leetcode daily streak achieved"

**Output:** "#leetcodeDailyStreakAchieved"

**Explanation:**

The first letter for all words except `"leetcode"` should be capitalized.

**Example 2:**

**Input:** caption = "can I Go There"

**Output:** "#canIGoThere"

**Explanation:**

The first letter for all words except `"can"` should be capitalized.

**Example 3:**

**Input:** caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

**Output:** "#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

**Explanation:**

Since the first word has length 101, we need to truncate the last two letters from the word.

**Constraints:**

* `1 <= caption.length <= 150`
* `caption` consists only of English letters and `' '`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3501_3600.s3583_count_special_triplets

// #Medium #Array #Hash_Table #Counting #2025_06_17_Time_238_ms_(55.56%)_Space_83.48_MB_(77.78%)

class Solution {
fun specialTriplets(nums: IntArray): Int {
val mod = 1_000_000_007
var res = 0
val left = mutableMapOf<Int, Int>()
val right = mutableMapOf<Int, Int>()
for (num in nums) {
right[num] = right.getOrDefault(num, 0) + 1
}
for (num in nums) {
right[num] = right[num]!! - 1
val ci = left.getOrDefault(num * 2, 0)
val ck = right.getOrDefault(num * 2, 0)
res = ((res + 1L * ci * ck) % mod).toInt()
left[num] = left.getOrDefault(num, 0) + 1
}
return res
}
}
67 changes: 67 additions & 0 deletions src/main/kotlin/g3501_3600/s3583_count_special_triplets/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
3583\. Count Special Triplets

Medium

You are given an integer array `nums`.

A **special triplet** is defined as a triplet of indices `(i, j, k)` such that:

* `0 <= i < j < k < n`, where `n = nums.length`
* `nums[i] == nums[j] * 2`
* `nums[k] == nums[j] * 2`

Return the total number of **special triplets** in the array.

Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** nums = [6,3,6]

**Output:** 1

**Explanation:**

The only special triplet is `(i, j, k) = (0, 1, 2)`, where:

* `nums[0] = 6`, `nums[1] = 3`, `nums[2] = 6`
* `nums[0] = nums[1] * 2 = 3 * 2 = 6`
* `nums[2] = nums[1] * 2 = 3 * 2 = 6`

**Example 2:**

**Input:** nums = [0,1,0,0]

**Output:** 1

**Explanation:**

The only special triplet is `(i, j, k) = (0, 2, 3)`, where:

* `nums[0] = 0`, `nums[2] = 0`, `nums[3] = 0`
* `nums[0] = nums[2] * 2 = 0 * 2 = 0`
* `nums[3] = nums[2] * 2 = 0 * 2 = 0`

**Example 3:**

**Input:** nums = [8,4,2,8,4]

**Output:** 2

**Explanation:**

There are exactly two special triplets:

* `(i, j, k) = (0, 1, 3)`
* `nums[0] = 8`, `nums[1] = 4`, `nums[3] = 8`
* `nums[0] = nums[1] * 2 = 4 * 2 = 8`
* `nums[3] = nums[1] * 2 = 4 * 2 = 8`
* `(i, j, k) = (1, 2, 4)`
* `nums[1] = 4`, `nums[2] = 2`, `nums[4] = 4`
* `nums[1] = nums[2] * 2 = 2 * 2 = 4`
* `nums[4] = nums[2] * 2 = 2 * 2 = 4`

**Constraints:**

* <code>3 <= n == nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3501_3600.s3584_maximum_product_of_first_and_last_elements_of_a_subsequence

// #Medium #Array #Two_Pointers #2025_06_17_Time_8_ms_(100.00%)_Space_80.95_MB_(50.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun maximumProduct(nums: IntArray, m: Int): Long {
var ma = nums[0].toLong()
var mi = nums[0].toLong()
var res = nums[0].toLong() * nums[m - 1]
for (i in m - 1..<nums.size) {
ma = max(ma, nums[i - m + 1].toLong())
mi = min(mi, nums[i - m + 1].toLong())
res = max(res, max(mi * nums[i], ma * nums[i]))
}
return res
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3584\. Maximum Product of First and Last Elements of a Subsequence

Medium

You are given an integer array `nums` and an integer `m`.

Return the **maximum** product of the first and last elements of any ****subsequences**** of `nums` of size `m`.

**Example 1:**

**Input:** nums = [-1,-9,2,3,-2,-3,1], m = 1

**Output:** 81

**Explanation:**

The subsequence `[-9]` has the largest product of the first and last elements: `-9 * -9 = 81`. Therefore, the answer is 81.

**Example 2:**

**Input:** nums = [1,3,-5,5,6,-4], m = 3

**Output:** 20

**Explanation:**

The subsequence `[-5, 6, -4]` has the largest product of the first and last elements.

**Example 3:**

**Input:** nums = [2,-1,2,-6,5,2,-5,7], m = 2

**Output:** 35

**Explanation:**

The subsequence `[5, 7]` has the largest product of the first and last elements.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
* `1 <= m <= nums.length`
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package g3501_3600.s3585_find_weighted_median_node_in_tree

// #Hard #Array #Dynamic_Programming #Tree #Binary_Search #Depth_First_Search
// #2025_06_17_Time_123_ms_(100.00%)_Space_184.68_MB_(100.00%)

import kotlin.math.ceil
import kotlin.math.ln

class Solution {
private lateinit var adj: MutableList<MutableList<IntArray>>
private lateinit var depth: IntArray
private lateinit var dist: LongArray
private lateinit var parent: Array<IntArray>
private var longMax = 0
private var nodes = 0

fun findMedian(n: Int, edges: Array<IntArray>, queries: Array<IntArray>): IntArray {
nodes = n
if (n > 1) {
longMax = ceil(ln(n.toDouble()) / ln(2.0)).toInt()
} else {
longMax = 1
}
adj = ArrayList()
for (i in 0..<n) {
adj.add(ArrayList())
}
for (edge in edges) {
val u = edge[0]
val v = edge[1]
val w = edge[2]
adj[u].add(intArrayOf(v, w))
adj[v].add(intArrayOf(u, w))
}
depth = IntArray(n)
dist = LongArray(n)
parent = Array(longMax) { IntArray(n) }
for (i in 0..<longMax) {
parent[i].fill(-1)
}
dfs(0, -1, 0, 0L)
buildLcaTable()
val ans = IntArray(queries.size)
var sabrelonta: IntArray
for (qIdx in queries.indices) {
sabrelonta = queries[qIdx]
val u = sabrelonta[0]
val v = sabrelonta[1]
ans[qIdx] = findMedianNode(u, v)
}

return ans
}

private fun dfs(u: Int, p: Int, d: Int, currentDist: Long) {
depth[u] = d
parent[0][u] = p
dist[u] = currentDist
for (edge in adj[u]) {
val v = edge[0]
val w = edge[1]
if (v == p) {
continue
}
dfs(v, u, d + 1, currentDist + w)
}
}

private fun buildLcaTable() {
for (k in 1..<longMax) {
for (node in 0..<nodes) {
if (parent[k - 1][node] != -1) {
parent[k][node] = parent[k - 1][parent[k - 1][node]]
}
}
}
}

private fun getKthAncestor(u: Int, k: Int): Int {
var u = u
for (p in longMax - 1 downTo 0) {
if (u == -1) {
break
}
if (((k shr p) and 1) == 1) {
u = parent[p][u]
}
}
return u
}

private fun getLCA(u: Int, v: Int): Int {
var u = u
var v = v
if (depth[u] < depth[v]) {
val temp = u
u = v
v = temp
}
u = getKthAncestor(u, depth[u] - depth[v])
if (u == v) {
return u
}
for (p in longMax - 1 downTo 0) {
if (parent[p][u] != -1 && parent[p][u] != parent[p][v]) {
u = parent[p][u]
v = parent[p][v]
}
}
return parent[0][u]
}

private fun findMedianNode(u: Int, v: Int): Int {
if (u == v) {
return u
}
val lca = getLCA(u, v)
val totalPathWeight = dist[u] + dist[v] - 2 * dist[lca]
val halfWeight = (totalPathWeight + 1) / 2L
return if (dist[u] - dist[lca] >= halfWeight) {
var curr = u
for (p in longMax - 1 downTo 0) {
val nextNode = parent[p][curr]
if (nextNode != -1 && (dist[u] - dist[nextNode] < halfWeight)) {
curr = nextNode
}
}
parent[0][curr]
} else {
val remainingWeightFromLCA = halfWeight - (dist[u] - dist[lca])
var curr = v
for (p in longMax - 1 downTo 0) {
val nextNode = parent[p][curr]
if (nextNode != -1 && depth[nextNode] >= depth[lca] &&
(dist[nextNode] - dist[lca]) >= remainingWeightFromLCA
) {
curr = nextNode
}
}
curr
}
}
}
Loading