๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. 1083 ์†ŒํŠธ [Gold V][python]

ttoance 2023. 6. 15. 09:00

https://www.acmicpc.net/problem/1083

N = int(input())
numList = list(map(int, input().split()))
S = int(input())

swipe = 0
while (swipe < len(numList)):
    possibleMax = numList[swipe]
    possibleMaxIndex = swipe
    count = 0
    
    # swipe ๋‹ค์Œ๋ถ€ํ„ฐ S๋ฒˆ ํšŸ์ˆ˜ ๋‚ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆซ์ž ์ฐพ๊ธฐ 
    ################ 
    for i in range(swipe + 1, len(numList)):
        # ํšŸ์ˆ˜ ๋„˜์–ด๊ฐ”์œผ๋ฉด ๋„˜์–ด๊ฐ 
        if (i - swipe) > S:
            break

        if numList[i] > possibleMax:
            possibleMax = numList[i]
            possibleMaxIndex = i
            count = i - swipe
     ################

    # swipe ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด swipe 
    ##################
    if (S - count) >= 0 and count > 0 :
        # print('check')
        del numList[possibleMaxIndex]
        numList.insert(swipe, possibleMax)

        S = S - count
	 ##################

    swipe += 1 

print(*numList)

 

์ฒ˜์Œ์ฝ๊ณ  ๋‚˜์„œ๋Š” bubble sortํ•˜๋ฉด ๋์ด๊ฒ ๋‹ค ํ•˜๊ณ  ํ’€์—ˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ์•„๋‹ˆ์—ˆ๋‹ค. 

5
1 2 3 4 5
4

์ด ์˜ˆ์‹œ์—์„œ ๋ฌธ์ œ๊ฐ€ ์›ํ•˜๋Š” ๋‹ต์€ 5 1 2 3 4 ์ด๊ณ , bubble sort๋กœ ํ•˜๋ฉด 2 3 4 5 1 ์ด๋‹ค. 

์™œ๋ƒํ•˜๋ฉด,  '์ด๋•Œ, ์†ŒํŠธํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋’ท์„œ๋Š” ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.' ๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— S๋ฒˆ ํšŸ์ˆ˜ ๋‚ด์—์„œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜์ธ 5๊ฐ€ ๋งจ ์•ž์œผ๋กœ ๋‚˜์™€์•ผ ํ•œ๋‹ค. 

 

runtime error ๋ฐ˜๋ก€>

7
70 60 50 40 30 20 10
1

>> 70 60 50 40 30 20 10

 

1% ๋ฐ˜๋ก€ >

5
5 4 1 2 3 
50

>> 5 4 3 2 1

 

 

์ฐธ๊ณ  ํ’€์ด > 

https://latte-is-horse.tistory.com/370

 

[solved.ac ๊ณจ๋“œ5] 1083_์†ŒํŠธ (ํŒŒ์ด์ฌ, ์ •๋ ฌ, ๊ทธ๋ฆฌ๋””)

https://github.com/mangbaam/CodingTest GitHub - mangbaam/CodingTest: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ๋ฐฑ์ค€ ๋“ฑ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ํ’€์ด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ๋ฐฑ์ค€ ๋“ฑ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ํ’€์ด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. Contrib

latte-is-horse.tistory.com

 

๋ฐ˜์‘ํ˜•