Skip to content

Conversation

@wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Aug 1, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/181188

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ฒ˜์Œ์— ์ด๋ฌธ์ œ๋ฅผ๋ณด๊ณ  ์ž ์‹œ ์กฐํ•ฉ์„ ๋– ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค.
๊ฐ ์กฐํ•ฉ๋ณ„๋กœ ๋‹ค ์‹œ๋„ํ•ด๋ณด๊ณ , ๊ทธ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ์˜ ์ˆ˜๋ฅผ ๋‹ต์œผ๋กœ returnํ•˜๋ คํ–ˆ์ง€๋งŒ 1๋ถ€ํ„ฐ 50๋งŒ์˜ ์กฐํ•ฉ์€ ๋„ˆ๋ฌด๋„ ํฌ๋”๊ตฐ์š”,,

๋ฐ”๋กœ ์ ‘๊ณ  ์ „์— ์˜คํ ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์ˆ˜์—…๋–„ ๋น„์Šทํ•œ์œ ํ˜•์„ ๋“ค์€ ๊ธฐ์–ต์ด ๋‚˜ ๊ทธ๋ฆฌ๋””๋ฅผ ๋‘๋ฒˆ์จฐ๋กœ ๋– ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค.

๊ตฌ๊ฐ„์ด [1, 3] ์ด๋ ‡๊ฒŒ ์žˆ๋‹ค๋ฉด ์ฒ˜์Œ์—” ์ฒซ๋ฒˆ์จฐ์›์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก์œผ๋ คํ–ˆ๋Š”๋ฐ ๊ฐ€๋งŒํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์ตœ๋Œ€์ค‘์ฒฉ์„ ์œ„ํ•ด์„  ์•ž์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ํ–ˆ์„๋Œธ ํ•ด๋‹น๊ตฌ๊ฐ„์˜ ๊ฐ€์žฅ ๋’ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก์•„์•ผ ์ตœ๋Œ€ํ•œ ๋งŽ์€๊ตฌ๊ฐ„์„ ๊ฒน์น  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰ ๊ตฌ๊ฐ„์„ ๋น ํŠธ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ตฌ๊ฐ„์˜ ๋์„ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ์ •๋ ฌ์„ ํ•ด์ฃผ๊ณ ,
์ฒซ๊ตฌ๊ฐ„๋ถ€ํ„ฐ ๋์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฏธ์‚ฌ์ผ์  ์š”๊ฒฉํ•ด์ค๋‹ˆ๋‹ค.

์š”๊ฒฉํ• ๋–„ ํ•ด๋‹น๊ตฌ๊ฐ„์˜ ๋‹ค๋ฅธ๋ฏธ์‚ฌ์ผ๋„ ์ œ๊ฑฐํ•ด์ฃผ๊ณ , ๋‚จ์€ ๊ตฌ๊ฐ„์ด ์—†์„๋–„๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๋””๋ฅผ ํ‘ผ์ง€ ์˜ค๋ž˜๋ผ์„œ ์ด๊ฑธ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค..

def solution(targets):
    count = 0
    # ๋’ค์˜ ์›์†Œ๋กœ ์ •๋ ฌ
    sorted_targets = sorted(targets, key = lambda x:x[1])
    
    # ์ฒซ๋ฒˆ์จฐ ๊ตฌ๊ฐ„๋ถ€ํ„ฐ ๋๋‚˜๋Š”์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฏธ์‚ฌ์ผ ์š”๊ฒฉ
    for target in sorted_targets:
        # ๋ชจ๋‘ ์š”๊ฒฉํ•˜๋ฉด ์ข…๋ฃŒ
        if len(targets) == 0:
            break
        if target not in targets:
            continue
            
        end = target[1]
        n = len(targets)
        selected = []
        
        flag = False
        for i in range(n):
            # ์‹œ์ž‘์€ end๋ณด๋‹ค ์ž‘๊ณ  ๋์€ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ตฌ๊ฐ„์ด๋ผ๋ฉด ์‚ญ์ œ
            if end > targets[i][0] and targets[i][1] >= end:
                selected.append(i)
                flag = True
        
        for i in reversed(selected):
            del targets[i]
        
        if flag == True:
            count += 1

    return count

์‚ญ์ œ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”์ˆœ๊ฐ„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”
์‚ฌ์‹ค ์ด๋ฌธ์ œ๋Š” ๊ตณ์ด ๊ตฌํ˜„์„ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค

def solution(targets):
    # ๋๋‚˜๋Š” ์ง€์  ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    targets.sort(key=lambda x: x[1])

    count = 0
    last_shot = -1
    for start, end in targets:
        if start >= last_shot:
            count += 1
            last_shot = end
            
    return count

๊ทธ๋ƒฅ ๋ฏธ์‚ฌ์ผ ๋ฐœ์‚ฌ์ง€์ ์„ ์ •ํ•ด๋‘๊ณ  ๋‹ค์Œ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์ ์ด ๋งˆ์ง€๋ง‰ ๋ฐœ์‚ฌ์ง€์ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด, ๊ทธ ๊ตฌ๊ฐ„์˜ ๋งˆ์ง€๋ง‰์ง€์ ์„ ๋‹ค์‹œ ๋ฐœ์‚ฌ์ง€์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ด์ค๋‹ˆ๋‹ค. ๊ฐ™๊ฑฐ๋‚˜ ํฐ ์ด์œ ๋Š”, ๊ฐ™์„๋–„๋„ ๊ฒฝ๊ณ„๊ฐ’์€ ์š”๊ฒฉ์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

Copy link
Collaborator

@kokeunho kokeunho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ๊ธˆ๋ฐฉ ํ’€์ด๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ
์ค€์šฉ๋‹˜์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋ฅผ ์ž˜ ์•ˆํ’€์–ด์„œ ๊ทธ๋Ÿฐ์ง€, ์ ‘๊ทผ ๋ฐฉ์‹์ด ์ž˜ ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋„ค์š”.
๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋Š” ์งง์•˜์–ด๋„ ์–ป์–ด๊ฐ€๋Š”๊ฒŒ ์žˆ์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค~

import java.util.*;

class Solution {
    public int solution(int[][] targets) {
        Arrays.sort(targets, (a, b) -> Integer.compare(a[1], b[1]));
        
        int count = 0;
        int missleX = -1;
        
        for (int[] target : targets) {
            int start = target[0];
            int end = target[1];
            
            if (missleX < start) {
                count++;
                missleX = end - 1;
            }
        }
        return count;
    }
}

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ฆฌ๋””์Šค๋Ÿฌ์šด ๋ฌธ์ œ์ด๊ตฐ์š”! 18๋ถ„ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹คใ…Žใ…Ž
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ดˆ๋ฐ˜์— ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ช‡๋ฒˆ ๋‹ค๋ค„๋ด์„œ์ธ์ง€, ์ด๋ ‡๊ฒŒ s ~ e ๊ฐ€ ์žˆ์œผ๋ฉด
s์™€ e์ค‘ ์–ด๋А๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•  ์ง€ ๊ณ ๋ฏผ์„ ํ•ด๋ณด๋Š” ํŽธ์ธ ๊ฒƒ ๊ฐ™์•„์š”.
(๋˜๋Š” ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์‚ด์ง ๊ณ ๋ คํ•˜๊ธด ํ•ฉ๋‹ˆ๋‹ค.)

์ฒ˜์Œ์— ๋“  ์ƒ๊ฐ์€ s๊ธฐ์ค€ ์ •๋ ฌ์ด์—ˆ๋Š”๋ฐ ๊ตฌํ˜„์„ ์กฐ๊ธˆ์”ฉ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์•ˆ๋˜๊ฒ ๋”๋ผ๊ตฌ์š”.
๊ทธ๋ž˜์„œ e๊ธฐ์ค€ ์ •๋ ฌ๋กœ ๋ฐ”๊พธ๊ณ  ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ค๋‹ˆ ์ •๋‹ต์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์˜ค๋žœ๋งŒ์— ๋ช…์พŒํ•˜๊ฒŒ ๋‹ต์„ ์ฐพ์€ ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚˜์„œ ๊ธฐ๋ถ„์ด ์ข‹๋„ค์š”.

์ฝ”๋“œ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.

#include <algorithm>
#include <vector>

using namespace std;

bool compareTo(const vector<int>& a, const vector<int>& b) {
    return a[1] < b[1];
}

int solution(vector<vector<int>> targets) {
    sort(targets.begin(), targets.end(), compareTo);

    int answer = 0;
    int curE = -1;
    for (int i = 0; i < targets.size(); i++) {
        int s = targets[i][0];
        int e = targets[i][1];
        
        if (s >= curE) {
            answer++;
            curE = e;
        }
    }
    
    return answer;
}

Copy link
Member

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „ ๊ทธ๋ฆฌ๋””๊ฐ€ ์ฐธ ์–ด๋ ต๋„ค์š”... ์•ฝ๊ฐ„ ์š”๋Ÿฐ ๊ธธ์ด/๋ง‰๋Œ€ ๊ด€๋ จ๋œ ๊ฑด ๋์  ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด ๋ฐ˜์€ ๋จน๊ณ  ๋“ค์–ด๊ฐ„๊ฑด๋ฐ ์ „ ๊ทธ ๋‹ค์Œ์ด ๊ณ„์† ์•ˆ๋– ์˜ค๋ฅด๋„ค์š” ํ—ˆ...

์š”๊ฒฉ์ด ๊ฐœ๊ตฌ๊ฐ„ (s, e)์— ๋Œ€ํ•ด ์ด๋ค„์ ธ์„œ ํฌ๊ฒฉ ์ขŒํ‘œ x๊ฐ€ ํ•ด๋‹น ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์  s๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๊ตฌ๊ฐ„ ๋์ ์œผ๋กœ ์ ํ”„ ์‹œํ‚ค๋ฉด์„œ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฐ๋‹ค... ์งœ๊ณ  ๋ณด๋ฉด ์ฐธ ๊ฐ„๋‹จํ•œ ๊ฒƒ์„...

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<vector<int>> targets) {
    sort(targets.begin(), targets.end(), [](vector<int>& lhs, vector<int>& rhs) {
        return lhs[1] < rhs[1];
    });
    
    int answer = 0;
    int x = 0;
    
    for(auto& t : targets) {
        if(x <= t[0]) {
            ++answer;
            x = t[1];
        }
    }
    
    return answer;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20๋ถ„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‹ต์ด ์•ˆ๋‚˜์™€์„œ ํ’€์ด๋ฅผ ๋ดค์Šต๋‹ˆ๋‹ค. ์ €๋„ ๊ทธ๋ฆฌ๋””๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์€๊ฑด ์•„๋‹Œ๋ฐ ์–ด๋–ป๊ฒŒ ๋งค๋ฒˆ ์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋„ค์š”.

๊ทธ๋ฆฌ๋””๋Š” ์ •๋ ฌ๊ณผ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ์ •๋ ฌ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ๋Š” ๊ธฐ์ค€์€ ์ •๋ ฌ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋ฐ”๋€Œ๋Š” ๋“ฏ ํ•˜๊ตฌ์š”.

์ „ ๋‹ค๋ฅธ ๋ถ„๋“ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ตฌ๊ฐ„์— ๋Œ€ํ•œ ๊ตต์€ ์„ (์•ฝ๊ฐ„ ํžˆ์Šคํ† ๊ทธ๋žจ? ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ๊ฐ™์€)์„ ๊ทธ๋ฆฌ๊ณ  ์ด ์„ ์„ ๊ตต๊ฒŒ ์œ ์ง€์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ตต์€ ์„ ์— ๋‹ค์Œ ์‹œ์ž‘ ์ง€์ ์ด ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ƒˆ๋กœ์šด ๊ตต์€ ์„ ์„ ๊ทธ๋ ธ๋‹ค๊ณ  ์น˜๋Š”๊ฑฐ์ฃ .

import java.util.*;

class Solution {
    public int solution(int[][] targets) {     
        Arrays.sort(targets, (a, b) -> a[0] - b[0]);
        int preStart = targets[0][0];
        int preEnd = targets[0][1];
        int answer = 1; // ๋ฉ˜ ์ฒ˜์Œ target์„ ์š”๊ฒฉํ•˜๋Š” ๋ฏธ์‚ฌ์ผ
        
        
        // ๊ตต์€ ์„ ์„ ๊ทธ๋ฆฐ๋‹ค๊ณ  ํ•˜๊ณ , ์ด ์„ ์„ ์ตœ๋Œ€ํ•œ ๊ตต๊ฒŒ ์œ ์ง€์‹œํ‚ค๋ฉด ๋จ
        // ์„ ์ด ๊ตต๊ฒŒ ์œ ์ง€๋˜์–ด์•ผ ๋” ๋งŽ์€ ๋ฏธ์‚ฌ์ผ์„ ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ
        for (int i = 1; i < targets.length; i++) {
            int curStart = targets[i][0];
            int curEnd = targets[i][1];
            
            if (preStart <= curStart && curStart < preEnd) {
                preStart = Math.max(preStart, curStart);
                preEnd = Math.min(preEnd, curEnd);
            } else {
                answer++;
                preStart = curStart;
                preEnd = curEnd;
            }
        }
        
        return answer;
    }
}

@g0rnn g0rnn merged commit 9151768 into main Aug 9, 2025
1 check passed
@g0rnn g0rnn deleted the 30-wnsmir branch August 9, 2025 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants