|
| 1 | +packageeasy; |
| 2 | + |
| 3 | +importjava.util.Arrays; |
| 4 | +importjava.util.HashMap; |
| 5 | +importjava.util.Map; |
| 6 | + |
| 7 | +importutils.CommonUtils; |
| 8 | + |
| 9 | +/**169. Majority Element QuestionEditorial Solution My Submissions |
| 10 | +Total Accepted: 132691 |
| 11 | +Total Submissions: 309653 |
| 12 | +Difficulty: Easy |
| 13 | +Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. |
| 14 | +
|
| 15 | +You may assume that the array is non-empty and the majority element always exist in the array. |
| 16 | +
|
| 17 | +*/ |
| 18 | +publicclassMajorityElement { |
| 19 | + |
| 20 | +publicintmajorityElement_bit_manipulation(int[]nums){ |
| 21 | +int[]bit =newint[32];//because an integer is 32 bits, so we use an array of 32 long |
| 22 | +for(intnum :nums){ |
| 23 | +for(inti =0;i <32;i++){ |
| 24 | +if((num >> (31-i) &1) ==1)bit[i]++;//this is to compute each number's ones frequency |
| 25 | + } |
| 26 | + } |
| 27 | +intres =0; |
| 28 | +//this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times |
| 29 | +for(inti =0;i <32;i++){ |
| 30 | +bit[i] =bit[i] >nums.length/2 ?1 :0;//we get rid of those that bits that are not part of the majority number |
| 31 | +res +=bit[i]*(1 << (31-i)); |
| 32 | + } |
| 33 | +returnres; |
| 34 | + } |
| 35 | + |
| 36 | +//saw a really clever solution on Discuss, though it didn't use bit manipulatoin |
| 37 | +//this is actually applying a famous algorithm called Moore Voting algorithm: http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.html |
| 38 | +publicintmajorityElement_moore_voting_algorithm(int[]nums){ |
| 39 | +intcount =1,majority =nums[0]; |
| 40 | +for(inti =1;i <nums.length;i++){ |
| 41 | +if(count ==0){ |
| 42 | +count++; |
| 43 | +majority =nums[i]; |
| 44 | + }elseif(nums[i] ==majority){ |
| 45 | +count++; |
| 46 | + }elsecount--; |
| 47 | + } |
| 48 | +returnmajority; |
| 49 | + } |
| 50 | + |
| 51 | +publicstaticvoidmain(String...strings){ |
| 52 | +int[]nums =newint[]{1,2,3,4,2,3,2,2,4,2}; |
| 53 | +MajorityElementtest =newMajorityElement(); |
| 54 | +System.out.println(test.majorityElement_bit_manipulation(nums)); |
| 55 | + } |
| 56 | + |
| 57 | +//my natural idea is to either compute the frequency of each unique number or sort it and return the median, I can hardly think of |
| 58 | +//how bit manipulation could come into play for this question |
| 59 | +//this is O(n) time. |
| 60 | +publicintmajorityElement_compute_frequency(int[]nums) { |
| 61 | +Map<Integer,Integer>map =newHashMap(); |
| 62 | +for(inti :nums){ |
| 63 | +map.put(i,map.getOrDefault(i,0) +1); |
| 64 | +if(map.get(i) >nums.length/2)returni; |
| 65 | + } |
| 66 | +return -1; |
| 67 | + } |
| 68 | + |
| 69 | +//This is O(nlogn) time. |
| 70 | +publicintmajorityElement_sort(int[]nums) { |
| 71 | +Arrays.sort(nums); |
| 72 | +returnnums[nums.length/2]; |
| 73 | + } |
| 74 | +} |