Skip to content

Commit

Permalink
implement Moore's Voting and Iceberg Query
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingyi Zhang committed Sep 6, 2017
1 parent a9a1119 commit 0e1b070
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Moores_Voting/IcebergQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Problem:
* Given an array with length n, find all elements appear more than ⌊ n/k ⌋ times
* */

import java.util.*;
public class IcebergQuery {
public List<Integer> findAllMajority(int[] nums, int k){
List<Integer> res = new ArrayList<>();
int[] candidates = new int[k - 1]; // at most k - 1 elements can appear more than n/k times
int[] count = new int[k - 1];
boolean inCandidate = false;
// time complexity O(k*n)
for(int num : nums){
inCandidate = false;
for(int i = 0; i < k - 1; ++i){ // check if num equals existing candidate
if(candidates[i] == num){
count[i]++;
inCandidate = true;
break;
}
}
if(inCandidate) continue;
for(int i = 0; i < k - 1; ++i){ // check if there is empty candidate position for num
if(count[i] == 0){
candidates[i] = num;
++count[i];
inCandidate = true;
break;
}
}
if(inCandidate) continue;
for(int i = 0; i < k - 1; ++i){ // reduce all count by 1. Like the histogram.
count[i]--;
}
}

// Second traverse to make sure all candidates are valid
count = new int[k - 1];
for(int num : nums){
for(int i = 0; i < k - 1; ++i){
if(num == candidates[i]){
count[i]++;
break;
}
}
}

for(int i = 0; i < count.length; ++i){
if(count[i] > nums.length / k) res.add(candidates[i]);
}

return res;
}
}

14 changes: 14 additions & 0 deletions Moores_Voting/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.*;
public class Main{
public static void main(String[] args){
int[] nums = {2, 2, 2, 2, 4, 1, 2, 4, 4, 4, 2};
MooresVoting lc = new MooresVoting();
IcebergQuery iq = new IcebergQuery();
System.out.println("The majority element is : " + lc.findMajority(nums));
List<Integer> res = iq.findAllMajority(nums, 3);
System.out.println("Iceberg Query find all majority:");
for(Integer num : res){
System.out.println(num);
}
}
}
29 changes: 29 additions & 0 deletions Moores_Voting/MooresVoting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Problem:
* Given an array of size n, find the majority element.
* The majority element is the element that appears more than ⌊ n/2 ⌋ times.
* */

public class MooresVoting{
public int findMajority(int[] nums){
int count = 0;
int candidate = 0;
for(int num : nums){
if(count == 0){
candidate = num;
++count;
}else{
if(candidate == num) ++count;
else --count;
}
}
// if we are not sure if such majority elements exists, we need to traverse array again
count = 0;
for(int num : nums){
if(num == candidate){
++count;
}
}
if(count > nums.length / 2) return candidate;
return -1; // not found the element more than [n / 2] times;
}
}

0 comments on commit 0e1b070

Please sign in to comment.