
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/majority-element/?envType=study-plan-v2&envId=top-interview-150
Majority Element - LeetCode
Can you solve this real interview question? Majority Element - Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists
leetcode.com
์ฃผ์ด์ง ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ n์ด๋ผ๊ณ ํ ๋, ๋ฐฐ์ด์ n/2๊ฐ ์ด์ ์กด์ฌํ๋ ์์๋ฅผ ๋ฐํํ๋ผ.
- ์
๋ ฅ
- nums | int arrray := ์ค๋ณต๋ ์์๊ฐ ํฌํจ๋ ๋ฐฐ์ด, | nums | == n
- ์ถ๋ ฅ
- k | int := ๋ฐฐ์ด์์ ๋ฐ ์ด์์ ์ฐจ์งํ๋ ์์
- ์กฐ๊ฑด
- | n | ∈ [1, 5 * 10^4]
- | nums[i] | ∈ [-10^9, 10^9]
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
ํด์๋งต์ ์ด์ฉํด ํน์ ์์๋ฅผ key๋ก, ํด๋น ์์์ ๋น๋์๋ฅผ value๋ก ์ ์ฅํ๋ค.
๋ง์ง๋ง์ผ๋ก ๋น๋์๊ฐ ๊ฐ์ฅ ๋์ ์์๋ฅผ ๋ฐํํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
nums์ ๊ธธ์ด๊ฐ ์ต๋ 50,000์ด๋ฏ๋ก for๋ฌธ์ ์ด์ฉํ์ฌ ํ๋์ฉ ํ์ํด๋ ๋๋ค.
HashMap์ ์ด์ฉํ์ฌ ์์์ ๊ฐ๊ฐ์ ๋น๋์๋ฅผ ์ ์ฅํ ๊ฒ์ด๋ค.
๐ ์์ฌ ์ฝ๋
hm = ์ซ์์ ๋น๋์๋ฅผ ๋ด์ ํด์๋งต
for (0๋ถํฐ, nums ๋๊น์ง, 1์ฉ ์ฆ๊ฐ) {
key๊ฐ i์ธ ์์์ value๋ฅผ 1 ์ฆ๊ฐ
}
ํด์๋งต์์ value๊ฐ ๊ฐ์ฅ ํฐ key๊ฐ ๋ฆฌํด
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
hm.put(nums[i], hm.getOrDefault(nums[i], 0) + 1);
}
int max_value = -1;
int max_key = -1;
for (Map.Entry<Integer, Integer> pair : hm.entrySet()) {
if (pair.getValue() > max_value) {
max_value = pair.getValue();
max_key = pair.getKey();
}
}
return max_key;
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์์ผ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
N์ ์ต๋ 50,000 ์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
ํ์ฌ ์ฝ๋๋ ํด์๋งต ์ ์ฒด๋ฅผ ์ํํ๋ฉด์ ๋น๋์(value)์ ์ต๋๋ฅผ ๊ตฌํ ๋ค ํด๋น ์์(key)๋ฅผ ๋ฆฌํดํ๋ค.
๋ฌธ์ ์์ ๋ฐฐ์ด ๊ธธ์ด์ ์ ๋ฐ ์ด์ ๋ํ๋๋ ์์๋ฅผ ๋ฆฌํดํ๋ผ๊ณ ํ์ผ๋ฏ๋ก,
ํด์๋งต์ ์ํํ๋ค๊ฐ ํน์ ์์(key)์ ๋น๋์(value)๊ฐ n/2 ์ด์์ด๋ผ๋ฉด ๋ฐ๋ก ๋ฆฌํดํ์ฌ ์ํ๋ฅผ ์ค๋จํ ์ ์๋ค.
class Solution {
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
hm.put(nums[i], hm.getOrDefault(nums[i], 0) + 1);
}
int N = nums.length / 2;
for (Map.Entry<Integer, Integer> pair : hm.entrySet()) {
if (pair.getValue() > N) {
return pair.getKey();
}
}
return 0;
}
}
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
nums ๋ฐฐ์ด์์ ๋ฐ ์ด์ ํน์ ์์๊ฐ ๋ฑ์ฅํ๋ค๋ฉด, ๋ฐฐ์ด์ด ์ ๋ ฌ๋์์ ๋ ํด๋น ์์๋ ์ค๊ฐ ์์น๋ฅผ ์ฐจ์งํ๋ค.
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
return nums[n/2];
}
}
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๊ธธ์ด๊ฐ n์ธ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋์ ํ๊ท ์๊ฐ๋ณต์ก๋๋ O(nlog(n))์ด๋ค.
๋๋ ์
nums ๋ฐฐ์ด์์ ๋ฐ ์ด์ ํน์ ์์๊ฐ ๋ฑ์ฅํ๋ค๋ฉด, ๋ฐฐ์ด์ด ์ ๋ ฌ๋์์ ๋ ํด๋น ์์๋ ์ค๊ฐ ์์น๋ฅผ ์ฐจ์งํ๋ค.
์ ๋ฐ์์ด ์ธ์๊น์๋ค.
๊ฐ ์์์ ๋น๋์๋ฅผ ๊ณ์ฐํ ํ์ ์์ด ์ ๋ ฌํ ๋ค ์ค๊ฐ ์์น์ ์์๋ฅผ ๋ฐํํ๋ฉด ๊ฐ๋จํ๋ค.
์๊ฐํด ๋ณด์ง ๋ชปํ ์์ด๋์ด๋ฅผ ํ๋์ฉ ์ฐจ๊ณก์ฐจ๊ณก ์์๋์! ๐
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Week 1-1. [Array / String] 121. Best Time to Buy and Sell Stock (0) | 2023.08.24 |
|---|---|
| Week 1-1. [Array / String] 189. Rotate Array (0) | 2023.08.24 |
| 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 |
| Week 1-1. [Array / String] 27. Remove Element (0) | 2023.08.23 |