Cracking the Code Interview (1): 跟踪数组的中位数
来源:互联网 发布:电脑网络重置后怎么办 编辑:程序博客网 时间:2024/05/22 19:57
题目:
随机生成一些数字,并保存到一个(可扩展的)数组中,如何跟踪数组的中位数?
题解:
用multiset分别实现最大堆和最小堆,最大堆保存数组的前半部分,最小堆保存数组的后半部分,每次添加新数字时和堆顶元素比较。同时保证两个堆的元素个数之差不超过1.
//// main.cpp// Combination//// Created by on 14-2-3.// Copyright (c) 2014年 . All rights reserved.//#include <iostream>#include <string>#include <vector>#include <set>#include <cstdlib>#include <ctime>using namespace std;typedef struct { bool operator()( int i, int j) { return i < j; }} Increase;typedef struct { bool operator()( int i, int j) { return i >= j; }} Decrease;typedef multiset<int, Increase> minHeap;typedef multiset<int, Decrease> maxHeap;class Solution {public: void FindMedian() { cout << "Please enter the number of integers in your array:"; int count; cin >> count; int i = 0; srand(static_cast<unsigned int>(time(0))); maxHeap heap1; minHeap heap2; while( i < count ) { int temp = rand()%100; cout << temp << " is added into the integer array" << endl; if(!heap1.size() && !heap2.size()) { heap1.insert(temp); i++; continue; } if(temp > *heap1.begin()) { heap2.insert(temp); if(static_cast<int>(heap1.size() - heap2.size()) >= 2) { int temp = *heap1.begin(); heap1.erase(heap1.begin()); heap2.insert(temp); } else if(static_cast<int>(heap2.size() - heap1.size()) >= 2) { int temp = *heap2.begin(); heap2.erase(heap2.begin()); heap1.insert(temp); } } else{ heap1.insert(temp); if(static_cast<int>(heap1.size() - heap2.size()) >= 2) { int temp = *heap1.begin(); heap1.erase(heap1.begin()); heap2.insert(temp); } else if(static_cast<int>(heap2.size() - heap1.size()) >= 2) { int temp = *heap2.begin(); heap2.erase(heap2.begin()); heap1.insert(temp); } } cout << "Intergers in the first heap (max heap) are:"; for(auto iter: heap1) cout << iter << " "; cout << endl; cout << "Intergers in the second heap (max heap) are:"; for(auto iter: heap2) cout << iter << " "; cout << endl; cout << "The median of the entire integer array is: "; if( heap1.size() > heap2.size()) cout << *heap1.begin() << endl << endl; else if(heap1.size() < heap2.size()) cout << *heap2.begin() << endl << endl; else cout << (*heap1.begin() + *heap2.begin())/2 << endl << endl; i++; } } };int main(int argc, const char * argv[]){ // insert code here...10 Solution a; a.FindMedian(); return 0;}
0 0
- Cracking the Code Interview (1): 跟踪数组的中位数
- Cracking the code interview总结
- cracking the code interview problem 1.1
- cracking the code interview problem 1.3
- cracking the code interview balanced tree python
- cracking the code interview LCA Python
- 《Cracking the Coding Interview程序员面试金典》----实时中位数
- cracking code interview
- Cracking the coding interview--Q1.1(python的位操作)
- Cracking the code interview 面试题:两个数相加
- Replace all spaces in string with ' ' (Cracking the Code Interview)
- 笔记摘录——Cracking the code interview
- cracking the code interview convert array to BST Python
- Cracking the Code Interview Chapter One-- TaylorZhangYuxin's Solusion
- Cracking the Code Interview Chapter Two-- TaylorZhangYuxin's Solusion
- cracking the code interview——c++实现
- Cracking the Code Interview Chapter Three-- TaylorZhangYuxin's Solusion
- Cracking The Coding Interview--Q1.1
- 性能测试之LoadRunner11 破解
- 关于HTML中的滚动条/去掉滚动条
- 读《草样年华1》
- lua中数学库
- HreoWinGauge2.0组件集应用示例----数据处理
- Cracking the Code Interview (1): 跟踪数组的中位数
- 【C++学习】vector的使用,输入小写,输出大写---ShinePans
- 【学习笔记】 网络流问题
- mysql数据库 显示乱码
- WinExec() system() ShellExecute() 区别
- Android Duplicate files copied in APK
- 金价变动 ajax
- 性能测试之LoardRunner 手动关联一
- IOS之UI添加动画