Skip to content

Commit 2003f8e

Browse files
committed
Add solution #1942
1 parent eecde51 commit 2003f8e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@
17631763
1938|[Maximum Genetic Difference Query](./solutions/1938-maximum-genetic-difference-query.js)|Hard|
17641764
1940|[Longest Common Subsequence Between Sorted Arrays](./solutions/1940-longest-common-subsequence-between-sorted-arrays.js)|Medium|
17651765
1941|[Check if All Characters Have Equal Number of Occurrences](./solutions/1941-check-if-all-characters-have-equal-number-of-occurrences.js)|Easy|
1766+
1942|[The Number of the Smallest Unoccupied Chair](./solutions/1942-the-number-of-the-smallest-unoccupied-chair.js)|Medium|
17661767
1943|[Describe the Painting](./solutions/1943-describe-the-painting.js)|Medium|
17671768
1944|[Number of Visible People in a Queue](./solutions/1944-number-of-visible-people-in-a-queue.js)|Hard|
17681769
1945|[Sum of Digits of String After Convert](./solutions/1945-sum-of-digits-of-string-after-convert.js)|Easy|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 1942. The Number of the Smallest Unoccupied Chair
3+
* https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/
4+
* Difficulty: Medium
5+
*
6+
* There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite
7+
* number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at
8+
* the party, they sit on the unoccupied chair with the smallest number.
9+
* - For example, if chairs 0, 1, and 5 are occupied when a friend comes, they will sit on chair
10+
* number 2.
11+
*
12+
* When a friend leaves the party, their chair becomes unoccupied at the moment they leave.
13+
* If another friend arrives at that same moment, they can sit in that chair.
14+
*
15+
* You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi],
16+
* indicating the arrival and leaving times of the ith friend respectively, and an integer
17+
* targetFriend. All arrival times are distinct.
18+
*
19+
* Return the chair number that the friend numbered targetFriend will sit on.
20+
*/
21+
22+
/**
23+
* @param {number[][]} times
24+
* @param {number} targetFriend
25+
* @return {number}
26+
*/
27+
var smallestChair = function(times, targetFriend) {
28+
const events = [];
29+
30+
for (let i = 0; i < times.length; i++) {
31+
events.push([times[i][0], 'arrive', i]);
32+
events.push([times[i][1], 'leave', i]);
33+
}
34+
35+
events.sort((a, b) => a[0] - b[0] || (a[1] === 'leave' ? -1 : 1));
36+
37+
const availableChairs = [];
38+
const occupiedChairs = new Map();
39+
let nextChair = 0;
40+
41+
for (const [time, eventType, friendId] of events) {
42+
if (eventType === 'leave') {
43+
const chairNumber = occupiedChairs.get(friendId);
44+
occupiedChairs.delete(friendId);
45+
availableChairs.push(chairNumber);
46+
availableChairs.sort((a, b) => a - b);
47+
} else {
48+
let assignedChair;
49+
if (availableChairs.length > 0) {
50+
assignedChair = availableChairs.shift();
51+
} else {
52+
assignedChair = nextChair++;
53+
}
54+
55+
occupiedChairs.set(friendId, assignedChair);
56+
57+
if (friendId === targetFriend) {
58+
return assignedChair;
59+
}
60+
}
61+
}
62+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy