Problem · Array
Rank Open Businesses
Learn this problemProblem statement
Each business candidate is represented as [score, distance, isOpen], where isOpen is 1 for open and 0 for closed. Return the indices of the top k open businesses.
Open businesses should be sorted by descending score. If two businesses have the same score, the one with the smaller distance comes first. If both score and distance are tied, keep the smaller original index first.
Function
rankOpenBusinesses(candidates: int[][], k: int) → int[]Examples
Example 1
candidates = [[90,4,1],[95,8,0],[90,2,1],[85,1,1]]k = 2return = [2,0]Candidate 1 is closed. Candidates 2 and 0 tie on score, so the smaller distance comes first.
Example 2
candidates = [[80,3,1],[80,2,1],[90,10,1]]k = 5return = [2,1,0]All open stores are returned because fewer than five stores are available.
Constraints
Closed businesses must not appear in the result. If fewer than k businesses are open, return all open businesses in ranked order.