0741-0750

741. Cherry Pickup $\star\star\star$

742. Closest Leaf in a Binary Tree $\star\star$

743. Network Delay Time $\star\star$

744. Find Smallest Letter Greater Than Target $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
 public:
  char nextGreatestLetter(vector<char>& letters, char target) {
    int l = 0;
    int r = letters.size();

    while (l < r) {
      int m = (l + r) >> 1;
      if (letters[m] <= target)
        l = m + 1;
      else
        r = m;
    }

    return letters[l % letters.size()];
  }
};

745. Prefix and Suffix Search $\star\star\star$

746. Min Cost Climbing Stairs $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int minCostClimbingStairs(vector<int>& cost) {
    const int n = cost.size();

    for (int i = 2; i < n; ++i) cost[i] += min(cost[i - 1], cost[i - 2]);

    return min(cost[n - 1], cost[n - 2]);
  }
};

747. Largest Number At Least Twice of Others $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int dominantIndex(vector<int>& nums) {
    int ans;
    int max = 0;
    int secondMax = 0;

    for (int i = 0; i < nums.size(); ++i) {
      if (nums[i] > max) {
        secondMax = max;
        max = nums[i];
        ans = i;
      } else if (nums[i] > secondMax)
        secondMax = nums[i];
    }

    return max >= 2 * secondMax ? ans : -1;
  }
};

748. Shortest Completing Word $\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
29
30
31
32
class Solution {
 public:
  string shortestCompletingWord(string licensePlate, vector<string>& words) {
    string ans;

    vector<int> map(26);
    for (char c : licensePlate)
      if (isalpha(c)) ++map[tolower(c) - 'a'];

    int min = INT_MAX;

    for (string& word : words) {
      if (word.length() >= min) continue;
      if (!isMatch(word, map)) continue;
      min = word.length();
      ans = word;
    }

    return ans;
  }

 private:
  bool isMatch(string& word, vector<int>& map) {
    vector<int> wordMap(26);
    for (char c : word) ++wordMap[c - 'a'];

    for (int i = 0; i < 26; ++i)
      if (wordMap[i] < map[i]) return false;

    return true;
  }
};

749. Contain Virus $\star\star\star$

750. Number Of Corner Rectangles $\star\star$