Skip to content

Commit 2329c58

Browse files
committed
Add solution #2127
1 parent baefd55 commit 2329c58

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
2099|[Find Subsequence of Length K With the Largest Sum](./2099-find-subsequence-of-length-k-with-the-largest-sum.js)|Medium|
450450
2114|[Maximum Number of Words Found in Sentences](./2114-maximum-number-of-words-found-in-sentences.js)|Easy|
451451
2116|[Check if a Parentheses String Can Be Valid](./2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
452+
2127|[Maximum Employees to Be Invited to a Meeting](./2127-maximum-employees-to-be-invited-to-a-meeting.js)|Hard|
452453
2129|[Capitalize the Title](./2129-capitalize-the-title.js)|Easy|
453454
2130|[Maximum Twin Sum of a Linked List](./2130-maximum-twin-sum-of-a-linked-list.js)|Medium|
454455
2154|[Keep Multiplying Found Values by Two](./2154-keep-multiplying-found-values-by-two.js)|Easy|
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 2127. Maximum Employees to Be Invited to a Meeting
3+
* https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/
4+
* Difficulty: Hard
5+
*
6+
* A company is organizing a meeting and has a list of n employees, waiting to be invited.
7+
* They have arranged for a large circular table, capable of seating any number of employees.
8+
*
9+
* The employees are numbered from 0 to n - 1. Each employee has a favorite person and they
10+
* will attend the meeting only if they can sit next to their favorite person at the table.
11+
* The favorite person of an employee is not themself.
12+
*
13+
* Given a 0-indexed integer array favorite, where favorite[i] denotes the favorite person of
14+
* the ith employee, return the maximum number of employees that can be invited to the meeting.
15+
*/
16+
17+
/**
18+
* @param {number[]} fav
19+
* @return {number}
20+
*/
21+
var maximumInvitations = function(fav) {
22+
const n = fav.length;
23+
const graph = new Array(n).fill().map(() => []);
24+
const seen = new Set();
25+
let result = 0;
26+
27+
for (let i = 0; i < n; i++) {
28+
graph[fav[i]].push(i);
29+
}
30+
31+
for (let i = 0; i < n; i++) {
32+
result += i < fav[i] || fav[fav[i]] != i ? 0 : dfs(i, fav[i]) + dfs(fav[i], i);
33+
}
34+
35+
for (let i = 0; i < n; i++) {
36+
let j = i;
37+
38+
if (!seen.has(i)) {
39+
const m = new Map();
40+
41+
for (let k = 0; !seen.has(j); k++) {
42+
seen.add(j);
43+
m.set(j, k);
44+
j = fav[j];
45+
}
46+
47+
result = Math.max(result, m.size - m.get(j) || 0);
48+
}
49+
}
50+
51+
function dfs(i, j) {
52+
return graph[i].reduce((max, n) => Math.max(max, n == j ? 0 : dfs(n, j)), 0) + 1;
53+
};
54+
55+
return result;
56+
};

0 commit comments

Comments
 (0)