Skip to content

Commit 941bb41

Browse files
authored
Added tasks 2182, 2183.
1 parent 21bc231 commit 941bb41

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13771377
| 2190 |[Most Frequent Number Following Key In an Array](src/main/java/g2101_2200/s2190_most_frequent_number_following_key_in_an_array/Solution.java)| Easy | Array, Hash_Table, Counting | 1 | 100.00
13781378
| 2188 |[Minimum Time to Finish the Race](src/main/java/g2101_2200/s2188_minimum_time_to_finish_the_race/Solution.java)| Hard | Array, Dynamic_Programming | 15 | 93.69
13791379
| 2187 |[Minimum Time to Complete Trips](src/main/java/g2101_2200/s2187_minimum_time_to_complete_trips/Solution.java)| Medium | Array, Binary_Search | 187 | 95.03
1380+
| 2183 |[Count Array Pairs Divisible by K](src/main/java/g2101_2200/s2183_count_array_pairs_divisible_by_k/Solution.java)| Hard | Array, Math, Number_Theory | 849 | 44.54
1381+
| 2182 |[Construct String With Repeat Limit](src/main/java/g2101_2200/s2182_construct_string_with_repeat_limit/Solution.java)| Medium | String, Greedy, Heap_Priority_Queue, Counting | 26 | 96.11
13801382
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros/Solution.java)| Medium | Simulation, Linked_List | 6 | 96.26
13811383
| 2177 |[Find Three Consecutive Integers That Sum to a Given Number](src/main/java/g2101_2200/s2177_find_three_consecutive_integers_that_sum_to_a_given_number/Solution.java)| Medium | Math, Simulation | 1 | 78.46
13821384
| 2176 |[Count Equal and Divisible Pairs in an Array](src/main/java/g2101_2200/s2176_count_equal_and_divisible_pairs_in_an_array/Solution.java)| Easy | Array | 4 | 78.29
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g2101_2200.s2182_construct_string_with_repeat_limit;
2+
3+
// #Medium #String #Greedy #Heap_Priority_Queue #Counting
4+
// #2022_06_07_Time_26_ms_(96.11%)_Space_63.6_MB_(62.78%)
5+
6+
@SuppressWarnings("java:S135")
7+
public class Solution {
8+
public String repeatLimitedString(String s, int repeatLimit) {
9+
char[] result = new char[s.length()];
10+
int[] freq = new int[128];
11+
int index = 0;
12+
for (char c : s.toCharArray()) {
13+
freq[c]++;
14+
}
15+
char max = 'z';
16+
char second = 'y';
17+
while (true) {
18+
while (max >= 'a' && freq[max] == 0) {
19+
max--;
20+
}
21+
if (max < 'a') {
22+
break;
23+
}
24+
second = (char) Math.min(max - 1, second);
25+
int count = Math.min(freq[max], repeatLimit);
26+
freq[max] -= count;
27+
while (count-- > 0) {
28+
result[index++] = max;
29+
}
30+
if (freq[max] == 0) {
31+
max = second--;
32+
continue;
33+
}
34+
while (second >= 'a' && freq[second] == 0) {
35+
second--;
36+
}
37+
if (second < 'a') {
38+
break;
39+
}
40+
result[index++] = second;
41+
freq[second]--;
42+
}
43+
return new String(result, 0, index);
44+
}
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2182\. Construct String With Repeat Limit
2+
3+
Medium
4+
5+
You are given a string `s` and an integer `repeatLimit`. Construct a new string `repeatLimitedString` using the characters of `s` such that no letter appears **more than** `repeatLimit` times **in a row**. You do **not** have to use all characters from `s`.
6+
7+
Return _the **lexicographically largest**_ `repeatLimitedString` _possible_.
8+
9+
A string `a` is **lexicographically larger** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears later in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters do not differ, then the longer string is the lexicographically larger one.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "cczazcc", repeatLimit = 3
14+
15+
**Output:** "zzcccac"
16+
17+
**Explanation:** We use all of the characters from s to construct the repeatLimitedString "zzcccac".
18+
19+
The letter 'a' appears at most 1 time in a row.
20+
21+
The letter 'c' appears at most 3 times in a row.
22+
23+
The letter 'z' appears at most 2 times in a row.
24+
25+
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
26+
27+
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
28+
29+
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "aababab", repeatLimit = 2
34+
35+
**Output:** "bbabaa"
36+
37+
**Explanation:** We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
38+
39+
The letter 'a' appears at most 2 times in a row. The letter 'b' appears at most 2 times in a row.
40+
41+
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
42+
43+
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
44+
45+
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= repeatLimit <= s.length <= 10<sup>5</sup></code>
50+
* `s` consists of lowercase English letters.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g2101_2200.s2183_count_array_pairs_divisible_by_k;
2+
3+
// #Hard #Array #Math #Number_Theory #2022_06_07_Time_849_ms_(44.54%)_Space_120.9_MB_(39.50%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
@SuppressWarnings("java:S2234")
9+
public class Solution {
10+
public long countPairs(int[] nums, int k) {
11+
long count = 0L;
12+
Map<Integer, Long> map = new HashMap<>();
13+
for (int num : nums) {
14+
int gd = gcd(num, k);
15+
int want = k / gd;
16+
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
17+
if (entry.getKey() % want == 0) {
18+
count += entry.getValue();
19+
}
20+
}
21+
map.put(gd, map.getOrDefault(gd, 0L) + 1L);
22+
}
23+
return count;
24+
}
25+
26+
private int gcd(int a, int b) {
27+
if (a > b) {
28+
return gcd(b, a);
29+
}
30+
if (a == 0) {
31+
return b;
32+
}
33+
return gcd(a, b % a);
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2183\. Count Array Pairs Divisible by K
2+
3+
Hard
4+
5+
Given a **0-indexed** integer array `nums` of length `n` and an integer `k`, return _the **number of pairs**_ `(i, j)` _such that:_
6+
7+
* `0 <= i < j <= n - 1` _and_
8+
* `nums[i] * nums[j]` _is divisible by_ `k`.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,2,3,4,5], k = 2
13+
14+
**Output:** 7
15+
16+
**Explanation:**
17+
18+
The 7 pairs of indices whose corresponding products are divisible by 2 are
19+
20+
(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
21+
22+
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
23+
24+
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1,2,3,4], k = 5
29+
30+
**Output:** 0
31+
32+
**Explanation:** There does not exist any pair of indices whose corresponding product is divisible by 5.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
37+
* <code>1 <= nums[i], k <= 10<sup>5</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2182_construct_string_with_repeat_limit;
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 repeatLimitedString() {
11+
assertThat(new Solution().repeatLimitedString("cczazcc", 3), equalTo("zzcccac"));
12+
}
13+
14+
@Test
15+
void repeatLimitedString2() {
16+
assertThat(new Solution().repeatLimitedString("aababab", 2), equalTo("bbabaa"));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2183_count_array_pairs_divisible_by_k;
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 countPairs() {
11+
assertThat(new Solution().countPairs(new int[] {1, 2, 3, 4, 5}, 2), equalTo(7L));
12+
}
13+
14+
@Test
15+
void countPairs2() {
16+
assertThat(new Solution().countPairs(new int[] {1, 2, 3, 4}, 5), equalTo(0L));
17+
}
18+
}

0 commit comments

Comments
 (0)