๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[codility] Lesson 4) FrogRiverOne MaxCounter

by Tamii 2021. 7. 10.
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ   4-1) - FrogRiverOne

์ฃผ์–ด์ง„ 1๋ถ€ํ„ฐ ์ฃผ์–ด์ง„ X๊ฐ’์ด ๋‚˜์˜จ ๊ฐ€์žฅ ๋น ๋ฅธ idx๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

 

 

์ฒซ ํ’€์ด (54%)

def solution(X, A):
    idArr = [0]*X
    for i in range(len(A)):
        idArr[A[i]-1]=1

        if sum(idArr)==X:
            return i
    return -1

1) idArr ์ƒ์„ฑ

2) A๋ฅผ ๋Œ๋ฉฐ ํ•ด๋‹น ๊ฐ’์„ idx๋กœ ๋„ฃ์–ด์คŒ 

3) idArr๊ฐ€ ๋‹ค ์ฐผ์„ ๋•Œ return

 

์—ญ์‹œ๋‚˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์ธํ•˜์—ฌ ์ ์ˆ˜๋ฅด ๋ฐ˜์ด๋‚˜ ๊นŽ์˜€๋‹ค.

์ œํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N ** 2)

 

 

์ด๋ฏธ for + if์˜ ์กฐํ•ฉ์œผ๋กœ N^2์ด ๋˜์–ด์žˆ์—‡๊ณ  

๊ทธ์•ˆ์—์„œ sum์—ฐ์‚ฐ๊นŒ์ง€ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ์ž๋ผ๊ณ  sum์„ ์—†์• ์•ผ ํ•œ๋‹ค!!

 

๋‹ค๋ฅธ ํ’€์ด (100%)

def solution(X, A):
    idArr = [0]*X
    idSum=0

    for i in range(len(A)):
        if idArr[A[i]-1]==0:
            idArr[A[i]-1]=1
            idSum+=1

        if idSum==X:
            return i
    return -1

์—ญ์‹œ๋‚˜ sum์„ ์—†์• ๋‹ˆ ํ•ด๊ฒฐ

๊ฐœ์„ ํ•œ์  1) ์–ด์ฐจํ”ผ for ์•ˆ์— if๊ฐ€ ์žˆ์„๊ฑฐ๋ฉด ์—ฐ์‚ฐ ์ž์ฒด๋„ ์กฐ๊ฑด์— ํ•ด๋‹นํ•  ๋•Œ๋งŒ ํ•˜๊ฒŒ ํ•จ

๊ฐœ์„ ํ•œ์  2) ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ๋•Œ๋งˆ๋‹ค Sum์„ ํ•˜์ง€ ์•Š๊ณ  idSum์„ ๋งŒ๋“ค์–ด ๋งŒ์กฑํ•จ์„ ํ™•์ธ

 


๐Ÿ“Œ   4-2) - MaxCounter

 

๋‚ด ํ’€์ด (55%)

def solution(N, A):

    ans=[0]*N
    for num in A:
        if 1<= num <=N:
            ans[num-1]+=1

        else :
            maxNum = max(ans)
            ans=[maxNum]*N

    return ans

1) A๋ฅผ ๋Œ๋ฉฐ  ์กฐ๊ฑด์„ ๋น„๊ตํ•œ๋‹ค.

2) ๊ทธ ์ˆซ์ž๊ฐ€ N+1 ์ด๋ฉด ๊ทธ ๋‹น์‹œ์˜ max๊ฐ’์œผ๋กœ ์ „์ฒด ans ๋ณ€๊ฒฝ

3) ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ฉด ํ•ด๋‹น ๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ํ•ด ans ๋ณ€๊ฒฝ

 ๋‹ค๋ฅธ ํ’€์ด (100%)

def solution(N, A):
    counters = N * [0]
    nextMax =  maxNum = 0

    for i in A:
        if i <= N:
            curr = counters[i-1] = max(counters[i-1] +1, maxNum+1)
            nextMax = max(curr,nextMax)
        else:
            maxNum =nextMax

    return [c if c > maxNum else maxNum for c in counters]

nextMax: ๋งค ์ƒํ™ฉ์˜ max ๊ฐ’

maxNum: ์กฐ๊ฑด์ด N+1์ผ ๋•Œ์˜ max ๊ฐ’ 

 

 

๋‚˜๋Š” ์กฐ๊ฑด์ด N+1์ผ๋•Œ ans ์ „์ฒด๋ฅผ ๋งค๋ฒˆ ans=[maxNum]*N  ๋‹ค์‹œ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ๋งŽ์ด ์†Œ์š”๋˜๋Š” ๋ฐ˜๋ฉด์— 

 

์กฐ๊ฑด์ด N+1์ด๋‚˜์˜ฌ ๋‹น์‹œ์˜ max๊ฐ’์„ ์ €์žฅํ•ด ๋‘๊ณ  

๋„ฃ์–ด์ค„๋•Œ๋งˆ๋‹ค ๊ทธ max+1 ๊ฐ’๊ณผ ๋„ฃ์–ด์ค„ ๊ฐ’(+1) ์„ ๋น„๊ตํ•ด ๋„ฃ์–ด์ฃผ๊ณ  

 

๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ๋„ ํƒ์ƒ‰ํ•˜์ง€ ์•Š์€ index ๋งŒ ๋งˆ์ง€๋ง‰ max๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

 

 

 

 

 

๐Ÿ’™ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์œ„ํ•ด ์ด๋ ‡๊ฒŒ ์‹ ๊ฒฝ์“ด ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ๋‹ค๋‹ˆ ๊ทธ ๋…ผ๋ฆฌ๋ ฅ์— ๋†€๋ž๋‹ค.

ํ•ต์‹ฌ์€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ๊ทธ ์กฐ๊ฑด์„ ๋งˆ์ฃผํ–ˆ์„ ๋•Œ์˜ ๋™์ž‘(์ „์ฒด๋‹ค ๋ฐ”๊ฟ”์ฃผ๊ธฐ) ์„ ์—†์•ด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 


 

๐Ÿ“Œ   4-3)  MissingInteger

๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‚˜์˜ค์ง€ ์•Š์€ ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜๋ฅผ return

์Œ์ˆ˜์ผ๋• 1, ๋‚˜์˜ค์ง€ ์•Š์€๊ฒŒ ์—†์„๋• max +1 

 

 

์ฒซ ํ’€์ด

def solution(A):

    A=list(filter(lambda x: x>0, A))
    A.sort()
    smallNum=1
    changeCnt=0
    
    if len(A)==0:
        return 1
    for i in range(len(A)-1):
        if A[i]+1!=A[i+1]:
            smallNum = A[i]+1
            changeCnt+=1

    if changeCnt==0:
        return max(A)+1
    else:
        return smallNum

1) A์—์„œ ์Œ์ˆ˜ ํ•„ํ„ฐ๋ง

2) A ๋ฐฐ์—ด์ด ๋น„์—ˆ์œผ๋ฉด 1 ๋ฆฌํ„ด

3) A ๋ฐฐ์—ด์„ ๋Œ๋ฉฐ ์—ฐ์†์ ์ด์ง€ ์•Š์€ ์ˆ˜๋ฅผ ๊ณ„์† smallNum์— ๋„ฃ์–ด์คŒ

4) ํ•œ๋ฒˆ๋„ ๋ฐ”๋€Œ์ง€ ์•Š์€ (์ฆ‰ ์—ฐ์†์ ์ธ) ๊ฐ’์€ max+1

 

๊ฒฐ๊ณผ๋Š” ์ฒ˜์ฐธ! 

์˜ˆ์™ธ์ฒ˜๋ฆฌ ์•ˆํ•œ ๋ถ€๋ถ„๊ณผ ์„ฑ๋Šฅ๋ฉด์—์„œ ๋‚ฎ์•„์กŒ๋‹ค.

 

 

๋‹ค๋ฅธ ํ’€์ด

def solution(A):
    A=list(set(A))
    A.sort()
    smallNum=1
    for i in A:

        if smallNum ==i:
            smallNum+=1

    return smallNum

smallNum์„ 1๋กœ ์žก์•„๋‘๊ณ  A๋ฅผ ๋Œ๋ฉฐ ๋น„๊ตํ•œ๋‹ค

์ด ๋กœ์ง์€ ๋ชจ๋“   ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋งŒ์กฑํ•œ๋‹ค.

 

์Œ์ˆ˜๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ : return 1

์—ฐ์†ํ•˜๋Š” ์ˆ˜๊ฐ€ ๋‹ค ์žˆ๋Š”๊ฒฝ์šฐ : return max+1

๋น ์ง€๋Š” ์ˆ˜๊ฐ€ ์žˆ๋Š”๊ฒฝ์šฐ : ์—ฐ์†ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„์—์„œ ๋ฉˆ์ถ˜ ๊ฐ’ (์ œ์ผ์ž‘์€ ๋‚˜์˜ค์ง€ ์•Š์€ ๊ฐ’)

 

 

์ฐธ๊ณ  :https://sooho-kim.tistory.com/33

 

๋Œ“๊ธ€