力扣-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,看来不是这么简单的事情
多半还是绕不过动态规划了

posted @ 2022-12-20 19:23  YaosGHC  阅读(26)  评论(0)    收藏  举报