Majority Number III

Description:

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.

Note

There is only one majority number in the array.

Challenge

O(n) time and O(k) extra space

Solution:

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @param k: As described
     * @return: The majority number
     */
    int majorityNumber(vector<int> nums, int k) {
        // write your code here
        auto sz = (int)nums.size();
        unordered_map<int, int> cache;
        for (int i = 0; i < sz; ++i) {
            auto tmp = nums[i];
            if (cache.find(tmp) != cache.end())
                ++cache[tmp];
            else if (cache.size() < k)
                cache[tmp] = 1;
            else {
                vector<int> gc;
                for (auto& e : cache)
                    if (--e.second == 0)
                    gc.push_back(e.first);
                for (auto e : gc) cache.erase(e);
            }
        }
        int rc = 0, maxn = 0;
        for (auto& e : cache) {
            if (e.second > maxn) {
                maxn = e.second;
                rc = e.first;
            }
        }
        return rc;
    }
};

转载于:https://www.cnblogs.com/deofly/p/majority-number-iii.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、付费专栏及课程。

余额充值