Skip to content

Commit 2cbc07e

Browse files
committed
Added tasks 3582-3585
1 parent 15faf4c commit 2cbc07e

File tree

5 files changed

+475
-0
lines changed
  • src/main/kotlin/g3501_3600
    • s3582_generate_tag_for_video_caption
    • s3583_count_special_triplets
    • s3584_maximum_product_of_first_and_last_elements_of_a_subsequence
    • s3585_find_weighted_median_node_in_tree

5 files changed

+475
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3585 |[Find Weighted Median Node in Tree](src/main/kotlin/g3501_3600/s3585_find_weighted_median_node_in_tree)| Hard | Array, Dynamic_Programming, Tree, Binary_Search, Depth_First_Search | 123 | 100.00
2092+
| 3584 |[Maximum Product of First and Last Elements of a Subsequence](src/main/kotlin/g3501_3600/s3584_maximum_product_of_first_and_last_elements_of_a_subsequence)| Medium | Array, Two_Pointers | 8 | 100.00
2093+
| 3583 |[Count Special Triplets](src/main/kotlin/g3501_3600/s3583_count_special_triplets)| Medium | Array, Hash_Table, Counting | 238 | 55.56
2094+
| 3582 |[Generate Tag for Video Caption](src/main/kotlin/g3501_3600/s3582_generate_tag_for_video_caption)| Easy | String, Simulation | 3 | 100.00
20912095
| 3580 |[Find Consistently Improving Employees](src/main/kotlin/g3501_3600/s3580_find_consistently_improving_employees)| Medium | Database | 449 | 91.67
20922096
| 3579 |[Minimum Steps to Convert String with Operations](src/main/kotlin/g3501_3600/s3579_minimum_steps_to_convert_string_with_operations)| Hard | String, Dynamic_Programming, Greedy | 107 | 100.00
20932097
| 3578 |[Count Partitions With Max-Min Difference at Most K](src/main/kotlin/g3501_3600/s3578_count_partitions_with_max_min_difference_at_most_k)| Medium | Array, Dynamic_Programming, Prefix_Sum, Sliding_Window, Queue, Monotonic_Queue | 33 | 100.00
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3582\. Generate Tag for Video Caption
5+
6+
Easy
7+
8+
You are given a string `caption` representing the caption for a video.
9+
10+
The following actions must be performed **in order** to generate a **valid tag** for the video:
11+
12+
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.
13+
14+
2. **Remove** all characters that are not an English letter, **except** the first `'#'`.
15+
16+
3. **Truncate** the result to a maximum of 100 characters.
17+
18+
19+
Return the **tag** after performing the actions on `caption`.
20+
21+
**Example 1:**
22+
23+
**Input:** caption = "Leetcode daily streak achieved"
24+
25+
**Output:** "#leetcodeDailyStreakAchieved"
26+
27+
**Explanation:**
28+
29+
The first letter for all words except `"leetcode"` should be capitalized.
30+
31+
**Example 2:**
32+
33+
**Input:** caption = "can I Go There"
34+
35+
**Output:** "#canIGoThere"
36+
37+
**Explanation:**
38+
39+
The first letter for all words except `"can"` should be capitalized.
40+
41+
**Example 3:**
42+
43+
**Input:** caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
44+
45+
**Output:** "#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
46+
47+
**Explanation:**
48+
49+
Since the first word has length 101, we need to truncate the last two letters from the word.
50+
51+
**Constraints:**
52+
53+
* `1 <= caption.length <= 150`
54+
* `caption` consists only of English letters and `' '`.
55+
56+
## Solution
57+
58+
```kotlin
59+
class Solution {
60+
fun generateTag(caption: String): String? {
61+
var caption = caption
62+
val sb = StringBuilder()
63+
sb.append('#')
64+
var space = false
65+
caption = caption.trim { it <= ' ' }
66+
for (i in 0..<caption.length) {
67+
val c = caption[i]
68+
if (c == ' ') {
69+
space = true
70+
}
71+
if (c >= 'A' && c <= 'Z') {
72+
if (space) {
73+
space = !space
74+
sb.append(c)
75+
} else {
76+
sb.append(c.lowercaseChar())
77+
}
78+
}
79+
if (c >= 'a' && c <= 'z') {
80+
if (space) {
81+
space = !space
82+
sb.append(c.uppercaseChar())
83+
} else {
84+
sb.append(c)
85+
}
86+
}
87+
}
88+
if (sb.length > 100) {
89+
return sb.substring(0, 100)
90+
}
91+
return sb.toString()
92+
}
93+
}
94+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3583\. Count Special Triplets
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
A **special triplet** is defined as a triplet of indices `(i, j, k)` such that:
11+
12+
* `0 <= i < j < k < n`, where `n = nums.length`
13+
* `nums[i] == nums[j] * 2`
14+
* `nums[k] == nums[j] * 2`
15+
16+
Return the total number of **special triplets** in the array.
17+
18+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [6,3,6]
23+
24+
**Output:** 1
25+
26+
**Explanation:**
27+
28+
The only special triplet is `(i, j, k) = (0, 1, 2)`, where:
29+
30+
* `nums[0] = 6`, `nums[1] = 3`, `nums[2] = 6`
31+
* `nums[0] = nums[1] * 2 = 3 * 2 = 6`
32+
* `nums[2] = nums[1] * 2 = 3 * 2 = 6`
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [0,1,0,0]
37+
38+
**Output:** 1
39+
40+
**Explanation:**
41+
42+
The only special triplet is `(i, j, k) = (0, 2, 3)`, where:
43+
44+
* `nums[0] = 0`, `nums[2] = 0`, `nums[3] = 0`
45+
* `nums[0] = nums[2] * 2 = 0 * 2 = 0`
46+
* `nums[3] = nums[2] * 2 = 0 * 2 = 0`
47+
48+
**Example 3:**
49+
50+
**Input:** nums = [8,4,2,8,4]
51+
52+
**Output:** 2
53+
54+
**Explanation:**
55+
56+
There are exactly two special triplets:
57+
58+
* `(i, j, k) = (0, 1, 3)`
59+
* `nums[0] = 8`, `nums[1] = 4`, `nums[3] = 8`
60+
* `nums[0] = nums[1] * 2 = 4 * 2 = 8`
61+
* `nums[3] = nums[1] * 2 = 4 * 2 = 8`
62+
* `(i, j, k) = (1, 2, 4)`
63+
* `nums[1] = 4`, `nums[2] = 2`, `nums[4] = 4`
64+
* `nums[1] = nums[2] * 2 = 2 * 2 = 4`
65+
* `nums[4] = nums[2] * 2 = 2 * 2 = 4`
66+
67+
**Constraints:**
68+
69+
* <code>3 <= n == nums.length <= 10<sup>5</sup></code>
70+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
71+
72+
## Solution
73+
74+
```kotlin
75+
class Solution {
76+
fun specialTriplets(nums: IntArray): Int {
77+
val mod = 1_000_000_007
78+
var res = 0
79+
val left = mutableMapOf<Int, Int>()
80+
val right = mutableMapOf<Int, Int>()
81+
for (num in nums) {
82+
right[num] = right.getOrDefault(num, 0) + 1
83+
}
84+
for (num in nums) {
85+
right[num] = right[num]!! - 1
86+
val ci = left.getOrDefault(num * 2, 0)
87+
val ck = right.getOrDefault(num * 2, 0)
88+
res = ((res + 1L * ci * ck) % mod).toInt()
89+
left[num] = left.getOrDefault(num, 0) + 1
90+
}
91+
return res
92+
}
93+
}
94+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3584\. Maximum Product of First and Last Elements of a Subsequence
5+
6+
Medium
7+
8+
You are given an integer array `nums` and an integer `m`.
9+
10+
Return the **maximum** product of the first and last elements of any ****subsequences**** of `nums` of size `m`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [-1,-9,2,3,-2,-3,1], m = 1
15+
16+
**Output:** 81
17+
18+
**Explanation:**
19+
20+
The subsequence `[-9]` has the largest product of the first and last elements: `-9 * -9 = 81`. Therefore, the answer is 81.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,3,-5,5,6,-4], m = 3
25+
26+
**Output:** 20
27+
28+
**Explanation:**
29+
30+
The subsequence `[-5, 6, -4]` has the largest product of the first and last elements.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [2,-1,2,-6,5,2,-5,7], m = 2
35+
36+
**Output:** 35
37+
38+
**Explanation:**
39+
40+
The subsequence `[5, 7]` has the largest product of the first and last elements.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
46+
* `1 <= m <= nums.length`
47+
48+
## Solution
49+
50+
```kotlin
51+
import kotlin.math.max
52+
import kotlin.math.min
53+
54+
class Solution {
55+
fun maximumProduct(nums: IntArray, m: Int): Long {
56+
var ma = nums[0].toLong()
57+
var mi = nums[0].toLong()
58+
var res = nums[0].toLong() * nums[m - 1]
59+
for (i in m - 1..<nums.size) {
60+
ma = max(ma, nums[i - m + 1].toLong())
61+
mi = min(mi, nums[i - m + 1].toLong())
62+
res = max(res, max(mi * nums[i], ma * nums[i]))
63+
}
64+
return res
65+
}
66+
}
67+
```

0 commit comments

Comments
 (0)