lintcode Sliding Window Median

http://www.lintcode.com/en/problem/sliding-window-median/

最近感觉自己需要让心情平静下来,慢慢做事情,做好实验室的事情保证自己的毕业,同时也要做好题目找到份好工作。

先从写博客开始吧,把做过的题目记下来,写写思路。

这道题我没有用O(n log n)的方法,实际上应该是O(n log k),需要用两个set互相之间倒数据就可以了,判断起来比较烦。

直接用map也能够通过,线性查找sliding window的中间值,得到结果。每次进入新元素之前,都删掉前面的元素,做计数。

通过每一个元素的计数得到哪个值是中间值,同时也靠计数决定某个数是否被erase。

class Solution {
public:
    /**
     * @param nums: A list of integers.
     * @return: The median of the element inside the window at each moving
     */
    vector<int> medianSlidingWindow(vector<int> &nums, int k) {
        // write your code here
        map<int, int> rec;
        vector<int> res;

        if (k == 0 || nums.empty() || nums.size() < k)
            return res;

        int n = nums.size();
        for (int i = 0; i < k; ++i)
            ++rec[nums[i]];

        // 虽然不明白怎么回事但是AC了
        // 这部分再想想
        auto getMid = [&](){
            int target = k >> 1;
            target -= 1 - (k & 0x1);

            int cur = 0;
            auto iter = rec.begin();
            for ( ; iter != rec.end(); ++iter){
                cur += iter->second;
                if (cur > target) break;
            }

            return iter->first;
        };

        res.push_back(getMid());

        for (int i = k; i < n; ++i){
            auto iter = rec.find(nums[i - k]);
            --iter->second;
            if (iter->second == 0) rec.erase(iter);
            ++rec[nums[i]];

            res.push_back(getMid());
        }

        return res;
    }
};
时间: 2024-08-06 11:32:35

lintcode Sliding Window Median的相关文章

Sliding Window Median

Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving. Have you met this question in a real interview? Y

Leetcode: Sliding Window Median

1 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. 2 3 Examples: 4 [2,3,4] , the median is 3 5 6 [2,3], the median is (2 + 3) / 2 = 2.

[Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Given an a

Leetcode480-Sliding Window Median

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Given an a

239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. For example, Given num

Sliding Window Maximum 解答

Question Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving. Example For array [1, 2, 7, 7, 8], movin

poj 2823 Sliding Window

poj 2823 Sliding Window 单调队列 单调队列是一个单调的状态(递增,或者递减) 所以需要维护这么一个状态 http://baike.baidu.com/link?url=ZcGM7Hzo8zVQUU6Oqqq18SlCMJ92ts3I1aXwQGDZw_NiDDlzIIV9GKlfs3X1fcHVppZHOU31geHZG4cOcRZOAK 固定 k 区间的单调 队列,求 最小值,如果 两个元素 A B ,如果 A 的 下标 比 B 小,但是 A 的 值 比 B 大,那么在

[LeetCode][JavaScript]Sliding Window Maximum

Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

POJ2823 Sliding Window(单调队列)

单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. ----------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<deque> #define rep(i,r) for(int i=0;i<r;i++) #define clr(x,c) memset