Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-kangrae-jo #21

Merged
merged 4 commits into from
Nov 23, 2024
Merged

6-kangrae-jo #21

merged 4 commits into from
Nov 23, 2024

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Nov 5, 2024

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

๋‹จ์†์นด๋ฉ”๋ผ

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

1h 12m

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

Greedy?

์ด๋ฒˆ ๋ฌธ์ œ ์œ ํ˜•์€ Greedy์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ ์†Œ๊ฐœํ•˜๋Š” ์œ ํ˜•์ด๋‹ˆ ์กฐ๊ธˆ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ €๋ฒˆ์— ์ €ํฌ๊ฐ€ ๋Œ€๋ฉด ๋ฏธํŒ… ๋•Œ 'BruteForce์™€ Greedy๊ฐ€ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€',
'Greedy์—์„œ ์ง„ํ™”ํ•˜๋ฉด DP์ธ๊ฐ€?' ๋ผ๋Š” ์˜๋ฌธ์„ ๊ฐ€์กŒ์—ˆ์ฃ .

Greedy๋Š” ๋ถ€๋ถ„์˜ ์ตœ์ ํ•ด๊ฐ€ ์ „์ฒด์˜ ์ตœ์ ํ•ด๊ฐ€ ๋˜๋Š” ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
BruteForce, DP์™€๋Š” ์™„์ „ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์„ ๊ฐ€์ง€๋„ค์š”.

Brute Force์™€๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์œผ๋กœ, Brute Force๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฐ˜๋ฉด, Greedy๋Š” ๋งค ์„ ํƒ์—์„œ ์ฆ‰๊ฐ์ ์œผ๋กœ ์ตœ์„ ์˜ ์„ ํƒ์„ ํ•˜์—ฌ ์ตœ์ ํ•ด๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค.

DP๋Š” ๋ฌธ์ œ๋ฅผ ๋” ์ž‘์€ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ณ , ๊ฐ ํ•˜์œ„ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ์ €์žฅํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
memorization์„ ํ†ตํ•ด ์ค‘๋ณต ๊ณ„์‚ฐ์„ ์ค„์ด๊ธฐ ๋•Œ๋ฌธ์— BruteForce๋ณด๋‹ค ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

์ฒ˜์Œ ํ’€์ด

์ฒ˜์Œ์—๋Š” ์ œ๊ฐ€ ์ด์ „์— ์„ค๋ช…ํ•œ encode&decode๋ฅผ ํ†ตํ•ด ๋ˆ„์ ํ•ฉ์„ ํ•˜๊ณ ,
์ตœ๋Œ€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„์ด ๋ช‡ ๊ตฐ๋ฐ์ธ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์˜ˆ์‹œ๋ฅผ ์ด ๋ฐฉ๋ฒ•๋Œ€๋กœ ํ•ด๋ณด๋ฉด 2๊ฐœ ๊ตฌ๊ฐ„์ด ๋‚˜์™€ ๋‹ต์ด 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
image

๊ทธ๋Ÿด์‹ธํ•˜์ฅฌ?

๊ทผ๋ฐ ์ด ๋ฐฉ๋ฒ•์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฒน์น˜์ง€ ์•Š๊ณ  ๋–จ์–ด์ ธ์žˆ๋Š” ๊ตฌ๊ฐ„์€ 1์ธ๋ฐ, ์ตœ๋Œ€๋กœ ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„๋งŒ ์„ผ๋‹ค๋ฉด ๊ทธ๊ณณ์—๋Š” ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ •๋‹ต ํ’€์ด

๊ทธ๋ ‡๊ฒŒ ์š”๋ฆฌ์กฐ๋ฆฌ ์ƒ๊ฐํ•ด๋ณด๋‹ค๊ฐ€ ์˜ˆ์ „์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—…์„ ๋“ค์—ˆ์„๋•Œ Greedy์˜ˆ์ œ๊ฐ€ ์ƒ๊ฐ๋‚ฌ์Šต๋‹ˆ๋‹ค.
์ผ์ด ์‹œ์ž‘ํ•˜๋Š” ์‹œ๊ฐ„, ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœํ•˜๋Š” ์„ ํƒ์„ ํ•˜๋ฉฐ ์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ, ์ž์„ธํžˆ ์„ค๋ช…ํ•˜์ง€๋Š” ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.

์–ด์จŒ๋“  ์ค‘์š”ํ•œ ์ ์€ ์ด ๋ฌธ์ œ๋„ ์‹œ์ž‘์ , ๋์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทผ๋ฐ ๋„์ €ํžˆ ์ด ๋ฌธ์ œ์— ์ ์šฉํ•˜์ง€๋Š” ๋ชปํ•˜๊ฒ ์–ด์„œ.. ์„œ์นญํ–ˆ์Šต๋‹ˆ๋‹ค. (์š”์ฆ˜ ์‹œ์›ํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.)

์ •๋‹ต ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. ์‹œ์ž‘์  ๊ธฐ์ค€์œผ๋กœ routes ์ •๋ ฌ
2. routes์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉฐ ๋น„๊ต ์—ฐ์‚ฐ
2-1. ๋งŒ์•ฝ ํ˜„์žฌ ๊ตฌ๊ฐ„์˜ ๋๋ณด๋‹ค ์งํ›„ ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์ด ๋” ํฐ ๊ฒฝ์šฐ 
     = ์นด๋ฉ”๋ผ ์„ค์น˜, ์งํ›„ ๊ตฌ๊ฐ„์˜ ๋์„ ํ˜„์žฌ๊ตฌ๊ฐ„์˜ ๋์œผ๋กœ ์„ค์ •
2-2. ๋งŒ์•ฝ ํ˜„์žฌ ๊ตฌ๊ฐ„์˜ ๋์ด ์งํ›„ ๊ตฌ๊ฐ„์˜ ๋ ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
     = ํ˜„์žฌ ๊ตฌ๊ฐ„์˜ ๋์„ ์งํ›„ ๊ตฌ๊ฐ„์˜ ๋์œผ๋กœ ์„ค์ •

์ˆ˜๋„์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์กฐ๊ธˆ ์ดํ•ด๊ฐ€ ์–ด๋ ค์šธ ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
์ €๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋ผ์„œ ์ง์ ‘ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๊ฐ€๋ฉด์„œ ๋น„๊ตํ•ด๋ณด๋‹ˆ ์œ„ ๋กœ์ง์ด ๋งž๋”๋ผ๊ณ ์š”.

Greedy๋Š” ๋กœ์ง์งœ๋Š”๊ฒŒ ์ „๋ถ€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ,
ํ•œ 45๋ถ„๋งŒ์— ๋‹ต์„ ๋ด๋ฒ„๋ ธ๋„ค์š”...
๋‹ค์Œ์—๋Š” ์ ์–ด๋„ 1์‹œ๊ฐ„์€ ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค...

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Greedy, BruteForce, DP๋ฅผ ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค.
์‹ค์ „ ๋ฌธ์ œ์—์„œ ์œ ํ˜•์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ฃผ๋Š” ๊ฒฝ์šฐ๋Š” ์—†์ง€๋งŒ, ์•Œ์•„๋‘๋ฉด ์ ๋‹นํ•  ๋•Œ ์ž˜ ์“ธ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

๊ทธ๋ฆฌ๊ณ  ๊ณ ์ˆ˜๋‹˜๋“ค ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ in์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์ง€ ์•Š๊ณ  out์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์กฐ๊ฑด๋ฌธ ํ•˜๋‚˜๋ฅผ ์ค„์—ฌ๋ฒ„๋ฆฌ๋”๋ผ๊ณ ์š”.
๋‹ค์Œ์—๋Š” ์ €๋„ compare์„ ์ž˜ ํ™œ์šฉํ•ด๋ณด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

def solution(routes):
    # 1. ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ๋ฅผ ์ง„์ถœ ์ง€์  ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
    routes.sort(key=lambda x: x[1])  #์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

    cameras = 0
    last_camera = -30001  # ์ดˆ๊ธฐ๊ฐ’

    # 2. ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์นด๋ฉ”๋ผ ์„ค์น˜
    for route in routes:
        if last_camera < route[0]:
            # ํ˜„์žฌ ์ฐจ๋Ÿ‰ ๊ฒฝ๋กœ๋ฅผ ์ปค๋ฒ„ํ•˜์ง€ ๋ชปํ•˜๋ฉด ์ƒˆ๋กœ์šด ์นด๋ฉ”๋ผ ์„ค์น˜
            cameras += 1
            last_camera = route[1]  # ํ˜„์žฌ ์ฐจ๋Ÿ‰์˜ ์ง„์ถœ ์ง€์ ์— ์นด๋ฉ”๋ผ ์„ค์น˜

    return cameras

๋จผ์ € ์ฐจ๋Ÿ‰์˜ ์ง„์ถœ ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋„๋กœ๋ฅผ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ์ฐจ๋Ÿ‰๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ฐจ๋Ÿ‰์„ ์ ์€ ์นด๋ฉ”๋ผ๋กœ ์ปค๋ฒ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ ์ •๋ ฌ๋œ ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ด์ „์— ์„ค์น˜๋œ ์นด๋ฉ”๋ผ๊ฐ€ ํ˜„์žฌ ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ๋ฅผ ์ปค๋ฒ„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ด์ „ ์นด๋ฉ”๋ผ๋กœ ์ปค๋ฒ„๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ์ƒˆ๋กœ์šด ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๊ณ , ๊ทธ ์œ„์น˜๋ฅผ ํ•ด๋‹น ์ฐจ๋Ÿ‰์˜ ์ง„์ถœ ์ง€์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ์ฐจ๋Ÿ‰ ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ์ฐจ๋Ÿ‰์˜ ์ง„์ถœ ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ƒˆ๋กœ์šด ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ทธ๋ฆฌ๋”” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

์ดˆ๊ธฐ ์นด๋ฉ”๋ผ ์œ„์น˜๋ฅผ ์ฐจ๋Ÿ‰ ๊ฒฝ๋กœ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ๋ฅผ ์ปค๋ฒ„ํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ํ‚ฅ์ž…๋‹ˆ๋‹ค.

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.

2 participants