Skip to content

Commit 89adc8d

Browse files
authored
Added tasks 2358, 2359, 2360.
1 parent e650fc5 commit 89adc8d

File tree

10 files changed

+258
-0
lines changed

10 files changed

+258
-0
lines changed

README.md

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

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2360 |[Longest Cycle in a Graph](src/main/java/g2301_2400/s2360_longest_cycle_in_a_graph/Solution.java)| Hard | Depth_First_Search, Graph, Topological_Sort | 37 | 90.19
1852+
| 2359 |[Find Closest Node to Given Two Nodes](src/main/java/g2301_2400/s2359_find_closest_node_to_given_two_nodes/Solution.java)| Medium | Depth_First_Search, Graph | 18 | 89.33
1853+
| 2358 |[Maximum Number of Groups Entering a Competition](src/main/java/g2301_2400/s2358_maximum_number_of_groups_entering_a_competition/Solution.java)| Medium | Array, Math, Binary_Search, Greedy | 0 | 100.00
18511854
| 2357 |[Make Array Zero by Subtracting Equal Amounts](src/main/java/g2301_2400/s2357_make_array_zero_by_subtracting_equal_amounts/Solution.java)| Easy | Array, Hash_Table, Sorting, Heap_Priority_Queue, Simulation | 1 | 98.24
18521855
| 2354 |[Number of Excellent Pairs](src/main/java/g2301_2400/s2354_number_of_excellent_pairs/Solution.java)| Hard | Array, Hash_Table, Binary_Search, Bit_Manipulation | 80 | 86.77
18531856
| 2353 |[Design a Food Rating System](src/main/java/g2301_2400/s2353_design_a_food_rating_system/FoodRatings.java)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 364 | 83.35
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g2301_2400.s2358_maximum_number_of_groups_entering_a_competition;
2+
3+
// #Medium #Array #Math #Binary_Search #Greedy
4+
// #2022_08_14_Time_0_ms_(100.00%)_Space_70.5_MB_(27.33%)
5+
6+
public class Solution {
7+
public int maximumGroups(int[] grades) {
8+
int len = grades.length;
9+
return (int) (-1 + Math.sqrt(1D + 8 * len)) / 2;
10+
}
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2358\. Maximum Number of Groups Entering a Competition
2+
3+
Medium
4+
5+
You are given a positive integer array `grades` which represents the grades of students in a university. You would like to enter **all** these students into a competition in **ordered** non-empty groups, such that the ordering meets the following conditions:
6+
7+
* The sum of the grades of students in the <code>i<sup>th</sup></code> group is **less than** the sum of the grades of students in the <code>(i + 1)<sup>th</sup></code> group, for all groups (except the last).
8+
* The total number of students in the <code>i<sup>th</sup></code> group is **less than** the total number of students in the <code>(i + 1)<sup>th</sup></code> group, for all groups (except the last).
9+
10+
Return _the **maximum** number of groups that can be formed_.
11+
12+
**Example 1:**
13+
14+
**Input:** grades = [10,6,12,7,3,5]
15+
16+
**Output:** 3
17+
18+
**Explanation:** The following is a possible way to form 3 groups of students:
19+
20+
- 1<sup>st</sup> group has the students with grades = [12]. Sum of grades: 12. Student count: 1
21+
22+
- 2<sup>nd</sup> group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
23+
24+
- 3<sup>rd</sup> group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
25+
26+
It can be shown that it is not possible to form more than 3 groups.
27+
28+
**Example 2:**
29+
30+
**Input:** grades = [8,8]
31+
32+
**Output:** 1
33+
34+
**Explanation:** We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= grades.length <= 10<sup>5</sup></code>
39+
* <code>1 <= grades[i] <= 10<sup>5</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2301_2400.s2359_find_closest_node_to_given_two_nodes;
2+
3+
// #Medium #Depth_First_Search #Graph #2022_08_14_Time_18_ms_(89.33%)_Space_112.3_MB_(69.17%)
4+
5+
public class Solution {
6+
public int closestMeetingNode(int[] edges, int node1, int node2) {
7+
final int n = edges.length;
8+
final Integer[] m1 = new Integer[n];
9+
final Integer[] m2 = new Integer[n];
10+
dfs(edges, m1, node1);
11+
dfs(edges, m2, node2);
12+
int index = -1;
13+
int dist = Integer.MAX_VALUE;
14+
for (int i = 0; i < n; i++) {
15+
if (m1[i] != null && m2[i] != null && dist > Math.max(m1[i], m2[i])) {
16+
dist = Math.max(m1[i], m2[i]);
17+
index = i;
18+
}
19+
}
20+
return index;
21+
}
22+
23+
private void dfs(int[] edges, Integer[] memo, int node) {
24+
int dist = 0;
25+
while (node != -1 && memo[node] == null) {
26+
memo[node] = dist++;
27+
node = edges[node];
28+
}
29+
}
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2359\. Find Closest Node to Given Two Nodes
2+
3+
Medium
4+
5+
You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge.
6+
7+
The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from `i`, then `edges[i] == -1`.
8+
9+
You are also given two integers `node1` and `node2`.
10+
11+
Return _the **index** of the node that can be reached from both_ `node1` _and_ `node2`_, such that the **maximum** between the distance from_ `node1` _to that node, and from_ `node2` _to that node is **minimized**_. If there are multiple answers, return the node with the **smallest** index, and if no possible answer exists, return `-1`.
12+
13+
Note that `edges` may contain cycles.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png)
18+
19+
**Input:** edges = [2,2,3,-1], node1 = 0, node2 = 1
20+
21+
**Output:** 2
22+
23+
**Explanation:** The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
24+
25+
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png)
30+
31+
**Input:** edges = [1,2,-1], node1 = 0, node2 = 2
32+
33+
**Output:** 2
34+
35+
**Explanation:** The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
36+
37+
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
38+
39+
**Constraints:**
40+
41+
* `n == edges.length`
42+
* <code>2 <= n <= 10<sup>5</sup></code>
43+
* `-1 <= edges[i] < n`
44+
* `edges[i] != i`
45+
* `0 <= node1, node2 < n`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2301_2400.s2360_longest_cycle_in_a_graph;
2+
3+
// #Hard #Depth_First_Search #Graph #Topological_Sort
4+
// #2022_08_14_Time_37_ms_(90.19%)_Space_102.8_MB_(72.92%)
5+
6+
public class Solution {
7+
public int longestCycle(int[] edges) {
8+
int n = edges.length;
9+
boolean[] vis = new boolean[n];
10+
boolean[] dfsvis = new boolean[n];
11+
int[] path = new int[n];
12+
int maxLength = -1;
13+
for (int i = 0; i < n; i++) {
14+
if (!vis[i]) {
15+
path[i] = 1;
16+
maxLength = Math.max(maxLength, dfs(i, 1, path, vis, dfsvis, edges));
17+
}
18+
}
19+
return maxLength;
20+
}
21+
22+
private int dfs(
23+
int node, int pathLength, int[] path, boolean[] vis, boolean[] dfsvis, int[] edges) {
24+
vis[node] = true;
25+
dfsvis[node] = true;
26+
int length = -1;
27+
if (edges[node] != -1 && !vis[edges[node]]) {
28+
path[edges[node]] = pathLength + 1;
29+
length = dfs(edges[node], pathLength + 1, path, vis, dfsvis, edges);
30+
} else if (edges[node] != -1 && dfsvis[edges[node]]) {
31+
length = pathLength - path[edges[node]] + 1;
32+
}
33+
dfsvis[node] = false;
34+
return length;
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2360\. Longest Cycle in a Graph
2+
3+
Hard
4+
5+
You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge.
6+
7+
The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from node `i`, then `edges[i] == -1`.
8+
9+
Return _the length of the **longest** cycle in the graph_. If no cycle exists, return `-1`.
10+
11+
A cycle is a path that starts and ends at the **same** node.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/06/08/graph4drawio-5.png)
16+
17+
**Input:** edges = [3,3,4,2,3]
18+
19+
**Output:** 3
20+
21+
**Explanation:** The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
22+
23+
The length of this cycle is 3, so 3 is returned.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-1.png)
28+
29+
**Input:** edges = [2,-1,3,1]
30+
31+
**Output:** -1
32+
33+
**Explanation:** There are no cycles in this graph.
34+
35+
**Constraints:**
36+
37+
* `n == edges.length`
38+
* <code>2 <= n <= 10<sup>5</sup></code>
39+
* `-1 <= edges[i] < n`
40+
* `edges[i] != i`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2358_maximum_number_of_groups_entering_a_competition;
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 maximumGroups() {
11+
assertThat(new Solution().maximumGroups(new int[] {10, 6, 12, 7, 3, 5}), equalTo(3));
12+
}
13+
14+
@Test
15+
void maximumGroups2() {
16+
assertThat(new Solution().maximumGroups(new int[] {8, 8}), equalTo(1));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2359_find_closest_node_to_given_two_nodes;
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 closestMeetingNode() {
11+
assertThat(new Solution().closestMeetingNode(new int[] {2, 2, 3, -1}, 0, 1), equalTo(2));
12+
}
13+
14+
@Test
15+
void closestMeetingNode2() {
16+
assertThat(new Solution().closestMeetingNode(new int[] {1, 2, -1}, 0, 2), equalTo(2));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2360_longest_cycle_in_a_graph;
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 longestCycle() {
11+
assertThat(new Solution().longestCycle(new int[] {3, 3, 4, 2, 3}), equalTo(3));
12+
}
13+
14+
@Test
15+
void longestCycle2() {
16+
assertThat(new Solution().longestCycle(new int[] {2, -1, 3, 1}), equalTo(-1));
17+
}
18+
}

0 commit comments

Comments
 (0)