반응형
1째 시도: correctness와 performace 둘 다 100%긴 한데 코드가 지저분하다.
def solution(A, K): # given an array A consisting of N integers and an integer K, returns the array A rotated K times.
N = len(A)
if N<2:
return A
if K>=N:
K = K%N
B=[]
for i in range(0, K):
B.append(A[N-K+i])
for j in range(0, N-K):
B.append(A[j])
return B
2째 시도: python의 list slicing 방법을 알게되어 적용했다. 좀 더 깔끔하게 코딩했다.
def solution(A, K): # given an array A consisting of N integers and an integer K, returns the array A rotated K times.
N = len(A)
if N<2:
return A
if K>=N:
K = K%N
return A[N-K:]+A[:N-K]
아직까지는 간단한 문제인 것 같다~~
'코딩 > 코딜리티 문제 풀기' 카테고리의 다른 글
코딜리티 2-2 OddOccurrencesInArray (python) (1) | 2020.11.09 |
---|---|
코딜리티 1 Binary Gap (python) + 티스토리 코드블럭 예쁘게하기 (1) | 2020.11.06 |
Codility (코딜리티) 로 코딩 연습하기! (0) | 2020.11.06 |
댓글