Data Structure Array: Maximum of all subarrays of size k
http://www.geeksforgeeks.org/maximum-of-all-subarrays-of-size-k/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; void printKMax(int arr[], int n, int k) {
deque<int> S;
S.push_back();
for (int i = ; i < k; i++) {
while (!S.empty() && arr[i] >= arr[S.back()]) S.pop_back();
S.push_back(i);
}
for (int i = k; i < n; i++) {
cout << arr[S.front()] << " ";
while (!S.empty() && i - k >= S.front()) S.pop_front();
while (!S.empty() && arr[S.back()] <= arr[i]) S.pop_back();
S.push_back(i);
}
cout << arr[S.front()] << endl;
} int main() {
int arr[] = {, , , , , , };
printKMax(arr, , );
return ;
}
Data Structure Array: Maximum of all subarrays of size k的更多相关文章
- Data Structure Array: Maximum circular subarray sum
http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...
- Data Structure Array: Maximum sum such that no two elements are adjacent
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/ #include <iostre ...
- Data Structure Array: Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
http://www.geeksforgeeks.org/given-an-array-arr-find-the-maximum-j-i-such-that-arrj-arri/ #include & ...
- Data Structure Array: Find the Missing Number
http://www.geeksforgeeks.org/find-the-missing-number/ 1. use sum formula, O(n), O(1) 2. use XOR, O(n ...
- Data Structure Array: Move all zeroes to end of array
http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...
- Data Structure Array: Find if there is a subarray with 0 sum
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #incl ...
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...
- Data Structure Array: Sort elements by frequency
http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...
- Data Structure Array: Largest subarray with equal number of 0s and 1s
http://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/ #include <iostream& ...
随机推荐
- PLSQL Developer设置及快捷键设置
1.登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的用户是dba,要展开tables目录,正常情况都需 ...
- Session和Cookie之间存在的区别与联系
一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...
- Memcache遍历查询所有键值的方法
直接举个Telnet命令行下遍历memcached所有key的方法: stats items STAT items:7:number1 STATitems:7:age188 END stats c ...
- PHP面试题及答案解析(4)—PHP核心技术
1.写出一个能创建多级目录的PHP函数. <?php /** * 创建多级目录 * @param $path string 要创建的目录 * @param $mode int 创建目录的模式,在 ...
- Linux基础ls命令
ls 命令是linux下最常用的命令,通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等.ls 命令在日常的linux操作中用的 ...
- root 执行过程权限问题
mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法 权限问题,授权 给 root 所有sql ...
- LeetCode78:Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- sublime使用技巧(3)-- 常用快捷键【持续更新】
♥ Ctrl + Shift + v 这样粘贴可以保持原格式,不会有缩进上的困扰 Ctrl + k 用Ctrl + d选中重复单词时跳过当前选中 Ctrl + Enter 在光标所在行的下一行新建一行 ...
- 基于HttpClient实现网络爬虫~以百度新闻为例
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791 基于HttpClient4.5实现网络爬虫请訪问这里:http:/ ...
- chattr
chattr 功能:设置文件隐藏属性常用参数:+ 增加某个特殊权限,其他原本存在的参数不动- 删除某个特殊权限,其他原本存在的参数不动= 设置一定,且仅有后面接的参数 i 文件 ...