Careercup - Facebook面试题 - 6026101998485504
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的更多相关文章
- Careercup - Facebook面试题 - 5344154741637120
		
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
 - Careercup - Facebook面试题 - 5765850736885760
		
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
 - Careercup - Facebook面试题 - 5733320654585856
		
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
 - Careercup - Facebook面试题 - 4892713614835712
		
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
 - Careercup - Facebook面试题 - 6321181669982208
		
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
 - Careercup - Facebook面试题 - 5177378863054848
		
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
 - Careercup - Facebook面试题 - 4907555595747328
		
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
 - Careercup - Facebook面试题 - 5435439490007040
		
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
 - Careercup - Facebook面试题 - 5188884744896512
		
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
 
随机推荐
- 9种CSS3炫酷图片展开预览展示动画特效
			
详细内容请点击 在线预览立即下载 这是一组共9款CSS3炫酷图片预览展示动画特效插件.css的新特性可以让我们制作出各种炫酷的动画效果.该图片预览展示动画特效就是一个很好的例子,该效果开始时图片堆叠在 ...
 - ASP.Net上传中文文件乱码
			
只要在Head中添加即可解决:<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
 - The Art of Computer Programming
			
<计算机程序设计艺术>即<The Art of Computer Programming>是计算机领域里颠峰级的里程碑,加上国外人士对它的推崇,所以提起它的大名简直就象法律书籍 ...
 - Part 48 to 51 Talking about Access Modifiers in C#
			
Part 48 Difference between Types and Type Members Part 49 Access Modifiers in C# Part 50 Internal an ...
 - C#判断奇偶数的函數
			
// 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...
 - IOS字体下载
			
结合书本与苹果官方给的例子后,总结下下载的方法. 苹果给我们提供了很多漂亮的字体,只是有些字体设备并没有内置,需要我们去下载才行. 系统提供给我们的字体名我们可以通过mac系统提供的字体册来查阅. 得 ...
 - c# js调用AjaxPro方法出错解析
			
公司的项目的框架中有一部分用到了AjaxPro这个方法,看到这个方法的我一脸懵逼,老老实实去百度了一下. AjaxPro是.NET平台下的一个回调式AJAX框架,使用简单,功能强大.顾名思义ajax, ...
 - hdu 2501 Tiling_easy version 递推
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2501 题目分析:已知有2*1,2*2,两种型号的瓷砖,要求铺满2*n的格子有多少种方法.可以考虑最左边 ...
 - storm  入门
			
Storm的典型用例有哪些呢? 流处理:正如前面的例子中所展示的,和其他流处理系统不同的是,使用Storm不需要中间队列. 连续计算:向客户端持续发送数据,以便它们能实时更新.显示结果,例如网站统计. ...
 - 获取Class对象的方法及Class类型的一些讨论
			
(1)Class.forName(className) (2)classname.Class 如果是数组,则是数组类型[].class (3)对象.getClass() 例: String path ...