Problem solution · C++

Fruits Into Baskets II

Fruits Into Baskets II: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Segment tree or range structure
Source
walkccc LeetCode Solutions
Length
78 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Fruits Into Baskets II, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 78 lines of C++ from the credited upstream file 3477.cpp.
  • The implementation visibly relies on sequence storage.
  • 1 loop block detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFruits Into Baskets II · C++C++
Use this to learn the idea, then write your own version.
class SegmentTree { public:  explicit SegmentTree(const vector<int>& nums) : n(nums.size()), tree(n * 4) {    build(nums, 0, 0, n - 1);  }   // Updates nums[i] to val.  void update(int i, int val) {    update(0, 0, n - 1, i, val);  }   // Returns the first index i where baskets[i] >= target, or -1 if not found.  int queryFirst(int target) {    return queryFirst(0, 0, n - 1, target);  }  private:  const int n;       // the size of the input array  vector<int> tree;  // the segment tree   void build(const vector<int>& nums, int treeIndex, int lo, int hi) {    if (lo == hi) {      tree[treeIndex] = nums[lo];      return;    }    const int mid = (lo + hi) / 2;    build(nums, 2 * treeIndex + 1, lo, mid);    build(nums, 2 * treeIndex + 2, mid + 1, hi);    tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]);  }   void update(int treeIndex, int lo, int hi, int i, int val) {    if (lo == hi) {      tree[treeIndex] = val;      return;    }    const int mid = (lo + hi) / 2;    if (i <= mid)      update(2 * treeIndex + 1, lo, mid, i, val);    else      update(2 * treeIndex + 2, mid + 1, hi, i, val);    tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]);  }   int queryFirst(int treeIndex, int lo, int hi, int target) {    if (tree[treeIndex] < target)      return -1;    if (lo == hi) {      // Found a valid position, mark it as used by setting to -1.      update(lo, -1);      return lo;    }    const int mid = (lo + hi) / 2;    const int leftChild = tree[2 * treeIndex + 1];    return leftChild >= target               ? queryFirst(2 * treeIndex + 1, lo, mid, target)               : queryFirst(2 * treeIndex + 2, mid + 1, hi, target);  }   int merge(int left, int right) const {    return max(left, right);  }}; class Solution { public:  int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {    int ans = 0;    SegmentTree tree(baskets);     for (const int fruit : fruits)      if (tree.queryFirst(fruit) == -1)        ++ans;     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗