Skip to content

Commit ae6e8dc

Browse files
author
Aarzoo
committed
add solutions, explanation and update the README file of the day 2127
1 parent 009a887 commit ae6e8dc

File tree

8 files changed

+406
-0
lines changed

8 files changed

+406
-0
lines changed

Algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
| 2064 | Minimized Maximum of Products Distributed to Any Store | [C++](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.cpp), [Java](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.java), [JavaScript](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.js), [Python](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.py), [Go](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.go) | [Explanation](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Explanation/explanation.md) | Medium |
7070
| 2070 | Most Beautiful Item for Each Query | [C++](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Explanation/explanation.md) | Medium |
7171
| 2116 | Check if a Parentheses String Can Be Valid | [C++](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Code/solution.cpp), [Java](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Code/solution.java), [JavaScript](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Code/solution.js), [Python](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Code/solution.py), [Go](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Code/solution.go) | [Explanation](./Algorithms/src/2116.%20Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/Explanation/explanation.md) | Medium |
72+
| 2127 | Maximum Employees to Be Invited to a Meeting | [C++](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Code/solution.cpp), [Java](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Code/solution.java), [JavaScript](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Code/solution.js), [Python](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Code/solution.py), [Go](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Code/solution.go) | [Explanation](./Algorithms/src/2127.%20Maximum%20Employees%20to%20Be%20Invited%20to%20a%20Meeting/Explanation/explanation.md) | Hard |
7273
| 2134 | Minimum Swaps to Group All 1's Together II | [C++](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.cpp), [Java](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.java), [JavaScript](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.js), [Python](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.py), [Go](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.go) | [Explanation](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Explanation/explanation.md) | Medium |
7374
| 2185 | Counting Words With a Given Prefix | [C++](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Code/solution.cpp), [Java](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Code/solution.java), [JavaScript](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Code/solution.js), [Python](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Code/solution.py), [Go](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Code/solution.go) | [Explanation](./Algorithms/src/2185.%20Counting%20Words%20With%20a%20Given%20Prefix/Explanation/explanation.md) | Easy |
7475
| 2270 | 2270. Number of Ways to Split Array | [C++](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.cpp), [Java](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.java), [JavaScript](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.js), [Python](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.py), [Go](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.go) | [Explanation](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Explanation/explanation.md) | Medium |
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Solution
2+
{
3+
public:
4+
int maximumInvitations(vector<int> &favorite)
5+
{
6+
int n = favorite.size();
7+
vector<int> inDegree(n, 0), chainLengths(n, 0);
8+
vector<bool> visited(n, false);
9+
10+
for (int fav : favorite)
11+
{
12+
inDegree[fav]++;
13+
}
14+
15+
queue<int> q;
16+
for (int i = 0; i < n; ++i)
17+
{
18+
if (inDegree[i] == 0)
19+
{
20+
q.push(i);
21+
}
22+
}
23+
24+
while (!q.empty())
25+
{
26+
int node = q.front();
27+
q.pop();
28+
visited[node] = true;
29+
30+
int next = favorite[node];
31+
chainLengths[next] = chainLengths[node] + 1;
32+
if (--inDegree[next] == 0)
33+
{
34+
q.push(next);
35+
}
36+
}
37+
38+
int maxCycle = 0, totalChains = 0;
39+
for (int i = 0; i < n; ++i)
40+
{
41+
if (!visited[i])
42+
{
43+
int current = i, cycleLength = 0;
44+
while (!visited[current])
45+
{
46+
visited[current] = true;
47+
current = favorite[current];
48+
cycleLength++;
49+
}
50+
51+
if (cycleLength == 2)
52+
{
53+
totalChains += 2 + chainLengths[i] + chainLengths[favorite[i]];
54+
}
55+
else
56+
{
57+
maxCycle = max(maxCycle, cycleLength);
58+
}
59+
}
60+
}
61+
62+
return max(maxCycle, totalChains);
63+
}
64+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
func maximumInvitations(favorite []int) int {
2+
n := len(favorite)
3+
inDegree := make([]int, n)
4+
chainLengths := make([]int, n)
5+
visited := make([]bool, n)
6+
7+
for _, fav := range favorite {
8+
inDegree[fav]++
9+
}
10+
11+
queue := []int{}
12+
for i := 0; i < n; i++ {
13+
if inDegree[i] == 0 {
14+
queue = append(queue, i)
15+
}
16+
}
17+
18+
for len(queue) > 0 {
19+
node := queue[0]
20+
queue = queue[1:]
21+
visited[node] = true
22+
23+
next := favorite[node]
24+
chainLengths[next] = chainLengths[node] + 1
25+
inDegree[next]--
26+
if inDegree[next] == 0 {
27+
queue = append(queue, next)
28+
}
29+
}
30+
31+
maxCycle, totalChains := 0, 0
32+
for i := 0; i < n; i++ {
33+
if !visited[i] {
34+
current, cycleLength := i, 0
35+
for !visited[current] {
36+
visited[current] = true
37+
current = favorite[current]
38+
cycleLength++
39+
}
40+
41+
if cycleLength == 2 {
42+
totalChains += 2 + chainLengths[i] + chainLengths[favorite[i]]
43+
} else {
44+
if cycleLength > maxCycle {
45+
maxCycle = cycleLength
46+
}
47+
}
48+
}
49+
}
50+
51+
if maxCycle > totalChains {
52+
return maxCycle
53+
}
54+
return totalChains
55+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public int maximumInvitations(int[] favorite) {
3+
int n = favorite.length;
4+
int[] inDegree = new int[n];
5+
int[] chainLengths = new int[n];
6+
boolean[] visited = new boolean[n];
7+
8+
for (int fav : favorite) {
9+
inDegree[fav]++;
10+
}
11+
12+
Queue<Integer> queue = new LinkedList<>();
13+
for (int i = 0; i < n; ++i) {
14+
if (inDegree[i] == 0) {
15+
queue.offer(i);
16+
}
17+
}
18+
19+
while (!queue.isEmpty()) {
20+
int node = queue.poll();
21+
visited[node] = true;
22+
23+
int next = favorite[node];
24+
chainLengths[next] = chainLengths[node] + 1;
25+
if (--inDegree[next] == 0) {
26+
queue.offer(next);
27+
}
28+
}
29+
30+
int maxCycle = 0, totalChains = 0;
31+
for (int i = 0; i < n; ++i) {
32+
if (!visited[i]) {
33+
int current = i, cycleLength = 0;
34+
while (!visited[current]) {
35+
visited[current] = true;
36+
current = favorite[current];
37+
cycleLength++;
38+
}
39+
40+
if (cycleLength == 2) {
41+
totalChains += 2 + chainLengths[i] + chainLengths[favorite[i]];
42+
} else {
43+
maxCycle = Math.max(maxCycle, cycleLength);
44+
}
45+
}
46+
}
47+
48+
return Math.max(maxCycle, totalChains);
49+
}
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var maximumInvitations = function (favorite) {
2+
const n = favorite.length;
3+
const inDegree = new Array(n).fill(0);
4+
const chainLengths = new Array(n).fill(0);
5+
const visited = new Array(n).fill(false);
6+
7+
favorite.forEach((fav) => inDegree[fav]++);
8+
9+
const queue = [];
10+
for (let i = 0; i < n; i++) {
11+
if (inDegree[i] === 0) {
12+
queue.push(i);
13+
}
14+
}
15+
16+
while (queue.length) {
17+
const node = queue.shift();
18+
visited[node] = true;
19+
20+
const next = favorite[node];
21+
chainLengths[next] = chainLengths[node] + 1;
22+
if (--inDegree[next] === 0) {
23+
queue.push(next);
24+
}
25+
}
26+
27+
let maxCycle = 0,
28+
totalChains = 0;
29+
for (let i = 0; i < n; i++) {
30+
if (!visited[i]) {
31+
let current = i,
32+
cycleLength = 0;
33+
while (!visited[current]) {
34+
visited[current] = true;
35+
current = favorite[current];
36+
cycleLength++;
37+
}
38+
39+
if (cycleLength === 2) {
40+
totalChains += 2 + chainLengths[i] + chainLengths[favorite[i]];
41+
} else {
42+
maxCycle = Math.max(maxCycle, cycleLength);
43+
}
44+
}
45+
}
46+
47+
return Math.max(maxCycle, totalChains);
48+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def maximumInvitations(self, favorite: List[int]) -> int:
3+
n = len(favorite)
4+
inDegree = [0] * n
5+
chainLengths = [0] * n
6+
visited = [False] * n
7+
8+
for fav in favorite:
9+
inDegree[fav] += 1
10+
11+
queue = deque(i for i in range(n) if inDegree[i] == 0)
12+
while queue:
13+
node = queue.popleft()
14+
visited[node] = True
15+
16+
next_node = favorite[node]
17+
chainLengths[next_node] = chainLengths[node] + 1
18+
inDegree[next_node] -= 1
19+
if inDegree[next_node] == 0:
20+
queue.append(next_node)
21+
22+
maxCycle = 0
23+
totalChains = 0
24+
for i in range(n):
25+
if not visited[i]:
26+
current, cycleLength = i, 0
27+
while not visited[current]:
28+
visited[current] = True
29+
current = favorite[current]
30+
cycleLength += 1
31+
32+
if cycleLength == 2:
33+
totalChains += 2 + chainLengths[i] + chainLengths[favorite[i]]
34+
else:
35+
maxCycle = max(maxCycle, cycleLength)
36+
37+
return max(maxCycle, totalChains)

0 commit comments

Comments
 (0)