0991-1000

991. Broken Calculator $\star\star$

992. Subarrays with K Different Integers $\star\star\star$

993. Cousins in Binary Tree $\star$

994. Rotting Oranges $\star$

995. Minimum Number of K Consecutive Bit Flips $\star\star\star$

996. Number of Squareful Arrays $\star\star\star$

997. Find the Town Judge $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  int findJudge(int N, vector<vector<int>>& trust) {
    vector<int> count(N);

    for (vector<int>& t : trust) {
      --count[t[0] - 1];
      ++count[t[1] - 1];
    }

    for (int i = 0; i < N; ++i)
      if (count[i] == N - 1) return i + 1;

    return -1;
  }
};

998. Maximum Binary Tree II $\star\star$

999. Available Captures for Rook $\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
class Solution {
 public:
  int numRookCaptures(vector<vector<char>>& board) {
    int ans = 0;
    int i0 = 0;
    int j0 = 0;

    for (int i = 0; i < 8; ++i)
      for (int j = 0; j < 8; ++j)
        if (board[i][j] == 'R') {
          i0 = i;
          j0 = j;
        }

    for (auto d : vector<vector<int>>({{1, 0}, {0, 1}, {-1, 0}, {0, -1}}))
      for (int i = i0 + d[0], j = j0 + d[1]; 0 <= i && i < 8 && 0 <= j && j < 8;
           i += d[0], j += d[1]) {
        if (board[i][j] == 'p') ++ans;
        if (board[i][j] != '.') break;
      }

    return ans;
  }
};

1000. Minimum Cost to Merge Stones $\star\star\star$