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

[codility] Lv2- OddOccurrencesInArray,Lv3-TapeEquilibrium ,TapeEquilibrium

by Tamii 2021. 6. 21.
๋ฐ˜์‘ํ˜•

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ์—๋งŒ ์˜์กดํ•˜๋˜ํ„ฐ๋ผ, coldility ๋ผ๋Š” ์ƒˆ๋กœ์šด ์ฝ”๋“œํ’€์ด ํ”Œ๋žซํผ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ ,

๋ฌธ์ œ๋ฅผ ์—ด์‹ฌํžˆ ํ’€์–ด๋ณด๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๋ฉด์„œ ์ฒ˜์Œ์œผ๋กœ ๋†€๋ž€ ๊ฒƒ์€ ์ฝ”๋“œ์˜ ํšจ์œจ์„ฑ์„ ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๊ฒƒ

๊ทธ๋™์•ˆ์€ ๊ทธ๋ ‡๊ฒŒ ํšจ์œจ์„ฑ์— ๋Œ€ํ•ด์„œ ๊นŠ๊ฒŒ ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ–ˆ์ง€๋งŒ, 

ํšจ์œจ์„ฑ์ด ์ข‹์ง€์•Š๊ฑฐ๋‚˜ ์ง€๋‚˜์นœ ๋ฐ˜๋ณต๋ฌธ์ด ๊ณ„์†๋˜๋Š” ์ฝ”๋“œ๋Š”.... score์—์„œ ๊นŽ์ด๊ฒŒ ๋œ๋‹ค...

 

๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ ๋ฐฐ์šด ์ 

1. ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. 

2. ์ž…๋ ฅ๊ฐ’์˜ ๋ฒ”์œ„ ํฌ๊ธฐ๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค.

3. ์‹œ์ž‘๋ณต์žก๋„์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ•˜์ž (https://rrecoder.tistory.com/63)

 


๐Ÿ“Œ   2- OddOccurrencesInArray

๋ฐฐ์—ด์—์„œ ์ž์‹ ๊ณผ ์ค‘๋ณตํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฐ’์„ return ํ•˜๋Š” ๋ฌธ์ œ

์ฒซ ํ’€์ด

def solution(A):

    cntDic={}

    for i in A:
        if i not in cntDic:
            cnt=0
            for j in A:
                if j==i:
                    cnt+=1
            cntDic[i]=cnt
    for num,cnt in cntDic.items():
        if cnt%2==1:
            return num

1) cnt ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ

2) ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ๊ฐ’์„ for๋ฌธ์„ ๋Œ๋ฉฐ ์ฐพ์•„์„œ ๋”ํ•ด์คŒ

3) ๊ทธ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๋Œ๋ฉฐ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฐ’์„ ๋”ํ•ด์คŒ

 

๊ฒฐ๊ณผ๋Š” ์ฒ˜์ฐธํ–ˆ๋‹ค. 

์ผ๋‹จ bigNumber๋ฅผ ์ „ํ˜€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•˜๋˜ ํ„ฐ์ด๊ธฐ์— ์„ฑ๋Šฅํ…Œ์ŠคํŠธ์—์„œ ์ „๋ถ€ ๋นต์ !

์ผ๋‹จ Detected time complexity:O(N**2)  ใ„น์ฆ‰ ์‹œ๊ฐ„๋ณต์žก๋„ ํ•œ๊ณ„๊ฐ€ O(N**2)์ธ๋ฐ

 

for๋ฌธ ์•ˆ์—์„œ if ๋ฌธ์„ ํ†ตํ•ด ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ N^2 

๊ทผ๋ฐ ๋‚˜๋Š” for ๋ฌธ ์•ˆ์—์„œ if ๋ฌธ ์•ˆ์—์„œ for ๋ฌธ ? 

 

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

def solution(A):
    if len(A) ==1:
        return A[0]
    A = sorted(A)

    for i in range(0,len(A),2):
        if i+1 == len(A):
            return A[i]
        if A[i]!=A[i+1]:
            return A[i]

์‹œ๊ฐ„๋ณต์žก๋„ : O(N) or O(N*logN)

sorted์˜ ์‹œ๊ฐ„๋ณต์žก๋„ : O(N*logN)

์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋”ฐ๋ผ ๋‘๊ฐœ์”ฉ for ๋ฌธ์„ ๋Œ๋ฉฐ ๋’ค์˜ ๊ฐ’๊ณผ ๋‹ค๋ฅด๋ฉด !

 

์ฐธ๊ณ  :https://wayhome25.github.io/algorithm/2017/04/30/OddOccurrencesInArray/


๐Ÿ“Œ  STEP 3-1 ) FrogJump 

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

def solution(X, Y, D):
    Y-=X

    if Y%D ==0:
        return Y//D
    else:
        return Y//D+1

๊ฐœ๊ตฌ๋ฆฌ๊ฐ€ ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ์†Œ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ , 

์ƒ๊ฐ์„ ์กฐ๊ธˆ ํ•ด๋ณด๋‹ˆ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค..

 


๐Ÿ“Œ  STEP 3-2 ) PermMissingElem

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

def solution(A):
    if len(A)==1:
        return A[0]
    A=sorted(A)
    
    for idx in range(1,len(A)+1):

        if A[idx]-1 != A[idx-1]:
            return A[idx]-1

1) A๋ฅผ ์ •๋ ฌํ•œ ํ›„

2) A๋ฅผ ๋Œ๋ฉฐ ํ˜„์žฌ ๊ฐ’+1 ๊ณผ ๋‹ค์Œ๊ฐ’์ด ๊ฐ™์ง€ ์•Š์œผ๋ฉด

3) ํ˜„์žฌ๊ฐ’+1์ด์—†๋Š”๊ฒƒ์ด๋‹ˆ return 

ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ดํ–ˆ๋Š”๋ฐ  ์˜ค๋‹ต์ด ๋˜์—ˆ๋‹ค.

๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋Š”๊ฑธ ๋ณด๋‹ˆ ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ๋ฒ”์œ„์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์ž‡๋Š” ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

๊ณ ๋กœ ์ •๋ ฌ์€ ํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค!

 

 

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

def solution(A):
	idxArr= [0]* (len(A)+1)
    
    for i in A:
    	idxArr[i-1]=1
    
    return idxArr.index(0)+1

1) A์˜ ์ธ๋ฑ์Šค ๋ฐฐ์—ด์„ ์ƒ์„ฑ

2) A๋ฅผ ๋Œ๋ฉฐ A์˜ ๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ

 

idxArr = [1,1,1,0,1] ์ด ๊ฐ’์ด ๋˜๋ฉด์„œ ์ฑ„์›Œ์ง€์ง€ ์•Š์€ ๋ถ€๋ถ„์˜ idx+1 ๊ฐ’์ด ๋‹ต์ด ๋œ๋‹ค.

 

 

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

def solution(A):
	return sum(range(len(A)+2)) - sum(A)

[1,2,3,5]   15-11 = 4 ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ

[0,1,2,3,4,5] - [1,2,3,5] ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 


๐Ÿ“Œ  STEP 3-3 ) TapeEquilibrium

๋‚ด ํ’€์ด1 ( 53% )

def solution(A):
    sumArr=[]
    length=len(A)
    for i in range(1,len(A)):
        sumArr.append(abs(sum(A[:i])-sum(A[i:length])))

    return min(sumArr)

1) ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ํ•ด๋‹น ์ˆซ์ž์˜ ๋ฒ”์œ„๊นŒ์ง€ slicingํ•œ ๋ฐฐ์—ด์˜ ํ•ฉ๊ณผ ์ฐจ๋ฅผ ๊ตฌํ•œ๋‹ค

2) ๊ทธ ๊ฐ’์„ sumArr์— ๋„ฃ๊ณ  ์ตœ์ข…๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

์•„๋งˆ min๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ์š”ํ•œ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•œ๋‹ค.

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

์—ญ์‹œ ๋ชจ๋“ ๋‚  ๋ชจ๋“  ์ˆœ๊ฐ„ ๋Š๋ ธ๋‹ค..

 

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

def solution(A):
    firstSum=0
    lastSum=sum(A)
    minSum=None
    for i in range(1,len(A)):
        firstSum +=A[i-1]
        lastSum-=A[i-1]
        diff = abs(firstSum-lastSum)
        if minSum==None:
            minSum = diff
        else:
            minSum = min(minSum,diff)
    return minSum

๋‚˜์™€ ๋‹ค๋ฅธ ์  (์ฆ‰ ์–ด๋””์—์„œ ์‹œ๊ฐ„์„ ๊ฐ์†Œ์‹œ์ผฐ๋‚˜.)

1) Sum์€ ํ•œ๋ฒˆ๋งŒ ํ•œ๋‹ค 

๊ทธ ํ›„ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ์œ„์น˜์— ๋”ฐ๋ฅธ ๊ฐ’์„ ๋”ํ•˜๊ณ  ๋นผ์ค€๋‹ค

2) min์€ ํ•„์š”์— ์˜ํ•ด ํ•˜์ง€๋งŒ ๋งŽ์€ ๊ฐ’๋“ค์„ ํ•œ๋ฒˆ์— ๋„ฃ์€ ํ›„ ํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋•Œ๊ทธ๋•Œ ์ž‘์€ ๋ฒ”์œ„๋กœ ํ•œ๋‹ค.

๋Œ“๊ธ€