문제
설명
There is a cycling road with bikes lined up in a row. If an owner wants to take out the bike located in N, next bikes should be taken out. Then, bikes must enter the end of the road in the order in which they exited. Complete a function that returns an array of bikes after they going out and in.
매개변수
- rowBikes : Array type with bikes in a regular sequence
- n : number type
아웃풋
Array should be returned.
범위
- 3 <= rowsBikes.length <= 100
- 1 <= n <= 100
Solution
n을 기준으로 0번 인덱스 요소에서 n까지 요소를 배열에 담기
마지막 인덱스 요소에서 n까지 요소를 배열에 담기
indexOf 메소드로 n의 인덱스를 찾고, reverse 메소드를 함께 사용하기
function rowBikesOut(rowBikes, n) {
let result = [];
const idx = rowBikes.indexOf(n);
let head = rowBikes.slice(0,idx);
let tail = rowBikes.slice(idx+1, rowBikes.length);
result.push(...head)
result.push(...tail.reverse());
return result;
}
반응형
'프로그래밍 > 알고리즘, 프로그래밍 언어' 카테고리의 다른 글
[BFS] 1에서 가장 먼 노드의 개수 구하기 (JavaScript) (0) | 2022.02.04 |
---|---|
[코딩테스트] identical characters (JavaScript) (0) | 2022.02.03 |
[코딩테스트] Codility 6-2 MaxProductOfThree (JavaScript) (0) | 2022.01.29 |
[코딩테스트] Codility 5-3 GenomicRangeQuery (JavaScript) (0) | 2022.01.27 |
[코딩테스트] Codility 5-4 MinAvgTwoSlice (JavaScript) - 평균의 수학적 특성 활용 (0) | 2022.01.26 |
댓글