java.lang.Object
g1901_2000.s1942_the_number_of_the_smallest_unoccupied_chair.Solution

public class Solution extends Object
1942 - The Number of the Smallest Unoccupied Chair.<p>Medium</p> <p>There is a party where <code>n</code> friends numbered from <code>0</code> to <code>n - 1</code> are attending. There is an <strong>infinite</strong> number of chairs in this party that are numbered from <code>0</code> to <code>infinity</code>. When a friend arrives at the party, they sit on the unoccupied chair with the <strong>smallest number</strong>.</p> <ul> <li>For example, if chairs <code>0</code>, <code>1</code>, and <code>5</code> are occupied when a friend comes, they will sit on chair number <code>2</code>.</li> </ul> <p>When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.</p> <p>You are given a <strong>0-indexed</strong> 2D integer array <code>times</code> where <code>times[i] = [arrival<sub>i</sub>, leaving<sub>i</sub>]</code>, indicating the arrival and leaving times of the <code>i<sup>th</sup></code> friend respectively, and an integer <code>targetFriend</code>. All arrival times are <strong>distinct</strong>.</p> <p>Return <em>the <strong>chair number</strong> that the friend numbered</em> <code>targetFriend</code> <em>will sit on</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> times = [[1,4],[2,3],[4,6]], targetFriend = 1</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Friend 0 arrives at time 1 and sits on chair 0.</p> </li> <li> <p>Friend 1 arrives at time 2 and sits on chair 1.</p> </li> <li> <p>Friend 1 leaves at time 3 and chair 1 becomes empty.</p> </li> <li> <p>Friend 0 leaves at time 4 and chair 0 becomes empty.</p> </li> <li> <p>Friend 2 arrives at time 4 and sits on chair 0.</p> </li> </ul> <p>Since friend 1 sat on chair 1, we return 1.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> times = [[3,10],[1,5],[2,6]], targetFriend = 0</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Friend 1 arrives at time 1 and sits on chair 0.</p> </li> <li> <p>Friend 2 arrives at time 2 and sits on chair 1.</p> </li> <li> <p>Friend 0 arrives at time 3 and sits on chair 2.</p> </li> <li> <p>Friend 1 leaves at time 5 and chair 0 becomes empty.</p> </li> <li> <p>Friend 2 leaves at time 6 and chair 1 becomes empty.</p> </li> <li> <p>Friend 0 leaves at time 10 and chair 2 becomes empty.</p> </li> </ul> <p>Since friend 0 sat on chair 2, we return 2.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == times.length</code></li> <li><code>2 <= n <= 10<sup>4</sup></code></li> <li><code>times[i].length == 2</code></li> <li><code>1 <= arrival<sub>i</sub> < leaving<sub>i</sub> <= 10<sup>5</sup></code></li> <li><code>0 <= targetFriend <= n - 1</code></li> <li>Each <code>arrival<sub>i</sub></code> time is <strong>distinct</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • smallestChair

      public int smallestChair(int[][] times, int targetFriend)