Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
这一题如果没空间要求就没那么麻烦了
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; void more(int arr[], int n, int k) {
map<int, int> S;
for (int i = ; i < n; i++) {
if (S.find(arr[i]) != S.end()) S[arr[i]]++;
else if (S.size() == k) {
for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) {
if (it->second == ) S.erase(it->first);
else it->second--;
}
}
else S[arr[i]] = ;
}
for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) {
int tmp = ;
for (int i = ; i < n; i++) {
if (arr[i] == it->first) tmp++;
}
if (tmp > n / k) cout << it->first << " ";
}
} int main() {
int arr[] = {, , , , , , };
more(arr, , );
return ;
}
Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times的更多相关文章
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- sklearn中报错ValueError: Expected 2D array, got 1D array instead:
from sklearn.linear_model import LinearRegression lr = LinearRegression() print(tr_x.shape,tr_y.shap ...
- 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:
决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...
- [已解决]报错:ValueError: Expected 2D array, got scalar array instead
报错代码: new_x = 84610 pre_y = model.predict(new_x) print(pre_y) 报错结果: ValueError: Expected 2D array, g ...
随机推荐
- js:获得时间
<script type="text/javascript"> function bodyLoad() { var dateTime = new Date(); var ...
- 解决eclipse中overlaps the location of another project: 'xxxx'
找遍网络发现各种解释,最常见的一种是: new -> android project -> create project from exist source出现如下错误信息:Invalid ...
- NB的CSS样式集锦1——CSS3滚动条美化,CSS3滚动条皮肤
转自:http://www.pengyaou.com/codecss3/POKDNMS_112.html CSS3 -webkit-scrollbar滚动条皮肤美化实现,利用-webkit-scrol ...
- 无需Root实现Android手机屏幕流畅投影到电脑进行演示(附软件下载)
近期要在公司的会议上演示App,须要在投影仪上显示出来给大家演示.因为投影仪不是智能投影仪,仅仅能将App先投影到自己的笔记本上.然后再将笔记本上的内容投影到投影仪上.该App是个游戏,实时交互性比較 ...
- Allegro PCB查看VIA孔的pad信息
1.勾选下图选项 2.选中via孔,右键-->>>Edit 3.弹出Padatack Designer
- vivado2013.4和modelsim联合仿真
vivado2013.4和modelsim联合仿真 Hello,Panda 最近在做Zynq的项目,曾经尝试使用ISE+PlanAhe ...
- Unity3D性能优化之Draw Call Batching
在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销. Unity在运行时 ...
- ipmi 最新和MegaCli 监控磁盘和raid信息
集群监控之 —— ipmi操作指南 原创 2010年03月23日 16:45:00 标签: 集群 / 服务器 / command / callback / user / interface 12224 ...
- 很全的php数组操作方法
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- CSS3 - 鼠标移入移出时改变样式
1,使用伪类实现样式切换伪类是CSS2.1时出现的新特性,让许多原本需要JavaScript才能做出来的效果使用CSS就能实现.比如实现下面的鼠标悬停效果,只要为:hover伪类应用一组新样式即可.当 ...