๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/rotate-array/?envType=study-plan-v2&envId=top-interview-150
Rotate Array - LeetCode
Can you solve this real interview question? Rotate Array - Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 step
leetcode.com
์ฃผ์ด์ง ๋ฐฐ์ด์ ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก k ๋งํผ rotate ํ๋ผ.
- ์
๋ ฅ
- nums | int arrray := ์ ์ ๋ฐฐ์ด, | nums | ∈ [1, 10^5]
- ์ถ๋ ฅ
- nums | int arrray := rotate ๋ ๋ฐฐ์ด, | nums | ∈ [1, 10^5]
- ์กฐ๊ฑด
- | nums[i] | ∈ [-2^31, 2^31 - 1]
- | k | ∈ [0, 10^5]
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
์ถ๊ฐ Array ๊ณต๊ฐ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค.
์ฐ์ nums ๋ฐฐ์ด์ ๊ฐ์ copied ๋ฐฐ์ด์ ๋ณต์ฌํด๋๋ค.
k๋งํผ rotate ํ ์ธ๋ฑ์ค(=idx)๋ฅผ ๊ตฌํ ๋ค, nums[idx]์ copied ๋ฐฐ์ด์ ๊ฐ์ ๋์ ํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
nums์ ๊ธธ์ด๊ฐ ์ต๋ 100,000์ด๋ฏ๋ก O(N)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ๋ ๋จ์ผ for๋ฌธ์ผ๋ก ๊ตฌํํ ๊ฒ์ด๋ค.
๐ ์์ฌ ์ฝ๋
n = nums ๋ฐฐ์ด ๊ธธ์ด
copied = nums๋ฅผ ๋ณต์ฌํ ๋ฐฐ์ด
for (0๋ถํฐ; n๊น์ง; 1์ฉ ์ฆ๊ฐ) {
idx = (i + k) % n
nums[idx]์ copied[i] ๋์
}
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int[] copied = new int[n];
for (int i=0; i<n; i++) {
copied[i] = nums[i];
}
for (int i=0; i<n; i++) {
int idx = (i+k) % n;
nums[idx] = copied[i];
}
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์์ผ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
N์ ์ต๋ 100,000์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
๋ณดํต 1์ด ์ ํ์ด๋ผ๊ณ ํ ๋, O(N)์ ์ฝ 1์ต๊น์ง O(N^2)์ ์ฝ 1๋ง๊น์ง ๊ฐ๋นํ ์ ์๋ค.
๋ฐ๋ผ์ ํด๋น ๋ฌธ์ ๋ฅผ 2์ค for๋ฌธ์ผ๋ก ๊ตฌํํ๋ค๋ฉด ํฐ์ง๊ฒ ๋ ๊ฒ์ด๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
copied ๋ฐฐ์ด์ ๊ฐ์ ๋ณต์ฌํ ๋, ์์์ ๊ตฌํํ ๊ฒ์ฒ๋ผ for๋ฌธ์ ์ด์ฉํด ๊ฐ์ ํ๋์ฉ ๋์ ํ ์๋ ์์ง๋ง Arrays.copyOf() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ๋จํ ๊ฐ์ ๋์์ ๊ตฌํํ ์ ์๋ค.
int n = nums.length;
int[] copied = Arrays.copyOf(nums, n); // ์๋ณธ ๋ฐฐ์ด, ๋ณต์ฌํ ๊ธธ์ด
๊ทธ๋ฆฌ๊ณ ๋ง์ฝ nums์ ๊ธธ์ด(= n)๊ฐ 5๋ผ๊ณ ๊ฐ์ ํ๋ค๋ฉด, ์ด ๋ 1 ๋งํผ rotate ํ๋ ๊ฒ๊ณผ 6๋งํผ rotate ํ๋ ๊ฒ์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ค.
๋ฌธ์ ์์ k๊ฐ ์ต๋ 100,000 ์ด๋ k %= n ์ผ๋ก k์ ๊ฐ์ ๋ฎ์ถ๋ ๊ฒ๋ ํจ์จ์ ์ผ๋ก ๊ณ์ฐํ๋๋ฐ ๋์์ด ๋ ๊ฒ์ด๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
k %= len;
reverse(nums, 0, len - 1); // step1. ์ ์ฒด reverse [7,6,5,4,3,2,1]
reverse(nums, 0, k - 1); // step2. 0๋ถํฐ k-1๊น์ง reverse [ 5,6,7,4,3,2,1 ]
reverse(nums, k, len - 1); // step3. k๋ถํฐ ๋ง์ง๋ง๊น์ง reverse [ 5,6,7,1,2,3,4 ]
}
public void reverse(int[] nums, int start, int end){
while (start < end){
// ์ ๋ ์์ swap ๊ณผ์ ๋ฐ๋ณต
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
while๋ฌธ์ ์ฌ์ฉํ์ฌ ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
๋๋ ์
k %= len ์ผ๋ก ๋ถํ์ํ ์ฐ์ฐ ๊ณผ์ ์ ์ค์ผ ์ ์์๋ค.
์ต์ ํ ํ ์ ์๋ ๋ถ๋ถ์ ๊ผผ๊ผผํ ์ฐพ๋ ์ฐ์ต์ ํด์ผ๊ฒ ๋ค.
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Week 1-1. [Array / String] 122. Best Time to Buy and Sell Stock II (0) | 2023.08.24 |
---|---|
Week 1-1. [Array / String] 121. Best Time to Buy and Sell Stock (0) | 2023.08.24 |
Week 1-1. [Array / String] 169. Majority Element (0) | 2023.08.23 |
Week 1-1. [Array / String] 80. Remove Duplicates from Sorted Array II (0) | 2023.08.23 |
Week 1-1. [Array / String] 26. Remove Duplicates from Sorted Array (0) | 2023.08.23 |