FastPrepCatch Fish with Reusable Baits
Problem · Array

Catch Fish with Reusable Baits

Learn this problem
EasyTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem 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 3 times.

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[]) → int

Examples

Example 1

fish = [1,2,3]baits = [1]return = 2

The 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 = 3

The 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 = 3

The 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^5
  • 1 ≤ baits.length ≤ 10^5
  • 1 ≤ fish[i] ≤ 10^9
  • 1 ≤ baits[i] ≤ 10^9

More Tiktok problems

drafts saved locally
public int countCaughtFish(int[] fish, int[] baits) {
    // Write your code here.
}
fish[1,2,3]
baits[1]
expected2
checking account