2 parents c15bfba + b6e1ba9 commit e900ccaCopy full SHA for e900cca
1 file changed
leetcode3/이진희/169. Majority Element.java
@@ -0,0 +1,30 @@
1
+/*
2
+
3
+1. 아이디어 : 공간복잡도를 O(1)로 유지하기 위해, 배열을 미리 정렬 후 완전탐색
4
+ 9ms, 37.40%가 나왔으므로, 좋은 풀이는 아닌것같음..
5
6
+2. 시간복잡도 : O(NlogN)
7
8
+3. 자료구조/알고리즘 : 완전탐색, 정렬
9
10
+ */
11
12
+class Solution {
13
+ public int majorityElement(int[] nums) {
14
+ int idx = 0;
15
+ double base = (double)nums.length/2;
16
+ int cnt = 1;
17
18
+ for(int i=1; i<nums.length; i++) {
19
+ int num = nums[idx];
20
21
+ if(cnt > base) return second;
22
+ if( != second) {
23
+ idx = i;
24
+ }
25
+ else cnt++;
26
27
28
+ return nums[idx];
29
30
+}
0 commit comments