0581-0590

581. Shortest Unsorted Continuous Subarray $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
 public:
  int findUnsortedSubarray(vector<int>& nums) {
    int min = INT_MAX;
    int max = INT_MIN;
    bool flag = false;

    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] < nums[i - 1]) flag = true;
      if (flag) min = std::min(min, nums[i]);
    }

    flag = false;

    for (int i = nums.size() - 2; i >= 0; --i) {
      if (nums[i] > nums[i + 1]) flag = true;
      if (flag) max = std::max(max, nums[i]);
    }

    int l;
    for (l = 0; l < nums.size(); ++l)
      if (nums[l] > min) break;

    int r;
    for (r = nums.size() - 1; r >= 0; --r)
      if (nums[r] < max) break;

    return l > r ? 0 : r - l + 1;
  }
};

582. Kill Process $\star\star$

583. Delete Operation for Two Strings $\star\star$

584. Find Customer Referee $\star$

585. Investments in 2016 $\star\star$

586. Customer Placing the Largest Number of Orders $\star$

587. Erect the Fence $\star\star\star$

588. Design In-Memory File System $\star\star\star$

589. N-ary Tree Preorder Traversal $\star$

590. N-ary Tree Postorder Traversal $\star$