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

23-alstjr7437 #79

Merged
merged 2 commits into from
May 10, 2024
Merged

23-alstjr7437 #79

merged 2 commits into from
May 10, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented May 1, 2024

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

ํ”„๋กœ์„ธ์Šค

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

15๋ถ„

โ‰๏ธ ์žก์„ค

์ค‘๊ฐ„๋ฐœํ‘œ ์ด์Šˆ๋กœ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์˜ ๋„์›€ ์š”์ฒญ์œผ๋กœ ์ธํ•ด ์กฐ๊ธˆ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค..
๊ทธ๋ž˜๋„ swift๋กœ ํ•˜๋‹ˆ๊นŒ ์กฐ๊ธˆ ์–ด๋ ต๋”๊ตฐ์š”
๊ทธ๋ฆฌ๊ณ  ์šด์˜์ฒด์ œ ๋ฐฐ์šด๊น€์— ์šฐ์„ ์ˆœ์œ„ ์Šค์ผ€์ค„๋ง์ด ์ƒ๊ฐ๋‚˜์„œ...

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

  1. ์šฐ์„ ์ˆœ์œ„ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ์ง„ํ–‰
  2. ํ˜„์žฌ 0๋ฒˆ์ด ์ œ์ผ ํฐ ์šฐ์„ ์ˆœ์œ„์ด๋ฉด ๋นผ์ค˜์•ผํ•จ
  3. ํ˜„์žฌ 0๋ฒˆ์ด ์ œ์ผ ํฐ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„๋‹ˆ๋ฉด ์ œ์ผ ๋’ค๋กœ ์˜ฎ๊ฒจ ์ค˜์•ผํ•จ (๊ทธ๋ฆฌ๊ณ  ์ˆœ์„œ๊ฐ€ ๋ฏธ๋ค„๋‚˜๋ฏ€๋กœ result += 1

๊ธฐ๋ณธ ๋กœ์ง์€ ์œ„์˜ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.



๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„์ด
location์ด ์–ธ์ œ return ๋˜๋Š”์ง€ ์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ

  1. ์šฐ์„ ์ˆœ์œ„ ๋ฆฌ์ŠคํŠธ์—์„œ ๋นผ์ฃผ๊ฒŒ ๋˜๋ฉด์„œ location์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฏ€๋กœ ์ฒ˜์Œ์— -1์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
  2. ๋งŒ์•ฝ์— max๊ฐ€ ์•„๋‹๋•Œ(์•ˆ๋นผ์ค„๋•Œ) location์ด ์Œ์ˆ˜๊ฐ€ ๋˜๋ฉด ๋‹ค์‹œ ์ตœ๋Œ€์˜ ๊ฐœ์ˆ˜๋กœ ์—…๋ฐ์ดํŠธ
  3. ๋งŒ์•ฝ์— max์ผ๋•Œ(๋นผ์ค„๋•Œ) locationa์ด ์Œ์ˆ˜๊ฐ€ ๋˜๋ฉด ๋ฉˆ์ถ”๊ณ  result ๋ฐ˜ํ™˜

์œผ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

import Foundation

func solution(_ priorities:[Int], _ location:Int) -> Int {
    var result = 0
    var priorities = priorities
    var location = location
    
    while priorities.count != 0 {
        location -= 1
        let p_max = priorities.max()!
        let temp = priorities[0]
        if p_max != temp {  // max๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ(๋นผ์•ผํ•  ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ)
            priorities.append(temp) // ๋’ค์— ๋„ฃ๊ธฐ
            priorities.removeFirst()    // ์ฒ˜์Œ๊บผ ๋นผ๊ธฐ
            if location < 0 {   // location์ด ์Œ์ˆ˜๋ฉด ์ œ์ผ ๋’ค์—์„œ ๋ถ€ํ„ฐ
                location = priorities.count - 1
            }
        } else {    // max์ผ ๊ฒฝ์šฐ(๋นผ์•ผํ•  ๊ฒฝ์šฐ)
            priorities.removeFirst()
            result += 1
            if location < 0 {   // location์ด ์ ์ ˆํ•˜๊ฒŒ ์˜ค๋ฉด ๋ฉˆ์ถ”๊ธฐ
                break
            }
        }
        
    }
    
    return result
}

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

์—„์ฒญ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ ์ž๋™ ์™„์„ฑ๋„ ์—†๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ํ•˜๋‹ˆ๊นŒ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์ด๋ž‘ ์„ž์—ฌ์„œ ๊ณ„์†ํ•ด์„œ ์ฐพ์•„๋ณด๋ฉด์„œ ํ–ˆ๋„ค์š”..

@tgyuuAn tgyuuAn merged commit 5302e78 into main May 10, 2024
@tgyuuAn tgyuuAn deleted the 23-alstjr7437 branch May 10, 2024 13:15
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