0871-0880

871. Minimum Number of Refueling Stops $\star\star\star$

872. Leaf-Similar Trees $\star$

873. Length of Longest Fibonacci Subsequence $\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
class Solution {
 public:
  int lenLongestFibSubseq(vector<int>& A) {
    const int n = A.size();

    int ans = 0;
    unordered_map<int, int> map;
    for (int i = 0; i < n; ++i) map[A[i]] = i;
    vector<vector<int>> dp(n, vector<int>(n, 2));

    for (int j = 0; j < n; ++j)
      for (int k = j + 1; k < n; ++k) {
        int ai = A[k] - A[j];
        if (ai < A[j] && map.count(ai)) {
          int i = map[ai];
          dp[j][k] = dp[i][j] + 1;
          ans = max(ans, dp[j][k]);
        }
      }

    return ans;
  }
};

874. Walking Robot Simulation $\star$

875. Koko Eating Bananas $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int minEatingSpeed(vector<int>& piles, int H) {
    int l = 1;
    int r = *max_element(piles.begin(), piles.end()) + 1;

    while (l < r) {
      int m = (l + r) >> 1;
      int hour = 0;
      for (int pile : piles) hour += (pile - 1) / m + 1;
      if (hour <= H)
        r = m;
      else
        l = m + 1;
    }

    return l;
  }
};

876. Middle of the Linked List $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  ListNode* middleNode(ListNode* head) {
    if (!head || !head->next) return head;

    auto slow = head;
    auto fast = head;

    while (fast && fast->next) {
      slow = slow->next;
      fast = fast->next->next;
    }

    return slow;
  }
};

877. Stone Game $\star\star$

878. Nth Magical Number $\star\star\star$

879. Profitable Schemes $\star\star\star$

880. Decoded String at Index $\star\star$