0961-0970

961. N-Repeated Element in Size 2N Array $\star$

962. Maximum Width Ramp $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  int maxWidthRamp(vector<int>& A) {
    int ans = 0;
    stack<int> stack;

    for (int i = 0; i < A.size(); ++i)
      if (stack.empty() || A[i] < A[stack.top()]) stack.push(i);

    for (int i = A.size() - 1; i > ans; --i)
      while (!stack.empty() && A[i] >= A[stack.top()])
        ans = max(ans, i - stack.top()), stack.pop();

    return ans;
  }
};

963. Minimum Area Rectangle II $\star\star$

964. Least Operators to Express Number $\star\star\star$

965. Univalued Binary Tree $\star$

966. Vowel Spellchecker $\star\star$

967. Numbers With Same Consecutive Differences $\star\star$

968. Binary Tree Cameras $\star\star\star$

969. Pancake Sorting $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
 public:
  vector<int> pancakeSort(vector<int>& A) {
    vector<int> ans;

    for (int target = A.size(); target >= 1; --target) {
      int index = find(A, target);
      reverse(A.begin(), A.begin() + index + 1);
      reverse(A.begin(), A.begin() + target);
      ans.push_back(index + 1);
      ans.push_back(target);
    }

    return ans;
  }

 private:
  int find(vector<int>& A, int target) {
    for (int i = 0; i < A.size(); ++i)
      if (A[i] == target) return i;
    throw;
  }
};

970. Powerful Integers $\star$