Skip to content

Commit 1cf71a2

Browse files
authored
Added tasks 3774, 3775, 3776
1 parent 939b480 commit 1cf71a2

9 files changed

Lines changed: 315 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3701_3800.s3774_absolute_difference_between_maximum_and_minimum_k_elements;
2+
3+
// #Easy #Array #Sorting #Mid_Level #Weekly_Contest_480
4+
// #2026_05_08_Time_5_ms_(96.82%)_Space_46.97_MB_(28.79%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int absDifference(int[] nums, int k) {
10+
Arrays.sort(nums);
11+
int maxSum = 0;
12+
int minSum = 0;
13+
for (int i = 0, j = nums.length - 1; i < k; i++, j--) {
14+
minSum += nums[i];
15+
maxSum += nums[j];
16+
}
17+
return maxSum - minSum;
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3774\. Absolute Difference Between Maximum and Minimum K Elements
2+
3+
Easy
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Find the absolute difference between:
8+
9+
* the **sum** of the `k` **largest** elements in the array; and
10+
* the **sum** of the `k` **smallest** elements in the array.
11+
12+
Return an integer denoting this difference.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [5,2,2,4], k = 2
17+
18+
**Output:** 5
19+
20+
**Explanation:**
21+
22+
* The `k = 2` largest elements are 4 and 5. Their sum is `4 + 5 = 9`.
23+
* The `k = 2` smallest elements are 2 and 2. Their sum is `2 + 2 = 4`.
24+
* The absolute difference is `abs(9 - 4) = 5`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [100], k = 1
29+
30+
**Output:** 0
31+
32+
**Explanation:**
33+
34+
* The largest element is 100.
35+
* The smallest element is 100.
36+
* The absolute difference is `abs(100 - 100) = 0`.
37+
38+
**Constraints:**
39+
40+
* `1 <= n == nums.length <= 100`
41+
* `1 <= nums[i] <= 100`
42+
* `1 <= k <= n`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3701_3800.s3775_reverse_words_with_same_vowel_count;
2+
3+
// #Medium #String #Two_Pointers #Simulation #Senior #Weekly_Contest_480
4+
// #2026_05_08_Time_41_ms_(96.14%)_Space_48.30_MB_(90.84%)
5+
6+
public class Solution {
7+
public String reverseWords(String s) {
8+
char[] wrd = s.toCharArray();
9+
String vowels = "aeiou";
10+
int left = 0;
11+
int right = 0;
12+
int firstWrd = 0;
13+
int anotherWrd = 0;
14+
while (left < wrd.length) {
15+
right = left;
16+
anotherWrd = 0;
17+
while (right < wrd.length && wrd[right] != ' ') {
18+
if (vowels.indexOf(wrd[right]) != -1) {
19+
if (left == 0) {
20+
firstWrd++;
21+
} else {
22+
anotherWrd++;
23+
}
24+
}
25+
right++;
26+
}
27+
if (left != 0 && anotherWrd == firstWrd) {
28+
int l = left;
29+
int r = right - 1;
30+
while (l < r) {
31+
char temp = wrd[r];
32+
wrd[r--] = wrd[l];
33+
wrd[l++] = temp;
34+
}
35+
}
36+
left = right + 1;
37+
}
38+
return new String(wrd);
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3775\. Reverse Words With Same Vowel Count
2+
3+
Medium
4+
5+
You are given a string `s` consisting of lowercase English words, each separated by a single space.
6+
7+
Determine how many vowels appear in the **first** word. Then, reverse each following word that has the **same vowel count**. Leave all remaining words unchanged.
8+
9+
Return the resulting string.
10+
11+
Vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "cat and mice"
16+
17+
**Output:** "cat dna mice"
18+
19+
**Explanation:**
20+
21+
* The first word `"cat"` has 1 vowel.
22+
* `"and"` has 1 vowel, so it is reversed to form `"dna"`.
23+
* `"mice"` has 2 vowels, so it remains unchanged.
24+
* Thus, the resulting string is `"cat dna mice"`.
25+
26+
**Example 2:**
27+
28+
**Input:** s = "book is nice"
29+
30+
**Output:** "book is ecin"
31+
32+
**Explanation:**
33+
34+
* The first word `"book"` has 2 vowels.
35+
* `"is"` has 1 vowel, so it remains unchanged.
36+
* `"nice"` has 2 vowels, so it is reversed to form `"ecin"`.
37+
* Thus, the resulting string is `"book is ecin"`.
38+
39+
**Example 3:**
40+
41+
**Input:** s = "banana healthy"
42+
43+
**Output:** "banana healthy"
44+
45+
**Explanation:**
46+
47+
* The first word `"banana"` has 3 vowels.
48+
* `"healthy"` has 2 vowels, so it remains unchanged.
49+
* Thus, the resulting string is `"banana healthy"`.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= s.length <= 10<sup>5</sup></code>
54+
* `s` consists of lowercase English letters and spaces.
55+
* Words in `s` are separated by a **single** space.
56+
* `s` does **not** contain leading or trailing spaces.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3701_3800.s3776_minimum_moves_to_balance_circular_array;
2+
3+
// #Medium #Array #Sorting #Greedy #Staff #Weekly_Contest_480
4+
// #2026_05_08_Time_2_ms_(100.00%)_Space_139.96_MB_(20.95%)
5+
6+
public class Solution {
7+
public long minMoves(int[] balance) {
8+
int n = balance.length;
9+
int j = -1;
10+
long total = 0;
11+
long res = 0;
12+
for (int i = 0; i < n; i++) {
13+
if (balance[i] < 0) {
14+
j = i;
15+
}
16+
total += balance[i];
17+
}
18+
if (j == -1) {
19+
return 0;
20+
}
21+
if (total < 0) {
22+
return -1;
23+
}
24+
for (int d = 1; balance[j] < 0; ++d) {
25+
long storage = balance[(j + d) % n] + (long) balance[(j - d % n + n) % n];
26+
res += Math.min(-balance[j], (int) storage) * d;
27+
balance[j] += (int) storage;
28+
}
29+
return res;
30+
}
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3776\. Minimum Moves to Balance Circular Array
2+
3+
Medium
4+
5+
You are given a **circular** array `balance` of length `n`, where `balance[i]` is the net balance of person `i`.
6+
7+
In one move, a person can transfer **exactly** 1 unit of balance to either their left or right neighbor.
8+
9+
Return the **minimum** number of moves required so that every person has a **non-negative** balance. If it is impossible, return `-1`.
10+
11+
**Note**: You are guaranteed that **at most** 1 index has a **negative** balance initially.
12+
13+
**Example 1:**
14+
15+
**Input:** balance = [5,1,-4]
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
One optimal sequence of moves is:
22+
23+
* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [5, 0, -3]`
24+
* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [4, 0, -2]`
25+
* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [3, 0, -1]`
26+
* Move 1 unit from `i = 0` to `i = 2`, resulting in `balance = [2, 0, 0]`
27+
28+
Thus, the minimum number of moves required is 4.
29+
30+
**Example 2:**
31+
32+
**Input:** balance = [1,2,-5,2]
33+
34+
**Output:** 6
35+
36+
**Explanation:**
37+
38+
One optimal sequence of moves is:
39+
40+
* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [1, 1, -4, 2]`
41+
* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [1, 0, -3, 2]`
42+
* Move 1 unit from `i = 3` to `i = 2`, resulting in `balance = [1, 0, -2, 1]`
43+
* Move 1 unit from `i = 3` to `i = 2`, resulting in `balance = [1, 0, -1, 0]`
44+
* Move 1 unit from `i = 0` to `i = 1`, resulting in `balance = [0, 1, -1, 0]`
45+
* Move 1 unit from `i = 1` to `i = 2`, resulting in `balance = [0, 0, 0, 0]`
46+
47+
Thus, the minimum number of moves required is 6.
48+
49+
**Example 3:**
50+
51+
**Input:** balance = [-3,2]
52+
53+
**Output:** \-1
54+
55+
**Explanation:**
56+
57+
It is impossible to make all balances non-negative for `balance = [-3, 2]`, so the answer is -1.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= n == balance.length <= 10<sup>5</sup></code>
62+
* <code>-10<sup>9</sup> <= balance[i] <= 10<sup>9</sup></code>
63+
* There is at most one negative value in `balance` initially.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3774_absolute_difference_between_maximum_and_minimum_k_elements;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void absDifference() {
11+
assertThat(new Solution().absDifference(new int[] {5, 2, 2, 4}, 2), equalTo(5));
12+
}
13+
14+
@Test
15+
void absDifference2() {
16+
assertThat(new Solution().absDifference(new int[] {100}, 1), equalTo(0));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3775_reverse_words_with_same_vowel_count;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void reverseWords() {
11+
assertThat(new Solution().reverseWords("cat and mice"), equalTo("cat dna mice"));
12+
}
13+
14+
@Test
15+
void reverseWords2() {
16+
assertThat(new Solution().reverseWords("book is nice"), equalTo("book is ecin"));
17+
}
18+
19+
@Test
20+
void reverseWords3() {
21+
assertThat(new Solution().reverseWords("banana healthy"), equalTo("banana healthy"));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3776_minimum_moves_to_balance_circular_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minMoves() {
11+
assertThat(new Solution().minMoves(new int[] {5, 1, -4}), equalTo(4L));
12+
}
13+
14+
@Test
15+
void minMoves2() {
16+
assertThat(new Solution().minMoves(new int[] {1, 2, -5, 2}), equalTo(6L));
17+
}
18+
19+
@Test
20+
void minMoves3() {
21+
assertThat(new Solution().minMoves(new int[] {-3, 2}), equalTo(-1L));
22+
}
23+
}

0 commit comments

Comments
 (0)