Majority Number III

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.

 

Example:

Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3

 

Analysis:
根据题目知道,majority number在数组中的个数要大于 n / k,所以最多只可能有k - 1个majority number(如果有k个,那么k * n / k 大于n,显然是不可能的)。维护一个size是k - 1的HashMap,key是数组中的值,value是该值在数组中的个数。遍历整个数组,如果该值已经在map中,则相对应的value加1。如果不存在,那么先检查map的size是否小于k - 1。是的话将该值添加进map中,然后value设为1。否则我们需要将map中所有key对应的value都减去1,如果value降到0,则从map中删去这个key。当整个数组遍历完,map中的值就是所有的可能的值。
这道题只要找唯一一个值,所以我们遍历map中的所有key,找到value最大的那个key就是我们要找的。
 
Code:
 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 }

 

Complexity:
时间复杂度是O(n * k)。
 
Reference:
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/
 

 

转载于:https://www.cnblogs.com/billzhou0223/p/5150145.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The starting configuration of this puzzle is a row of cells, with disks located on cells through . The goal is to move the disks to the end of the row using a constrained set of actions. At each step, a disk can only be moved to an adjacent empty cell, or to an empty cell two spaces away if another disk is located on the intervening square. Given these restrictions, it can be seen that in many cases, no movements will be possible for the majority of the disks. For example, from the starting position, the only two options are to move the last disk from cell to cell , or to move the second-to-last disk from cell to cell . 1. [15 points] Write a function solve_identical_disks(length, n) that returns an optimal solution to the above problem as a list of moves, where length is the number of cells in the row and n is the number of disks. Each move in the solution should be a twoelement tuple of the form (from, to) indicating a disk movement from the cell from to the cell to. As suggested by its name, this function should treat all disks as being identical. Your solver for this problem should be implemented using a breadth-first graph search. The exact solution produced is not important, as long as it is of minimal length. Unlike in the previous two sections, no requirement is made with regards to the manner in which puzzle configurations are represented. Before you begin, think carefully about which data structures might be best suited for the problem, as this choice may affect the efficiency of your search
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值