0861-0870

861. Score After Flipping Matrix $\star\star$

862. Shortest Subarray with Sum at Least K $\star\star\star$

863. All Nodes Distance K in Binary Tree $\star\star$

864. Shortest Path to Get All Keys $\star\star\star$

865. Smallest Subtree with all the Deepest Nodes $\star\star$

866. Prime Palindrome $\star\star$

867. Transpose Matrix $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<vector<int>> transpose(vector<vector<int>>& A) {
    vector<vector<int>> ans(A[0].size(), vector<int>(A.size()));

    for (int i = 0; i < A.size(); ++i)
      for (int j = 0; j < A[0].size(); ++j) ans[j][i] = A[i][j];

    return ans;
  }
};

868. Binary Gap $\star$

869. Reordered Power of 2 $\star\star$

870. Advantage Shuffle $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  vector<int> advantageCount(vector<int>& A, vector<int>& B) {
    multiset<int> set(A.begin(), A.end());

    for (int i = 0; i < B.size(); ++i) {
      auto p = *set.rbegin() <= B[i] ? set.begin() : set.upper_bound(B[i]);
      A[i] = *p;
      set.erase(p);
    }

    return A;
  }
};