# 581. Shortest Unsorted Continuous Subarray # 769. Max Chunks To Make Sorted

本文介绍了解决两道LeetCode题目——寻找最短无序连续子数组和最大块数以使数组排序的方法。通过双向扫描策略,分别找到无序子数组的边界与最优分割点,实现高效算法。

581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

Then length of the input array is in range [1, 10,000].
The input array may contain duplicates, so ascending order here means <=.

  参考了讨论区的大神思路:先定义两个标签索引变量begin = -1 与 end = -2(选择-1与-2这两个数是为了当数组中不存在非降序子列时返回 end - begin + 1==0),接着同时开始从头到尾(定义变量max表示搜索到的最大值)以及从尾到头(定义变量min表示搜索到的最小值)两个方向的搜索:
①从头到尾:当当前值比max小时(后值比前值小,不是升序),说明无序子列的end至少在当前位置。
②从尾到头:当当前值比min大时(前值比后值大,不是升序),说明无序子列的begin至少要从该位置开始

class Solution {
public:
    //参考了讨论区的最高评论答案
    int findUnsortedSubarray(vector<int>& nums) {
        int begin = -1, end = -2;
        int max = nums[0], min = nums[nums.size()-1];
        int n = nums.size();
        for(int i = 1; i<nums.size(); i++)   //向右从1到n-1,向左从n-2到0
        {
            
            if(nums[i] < max)
                end = i;
            if(nums[n-1-i] > min)
                begin = n-1-i;
            max = nums[i]>max ? nums[i]:max;
            min = nums[n-1-i]<min ? nums[n-1-i]:min;
        }
        return end - begin + 1;
    }
};

769. Max Chunks To Make Sorted

Given an array arr that is a permutation of [0, 1, …, arr.length - 1], we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.

What is the most number of chunks we could have made?

Example 1:

Input: arr = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn’t sorted.

Example 2:

Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

Note:

arr will have length in range [1, 10].
arr[i] will be a permutation of [0, 1, …, arr.length - 1].

设置一个记录最大值的int变量,在数组中从左向右搜索,当当前搜索的最大值与当前索引相同时,分块加1。注意,当当前最大值等于当前索引时,数组中在当前索引下的值分两种情况:
①:如果当前最大值大于数组在当前索引下的值,则在后续的搜索过程中不会有比当前值更大的值了(因为数组中的数是从0到size-1的,如果排好序应该是[0,1,2…size-1])此时分块+1.
②如果当前最大值小于数组在当前索引下的值,说明在该索引之后有比当前最大值小的值需要排序,如果此时形成分块,无法包括这个小值,导致最后整个数组无法正常排序

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int maxnum = 0;
        int chunksnum = 0;
        for(int i = 0; i<arr.size(); i++)
        {
            maxnum = max(maxnum, arr[i]);
            if(maxnum == i)
            {
                chunksnum++;
            }
        }
        return chunksnum;
    }
};

tip:
这两题都是在一个数组中找无序数列排序使得整个数组有序,注意他们各自寻找无序子列的方法。
合理设置标签指针,对数组同时进行正序与倒序的搜索

欧姆龙FINS(工厂集成网络系统)协议是专为该公司自动化设备间数据交互而设计的网络通信标准。该协议构建于TCP/IP基础之上,允许用户借助常规网络接口执行远程监控、程序编写及信息传输任务。本文档所附的“欧ronFins.zip”压缩包提供了基于C与C++语言开发的FINS协议实现代码库,旨在协助开发人员便捷地建立与欧姆龙可编程逻辑控制器的通信连接。 FINS协议的消息框架由指令头部、地址字段、操作代码及数据区段构成。指令头部用于声明消息类别与长度信息;地址字段明确目标设备所处的网络位置与节点标识;操作代码定义了具体的通信行为,例如数据读取、写入或控制器指令执行;数据区段则承载实际交互的信息内容。 在采用C或C++语言实施FINS协议时,需重点关注以下技术环节: 1. **网络参数设置**:建立与欧姆龙可编程逻辑控制器的通信前,必须获取控制器的网络地址、子网划分参数及路由网关地址,这些配置信息通常记载于设备技术手册或系统设置界面。 2. **通信链路建立**:通过套接字编程技术创建TCP连接至控制器。该过程涉及初始化套接字实例、绑定本地通信端口,并向控制器网络地址发起连接请求。 3. **协议报文构建**:依据操作代码与目标功能构造符合规范的FINS协议数据单元。例如执行输入寄存器读取操作时,需准确配置对应的操作代码与存储器地址参数。 4. **数据格式转换**:协议通信过程中需进行二进制数据的编码与解码处理,包括将控制器的位状态信息或数值参数转换为字节序列进行传输,并在接收端执行逆向解析。 5. **异常状况处理**:完善应对通信过程中可能出现的各类异常情况,包括连接建立失败、响应超时及错误状态码返回等问题的处理机制。 6. **数据传输管理**:运用数据发送与接收函数完成信息交换。需注意FINS协议可能涉及数据包的分割传输与重组机制,因单个协议报文可能被拆分为多个TCP数据段进行传送。 7. **响应信息解析**:接收到控制器返回的数据后,需对FINS响应报文进行结构化解析,以确认操作执行状态并提取有效返回数据。 在代码资源包中,通常包含以下组成部分:展示连接建立与数据读写操作的示范程序;实现协议报文构建、传输接收及解析功能的源代码文件;说明库函数调用方式与接口规范的指导文档;用于验证功能完整性的测试案例。开发人员可通过研究这些材料掌握如何将FINS协议集成至实际项目中,从而实现与欧姆龙可编程逻辑控制器的高效可靠通信。在工程实践中,还需综合考虑网络环境稳定性、通信速率优化及故障恢复机制等要素,以确保整个控制系统的持续可靠运行。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
A player wants to press the direction buttons as little as possible so that both frogs arrive at the destination to finish the game. Find the minimum number of times the player pressed the buttons to finish the game. If the frog or green frog cannot arrive at the destination, output -1. [Figure] For example, let&#39;s look at the case where a grid with six rows and six columns is given. The frog is located at (6, 1), the green frog is located at (1, 6), and the destination is (2, 3) as shown in the [Figure]. Walls are marked with #. First, the frog arrives at the destination first by moving eight times as shown on the left side of the [Figure], and the green frog moves to the opposite direction at the same time and is located at (6, 4). Next, as shown on the right side of the [Figure], when the green frog arrives at the destination by moving it five times, you pressed the buttons 13 times for both frogs to arrive at the destination. This is the minimum number of times you press the buttons. [Constraints] 1. N and M are integers from 2 to 40. 2. R1, R2, and R3 are integers from 1 to N. 3. C1, C2, and R3 are integers from 1 to M. 4. Both frogs cannot be at the same location while they are moving. 5. Walls are not the start and destination of both frogs, and the start positions of the two frogs are not their destinations. [Input] First, the number T of test cases is given, followed by T test cases. On the first line of each test case, N and M are given, separated by spaces. On the next line, R1, C1, R2, C2, R3, and C3 are given in the order, separated by spaces. Over the next N lines, a string of length M is given..” represents a position in the grid to which both frogs can move, and “#” represents a wall. [Output] Output one line per test case. For each test case, output “#x” (where x is the number of the test case, starting from 1), leave a space, and then output the answer of the relevant test case. [Example of Input and Output] (Input) 1 6 6 6 1 1 6 2 3 ....#. .#..#. .#..#. .#..#. .#.##. .#....
09-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值