FastPrepDeduplicate Card-Pile Prefixes

Deduplicate Card-Pile Prefixes

Google logoGoogle● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Process card piles from left to right. For pile i, its cleaned first k cards must contain no value that appears among the cleaned first k cards of any of the previous d piles.

Clean a pile by scanning it from left to right and deleting the minimum possible number of conflicting cards until either k valid prefix cards have been kept or the pile ends. Once k valid prefix cards have been kept, append the unscanned suffix unchanged because cards beyond the prefix do not participate in comparisons.

The first pile has no earlier restrictions. Return all cleaned piles in their original order. When fewer than d earlier piles exist, compare against all of them. k may exceed a pile's final length.

Function

cleanPilePrefixes(piles: int[][], k: int, d: int) → int[][]

Examples

Example 1

piles = [[3,2,4,5,2],[2,1,3,4]]k = 2d = 1return = [[3,2,4,5,2],[1,4]]

The second pile deletes 2 and 3 so its cleaned first two cards, [1,4], avoid [3,2].

Example 2

piles = [[1,2,9],[2,3,8],[1,4,7,5]]k = 2d = 2return = [[1,2,9],[3,8],[4,7,5]]

Each later prefix avoids the union of up to two previous cleaned prefixes.

Example 3

piles = [[1],[1,1],[1,2]]k = 2d = 1return = [[1],[],[1,2]]

The empty cleaned second pile contributes no forbidden prefix values to the third pile.

Constraints

  • 1 <= piles.length <= 100000.
  • The total number of cards is at most 200000.
  • 0 <= k,d <= 100000.
  • Card values are signed 32-bit integers.

More Google problems

See Google hiring insights
public int[][] cleanPilePrefixes(int[][] piles, int k, int d) {
    // Write your code here.
}
piles[[3,2,4,5,2],[2,1,3,4]]
k2
d1
expected[[3,2,4,5,2],[1,4]]
Checking account…