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

7-fnzksxl #30

Merged
merged 1 commit into from
Feb 19, 2024
Merged

7-fnzksxl #30

merged 1 commit into from
Feb 19, 2024

Conversation

fnzksxl
Copy link
Member

@fnzksxl fnzksxl commented Feb 18, 2024

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

๊ตฌ๋ช…๋ณดํŠธ

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

30๋ถ„

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

์•„์ด๋””์–ด๋Š” ๋งค์šฐ ๊ฐ„.๋‹จํ•ฉ๋‹ˆ๋‹ค.
ํ˜„์žฌ people ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์ด limit๋ณด๋‹ค ๋ฌด๊ฑฐ์šด๊ฐ€? ์•„๋‹Œ๊ฐ€?๋งŒ
๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋‘ ๋ช…์„ ํƒœ์›Œ์•ผํ• ์ง€ ํ•œ ๋ช…๋งŒ ํƒœ์›Œ์•ผํ• ์ง€ ์•Œ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?

์ด๋ฅผ ์œ„ํ•ด์„œ ๋จผ์ € ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ people ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•ด์ค€ ๋’ค, ๊ฐ€์žฅ ์•ž๊ณผ ๋’ค๋ฅผ ๋”ํ•ด limit์™€ ๋น„๊ตํ•ด์ค๋‹ˆ๋‹ค.

image

  1. ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ > limit
    image

  2. ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ <= limit
    image

์ด๋ฅผ people ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์ค์‹œ๋‹ค.

from collections import deque

def solution(people, limit):
    answer = 0
    heavy = 0
    people.sort()
    
    q = deque(people)
    while q:
        heavy = q.pop()
        if len(q) and heavy+q[0] <= limit:
            q.popleft()
            
        answer+=1
        
    return answer

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

๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋Š” ์‹œ์ž‘ ์ „์— ์ •๋ ฌ์„ ์ž์ฃผ ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค.

Comment on lines +8 to +14
q = deque(people)
while q:
heavy = q.pop()
if len(q) and heavy+q[0] <= limit:
q.popleft()

answer+=1
Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ ์ €๋„ ํ’€์ด๋ฅผ ์•ˆ๋ณด๊ณ  ์šฐ์„  ํ’€์–ด๋ดค๋Š”๋ฐ ์ƒ๊ฐํ•œ ์•„์ด๋””์–ด๋Š” ๋˜‘๊ฐ™๊ตฐ์š”!!

์ €๋Š” ๋ฑ ์“ฐ๋Š” ๋Œ€์‹ ์— ์–ด์งœํ”ผ ๋ฌธ์ œ์— 2๋ช…์”ฉ ๋ฐ–์— ์•ˆ๋œ๋‹ค๊ณ  ํ•ด์„œ start์™€ end๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!

def solution(people, limit):
    people.sort()
    start = 0
    end = len(people) -1
    answer = 0
    while(start<=end):             
        if(people[start]+people[end] <= limit): 
            start += 1   
        end-=1
        answer +=1
    return answer

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜ค, ๋ฆฌ์ŠคํŠธ์˜ ์–‘๋‹จ์—์„œ๋ถ€ํ„ฐ ๋‚ด๋ ค์˜ค๋Š” ๋ฐฉ์‹์ด๊ตฐ์š”. ์ด๊ฒŒ ์ข€ ๋” ๊น”๋”ํ•œ ํ’€์ด ๊ฐ™์Šต๋‹ˆ๋‹ค.. ใ…‹ใ…‹ใ…‹ใ…‹

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 19, 2024

์ด ์‚ฌ๋žŒ ๊ธ€์ž ์ž˜์“ด๋‹ค...

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

์„ค๋ช…์„ ๋ณด๊ณ  ํ˜ผ์ž ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋Š”๋ฐ ์™œ ์ž๊พธ index out of range ๊ฐ€ ๋œจ์ง€... ํ•˜๋‹ค๊ฐ€ 2๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ deque ์—์„œ ๋ชจ๋“  ์›์†Œ๊ฐ€ ๋น ์ ธ ๋‚˜๊ฐˆ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋„ค์š” ใ…‹ใ…‹ใ…‹

from collections import deque
def solution(people, limit):
    answer = 0
    people.sort()
    deq = deque(people)
    
    while deq :
        heavyPeople = deq.pop()
        if len(deq) !=0 and heavyPeople + deq[0] <= limit :
            deq.popleft()
        answer += 1
        
    return answer

@fnzksxl
Copy link
Member Author

fnzksxl commented Feb 19, 2024

@tgyuuAn ํ„ฐ์น˜ํŽœ.. ์ด๋‹ˆ๊นŒ์š” ใ…‹ใ…‹ใ…‹ใ…‹

@fnzksxl
Copy link
Member Author

fnzksxl commented Feb 19, 2024

@wkdghdwns199 ๋„น ๋‹ค ๋น ์ ธ๋‚˜๊ฐ€๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ผ ์ฝ”๋“œ๊ฐ€ ํ„ฐ์ง‘๋‹ˆ๋‹ค.!

๊ทธ๋Ÿฐ๋ฐ

if len(q) != 0 and ~~:

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜์‹œ๋ฉด ๋ถˆํ•„์š”ํ•œ ํŒŒ์ด์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ƒ๊ฒจ์„œ ๋น„๊ตํ•˜๋Š” ํ˜•ํƒœ๋กœ if๋ฌธ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ฒŒ ๋˜๋Š”๋ฐ์š”.
์–ด์ฐจํ”ผ 0์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆซ์ž๋Š” ํ•ญ์ƒ True ๊ฐ’์ด๋‹ˆ๊นŒ len(q)๋งŒ ์‚ฌ์šฉํ•ด์„œ ๋น„๊ตํ•˜๋Š”๊ฒŒ
์ข€ ๋” ํŒŒ์ด์ฌ์Šค๋Ÿฌ์šด ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐ๋˜๋„ค์š”!

@fnzksxl fnzksxl merged commit 173d1c0 into main Feb 19, 2024
1 check passed
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.

4 participants