[알고리즘] LeetCode - Cinema Seat Allocation

Jerry·2021년 8월 20일
0

LeetCode

목록 보기
66/73
post-thumbnail

LeetCode - Cinema Seat Allocation

문제 설명

A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.

Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved.

Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.

입출력 예시

Example 1:

Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4
Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.

Example 2:

Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
Output: 2

제약사항

1 <= n <= 10^9
1 <= reservedSeats.length <= min(10*n, 10^4)
reservedSeats[i].length == 2
1 <= reservedSeats[i][0] <= n
1 <= reservedSeats[i][1] <= 10
All reservedSeats[i] are distinct.

Solution

[전략]
1. 배열을 row, column 순으로 정렬하여
2. 각각의 reservedSeat 사이가 4명이 앉을수있는 곳인지 판단한다

import java.util.*;

class Solution {
    public int maxNumberOfFamilies(int n, int[][] reservedSeats) {

        Arrays.sort(reservedSeats, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
        int rowNum = 1;
        int lastOccupied = 0;
        int count = 0;
        for (int i = 0; i < reservedSeats.length; i++) {

            if (reservedSeats[i][0] > rowNum) { // row 바뀜

                int diffRow = reservedSeats[i][0] - rowNum;
                // 한줄이 전체 비었을때 최대 2 그룹이 앉을 수 있음
                if (diffRow > 1) {
                    count += (diffRow - 1) * 2;
                }
                // 이전 줄의 마지막 자리가 어디냐에 따라 달라짐
                if (lastOccupied < 2) {
                    count += 2;
                } else if (lastOccupied < 6) {
                    count++;
                }
                rowNum = reservedSeats[i][0];
                lastOccupied = 0;
            }

            if (reservedSeats[i][1] >= 6 && reservedSeats[i][1] <= 7) {
                if (lastOccupied < 2) {
                    count++;
                }
            } else if (reservedSeats[i][1] >= 8 && reservedSeats[i][1] <= 9) {
                if (lastOccupied < 4) {
                    count++;
                }
            } else if (reservedSeats[i][1] == 10) {
                if (lastOccupied < 2) {
                    count += 2;
                } else if (lastOccupied < 6) {
                    count++;
                }
            }

            lastOccupied = reservedSeats[i][1];
        }

        if (lastOccupied < 2) {
            count += 2;
        } else if (lastOccupied < 6) {
            count++;
        }
        if (rowNum < n) {
            count += (n - rowNum) * 2;
        }
        return count;
    }
}
profile
제리하이웨이

18개의 댓글

comment-user-thumbnail
2025년 4월 14일

It’s appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or advice. Perhaps you could write next articles referring to this article. I desire to read even more things about it! Hsrp number plate

답글 달기
comment-user-thumbnail
2025년 4월 19일

Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. bestdesiccantdehumidifiers

답글 달기
comment-user-thumbnail
2025년 4월 19일

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also mobile trading platform

답글 달기
comment-user-thumbnail
2025년 4월 19일

Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. 이혼전문변호사

답글 달기
comment-user-thumbnail
2025년 5월 7일

Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. Choco

답글 달기
comment-user-thumbnail
2025년 5월 15일

Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles . 188Betct

답글 달기
comment-user-thumbnail
2025년 5월 15일

I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent. 국제 문자

답글 달기
comment-user-thumbnail
2025년 6월 11일

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. paldo777

답글 달기
comment-user-thumbnail
2025년 6월 11일

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. yabos88

답글 달기
comment-user-thumbnail
2025년 6월 11일

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 11일

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 12일

Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback. paldo777 casino app

답글 달기
comment-user-thumbnail
2025년 6월 14일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. situs toto macau

답글 달기
comment-user-thumbnail
2025년 6월 14일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. situs toto macau

답글 달기
comment-user-thumbnail
2025년 6월 14일

Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. situs dewanaga77

답글 달기
comment-user-thumbnail
2025년 6월 14일

very interesting keep posting. situs api66

답글 달기
comment-user-thumbnail
2025년 6월 16일

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.Agen Togel

답글 달기
comment-user-thumbnail
2일 전

Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us.best SoundCloud downloader online

답글 달기