
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
Remove Duplicates from Sorted Array - LeetCode
Can you solve this real interview question? Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element ap
leetcode.com
์ฃผ์ด์ง ๋ฐฐ์ด์์ ์ค๋ณต๋๋ ์์๋ฅผ ์ ๊ฑฐํ๋ผ.
- ์
๋ ฅ
- nums | int arrray := ์ค๋ณต๋ ์์๊ฐ ํฌํจ๋ ๋ฐฐ์ด, | nums | ∈ [1, 3 * 10^4]
- nums | int arrray := ์ค๋ณต๋ ์์๊ฐ ํฌํจ๋ ๋ฐฐ์ด, | nums | ∈ [1, 3 * 10^4]
- ์ถ๋ ฅ
- k | int := nums ๋ฐฐ์ด์์ unique ํ ์์์ ๊ฐ์
- ์กฐ๊ฑด
- | nums[i] | ∈ [-100, 100]
- nums ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋์ด ์๋ค.
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
Two Pointer ๋ฐฉ์์ ์๊ฐํ๋ค.
์ฒ์์๋ visited ๋ฐฐ์ด์ ํ๋ ์์ฑํ๋ค. ๊ทธ๋ฆฌ๊ณ rp๋ก nums ๋ฐฐ์ด์ ์ํํ๋ฉด์ ํ ๋ฒ ์ด์ ๋์จ ์์๋ visited ๋ฐฐ์ด์ ์ฒดํฌํด๋๋ค.
visited ๋ฐฐ์ด์ ์ฒดํฌ๋์ง ์์ ์์๋ค์ lp์ ์ ์ฅํ๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก๋ ๋ฌธ์ ์์ ์ฃผ์ด์ง๋ nums ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด์๊ธฐ ๋๋ฌธ์ Two Pointer๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋จ์ํ๊ฒ ํด๊ฒฐํ ์ ์์๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
nums์ ๊ธธ์ด๊ฐ ์ต๋ 30,000์ด๋ฏ๋ก for๋ฌธ์ ์ด์ฉํ์ฌ ํ๋์ฉ ํ์ํด๋ ๋๋ค.
๐ ์์ฌ ์ฝ๋
visited = -100๋ถํฐ 100๊น์ง ์๋ฅผ ๋ํ๋ด๋ ๋ฐฐ์ด
lp, rp = 0, 0
while (rp๊ฐ nums๋ฅผ ๋์ง ์๋ ๋์) {
if (visited[nums[rp]]๊ฐ ์ฒดํฌ๋์ด ์์ง ์์ผ๋ฉด) {
nums[lp]์ nums[rp]๋ฅผ ๋์
ํ๋ค.
lp++;
}
rp++;
}
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public int removeDuplicates(int[] nums) {
int[] visited = new int[201];
int lp = 0;
int rp = 0;
while (rp < nums.length) {
if (nums[rp] >= 0 && visited[nums[rp]] == 0) {
visited[nums[rp]] = 1;
nums[lp] = nums[rp];
lp++;
} else if (nums[rp] < 0 && visited[-(nums[rp]-100)] == 0) {
visited[-(nums[rp]-100)] = 1;
nums[lp] = nums[rp];
lp++;
}
rp++;
}
return lp;
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์์ผ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค. N์ ์ต๋ 30,000 ์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
์๊ฐ ๋ณต์ก๋๋ O(N)์ผ๋ก ๋์์ง ์์ง๋ง ์ฝ๋๊ฐ ๋ณต์กํ๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
๋ฌธ์ ์์ nums๋ ์ด๋ฏธ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋์ด ์๋ค๊ณ ํ๋ค. ํด๋น ์กฐ๊ฑด์ ๋์ณค๋คใ
๊ทธ๋ ๋ค๋ฉด ๊ฐ์ ์ฒดํฌํ๋ visited ๋ฐฐ์ด์ ์ฌ์ฉํ์ง ์๊ณ ๋จ์ํ ๋ฐ๋ก ์ด์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ํด๊ฒฐํ ์ ์๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public int removeDuplicates(int[] nums) {
int lp = 1;
for (int rp = 1; rp < nums.length; rp++) {
if (nums[rp] != nums[rp-1]) {
nums[lp] = nums[rp];
lp++;
}
}
return lp;
}
}
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์์ผ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค. N์ ์ต๋ 30,000 ์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
๐ ๋ด ์ฝ๋์ ์ ์ฉํด๋ณด๊ธฐ

๋ฌธ์ ์์ nums๋ ์ด๋ฏธ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋์ด ์๋ค๊ณ ํ๋ค.
๊ทธ๋ ๋ค๋ฉด ๊ฐ์ ์ฒดํฌํ๋ visited ๋ฐฐ์ด์ ์ฌ์ฉํ์ง ์๊ณ ๋จ์ํ ๋ฐ๋ก ์ด์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ํด๊ฒฐํ ์ ์๋ค.
class Solution {
public int removeDuplicates(int[] nums) {
int lp = 1;
int rp = 1;
while (rp < nums.length) {
if (nums[rp] != nums[lp-1]) {
nums[lp] = nums[rp];
lp++;
}
rp++;
}
return lp;
}
}
๋๋ ์
๋์ฑ ์ฝ๊ณ ๊ฐ๊ฒฐํ๊ฒ ๊ตฌํํ ์ ์๋ ๋ฌธ์ ์๋๋ฐ ์ฌ์ํ ์กฐ๊ฑด์ ๋์ณ ์๊ฐ๋ ์ค๋ ๊ฑธ๋ฆฌ๊ณ ์ฝ๋๋ ๋ณต์กํด์ก๋ค.
์์ผ๋ก๋ ์กฐ๊ฑด ํ๋๋ฅผ ๋์ณ ์ด๋ ต๊ฒ ์๊ฐํ๋ ์ผ์ด ์๋๋ก ๋ฌธ์ ๋ฅผ ๊ผผ๊ผผํ ์ฝ์!
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Week 1-1. [Array / String] 189. Rotate Array (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] 27. Remove Element (0) | 2023.08.23 |
| Week 1-1. [Array / String] 88. Merge Sorted Array (0) | 2023.08.23 |