aboutsummaryrefslogtreecommitdiffstats
path: root/581/main.cpp
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-05-03 17:51:11 +0000
committerterminaldweller <thabogre@gmail.com>2022-05-03 17:51:11 +0000
commitc8b163e20608c6d076b6e2444770e02aeb8901d2 (patch)
tree42f437f1fd07a758366a78de8938e333724cbddd /581/main.cpp
parentupdate (diff)
downloadleetcode-c8b163e20608c6d076b6e2444770e02aeb8901d2.tar.gz
leetcode-c8b163e20608c6d076b6e2444770e02aeb8901d2.zip
update
Diffstat (limited to '581/main.cpp')
-rw-r--r--581/main.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/581/main.cpp b/581/main.cpp
new file mode 100644
index 0000000..7aac338
--- /dev/null
+++ b/581/main.cpp
@@ -0,0 +1,45 @@
+
+#include "header.hpp"
+#include <algorithm>
+
+class Solution {
+public:
+ static int findUnsortedSubarray(std::vector<int> &nums) {
+ std::vector<int> nums_copy;
+ nums_copy.resize(nums.size());
+ std::copy(nums.begin(), nums.end(), nums_copy.begin());
+ std::sort(nums_copy.begin(), nums_copy.end());
+ int l = 0;
+ int n = nums.size() - 1;
+ bool begin = 0;
+ bool end = 0;
+ while (n > l) {
+ if (nums_copy[l] == nums[l]) {
+ l++;
+ } else {
+ begin = true;
+ break;
+ }
+ }
+ while (n > l) {
+ if (nums_copy[n] == nums[n]) {
+ n--;
+ } else {
+ end = true;
+ break;
+ }
+ }
+ if (end || begin)
+ return n - l + 1;
+ else
+ return 0;
+ }
+};
+
+int main(int argc, char **argv) {
+ std::vector<int> nums1 = {2, 6, 4, 8, 10, 9, 15};
+ std::vector<int> nums2 = {1, 2, 3, 4};
+ std::cout << Solution::findUnsortedSubarray(nums1) << std::endl;
+ std::cout << Solution::findUnsortedSubarray(nums2) << std::endl;
+ return 0;
+}