Gotta Catch 'Em All
InterviewTime limit2sMemory limit512 MB
Given N Pokemon points with types, find the smallest axis-parallel square that contains Pokemon of at least K different types.
- Level
Medium6 of 10
- Topics
- Binary search, Sliding window, Two pointers
- Solved
- No attempts yet
Problem
Ash Ketchum is a Pokemon trainer who wants to catch every Pokemon in the world. He has caught most of them and needs K more different types to complete his set.
Ash lives in a 2D grid world. Pokemons spawn at integer coordinates, and several Pokemons can share the same type. Ash needs K different types, not any K Pokemons.
Ash throws a large square net from the sky. A Pokemon is caught if it lies inside the net or on one of its edges. Each side of the square is parallel to the x-axis or the y-axis. Find the minimum side length of a square that contains Pokemons of K different types. The net must have positive area, so the side must be positive.
Input
The first line contains a single integer T, the number of test cases ().
Each test case starts with a line containing two integers N and K. N is the number of Pokemons (), and K is the number of types ().
The next N lines each contain three integers X, Y, and Z: the x-coordinate, the y-coordinate, and the type of a Pokemon. , , and .
Output
For each test case, print a single line containing the minimum side of a square that contains Pokemons of K different types.