LeetCode 1645: Hopper Company Queries II Solution

Master LeetCode problem 1645 (Hopper Company Queries II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1645. Hopper Company Queries II

Problem Explanation

Explanation:

This problem can be solved efficiently using the Segment Tree data structure. We can build a segment tree where each node represents a range of values in the input array. The leaf nodes store the original array elements, and the internal nodes store the aggregate information (in this case, the minimum value) of their child nodes.

To answer each query, we can perform a range query on the segment tree to find the minimum value within the given range.

Algorithmic Idea:

  1. Build a segment tree from the input array.
  2. Perform a range query for each query to find the minimum value within the given range.

Time Complexity:

  • Building the segment tree: O(n)
  • Answering each query: O(log n) Overall time complexity: O(n + q * log n), where n is the number of elements in the input array and q is the number of queries.

Space Complexity:

  • Segment tree: O(n)

: :

Solution Code

class SegmentTree {
    int[] segmentTree;

    public SegmentTree(int[] nums) {
        int n = nums.length;
        int height = (int)Math.ceil(Math.log(n) / Math.log(2));
        int maxSize = 2 * (int)Math.pow(2, height) - 1;
        segmentTree = new int[maxSize];
        buildSegmentTree(nums, 0, n - 1, 0);
    }

    private int buildSegmentTree(int[] nums, int start, int end, int index) {
        if (start == end) {
            segmentTree[index] = nums[start];
            return nums[start];
        }
        int mid = start + (end - start) / 2;
        segmentTree[index] = Math.min(buildSegmentTree(nums, start, mid, 2 * index + 1),
                                      buildSegmentTree(nums, mid + 1, end, 2 * index + 2));
        return segmentTree[index];
    }

    public int rangeQuery(int start, int end, int n) {
        return rangeQueryHelper(0, n - 1, start, end, 0);
    }

    private int rangeQueryHelper(int segStart, int segEnd, int queryStart, int queryEnd, int index) {
        if (queryStart <= segStart && queryEnd >= segEnd) {
            return segmentTree[index];
        }
        if (queryStart > segEnd || queryEnd < segStart) {
            return Integer.MAX_VALUE;
        }
        int mid = segStart + (segEnd - segStart) / 2;
        return Math.min(rangeQueryHelper(segStart, mid, queryStart, queryEnd, 2 * index + 1),
                        rangeQueryHelper(mid + 1, segEnd, queryStart, queryEnd, 2 * index + 2));
    }
}

class HopperCompanyQueriesII {
    public int[] minInterval(int[] nums, int[][] queries) {
        SegmentTree st = new SegmentTree(nums);
        int n = nums.length;
        int q = queries.length;
        int[] result = new int[q];
        for (int i = 0; i < q; i++) {
            int start = queries[i][0];
            int end = queries[i][1];
            result[i] = st.rangeQuery(start, end, n);
        }
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1645 (Hopper Company Queries II)?

This page provides optimized solutions for LeetCode problem 1645 (Hopper Company Queries II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1645 (Hopper Company Queries II)?

The time complexity for LeetCode 1645 (Hopper Company Queries II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1645 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1645 in Java, C++, or Python.

Back to LeetCode Solutions