Catch Fish with Reusable Baits
Learn this problemProblem statement
You are given two arrays of positive integers, fish and baits. Each value in fish is a fish size, and each value in baits is the size of one bait.
- A bait can catch a fish only when the bait is strictly smaller than that fish.
- A caught fish is removed and cannot be caught again.
- Each bait can be used at most
3times.
Process the baits from largest to smallest. For each bait, repeatedly catch the largest remaining fish that it can catch. Move to the next bait after the current bait has been used three times or when it cannot catch any remaining fish.
Return the total number of caught fish after every bait has been processed.
Function
countCaughtFish(fish: int[], baits: int[]) → intExamples
Example 1
fish = [1,2,3]baits = [1]return = 2The bait of size 1 catches fish of sizes 3 and 2. It cannot catch the remaining fish of size 1, so the result is 2.
Example 2
fish = [2,2,3,4]baits = [1]return = 3The only bait is smaller than every fish, but it can be used only three times. It catches fish of sizes 4, 3, and 2.
Example 3
fish = [1,4,3,2]baits = [1,1]return = 3The first bait catches fish of sizes 4, 3, and 2. The only remaining fish has size 1, so the second bait cannot catch it.
Constraints
1 ≤ fish.length ≤ 10^51 ≤ baits.length ≤ 10^51 ≤ fish[i] ≤ 10^91 ≤ baits[i] ≤ 10^9