2014-05-02 10:47

题目链接

原题:

 Given an unordered array of positive integers, create an algorithm that makes sure no group of integers of size bigger than M have the same integers. 

 Input: ,,,,,,,, M =
Output: ,,,,,,,,

题目:给定一个未排序的长度为N的整数数组,和一个正整数M。请设计算法,将N个数分成M个数一组,并且每一组都不包含重复元素。

解法:本题的要求是每个组不能出现重复的数,那么换句话说就是把重复的元素分到不同的组去。我们按打扑克时发牌的方式,将重复的牌聚在一起,然后轮流发给每个组,就能保证他们不出现在一组里了。要要重复的牌聚在一次,既可以通过排序,也可以通过哈希表来统计个数。

代码:

 // http://www.careercup.com/question?id=6026101998485504
// Actually, I don't quite understand the problem. The poster of this problem gave it too vague..
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <xiosbase>
using namespace std; class Solution {
public:
void disperse(vector<int> &v, int m) {
int n = (int)v.size(); if (n <= ) {
return;
} int count;
unordered_map<int, int> um; int i; for (i = ; i < n; ++i) {
++um[v[i]];
} unordered_map<int, int>::iterator umit, umit2;
i = ;
while (!um.empty()) {
count = ;
umit = um.begin();
while (umit != um.end()) {
v[i++] = umit->first;
--(umit->second);
++count;
if (umit->second == ) {
// remove empty items to speed up the search.
umit2 = umit;
++umit;
um.erase(umit2);
} else {
++umit;
} if (count == m) {
// pick at most m distinct elements at each round.
break;
}
}
}
};
}; int main()
{
int n, m;
vector<int> v;
Solution sol;
int i; ios::sync_with_stdio(false); while (cin >> n && n > ) {
v.resize(n); for (i = ; i < n; ++i) {
cin >> v[i];
}
cin >> m;
sol.disperse(v, m);
for (i = ; i < n; ++i) {
i ? cout << ' ', : ;
cout << v[i];
}
cout << endl; v.clear();
} return ;
}

Careercup - Facebook面试题 - 6026101998485504的更多相关文章

  1. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  2. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  3. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  4. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  5. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  6. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. 代码研磨 Slim v3 (一)--app->get()&route->add()

    index.php代码如下: $app->get('/forbase', function ($request, $response, $args){ Example\Module\Base:: ...

  2. 三.CSS层叠机制

    概述 层叠就是对样式的层叠.是某种样式在样式表中逐层叠加的过程.让浏览器对某个标签特定属性值的多个来源,最终确定使用那个值.层叠是整个CSS的核心机制. HTML文档样式的来源 1.浏览器默认样式,每 ...

  3. 各种LICENSE的作用--GET

    许 多开发者和设计者希望把他们的作品作为开源项目共享,他们希望其他人能够利用和共享他们的代码. 而各种开源社区就是因为这个原因而充满活力.开源软件可以用于你能想象得到的任何应用程序,许多web设计人员 ...

  4. Cocos2d-x移植到WindowsPhone8移植问题-libcurl库移植问题

    在Cocos2d-x 3.x最新版本中提供了Windows Phone 8平台移植libcurl库所需要的头文件和库文件.但要在Windows Phone 8平台成功移植libcurl库还是很不容易, ...

  5. web HTML5 调用摄像头的代码

    最近公司要求做一个在线拍照的功能,具体代码如下: <html> <head> <title>html5调用摄像头拍照</title> <style ...

  6. 使用Struts2 验证框架,验证信息重复多次出现

    版权声明:本文为博主原创文章,未经博主允许不得转载. 问题描述:第一次提交表单.某个数据不符合规则,就会出现一条错误信息.再次提交,上次显示的错误信息不消失,又多出一条一模一样的错误信息.提交几次,就 ...

  7. Java关键字及其作用

    Java关键字及其作用 一. 关键字总览 访问控制 private protected public             类,方法和变量修饰符 abstract class extends fin ...

  8. (转)Linux性能调优之虚拟内存篇

    简单地说,虚拟内存就是硬盘中的一块区域,它用来存放内存里使用频率不高的页面文件.好处是为了提高内存的利用率 - 让使用频率高的页面文件活动在内存区域中,提高CPU对数据操作的速度.在Linux中,这个 ...

  9. http: URL、请求方式

    URL:统一资源定位符,可以从互联网得到和访问资源,是标准资源的位置 结构包括协议.服务器名称(IP地址).端口(有的服务器需要).路径.文件名 协议:告诉浏览器如何处理打开的文件,常用的就是http ...

  10. AOJ 0558 Cheese

    Cheese Time Limit : 8 sec, Memory Limit : 65536 KB チーズ (Cheese) 問題 今年も JOI 町のチーズ工場がチーズの生産を始め,ねずみが巣から ...