7/27 Coding Test

๊น€ํƒœ์ค€ยท2023๋…„ 7์›” 28์ผ
0

Coding Test - BOJ

๋ชฉ๋ก ๋ณด๊ธฐ
37/64
post-thumbnail

โœ… BOJ Implementation

๐ŸŽˆ 1138 ํ•œ ์ค„๋กœ ์„œ๊ธฐ

n๋ช…์˜ ์‚ฌ๋žŒ๋“ค์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
ํ•ด๋‹น ๋ฐฐ์—ด์—๋Š” ์ž๊ธฐ๋ณด๋‹ค ํ‚ค๊ฐ€ ํฐ ์‚ฌ๋žŒ์ด ์™ผ์ชฝ์— ๋ช‡๋ช…์ด ์žˆ์—ˆ๋Š”์ง€ ์ฃผ์–ด์ง„๋‹ค.

import sys
input = sys.stdin.readline

n = int(input())
left = list(map(int, input().split()))
answer = [0] * n
for i in range(n):
    cnt = 0
    for j in range(n):
        if left[i] == cnt and answer[j] == 0:
            answer[j] = i+1
            break
        elif answer[j] == 0:
            cnt += 1
print(' '.join(map(str, answer)))

< ํ’€์ด ๊ณผ์ • >
์ž…๋ ฅ๊ฐ’์œผ๋กœ n๊ณผ left ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ  answer ๋ฐฐ์—ด๋กœ n๊ฐœ์˜ 0์„ ์ €์žฅํ•ด๋‘”๋‹ค.
์ดํ›„ ๋‹ค์Œ๊ณผ์ •์„ ๊ฑฐ์ณ answer์— ๊ฐ’๋“ค์„ ๋„ฃ๋Š”๋‹ค.

  • for๋ฌธ์œผ๋กœ i๋ฒˆ์งธ ์ธ์›์ด ์™ผ์ชฝ์— ๋ช‡๋ช…์ด ์žˆ๋‚˜ ํ™•์ธ, cnt๋กœ ์•ž์‚ฌ๋žŒ ์ธ์› ์ˆ˜ ์ฒดํ‚น
  • ๋‘๋ฒˆ์งธ for๋ฌธ์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์ด cnt์™€ ๋™์ผํ•˜๊ณ , answer ๋ฐฐ์—ด์ด 0์ธ ๊ฒฝ์šฐ answer์˜ ํ•ด๋‹น ์นธ์— i+1 ์ž…๋ ฅํ•˜๊ณ  break ์ฒ˜๋ฆฌ
  • cnt์™€ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ cnt + 1
    -> ์ตœ์ข…์ ์œผ๋กœ answer๋ฐฐ์—ด๋งŒ ์ž…๋ ฅ๊ฐ’๊ณผ ๋™์ผํ•œ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ.
profile
To be a DataScientist

0๊ฐœ์˜ ๋Œ“๊ธ€