-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement Moore's Voting and Iceberg Query
- Loading branch information
Yingyi Zhang
committed
Sep 6, 2017
1 parent
a9a1119
commit 0e1b070
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |