ํฐ์คํ ๋ฆฌ ๋ทฐ
๐จโ๐ป ์ฝ๋ฉํ
์คํธ/Codility
Lesson 2: Arrays โ Cyclic Rotation
dirmathfl 2020. 6. 6. 09:12728x90
๋ฐ์ํ
๋ฌธ์
https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/
์ฃผ์ด์ง ๋ฐฐ์ด์ K์ ์๋งํผ ์ค๋ฅธ์ชฝ์ผ๋ก shiftํ ํ ๋ณํ๋ ๋ฐฐ์ด์ ๋ฐํํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
๋ฌธ์ ํ์ด
K๊ฐ ์ฃผ์ด์ง ๋ฐฐ์ด ๋ณด๋ค ํด ๊ฒฝ์ฐ, K ๊ฐ์
K %= len(A)
๋ก ๋ณ๊ฒฝํ๋ฉด K๊ฐ ํฌ๋๋ผ๋ ์ฃผ์ด์ง ๋ฐฐ์ด ํฌ๊ธฐ ๋งํผ shiftํ๋ค.
์ฝ๋
from collections import deque
def solution(A, K):
length = len(A)
if not length or length == K:
return A
if K > length:
K %= length
lst = deque(A)
lst.rotate(K)
return list(lst)
deque
๋ฅผ ํ์ฉํ๋ฉด, rotate๋ฅผ ํตํด shift๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.- ์ง์ ๊ตฌํํ๋ ๊ฒ์ ๋นํด rotate๋ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์, ๋ค๋ก shift ํ๋ค.
728x90
๋ฐ์ํ
๊ด๊ณ
๊ด๊ณ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > Codility' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Lesson 3: Complexity โ Tape Equilibrium (0) | 2020.06.06 |
---|---|
Lesson 3: Time Complexity โ Perm Missing Elem (0) | 2020.06.06 |
Lesson 3: Time Complexity โ Flog Jmp (0) | 2020.06.06 |
Lesson 2: Arrays -> Odd Occurrences In Array (0) | 2020.06.06 |
Lesson 1: Iterations โ Binary Gap (0) | 2020.06.06 |
๋๊ธ
CEO๊ธฐ๊ณ๋ ์ํค๋ ๋๋ก ๋์ํ๋ฉฐ, ์ฌ๋์ ์ค์๋ก ์ธํด ๊ธฐ๊ณ๋ ์ ์์๋ํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
ํ๋ก๊ทธ๋๋จธ์ด์ง๋ง ์ด์คํ๊ฒ ์ปดํจํฐ๋ฅผ ๋์์์ผ ์ค๋ฅ์ ์ง๋ฉดํ๋ ์ข์ถฉ์ฐ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
๋ํ ์์ํ๊ฒ ์ผ์๋ค๊ณผ ์๊ฐ๋ค์ ์ ๋ฆฌํ๊ณ ๊ธฐ๋กํ๊ณ ์์ต๋๋ค.
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ