search
HomeBackend DevelopmentPHP TutorialFind Building Where Alice and Bob Can Meet

Find Building Where Alice and Bob Can Meet

2940. Find Building Where Alice and Bob Can Meet

Difficulty: Hard

Topics: Array, Binary Search, Stack, Binary Indexed Tree, Segment Tree, Heap (Priority Queue), Monotonic Stack

You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building.

If a person is in building i, they can move to any other building j if and only if i

You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi.

Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.

Example 1:

  • Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
  • Output: [2,5,-1,5,2]
  • Explanation: In the first query, Alice and Bob can move to building 2 since heights[0]
  • In the second query, Alice and Bob can move to building 5 since heights[0]
  • In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
  • In the fourth query, Alice and Bob can move to building 5 since heights[3]
  • In the fifth query, Alice and Bob are already in the same building.
  • For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
  • For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.

Example 2:

  • Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
  • Output: [7,6,-1,4,6]
  • Explanation: In the first query, Alice can directly move to Bob's building since heights[0]
  • In the second query, Alice and Bob can move to building 6 since heights[3]
  • In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
  • In the fourth query, Alice and Bob can move to building 4 since heights[3]
  • In the fifth query, Alice can directly move to Bob's building since heights[1]
  • For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
  • For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.

Constraints:

  • 1 4
  • 1 9
  • 1 4
  • queries[i] = [ai, bi]
  • 0 i, bi

Hint:

  1. For each query [x, y], if x > y, swap x and y. Now, we can assume that x
  2. For each query [x, y], if x == y or heights[x]
  3. Otherwise, we need to find the smallest index t such that y
  4. To find index t for each query, sort the queries in descending order of y. Iterate over the queries while maintaining a monotonic stack which we can binary search over to find index t.

Solution:

The problem requires determining the leftmost building where Alice and Bob can meet given their starting buildings and movement rules. Each query involves finding a meeting point based on building heights. This is challenging due to the constraints on movement and the need for efficient computation.

Key Points

  1. Alice and Bob can move to another building if its height is strictly greater than the current building.
  2. For each query, find the leftmost valid meeting point, or return -1 if no such building exists.
  3. The constraints demand a solution better than a naive O(n²) approach.

Approach

  1. Observations:

    • If a == b, Alice and Bob are already at the same building.
    • If heights[a]
    • Otherwise, find the smallest building index t > b where:
      • heights[a]
      • heights[b]
  2. Optimization Using Monotonic Stack:

    • A monotonic stack helps efficiently track the valid buildings Alice and Bob can move to. Buildings are added to the stack in a way that ensures heights are in decreasing order, enabling fast binary searches.
  3. Query Sorting:

    • Sort the queries in descending order of b to process buildings with larger indices first. This ensures that we build the stack efficiently as we move from higher to lower indices.
  4. Binary Search on Stack:

    • For each query, use binary search on the monotonic stack to find the smallest index t that satisfies the conditions.

Plan

  1. Sort queries based on the larger of the two indices (b) in descending order.
  2. Traverse the array backward while maintaining a monotonic stack of valid indices.
  3. For each query, check trivial cases (a == b or heights[a]
  4. For non-trivial cases, use the stack to find the leftmost valid building via binary search.
  5. Return the results in the original query order.

Solution Steps

  1. Preprocess Queries:

    • Ensure a
    • Sort queries by b in descending order.
  2. Iterate Through Queries:

    • Maintain a monotonic stack as we traverse the array.
    • For each query:
      • If a == b, the answer is b.
      • If heights[a]
      • Otherwise, use the stack to find the smallest valid index t > b.
  3. Binary Search on Stack:

    • Use binary search to quickly find the smallest index t on the stack that satisfies heights[t] > heights[a].
  4. Restore Original Order:

    • Map results back to the original query indices.
  5. Return Results.

Let's implement this solution in PHP: 2940. Find Building Where Alice and Bob Can Meet

<?php /**
 * @param Integer[] $heights
 * @param Integer[][] $queries
 * @return Integer[]
 */
function leftmostBuildingQueries($heights, $queries) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

/**
 * @param $queries
 * @return array
 */
private function getIndexedQueries($queries) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

/**
 * @param $stack
 * @param $a
 * @param $heights
 * @return mixed|null
 */
private function findUpperBound($stack, $a, $heights) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

class IndexedQuery {
    public $queryIndex;
    public $a; // Alice's index
    public $b; // Bob's index

    /**
     * @param $queryIndex
     * @param $a
     * @param $b
     */
    public function __construct($queryIndex, $a, $b) {
        $this->queryIndex = $queryIndex;
        $this->a = $a;
        $this->b = $b;
    }
}

// Test the function
$heights = [6, 4, 8, 5, 2, 7];
$queries = [[0, 1], [0, 3], [2, 4], [3, 4], [2, 2]];
print_r(leftmostBuildingQueries($heights, $queries));

$heights = [5, 3, 8, 2, 6, 1, 4, 6];
$queries = [[0, 7], [3, 5], [5, 2], [3, 0], [1, 6]];
print_r(leftmostBuildingQueries($heights, $queries));
?>

Explanation:

  1. Sorting Queries: The queries are sorted by b in descending order to process larger indices first, which allows us to update our monotonic stack as we process.
  2. Monotonic Stack: The stack is used to keep track of building indices where Alice and Bob can meet. We only keep buildings that have a height larger than any previously seen buildings in the stack.
  3. Binary Search: When answering each query, we use binary search to efficiently find the smallest index t where the conditions are met.

Example Walkthrough

Input:

  • heights = [6,4,8,5,2,7]
  • queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]

Process:

  1. Sort Queries:

    • Indexed queries: [(2,4), (3,4), (0,3), (0,1), (2,2)]
  2. Build Monotonic Stack:

    • Start at the highest index and add indices to the stack:
      • At index 5: Stack = [5]
      • At index 4: Stack = [5, 4]
      • ...
  3. Query Processing:

    • For query [0,1], heights[0]
    • ...

Output:

[2, 5, -1, 5, 2]

Time Complexity

  1. Query Sorting: O(Q log Q) where Q is the number of queries.
  2. Monotonic Stack Construction: O(N) where N is the length of heights.
  3. Binary Search for Each Query: O(Q log N).

Overall: O(N Q log (Q N)).

Output for Example

Input:

$heights = [6, 4, 8, 5, 2, 7];
$queries = [[0, 1], [0, 3], [2, 4], [3, 4], [2, 2]];

Output:

print_r(findBuilding($heights, $queries)); // [2, 5, -1, 5, 2]

This approach efficiently handles large constraints by leveraging a monotonic stack and binary search. It ensures optimal query processing while maintaining correctness.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

  • LinkedIn
  • GitHub

The above is the detailed content of Find Building Where Alice and Bob Can Meet. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools