A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 6, 7]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.
Given an array a of n integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.
Function Description
Complete the function rotLeft in the editor below.
rotLeft has the following parameter(s):
- int a[n]: the array to rotate
- int d: the number of rotations
Returns
- int a'[n]: the rotated array
func rotLeft(a: [Int], d: Int) -> [Int] {
// Write your code here
var arr: [Int] = Array(a[d..<a.count] + a[0..<d])
return arr
}
// 힌트) 1<= d <=n
// 매개 변수 조작 X
// Dynamic Array처럼 이동/Resize 원리
'Computer Science > Algorithm with Code' 카테고리의 다른 글
[HackerRank] Preparation_Kit (10. Ransom Note) [Dictionaries & Hashmaps] (2) | 2024.09.27 |
---|---|
[HackerRank] Preparation_Kit (08. Minimum Swaps 2) [Array] (1) | 2024.09.26 |
[HackerRank] Preparation_Kit (05. 2D Array - DS) [Array] (0) | 2024.09.19 |
[HackerRank] Preparation_Kit (04. Repeated String) (1) | 2024.09.17 |
[HackerRank] Preparation_Kit (03. Jumping on the Clouds) (0) | 2024.09.16 |