力扣-581-最短无序连续子序列

能不能把问题转化为找第一个逆序对和最后一个逆序对

int findUnsortedSubarray(vector<int>& nums) {
int res = 0;
int startIndex=-1, endIndex=-1;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] <= nums[i - 1]) {
if (startIndex != -1) endIndex = i;
else startIndex = i;
}
}
if (startIndex != -1) {
if (endIndex != -1) res = endIndex - startIndex + 2;
else res = 2;
}
return res;
}

是我too young too simple,看来不是这么简单的事情
多半还是绕不过动态规划了

本文作者:YaosGHC

本文链接:https://www.cnblogs.com/yaocy/p/16994928.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   YaosGHC  阅读(22)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起