0691-0700

691. Stickers to Spell Word $\star\star\star$

692. Top K Frequent Words $\star\star$

693. Binary Number with Alternating Bits $\star$

694. Number of Distinct Islands $\star\star$

695. Max Area of Island $\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
24
25
26
27
28
class Solution {
 public:
  int maxAreaOfIsland(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();

    int ans = 0;
    vector<vector<bool>> visited(m, vector<bool>(n));

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) ans = max(ans, dfs(grid, i, j, visited));

    return ans;
  }

 private:
  int dfs(vector<vector<int>>& grid, int i, int j,
          vector<vector<bool>>& visited) {
    if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() ||
        visited[i][j] || grid[i][j] == 0)
      return 0;

    visited[i][j] = true;

    return 1 + dfs(grid, i + 1, j, visited) + dfs(grid, i - 1, j, visited) +
           dfs(grid, i, j + 1, visited) + dfs(grid, i, j - 1, visited);
  }
};

696. Count Binary Substrings $\star$

697. Degree of an Array $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
 public:
  int findShortestSubArray(vector<int>& nums) {
    int ans = 0;
    int degree = 0;
    unordered_map<int, int> firstSeen;
    unordered_map<int, int> numCounts;

    for (int i = 0; i < nums.size(); ++i) {
      if (!firstSeen.count(nums[i])) firstSeen[nums[i]] = i;
      ++numCounts[nums[i]];
      if (numCounts[nums[i]] > degree) {
        degree = numCounts[nums[i]];
        ans = i - firstSeen[nums[i]] + 1;
      } else if (numCounts[nums[i]] == degree)
        ans = min(ans, i - firstSeen[nums[i]] + 1);
    }

    return ans;
  }
};

698. Partition to K Equal Sum Subsets $\star\star$

699. Falling Squares $\star\star\star$

700. Search in a Binary Search Tree $\star$