标签:
Question:
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k
of the size of the array.
Find it.
Given [3,1,2,3,2,3,3,4,4,4]
and k=3
, return 3
.
1 public class Solution { 2 /** 3 * @param nums: A list of integers 4 * @param k: As described 5 * @return: The majority number 6 */ 7 public int majorityNumber(ArrayList<Integer> nums, int k) { 8 if(nums == null || nums.size() == 0) { 9 return Integer.MIN_VALUE; 10 } 11 12 //most k - 1 values in the map 13 HashMap<Integer, Integer> counters = new HashMap<Integer, Integer>(); 14 for(int num : nums) { 15 if(counters.containsKey(num)) { 16 counters.put(num, counters.get(num) + 1); 17 }else { 18 if(counters.size() < k - 1) { 19 counters.put(num, 1); 20 }else { 21 update(counters); 22 } 23 } 24 } 25 26 //corner case 27 if(counters.size() == 0) { 28 return Integer.MIN_VALUE; 29 } 30 31 //clear count 32 for(int key : counters.keySet()) { 33 counters.put(key, 0); 34 } 35 36 //recalculate the counter 37 for(int num : nums) { 38 if(counters.containsKey(num)) { 39 counters.put(num, counters.get(num) + 1); 40 } 41 } 42 43 int result = 0, maxCount = Integer.MIN_VALUE; 44 for(int key : counters.keySet()) { 45 int count = counters.get(key); 46 if(count > maxCount) { 47 result = key; 48 maxCount = count; 49 } 50 } 51 52 return result; 53 } 54 55 private void update(HashMap<Integer, Integer> counters) { 56 ArrayList<Integer> removeList = new ArrayList<Integer>(); 57 for(int key : counters.keySet()) { 58 counters.put(key, counters.get(key) - 1); 59 if(counters.get(key) == 0) { 60 removeList.add(key); 61 } 62 } 63 64 for(int key : removeList) { 65 counters.remove(key); 66 } 67 } 68 }
标签:
原文地址:http://www.cnblogs.com/billzhou0223/p/5150145.html