Skip to content

Commit a51661c

Browse files
authored
Added tasks 2511, 2512, 2513, 2514, 2515
1 parent e7baaab commit a51661c

File tree

16 files changed

+561
-0
lines changed

16 files changed

+561
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,11 @@ implementation 'com.github.javadev:leetcode-in-java:1.19'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2515 |[Shortest Distance to Target String in a Circular Array](src/main/java/g2501_2600/s2515_shortest_distance_to_target_string_in_a_circular_array/Solution.java)| Easy | Array, String | 1 | 62.21
1852+
| 2514 |[Count Anagrams](src/main/java/g2501_2600/s2514_count_anagrams/Solution.java)| Hard | String, Hash_Table, Math, Counting, Combinatorics | 22 | 100.00
1853+
| 2513 |[Minimize the Maximum of Two Arrays](src/main/java/g2501_2600/s2513_minimize_the_maximum_of_two_arrays/Solution.java)| Medium | Math, Binary_Search, Number_Theory | 0 | 100.00
1854+
| 2512 |[Reward Top K Students](src/main/java/g2501_2600/s2512_reward_top_k_students/Solution.java)| Medium | Array, String, Hash_Table, Sorting, Heap_Priority_Queue | 72 | 94.76
1855+
| 2511 |[Maximum Enemy Forts That Can Be Captured](src/main/java/g2501_2600/s2511_maximum_enemy_forts_that_can_be_captured/Solution.java)| Easy | Array, Two_Pointers | 0 | 100.00
18511856
| 2509 |[Cycle Length Queries in a Tree](src/main/java/g2501_2600/s2509_cycle_length_queries_in_a_tree/Solution.java)| Hard | Tree, Binary_Tree | 12 | 99.26
18521857
| 2508 |[Add Edges to Make Degrees of All Nodes Even](src/main/java/g2501_2600/s2508_add_edges_to_make_degrees_of_all_nodes_even/Solution.java)| Hard | Hash_Table, Graph | 33 | 100.00
18531858
| 2507 |[Smallest Value After Replacing With Sum of Prime Factors](src/main/java/g2501_2600/s2507_smallest_value_after_replacing_with_sum_of_prime_factors/Solution.java)| Medium | Math, Number_Theory | 1 | 84.27
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2501_2600.s2511_maximum_enemy_forts_that_can_be_captured;
2+
3+
// #Easy #Array #Two_Pointers #2023_03_21_Time_0_ms_(100.00%)_Space_40.9_MB_(19.40%)
4+
5+
public class Solution {
6+
public int captureForts(int[] forts) {
7+
int maxCapture = 0;
8+
int n = forts.length;
9+
int i = 0;
10+
while (i < n) {
11+
if (forts[i] == 1) {
12+
int currMax = 0;
13+
i++;
14+
while (i < n && forts[i] == 0) {
15+
currMax++;
16+
i++;
17+
}
18+
if (i < n && forts[i] == -1) {
19+
maxCapture = Math.max(maxCapture, currMax);
20+
}
21+
i--;
22+
} else if (forts[i] == -1) {
23+
int currMax = 0;
24+
i++;
25+
while (i < n && forts[i] == 0) {
26+
currMax++;
27+
i++;
28+
}
29+
if (i < n && forts[i] == 1) {
30+
maxCapture = Math.max(maxCapture, currMax);
31+
}
32+
i--;
33+
}
34+
i++;
35+
}
36+
return maxCapture;
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2511\. Maximum Enemy Forts That Can Be Captured
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `forts` of length `n` representing the positions of several forts. `forts[i]` can be `-1`, `0`, or `1` where:
6+
7+
* `-1` represents there is **no fort** at the <code>i<sup>th</sup></code> position.
8+
* `0` indicates there is an **enemy** fort at the <code>i<sup>th</sup></code> position.
9+
* `1` indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.
10+
11+
Now you have decided to move your army from one of your forts at position `i` to an empty position `j` such that:
12+
13+
* `0 <= i, j <= n - 1`
14+
* The army travels over enemy forts **only**. Formally, for all `k` where `min(i,j) < k < max(i,j)`, `forts[k] == 0.`
15+
16+
While moving the army, all the enemy forts that come in the way are **captured**.
17+
18+
Return _the **maximum** number of enemy forts that can be captured_. In case it is **impossible** to move your army, or you do not have any fort under your command, return `0`_._
19+
20+
**Example 1:**
21+
22+
**Input:** forts = [1,0,0,-1,0,0,0,0,1]
23+
24+
**Output:** 4
25+
26+
**Explanation:**
27+
28+
- Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
29+
30+
- Moving the army from position 8 to position 3 captures 4 enemy forts.
31+
32+
Since 4 is the maximum number of enemy forts that can be captured, we return 4.
33+
34+
**Example 2:**
35+
36+
**Input:** forts = [0,0,1,-1]
37+
38+
**Output:** 0
39+
40+
**Explanation:** Since no enemy fort can be captured, 0 is returned.
41+
42+
**Constraints:**
43+
44+
* `1 <= forts.length <= 1000`
45+
* `-1 <= forts[i] <= 1`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g2501_2600.s2512_reward_top_k_students;
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #Heap_Priority_Queue
4+
// #2023_03_21_Time_72_ms_(94.76%)_Space_51.1_MB_(21.67%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.PriorityQueue;
10+
11+
public class Solution {
12+
public List<Integer> topStudents(
13+
String[] positiveFeedback,
14+
String[] negativeFeedback,
15+
String[] report,
16+
int[] studentId,
17+
int k) {
18+
HashMap<String, Integer> feedback = new HashMap<>();
19+
for (String s : positiveFeedback) {
20+
feedback.put(s, 3);
21+
}
22+
for (String s : negativeFeedback) {
23+
feedback.put(s, -1);
24+
}
25+
PriorityQueue<Student> pq =
26+
new PriorityQueue<>(
27+
(a, b) -> {
28+
int result = Integer.compare(a.points, b.points);
29+
return result == 0 ? Integer.compare(a.id, b.id) : -result;
30+
});
31+
for (int i = 0; i < report.length; i++) {
32+
String[] split = report[i].split(" ");
33+
int sum = 0;
34+
for (String subStr : split) {
35+
sum += feedback.getOrDefault(subStr, 0);
36+
}
37+
pq.offer(new Student(studentId[i], sum));
38+
}
39+
List<Integer> result = new ArrayList<>();
40+
while (!pq.isEmpty() && k-- > 0) {
41+
result.add(pq.poll().id);
42+
}
43+
return result;
44+
}
45+
46+
private static class Student {
47+
int points;
48+
int id;
49+
50+
public Student(int id, int points) {
51+
this.id = id;
52+
this.points = points;
53+
}
54+
}
55+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2512\. Reward Top K Students
2+
3+
Medium
4+
5+
You are given two string arrays `positive_feedback` and `negative_feedback`, containing the words denoting positive and negative feedback, respectively. Note that **no** word is both positive and negative.
6+
7+
Initially every student has `0` points. Each positive word in a feedback report **increases** the points of a student by `3`, whereas each negative word **decreases** the points by `1`.
8+
9+
You are given `n` feedback reports, represented by a **0-indexed** string array `report` and a **0-indexed** integer array `student_id`, where `student_id[i]` represents the ID of the student who has received the feedback report `report[i]`. The ID of each student is **unique**.
10+
11+
Given an integer `k`, return _the top_ `k` _students after ranking them in **non-increasing** order by their points_. In case more than one student has the same points, the one with the lower ID ranks higher.
12+
13+
**Example 1:**
14+
15+
**Input:** positive\_feedback = ["smart","brilliant","studious"], negative\_feedback = ["not"], report = ["this student is studious","the student is smart"], student\_id = [1,2], k = 2
16+
17+
**Output:** [1,2]
18+
19+
**Explanation:** Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.
20+
21+
**Example 2:**
22+
23+
**Input:** positive\_feedback = ["smart","brilliant","studious"], negative\_feedback = ["not"], report = ["this student is not studious","the student is smart"], student\_id = [1,2], k = 2
24+
25+
**Output:** [2,1]
26+
27+
**Explanation:**
28+
29+
- The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points.
30+
31+
- The student with ID 2 has 1 positive feedback, so he has 3 points. Since student 2 has more points, [2,1] is returned.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= positive_feedback.length, negative_feedback.length <= 10<sup>4</sup></code>
36+
* `1 <= positive_feedback[i].length, negative_feedback[j].length <= 100`
37+
* Both `positive_feedback[i]` and `negative_feedback[j]` consists of lowercase English letters.
38+
* No word is present in both `positive_feedback` and `negative_feedback`.
39+
* `n == report.length == student_id.length`
40+
* <code>1 <= n <= 10<sup>4</sup></code>
41+
* `report[i]` consists of lowercase English letters and spaces `' '`.
42+
* There is a single space between consecutive words of `report[i]`.
43+
* `1 <= report[i].length <= 100`
44+
* <code>1 <= student_id[i] <= 10<sup>9</sup></code>
45+
* All the values of `student_id[i]` are **unique**.
46+
* `1 <= k <= n`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2501_2600.s2513_minimize_the_maximum_of_two_arrays;
2+
3+
// #Medium #Math #Binary_Search #Number_Theory
4+
// #2023_03_21_Time_0_ms_(100.00%)_Space_39.4_MB_(35.80%)
5+
6+
public class Solution {
7+
private int gcd(int a, int b) {
8+
if (b == 0) {
9+
return a;
10+
}
11+
return gcd(b, a % b);
12+
}
13+
14+
public int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {
15+
long lo = 1;
16+
long hi = (int) 10e10;
17+
if (divisor2 == 0) {
18+
divisor2 = 1;
19+
}
20+
long lcm = ((long) divisor1 * (long) divisor2) / gcd(divisor1, divisor2);
21+
while (lo < hi) {
22+
long mi = lo + (hi - lo) / 2;
23+
int cnt1 = (int) (mi - mi / divisor1);
24+
int cnt2 = (int) (mi - mi / divisor2);
25+
int cntAll = (int) (mi - mi / lcm);
26+
if (cnt1 < uniqueCnt1 || cnt2 < uniqueCnt2 || cntAll < uniqueCnt1 + uniqueCnt2) {
27+
lo = mi + 1;
28+
} else {
29+
hi = mi;
30+
}
31+
}
32+
return (int) lo;
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2513\. Minimize the Maximum of Two Arrays
2+
3+
Medium
4+
5+
We have two arrays `arr1` and `arr2` which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:
6+
7+
* `arr1` contains `uniqueCnt1` **distinct** positive integers, each of which is **not divisible** by `divisor1`.
8+
* `arr2` contains `uniqueCnt2` **distinct** positive integers, each of which is **not divisible** by `divisor2`.
9+
* **No** integer is present in both `arr1` and `arr2`.
10+
11+
Given `divisor1`, `divisor2`, `uniqueCnt1`, and `uniqueCnt2`, return _the **minimum possible maximum** integer that can be present in either array_.
12+
13+
**Example 1:**
14+
15+
**Input:** divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
We can distribute the first 4 natural numbers into arr1 and arr2.
22+
23+
arr1 = [1] and arr2 = [2,3,4].
24+
25+
We can see that both arrays satisfy all the conditions.
26+
27+
Since the maximum value is 4, we return it.
28+
29+
**Example 2:**
30+
31+
**Input:** divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
Here arr1 = [1,2], and arr2 = [3] satisfy all conditions.
38+
39+
Since the maximum value is 3, we return it.
40+
41+
**Example 3:**
42+
43+
**Input:** divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6].
50+
51+
It can be shown that it is not possible to obtain a lower maximum satisfying all conditions.
52+
53+
**Constraints:**
54+
55+
* <code>2 <= divisor1, divisor2 <= 10<sup>5</sup></code>
56+
* <code>1 <= uniqueCnt1, uniqueCnt2 < 10<sup>9</sup></code>
57+
* <code>2 <= uniqueCnt1 + uniqueCnt2 <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g2501_2600.s2514_count_anagrams;
2+
3+
// #Hard #String #Hash_Table #Math #Counting #Combinatorics
4+
// #2023_03_21_Time_22_ms_(100.00%)_Space_43_MB_(98.15%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private static final int MOD = (int) 1e9 + 7;
10+
11+
public int countAnagrams(String s) {
12+
var charArray = s.toCharArray();
13+
long ans = 1L;
14+
long mul = 1L;
15+
var cnt = new int[26];
16+
int j = 0;
17+
for (char c : charArray) {
18+
if (c == ' ') {
19+
Arrays.fill(cnt, 0);
20+
j = 0;
21+
} else {
22+
mul = mul * ++cnt[c - 'a'] % MOD;
23+
ans = ans * ++j % MOD;
24+
}
25+
}
26+
return (int) (ans * pow(mul, MOD - 2) % MOD);
27+
}
28+
29+
private long pow(long x, int n) {
30+
var res = 1L;
31+
for (; n > 0; n /= 2) {
32+
if (n % 2 > 0) {
33+
res = res * x % MOD;
34+
}
35+
x = x * x % MOD;
36+
}
37+
return res;
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2514\. Count Anagrams
2+
3+
Hard
4+
5+
You are given a string `s` containing one or more words. Every consecutive pair of words is separated by a single space `' '`.
6+
7+
A string `t` is an **anagram** of string `s` if the <code>i<sup>th</sup></code> word of `t` is a **permutation** of the <code>i<sup>th</sup></code> word of `s`.
8+
9+
* For example, `"acb dfe"` is an anagram of `"abc def"`, but `"def cab"` and `"adc bef"` are not.
10+
11+
Return _the number of **distinct anagrams** of_ `s`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "too hot"
16+
17+
**Output:** 18
18+
19+
**Explanation:** Some of the anagrams of the given string are "too hot", "oot hot", "oto toh", "too toh", and "too oht".
20+
21+
**Example 2:**
22+
23+
**Input:** s = "aa"
24+
25+
**Output:** 1
26+
27+
**Explanation:** There is only one anagram possible for the given string.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= s.length <= 10<sup>5</sup></code>
32+
* `s` consists of lowercase English letters and spaces `' '`.
33+
* There is single space between consecutive words.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2501_2600.s2515_shortest_distance_to_target_string_in_a_circular_array;
2+
3+
// #Easy #Array #String #2023_03_21_Time_1_ms_(62.21%)_Space_42.6_MB_(84.53%)
4+
5+
public class Solution {
6+
public int closetTarget(String[] words, String target, int startIndex) {
7+
int n = words.length;
8+
if (words[startIndex].equals(target)) {
9+
return 0;
10+
}
11+
int ld = -1;
12+
int rd;
13+
int ans = Integer.MAX_VALUE;
14+
for (int i = (startIndex + 1) % n; i != startIndex; i = (i + 1) % n) {
15+
if (words[i].equals(target)) {
16+
ld = i > startIndex ? startIndex + (n - i) : startIndex - i;
17+
rd = i > startIndex ? i - startIndex : n - startIndex + i;
18+
ans = Math.min(ans, Math.min(ld, rd));
19+
}
20+
}
21+
if (ld == -1) {
22+
return -1;
23+
}
24+
return ans;
25+
}
26+
}

0 commit comments

Comments
 (0)