본문 바로가기
CodeSignal/Arcade

CodeSignal [34/60] extractEachKth

by 벤진[Benzene] 2020. 5. 24.

Problem:

Given array of integers, remove each kth element from it.

 

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].

 

-Summary-

1. Create a 'ans' list for answer list

2. While loop을 돌리면서 index 가 지워지는 번호에 해당하지 않는경우 ans list에 넣어준다.

3. loop을 다 돌리면 지워지는 Kth index element만 지워진 'ans' list값을 return.

 

Additional Memory 를 만들어 문제를 풀었지만, 다른 사람이 submit한것을 보니 del() 함수를 이용하여 additional memory 를 쓰지 않고 in-place 로 바로 문제를 풀수 있었다.

del inputArray[k-1::k]

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

댓글