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

1-kjs254 #2

Merged
merged 4 commits into from
Feb 15, 2024
Merged

1-kjs254 #2

merged 4 commits into from
Feb 15, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Feb 12, 2024

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

๊ธฐ๋Šฅ ๊ฐœ๋ฐœ

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

1์‹œ๊ฐ„

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

๊ตฌํ˜„ ๋™์ž‘์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


  1. ๊ฐ ์ž‘์—… ๋ณ„๋กœ ๋ช‡ % ๋‚จ์•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

  2. ๊ฐ ์ž‘์—… ๋ณ„๋กœ ๋ถ€์—ฌ๋œ ์Šคํ”ผ๋“œ๋ฅผ ๋‚˜๋ˆ”์œผ๋กœ์จ ๋ฉฐ์น  ๋’ค์— ๋ฐฐํฌ๋  ์ง€ ํ™•์ธํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ days๋ฅผ ๋งŒ๋“ ๋‹ค.

  3. ์Šคํƒ stk๊ฐ€ ๋น„์–ด ์žˆ๋‹ค๋ฉด days ์ฒซ ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

  4. ์Šคํƒ์˜ ์ตœ๋Œ€ ๊ฐ’์ด days ์ฒซ ์š”์†Œ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์€ ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๋œป์œผ๋กœ ๋ฐฐํฌ๊ฐ€ ๋™์‹œ์— ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ stk์— pop ํ•˜์—ฌ ์ถ”๊ฐ€ํ•œ๋‹ค.

  5. stk์˜ ์ตœ๋Œ€ ๊ฐ’์ด days ์ฒซ ์š”์†Œ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๋’ค์˜ ์ž‘์—…์ด ๋ฐฐํฌ๋˜๊ธฐ ์ „์— ํ˜„ ์ž‘์—…์ด ๋จผ์ € ๋ฐฐํฌ๋œ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ stk์— ์Œ“์ธ ์ž‘์—…์„ ๋ชจ๋‘ ๋ฐฐํฌํ•œ๋‹ค. (answer์— stk ๊ธธ์ด๋งŒํผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.)

  6. 3-5 ์ž‘์—…์„ days์˜ ์š”์†Œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.




์ „์ฒด ์ฝ”๋“œ

def solution(progresses, speeds):
    answer = []
    days = []
    stk = []

    for i,p in enumerate(progresses):
        term = 100-p
        s = speeds[i]
        
        if term%s==0:
            days.append(term//s)
        else:
            days.append(term//s+1)
            
    while days:
        if not stk:
            stk.append(days.pop(0))
        elif max(stk)>=days[0]:
            stk.append(days.pop(0))
        else:
            answer.append(len(stk))
            stk.clear()
    
    answer.append(len(stk))
    return answer

๋งˆ์ง€๋ง‰์— stk์˜ ๊ธธ์ด๋ฅผ ํ•œ๋ฒˆ ๋” ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ days์— ์š”์†Œ๊ฐ€ ์—†์„ ๋•Œ answer์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ ๋ˆ„๋ฝ๋˜๊ธธ๋ž˜ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

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

์•„์ง ์ž๋ฃŒ๊ตฌ์กฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ๋‹จ์ˆœํžˆ ๋ฆฌ์ŠคํŠธ ๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค..ใ…Žใ…Ž

์‰ฌ์šด ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ค์„œ ๊ณต๋ถ€๋ฅผ ๋งŽ์ด ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค!

@9kyo-hwang
Copy link

9kyo-hwang commented Feb 12, 2024

์•ˆ๋…•ํ•˜์„ธ์š” ์˜†๋™๋„ค(3ํŒ€)์—์„œ ๋†€๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.

  1. ํŒŒ์ด์ฌ์—์„œ๋Š” list๋ฅผ ์Šคํƒ์ฒ˜๋Ÿผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์  ๋ฐฐ์—ด ๊ธฐ๋ฐ˜์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋งจ ๋์—์„œ์˜ ์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ $O(1)$๋กœ ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์Šคํƒ๊ณผ ์‚ฌ์‹ค์ƒ ๋™์ผํ•œ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  1. ๋ฌธ์ œ์—์„œ stack์ด๋ผ ์„ค๋ช…ํ•˜์…จ์ง€๋งŒ, ์‚ฌ์‹ค์€ queue์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์…จ์Šต๋‹ˆ๋‹ค.
  • pop(0), ์ฆ‰ ๋งจ ์•ž ์š”์†Œ๋ฅผ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์‚ฝ์ž…์€ append๋ฅผ ํ†ตํ•ด ๋งจ ๋’ค๋กœ ๋„ฃ์œผ์…จ์ฃ . ์ด๋Š” ์„ ์ž…์„ ์ถœ ๊ตฌ์กฐ์ธ queue์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ๋™์  ๋ฐฐ์—ด ๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ์„ค๋ช…์ฒ˜๋Ÿผ ๋งจ ์•ž ์š”์†Œ๋‚˜ ์ค‘๊ฐ„ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ์— ์ ํ•ฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฐ์†ํ•ด์„œ ์กด์žฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ค‘๊ฐ„์— ์›์†Œ ํ•˜๋‚˜๋ฅผ ์ง€์šฐ๋ฉด ๊ทธ ์ž๋ฆฌ๋ฅผ ๋ฉ”์šฐ๊ธฐ ์œ„ํ•ด ๋’ท ์š”์†Œ๋“ค์„ ํ•œ ์นธ์”ฉ ๋•ก๊ธฐ๋Š” ์ž‘์—…์ด ๊ฐ™์ด ์ˆ˜๋ฐ˜๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ $O(n)$๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.
  • ํŒŒ์ด์ฌ์—์„œ๋Š” collections ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— deque์ด ์กด์žฌํ•ด ์ด๋ฅผ importํ•˜์—ฌ ํ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ์•Œ์•„๋ณด์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 12, 2024

์•ˆ๋…•ํ•˜์„ธ์š” ์˜†๋™๋„ค(3ํŒ€)์—์„œ ๋†€๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.

  1. ํŒŒ์ด์ฌ์—์„œ๋Š” list๋ฅผ ์Šคํƒ์ฒ˜๋Ÿผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์  ๋ฐฐ์—ด ๊ธฐ๋ฐ˜์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋งจ ๋์—์„œ์˜ ์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ O(1)๋กœ ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์Šคํƒ๊ณผ ์‚ฌ์‹ค์ƒ ๋™์ผํ•œ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  1. ๋ฌธ์ œ์—์„œ stack์ด๋ผ ์„ค๋ช…ํ•˜์…จ์ง€๋งŒ, ์‚ฌ์‹ค์€ queue์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์…จ์Šต๋‹ˆ๋‹ค.
  • pop(0), ์ฆ‰ ๋งจ ์•ž ์š”์†Œ๋ฅผ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์‚ฝ์ž…์€ append๋ฅผ ํ†ตํ•ด ๋งจ ๋’ค๋กœ ๋„ฃ์œผ์…จ์ฃ . ์ด๋Š” ํ›„์ž…์„ ์ถœ ๊ตฌ์กฐ์ธ queue์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ๋™์  ๋ฐฐ์—ด ๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ์„ค๋ช…์ฒ˜๋Ÿผ ๋งจ ์•ž ์š”์†Œ๋‚˜ ์ค‘๊ฐ„ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ์— ์ ํ•ฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฐ์†ํ•ด์„œ ์กด์žฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ค‘๊ฐ„์— ์›์†Œ ํ•˜๋‚˜๋ฅผ ์ง€์šฐ๋ฉด ๊ทธ ์ž๋ฆฌ๋ฅผ ๋ฉ”์šฐ๊ธฐ ์œ„ํ•ด ๋’ท ์š”์†Œ๋“ค์„ ํ•œ ์นธ์”ฉ ๋•ก๊ธฐ๋Š” ์ž‘์—…์ด ๊ฐ™์ด ์ˆ˜๋ฐ˜๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • ํŒŒ์ด์ฌ์—์„œ๋Š” collections ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— deque์ด ์กด์žฌํ•ด ์ด๋ฅผ importํ•˜์—ฌ ํ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ์•Œ์•„๋ณด์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

๋ง๋ถ™์ด์ž๋ฉด pop(0)์œผ๋กœ ํ•  ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ฒจ์ ธ ์žˆ๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜ n๊ฐœ ๋งŒํผ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ $O(n)$์ด ์†Œ๋ชจ ๋ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ž๋ฃŒ๊ตฌ์กฐ deque๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด popleft()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋กœ $O(1)$๋กœ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์–ด์š”.

@9kyo-hwang
Copy link

  • pop(0), ์ฆ‰ ๋งจ ์•ž ์š”์†Œ๋ฅผ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์‚ฝ์ž…์€ append๋ฅผ ํ†ตํ•ด ๋งจ ๋’ค๋กœ ๋„ฃ์œผ์…จ์ฃ . ์ด๋Š” ํ›„์ž…์„ ์ถœ ์„ ์ž…์„ ์ถœ ๊ตฌ์กฐ์ธ queue์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์„ ์ž…์„ ์ถœ์ž…๋‹ˆ๋‹ค... ์‹ค์ˆ˜ ใ…œ

@miniron-v
Copy link
Member

miniron-v commented Feb 12, 2024

์ฒซ PR ์ถ•ํ•˜๋“œ๋ฆฝ๋‹ˆ๋‹ค! ์ˆ˜๋„ ์ฝ”๋“œ ์„ค๋ช…์ด ์ž˜ ๋˜์–ด ์žˆ์–ด ํŒŒ์ด์ฌ์„ ๋ชฐ๋ผ๋„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„ค์š”. ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

while days:
        if not stk:
            stk.append(days.pop(0))
        elif max(stk)>=days[0]:
            stk.append(days.pop(0))
        else:
            answer.append(len(stk))
            stk.clear()

์ด ๋ถ€๋ถ„์€

1. ์Šคํƒ์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด append(์ถ”๊ฐ€)
2. days[0]๋ณด๋‹ค ํฐ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด append(์ถ”๊ฐ€)
3. days[0]์ด ๋” ํฌ๋‹ค๋ฉด ์ •๋‹ต์— ์ผ์ˆ˜(์Šคํƒ ํฌ๊ธฐ) ๊ธฐ๋กํ•˜๊ณ  ํด๋ฆฌ์–ด

๋กœ ๋™์ž‘ํ•˜๋Š”๋ฐ์š”, ์•„๋งˆ ์ €๋ผ๋ฉด

1. days์— ์ ‘๊ทผํ•  ๋•Œ, pop(0)๊ณผ 0๋ฒˆ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๋Œ€์‹  ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ฆ๊ฐ€
1-1. ์ด๋Ÿฌ๋ฉด pop ์—ฐ์‚ฐ ์ž์ฒด๋ฅผ ์•ˆ ์“ฐ๋‹ˆ ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.
2. max๊ฐ’์„ ๋งค ๋ฒˆ ๊ตฌํ•˜๋Š” ๋Œ€์‹ , ํ์˜ ๋งจ ์•ž๋งŒ ๋ณด๊ฑฐ๋‚˜, days[i]๊ฐ€ ๋” ์ž‘์€ ๊ฒฝ์šฐ max ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถ”๊ฐ€
2-1. ์ด ๊ฒฝ์šฐ, ๋งค ๋ฒˆ max๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์Šคํƒ์˜ ์›์†Œ๋“ค์„ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ , stack์˜ top, ๋˜๋Š” queue์˜ front๋งŒ ๋ณด๊ณ ๋„ ํ–‰๋™์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒŒ ๊ณง max๊ฐ’์ด ๋ผ์š”!

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

i = 0
while i < len(days):
        if not stk:
            stk.append(days[i])
            i += 1
        elif stk[0] >= days[i]:
            stk.append(days[i])
            i += 1
        else:
            answer.append(len(stk))
            stk.clear()

(ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ฝ”๋ฉ˜ํŠธ ์ฃผ์„ธ์š”...! ํŒŒ์ด์ฌ ๋ชจ๋ฆ„)

๊ทธ๋ž˜๋„ ์–ธ์ œ๋‚˜ ๋ฌธ์ œ๋Š” ํ’€์—ˆ๋‹ค๋ฉด ์žฅ๋•ก์ด์ฃ . ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

โœ”๏ธ ์ €๋„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉฐ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์•˜์œผ๋‚˜ ๊ฒฐ๊ตญ ํ’€์ดํ•˜์‹  ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ์•„์˜ค๋”๋ผ๊ณ ์š”...
sudo code๊ฐ€ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์…จ๊ณ , ํšจ๊ณผ์ ์ธ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ์œ„์—์„œ ๋งŽ์€ ๋ถ„๋“ค์ด ์–ธ๊ธ‰ํ•ด์ฃผ์…จ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ list, queue ์™ธ์—๋„ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ๋„ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐฐํฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ์— Linked list๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. ๋ฌผ๋ก  ๋‹ค๋ฅธ ์ž๋ฃŒ๋“ค๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
โ–ถ๏ธ ์ด ๋‚ด์šฉ์€ ํ”ผ๋“œ๋ฐฑ ํ•œ๋ฒˆ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”

โœ”๏ธ์ด ๋ฌธ์ œ์—์„œ ๋‹ค์–‘ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‚ฌ์šฉํ•ด๋ณด์‹œ๋ฉด ๋” ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

Comment on lines +15 to +22
while days:
if not stk:
stk.append(days.pop(0))
elif max(stk)>=days[0]:
stk.append(days.pop(0))
else:
answer.append(len(stk))
stk.clear()
Copy link
Collaborator

Choose a reason for hiding this comment

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

stk๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฐํฌํ•  ์ž‘์—…์„ ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ์ž‘์—…์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ๋ฒ•์ด ์ธ์ƒ์ ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ง€๊ธˆ ๋ง‰ ์‹œ์ž‘ํ•œ ๋‹จ๊ณ„๋ผ ์ž˜์€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์•„์ด๋””์–ด ์ž์ฒด๋Š” ๊ตฐ๋”๋”๊ธฐ ์—†๋‹ค๊ณ  ๋Š๊ปด์ง‘๋‹ˆ๋‹ค. ํŠนํžˆ stk๋ฐฐ์—ด์— days๋ฐฐ์—ด ์š”์†Œ๋“ค์„ ์˜ฎ๊ฒจ์™€์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ œ๊ฑฐํ•˜๋Š” ๋ถ€๋ถ„์ด ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์•„์ด๋””์–ด ์–ป๊ณ ๊ฐ‘๋‹ˆ๋‹ค. ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

@kjs254
Copy link
Member Author

kjs254 commented Feb 15, 2024

์ฒซ PR ์ถ•ํ•˜๋“œ๋ฆฝ๋‹ˆ๋‹ค! ์ˆ˜๋„ ์ฝ”๋“œ ์„ค๋ช…์ด ์ž˜ ๋˜์–ด ์žˆ์–ด ํŒŒ์ด์ฌ์„ ๋ชฐ๋ผ๋„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„ค์š”. ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

while days:
        if not stk:
            stk.append(days.pop(0))
        elif max(stk)>=days[0]:
            stk.append(days.pop(0))
        else:
            answer.append(len(stk))
            stk.clear()

์ด ๋ถ€๋ถ„์€

1. ์Šคํƒ์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด append(์ถ”๊ฐ€)
2. days[0]๋ณด๋‹ค ํฐ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด append(์ถ”๊ฐ€)
3. days[0]์ด ๋” ํฌ๋‹ค๋ฉด ์ •๋‹ต์— ์ผ์ˆ˜(์Šคํƒ ํฌ๊ธฐ) ๊ธฐ๋กํ•˜๊ณ  ํด๋ฆฌ์–ด

๋กœ ๋™์ž‘ํ•˜๋Š”๋ฐ์š”, ์•„๋งˆ ์ €๋ผ๋ฉด

1. days์— ์ ‘๊ทผํ•  ๋•Œ, pop(0)๊ณผ 0๋ฒˆ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๋Œ€์‹  ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ฆ๊ฐ€
1-1. ์ด๋Ÿฌ๋ฉด pop ์—ฐ์‚ฐ ์ž์ฒด๋ฅผ ์•ˆ ์“ฐ๋‹ˆ ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.
2. max๊ฐ’์„ ๋งค ๋ฒˆ ๊ตฌํ•˜๋Š” ๋Œ€์‹ , ํ์˜ ๋งจ ์•ž๋งŒ ๋ณด๊ฑฐ๋‚˜, days[i]๊ฐ€ ๋” ์ž‘์€ ๊ฒฝ์šฐ max ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถ”๊ฐ€
2-1. ์ด ๊ฒฝ์šฐ, ๋งค ๋ฒˆ max๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์Šคํƒ์˜ ์›์†Œ๋“ค์„ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ , stack์˜ top, ๋˜๋Š” queue์˜ front๋งŒ ๋ณด๊ณ ๋„ ํ–‰๋™์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒŒ ๊ณง max๊ฐ’์ด ๋ผ์š”!

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

i = 0
while i < len(days):
        if not stk:
            stk.append(days[i])
            i += 1
        elif stk[0] >= days[i]:
            stk.append(days[i])
            i += 1
        else:
            answer.append(len(stk))
            stk.clear()

(ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ฝ”๋ฉ˜ํŠธ ์ฃผ์„ธ์š”...! ํŒŒ์ด์ฌ ๋ชจ๋ฆ„)

๊ทธ๋ž˜๋„ ์–ธ์ œ๋‚˜ ๋ฌธ์ œ๋Š” ํ’€์—ˆ๋‹ค๋ฉด ์žฅ๋•ก์ด์ฃ . ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!

์‚ญ์ œ์™€ ๋ฐ˜ํ™˜์„ ๋™์‹œ์— ํ•˜๋Š” pop()์—๋งŒ ์ง‘์ฐฉํ–ˆ์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๊น”๋”ํ•˜๊ณ  ๋” ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ์š”

์ƒ๊ฐํ•ด๋ณด๋‹ˆ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ •๋ง๋กœ ์‚ญ์ œ๋  ํ•„์š”๊ฐ€ ์—†์œผ๋‹ˆ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํ˜„๋ช…ํ•˜๋„ค์š”.. ์ž˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค โ— โ—

@kjs254
Copy link
Member Author

kjs254 commented Feb 15, 2024

โœ”๏ธ ์ €๋„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉฐ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์•˜์œผ๋‚˜ ๊ฒฐ๊ตญ ํ’€์ดํ•˜์‹  ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ์•„์˜ค๋”๋ผ๊ณ ์š”... sudo code๊ฐ€ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์…จ๊ณ , ํšจ๊ณผ์ ์ธ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ์œ„์—์„œ ๋งŽ์€ ๋ถ„๋“ค์ด ์–ธ๊ธ‰ํ•ด์ฃผ์…จ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ list, queue ์™ธ์—๋„ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ๋„ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐฐํฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ์— Linked list๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. ๋ฌผ๋ก  ๋‹ค๋ฅธ ์ž๋ฃŒ๋“ค๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
โ–ถ๏ธ ์ด ๋‚ด์šฉ์€ ํ”ผ๋“œ๋ฐฑ ํ•œ๋ฒˆ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”

โœ”๏ธ์ด ๋ฌธ์ œ์—์„œ ๋‹ค์–‘ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‚ฌ์šฉํ•ด๋ณด์‹œ๋ฉด ๋” ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

linked list์˜ ์žฅ์ ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๊ฐ„์— ์‚ฝ์ž…๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋˜์–ด๋„ ํ•ญ์ƒ $O(1)$์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ์—์„œ๋Š” ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐฐํฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ์— ์œ„์—์„œ ๋ง์”€ํ•˜์‹  deque์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

linked list ์—ญ์‹œ deque์ด ๊ฐ€์ง„ ์žฅ์ ์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ์ €๋„ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

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