I recently took a coding assessment where one of the problems was based on a very interesting twist on greedy + two-pointer + sorting techniques — and I wanted to share the question for the community.
You're on a haunted straight road of length L with N ghosts positioned along it.
Each ghost is described by:
A position A[i] on the road (0 ≤ A[i] ≤ L)
A stamina level B[i]
You are allowed to trap exactly one ghost, and this ghost becomes your trap ghost. Starting from this position, you can move only to the right, hunting more ghosts under the following rules:
Rules for hunting:
You may only hunt ghosts whose stamina is greater than or equal to the trap ghost's stamina.
Hunting a ghost costs energy equal to:
(distance from the trap)×(ghost’s stamina)
The total energy used must not exceed a given limit E.
Goal:
Determine the maximum number of ghosts you can hunt including the trap ghost, under the above constraints.
Input:
L = 20
N = 5
E = 100
A = [2, 5, 10, 14, 18]
B = [3, 2, 4, 5, 6]
Output: 3Explanation: