239. Sliding Window Maximum (受CSDN限制,本文为22号发表,但实际上是1月早写好的)

题目


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 nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

Therefore, return the max sliding window as [3,3,5,5,6,7].

Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

Follow up:
Could you solve it in linear time?

题解

这是一道Hard的题。一个直接的想法是用一个优先队列(堆),每次移动都把第一个数从堆中删除,再加入新的数,堆顶的元素也即最大值。由于C++的priority_queue貌似没有提供直接删除某个元素的方法,或者删除的方法的复杂度较高,因此我采用了一些辅助措施。每次移动时不把数删除,而是直接把新数存入堆中,但是把原来要删除的数加入到一个multiset中,之后取出堆顶元素,如果这个元素在multiset中存在,说明是本来要删除的元素,我们把他丢弃,继续取出堆顶元素,直到取出的数不在multiset中存在即可。

代码

#include <iostream>
#include <vector>
#include <queue>
#include <set>

using namespace std;

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        if(k == 0){
            return vector<int>();
        }
        priority_queue<int> q;
        vector<int> maxs;
        for(int i = 0; i < k; i++){
            q.push(nums[i]);
        }
        int max = q.top();
        maxs.push_back(max);
        multiset<int> reserved;
        for(int i = k; i < nums.size(); i++){
            reserved.insert(nums[i - k]);
            q.push(nums[i]);
            max = q.top();
            while(removeOne(reserved, max)){
                q.pop();
                max = q.top();
            }
            maxs.push_back(max);
        }
        return maxs;
    }

    bool removeOne(multiset<int> &s, int value){
        multiset<int>::iterator it = s.find(value);
        if(it != s.end()){
            s.erase(it);
            return true;
        }
        return false;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者hyb1996,源码Auto.js,一个主要由无障碍服务实现的不需要Root权限的类似按键精灵的自动操作软件,可以实现自动点击、滑动、输入文字、打开应用等。 同时有Sublime Text 插件可提供基础的在桌面开发的功能。 下载地址:酷安 Alpha版本:Releases 特性 简单易用的自动操作函数 悬浮窗录制和运行 更专业&强大的选择器API,提供对屏幕上的控件的寻找、遍历、获取信息、操作等。类似于Google的UI测试框架UiAutomator,您也可以把他当做移动版UI测试框架使用 采用JavaScript为脚本语言,并支持代码补全、变量重命名、代码格式化、查找替换等功能,可以作为一个JavaScript IDE使用 支持使用e4x编界面,并可以将JavaScript打包为apk文件,您可以用它来开发小工具应用 支持使用Root权限以提供更强大的屏幕点击、滑动、录制功能和运行shell命令。录制录制可产生js文件或二进制文件,录制动作的回放比较流畅 提供截取屏幕、保存截图、图片找色等函数,可进行简单的游戏脚本制作;未来将加入找图功能 可作为Tasker插件使用,结合Tasker可胜任日常工作流 带有界面分析工具,类似Android Studio的LayoutInspector,可以分析界面层次和范围、获取界面上的控件信息 与脚本精灵、按键精灵等软件的区别是: Auto.js主要以自动化、工作流为目标,更多地是方便日常生活工作,例如启动游戏时自动屏蔽通知、一键与特定联系人微信视频(知乎上出现过该问题,老人难以进行复杂的操作和子女进行微信视频)等 Auto.js兼容性更好。以坐标为基础的按键精灵、脚本精灵很容易出现分辨率问题,而以控件为基础的Auto.js则没有这个问题 Auto.js执行大部分任务不需要root权限。只有需要精确坐标点击、滑动的相关函数才需要root权限 尽管如此,Auto.js的大部分用户仍然是用来点赞、签到、刷游戏的:)
The Sliding Window Protocol is a flow control protocol used in computer networks to ensure reliable and efficient data transfer between two nodes. It is implemented using a sliding window, which is a buffer of fixed size that stores the data packets to be transmitted and received. The sliding window protocol is a stop-and-wait protocol, which means that the sender sends a packet and waits for an acknowledgement from the receiver before sending the next packet. The receiver sends an acknowledgement packet to the sender indicating that it has received the packet successfully. The sliding window protocol has two parameters: the window size and the sequence number. The window size represents the number of packets that can be sent without waiting for an acknowledgement. The sequence number is a unique identifier assigned to each packet to ensure that the packets are delivered in the correct order. Here is a sample program in Python that implements the Sliding Window Protocol: ```python import socket import time # Define the window size and sequence number WINDOW_SIZE = 4 SEQ_NUM_SIZE = 4 # Define the packet format PACKET_FORMAT = "!I1024s" # Define the server address and port SERVER_ADDRESS = "localhost" SERVER_PORT = 12345 # Define the data to be sent DATA = "Hello, world!".encode("utf-8") # Create the socket and connect to the server client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((SERVER_ADDRESS, SERVER_PORT)) # Initialize the sequence number and window seq_num = 0 window_start = 0 window_end = WINDOW_SIZE # Send the packets while window_start < len(DATA): # Send the packets in the current window for i in range(window_start, window_end): # Create the packet packet_data = DATA[i:i+1024] packet_seq_num = seq_num.to_bytes(SEQ_NUM_SIZE, byteorder="big") packet = struct.pack(PACKET_FORMAT, packet_seq_num, packet_data) # Send the packet client_socket.send(packet) # Increment the sequence number seq_num += 1 # Wait for the acknowledgements ack_received = False while not ack_received: # Set the timeout client_socket.settimeout(1) # Wait for the acknowledgement try: ack = client_socket.recv(1024) # Check if the acknowledgement is valid if ack: ack_seq_num = int.from_bytes(ack, byteorder="big") if ack_seq_num == window_start: ack_received = True # Update the window window_start += 1 window_end += 1 except socket.timeout: # If the timeout occurs, resend the packets in the current window for i in range(window_start, window_end): packet_data = DATA[i:i+1024] packet_seq_num = (seq_num - WINDOW_SIZE + i).to_bytes(SEQ_NUM_SIZE, byteorder="big") packet = struct.pack(PACKET_FORMAT, packet_seq_num, packet_data) client_socket.send(packet) # Wait for a short period of time before sending the next window time.sleep(0.1) # Close the socket client_socket.close() ``` In this program, the client sends the data in packets of size 1024 bytes and waits for an acknowledgement from the server before sending the next packet. The program uses a sliding window of size 4, which means that the client can send up to 4 packets at a time without waiting for an acknowledgement. The program also implements a timeout mechanism to handle lost packets. If the client does not receive an acknowledgement within 1 second, it resends the packets in the current window. Overall, the Sliding Window Protocol provides reliable and efficient data transfer in computer networks by using a sliding window to control the flow of data between two nodes.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值