标签:
Given an array of integers, the majority number is the number that occurs more than half
of the size of the array. Find it.
Example
Given [1, 1, 1, 1, 2, 2, 2]
, return 1
分析:
既然这里只有一个majority number,那么它的个数减去其它number个数之和还是为正值。
1 public class Solution { 2 /** 3 * cnblogs.com/beiyeqingteng/ 4 */ 5 public int majorityNumber(ArrayList<Integer> nums) { 6 int number = nums.get(0); 7 int count = 1; 8 9 for (int i = 1; i < nums.size(); i++) { 10 if (count == 0) { 11 number = nums.get(i); 12 count = 1; 13 } else { 14 if (number == nums.get(i)) { 15 count++; 16 } else { 17 count--; 18 } 19 } 20 } 21 return number; 22 } 23 }
Given an array of integers, the majority number is the number that occursmore than 1/3
of the size of the array.
Example
Given [1, 1, 1, 1, 2, 2, 2]
, return 1。
At last, at least one of the two majority numbers must be more than 1 / 3 of the array.
1 public class Solution { 2 /** 3 * @param nums: A list of integers 4 * @return: The majority number that occurs more than 1/3 5 * cnblogs.com/beiyeqingteng/ 6 * 7 */ 8 public int majorityNumber(ArrayList<Integer> nums) { 9 if (nums == null || nums.size() == 0) return -1; 10 int number1 = 0, number2 = 0, count1 = 0, count2 = 0; 11 12 for (Integer i : nums) { 13 if (number1 == i) {// case 1 14 count1++; 15 } else if (number2 == i) {// case 1 16 count2++; 17 } else if (count1 == 0) {// case 2 18 number1 = i; 19 count1 = 1; 20 } else if (count2 == 0 && number1 != i) {// case 2 21 // number1 != i is optional and it will never be executed, but I put it here to make the code 22 // easier to understand. 23 number2 = i; 24 count2 = 1; 25 } else { // case 3 26 count1--; 27 count2--; 28 } 29 } 30 31 // [1,1,1,1,2,2,3,3,4,4,4] cannot pass if the code below is not added. 32 count1 = 0; 33 count2 = 0; 34 35 for (Integer i : nums) { 36 if (number1 == i) { 37 count1++; 38 } else if (number2 == i) { 39 count2++; 40 } 41 } 42 43 return count1 > count2 ? number1 : number2; 44 } 45 }
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.
Example
Given [3,1,2,3,2,3,3,4,4,4]
and k=3
, return 3
.
分析:
Same as above, as there could be at most (k – 1) elements occuring more than 1 / k of the array, we have (k – 1) slots for majority number candidates. The voting rule is the same as above.
Careful for the ConcurrentModificationException in HashMap, we should remove (write) the keys during the HashMap being iterated (read). Write the hashmap after read.
1 public class Solution { 2 /** 3 * @param nums: A list of integers 4 * @param k: As described 5 * @return: The majority number 6 * cnblogs.com/beiyeqingteng/ 7 */ 8 public int majorityNumber(ArrayList<Integer> nums, int k) { 9 if (nums == null || nums.size() == 0 || k < 2) return -1; 10 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 11 12 for (int n : nums) { 13 if (map.containsKey(n)) { 14 map.put(n, map.get(n) + 1); 15 } else { 16 // note: if we change condition to map.size() > k - 1, in this case, we assume 17 // there are at most k candidates, not k - 1, we can ignore the statements from 18 // line 27 - 35 19 if (map.size() >= k - 1) { 20 decreaseVotes(map); 21 } else { 22 map.put(n, 1); 23 } 24 } 25 } 26 27 for (int key : map.keySet()) { 28 map.put(key, 0); 29 } 30 31 for (int n : nums) { 32 if (map.containsKey(n)) { 33 map.put(n, map.get(n) + 1); 34 } 35 } 36 37 int maxKey = 0; 38 int maxCount = 0; 39 for (int key : map.keySet()) { 40 if (map.get(key) > maxCount) { 41 maxCount = map.get(key); 42 maxKey = key; 43 } 44 } 45 return maxKey; 46 47 } 48 49 private void decreaseVotes(Map<Integer, Integer> map) { 50 Set<Integer> keySet = map.keySet(); 51 List<Integer> removeList = new ArrayList<>(); 52 for (Integer key : keySet) { 53 if (map.get(key) == 1) { 54 removeList.add(key); 55 } 56 else { 57 map.put(key, map.get(key) - 1); 58 } 59 } 60 //remove candidates with 0 votes and free the slot 61 for (Integer key : removeList) { 62 map.remove(key); 63 } 64 } 65 }
Reference:
http://blog.welkinlan.com/2015/05/29/majority-number-lintcode-java/
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5628525.html