LeetCode 3161: Block Placement Queries Solution

Master LeetCode problem 3161 (Block Placement Queries), 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.

3161. Block Placement Queries

Problem Explanation

Explanation

To solve this problem, we can use a data structure like a TreeSet in Java or a set in C++ and Python to keep track of the positions of obstacles on the number line. For each query of type 2, we check if there is any obstacle within the range [0, x] that would block the placement of the block of size sz. If there is no obstacle, we can place the block, otherwise, we cannot.

Here is the step-by-step algorithm:

  1. Initialize an empty set to store the positions of obstacles.
  2. Iterate through each query in the input queries.
  3. If the query is of type 1, add the position of the obstacle to the set.
  4. If the query is of type 2, check if there is any obstacle within the range [0, x] that would block the placement of the block of size sz.
  5. Return true if no obstacle is found, otherwise return false.

Time complexity:

  • The time complexity of this algorithm is O(nlogn) where n is the number of queries. This is because for each query, we perform a search in the set which has a logn complexity.

Space complexity:

  • The space complexity is O(n) to store the positions of obstacles.

Solution Code

class Solution {
    public boolean[] canPlaceBlocks(int[][] queries) {
        TreeSet<Integer> obstacles = new TreeSet<>();
        boolean[] results = new boolean[queries.length];

        for (int i = 0; i < queries.length; i++) {
            if (queries[i][0] == 1) {
                obstacles.add(queries[i][1]);
            } else {
                int x = queries[i][1];
                int sz = queries[i][2];
                Integer prev = obstacles.floor(x);
                Integer next = obstacles.ceiling(0);

                if ((prev == null || x - prev >= sz) && (next == null || next - x >= sz)) {
                    results[i] = true;
                } else {
                    results[i] = false;
                }
            }
        }

        return results;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3161 (Block Placement Queries)?

This page provides optimized solutions for LeetCode problem 3161 (Block Placement Queries) 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 3161 (Block Placement Queries)?

The time complexity for LeetCode 3161 (Block Placement Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3161 on DevExCode?

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

Back to LeetCode Solutions