0821-0830

821. Shortest Distance to a Character $\star$

822. Card Flipping Game $\star\star$

823. Binary Trees With Factors $\star\star$

824. Goat Latin $\star$

825. Friends Of Appropriate Ages $\star\star$

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

    for (int age : ages) ++count[age];

    for (int i = 15; i <= 120; ++i) ans += count[i] * (count[i] - 1);

    for (int i = 15; i <= 120; ++i)
      for (int j = i / 2 + 8; j < i; ++j) ans += count[i] * count[j];

    return ans;
  }
};

826. Most Profit Assigning Work $\star\star$

827. Making A Large Island $\star\star\star$

828. Unique Letter String $\star\star\star$

829. Consecutive Numbers Sum $\star\star\star$

830. Positions of Large Groups $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  vector<vector<int>> largeGroupPositions(string S) {
    const int n = S.length();

    vector<vector<int>> ans;

    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && S[j] == S[i]) ++j;
      if (j - i >= 3) ans.push_back({i, j - 1});
    }

    return ans;
  }
};