Skip to content

Commit c94f639

Browse files
committed
Added tasks 3582-3585
1 parent 2cba409 commit c94f639

File tree

5 files changed

+472
-0
lines changed
  • src/main/java/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

+472
-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/java/g3501_3600/s3585_find_weighted_median_node_in_tree)| Hard | Array, Dynamic_Programming, Tree, Binary_Search, Depth_First_Search | 66 | 94.96
2092+
| 3584 |[Maximum Product of First and Last Elements of a Subsequence](src/main/java/g3501_3600/s3584_maximum_product_of_first_and_last_elements_of_a_subsequence)| Medium | Array, Two_Pointers | 4 | 86.42
2093+
| 3583 |[Count Special Triplets](src/main/java/g3501_3600/s3583_count_special_triplets)| Medium | Array, Hash_Table, Counting | 30 | 99.81
2094+
| 3582 |[Generate Tag for Video Caption](src/main/java/g3501_3600/s3582_generate_tag_for_video_caption)| Easy | String, Simulation | 2 | 99.93
20912095
| 3580 |[Find Consistently Improving Employees](src/main/java/g3501_3600/s3580_find_consistently_improving_employees)| Medium | Database | 449 | 91.67
20922096
| 3579 |[Minimum Steps to Convert String with Operations](src/main/java/g3501_3600/s3579_minimum_steps_to_convert_string_with_operations)| Hard | String, Dynamic_Programming, Greedy | 50 | 98.37
20932097
| 3578 |[Count Partitions With Max-Min Difference at Most K](src/main/java/g3501_3600/s3578_count_partitions_with_max_min_difference_at_most_k)| Medium | Array, Dynamic_Programming, Prefix_Sum, Sliding_Window, Queue, Monotonic_Queue | 16 | 99.88
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
59+
public class Solution {
60+
public String generateTag(String caption) {
61+
StringBuilder sb = new StringBuilder();
62+
sb.append('#');
63+
boolean space = false;
64+
caption = caption.trim();
65+
for (int i = 0; i < caption.length(); i++) {
66+
char c = caption.charAt(i);
67+
if (c == ' ') {
68+
space = true;
69+
}
70+
if (c >= 'A' && c <= 'Z') {
71+
if (space) {
72+
space = !space;
73+
sb.append(c);
74+
} else {
75+
sb.append(Character.toLowerCase(c));
76+
}
77+
}
78+
if (c >= 'a' && c <= 'z') {
79+
if (space) {
80+
space = !space;
81+
sb.append(Character.toUpperCase(c));
82+
} else {
83+
sb.append(c);
84+
}
85+
}
86+
}
87+
if (sb.length() > 100) {
88+
return sb.substring(0, 100);
89+
}
90+
return sb.toString();
91+
}
92+
}
93+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
75+
public class Solution {
76+
public int specialTriplets(int[] nums) {
77+
long ans = 0;
78+
int[] sum = new int[200002];
79+
int[] left = new int[nums.length];
80+
for (int i = 0; i < nums.length; i++) {
81+
int curr = nums[i];
82+
sum[curr]++;
83+
left[i] = sum[curr * 2];
84+
}
85+
for (int i = 0; i < nums.length; i++) {
86+
int curr = nums[i];
87+
long leftCount = curr == 0 ? left[i] - 1 : left[i];
88+
long rightCount = sum[curr * 2] - (long) left[i];
89+
if (leftCount != 0 && rightCount != 0) {
90+
ans += leftCount * rightCount;
91+
}
92+
}
93+
return (int) (ans % 1000000007);
94+
}
95+
}
96+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
51+
public class Solution {
52+
public long maximumProduct(int[] nums, int m) {
53+
long ma = nums[0];
54+
long mi = nums[0];
55+
long res = (long) nums[0] * nums[m - 1];
56+
for (int i = m - 1; i < nums.length; ++i) {
57+
ma = Math.max(ma, nums[i - m + 1]);
58+
mi = Math.min(mi, nums[i - m + 1]);
59+
res = Math.max(res, Math.max(mi * nums[i], ma * nums[i]));
60+
}
61+
return res;
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)