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 ...
随机推荐
- 学习Slim Framework for PHP v3 ( 二)
昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...
- shell常用代码积累
1.使用getopts从命令行接收参数 例: while getopts h:u:p: OPTION do case $OPTION in h) echo "主机地址:$OPTARG&quo ...
- 第六十八篇、OC_按照某一字段对数值进行排序
代码中是根据"create_time_" 进行排序 ascending:决定的是升序还是降序排序 NSSortDescriptor *sortDescriptor = [[NS ...
- 【学习笔记】【C语言】关键字
1.关键字就是C语言提供的有特殊含义的符号,也叫做“保留字” *C语言一共提供了32个关键字,这些关键字都被C语言赋予了特殊含义 auto double int struct break else l ...
- 关于IIS中WEB网站访问弹“验证输入框”及“401限制访问”的解决办法
最近在配置IIS网站的过程中,不管是本地还是外部访问配置的网站,出现了需要输入用户名.密码.及域的对话框,解决之后又出现页面401限制访问的错误页面. 就这两项错误,依次做一下解决办法的整理. 解决流 ...
- 20141017--循环语句whlie,do
int n = 0; ; ) {//n必须小于100,如果等于100则会再进来进行一次运算,变为101. //因为下面的语句中用到了continue,状态改变n++不能用到最后 n++; ) { m ...
- java集合(交集,并集,差集)
说明:这里没有求差集的代码,有了交集和并集,差集=并集-交集 package com; import java.util.ArrayList; import java.util.HashS ...
- 杭电ACM2098--分拆素数和
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2098 这是源码.其实我本不想拿出源码,毕竟源码很容易被复制. 我这里刚开始出错的地方有 0_0_12811 ...
- [Silverlight] Visual Studio2010不能安装Silverlight4_Tools,提示语言不一致
今天在装Silverlight4_Tools时出现“必须先安装与 Silverlight Tools 4 语言版本相一致的 Visual Studio 2010.Visual Web Develope ...
- C++ Maps 映射
C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空 ...