-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMajorityElement-ii.java
100 lines (94 loc) · 2.31 KB
/
MajorityElement-ii.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*https://leetcode.com/problems/majority-element-ii/*/
/*https://binarysearch.com/problems/Submajority-Vote*/
/* Using Extra space */
class Solution {
public List<Integer> majorityElement(int[] nums)
{
int n = nums.length, k = n/3;
List<Integer> list = new LinkedList<Integer>();
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<n;i++)
{
if(!map.containsKey(nums[i]))
{
map.put(nums[i],1);
}
else
{
map.put(nums[i],map.get(nums[i])+1);
}
if(map.get(nums[i])==k+1)
{
list.add(nums[i]);
}
}
return list;
}
}
/* Without extra space but nlogn time */
class Solution {
public List<Integer> majorityElement(int[] nums)
{
Arrays.sort(nums);
int i = 0;
int n = nums.length, k = n/3;
List<Integer> list = new LinkedList<Integer>();
while(i+k<n)
{
if(nums[i]==nums[i+k])
{
list.add(nums[i]);
i = k==0?i+1:i+k;
while(i>0 && i<n && nums[i]==nums[i-1])i++;
}
else
{
i++;
}
}
return list;
}
}
/* Please Notify me once you get any O(N) time and O(1) space solution */
/*Got it*/
class Solution {
public List majorityElement(int[] nums) {
int candidate1=0,candidate2=0,count1=0,count2=0;
int n=nums.length;
for(int i=0;i<nums.length;i++){
if(candidate1==nums[i]){
count1+=1;
}
else if(candidate2==nums[i]){
count2+=1;
}
else if(count1==0){
candidate1=nums[i];
count1=1;
}
else if(count2==0){
candidate2=nums[i];
count2=1;
}
else{
count1-=1;
count2-=1;
}
}
count1=count2=0;
for(int i=0;i<nums.length;i++){
if(candidate1==(nums[i])){
count1+=1;
}
else if(candidate2==(nums[i])){
count2+=1;
}
}
List <Integer> l = new ArrayList<>();
if(count1>(n/3))
l.add(candidate1);
if(count2>(n/3))
l.add(candidate2);
return l;
}
}