3.7 KiB
title | date | description | type | tags | ||
---|---|---|---|---|---|---|
Dead Pixels | 2023-12-19T21:15:01Z | A nice algorithms problem. | post |
|
The problem
Billiam Wender just bought a new BenQ gaming monitor! His monitor is an N by M rectangle of pixels. Each pixel currently has a brightness level between 0 and 255 inclusive.
However, Billiam suspects that his gaming monitor is defective and contains dead pixels! Oh no! He would like to find which subrectangle (a smaller rectangle contained inside the entire rectangle) of the monitor's grid of pixels has the lowest average brightness level, because that's probably where the dead pixels are located. Help Billiam find the dead pixels! Output a single integer, the smallest average brightness of any subrectangle of the monitor's rectangular grid of pixels.
An example
Let's say Billiam's monitor is a 5 by 5 rectangle, which would probably be super useless in real life, but whatever. Here are the brightness levels:
255 255 127 63 63
127 0 0 0 0
255 0 0 0 0
63 0 0 0 0
63 63 63 63 63
There is a huge 3 by 4 subrectangle on the right of pixels all with a brightness value of 0. The average brightness level of this subrectangle is the answer, 0. With 12 dead pixels, Billiam should definitely return this monitor.
Solution
You really think I'll just hand you the solution right away? Go outside and talk a walk and think about it.
OK, if you insist on reading the solution, here it is:
{{< rawhtml >}}
Click to show solution
The subrectangle with the smallest average brightness is just the pixel with the lowest brightness! We can find that in O(NM) time by scanning through the entire array of pixels. I also asked ChatGPT to solve this problem, and its initial attempt was an O(N^3 M^3) brute force. After asking it to improve its solution, it wrote an O(N^2 M^2) program using prefix sums, but couldn't do any better. Yay, I guess that means I have a new favorite problem for stumping LLMs, in addition to my old favorite, "What's the average aspect ratio of a human?"A variant
If you felt trolled by this problem, here's a more interesting variant. What's the average brightness of the subrectangle of size at least 1 by 2 with the lowest average brightness?
We can apply the same reasoning as in the original problem. A 2 by 2 rectange can't be the answer, because we could just split it into two 1 by 2 rectanges, and one of those rectangles will have a lower brightness.
At this point, you might think we can just search through all 1 by 2 subrectangles, but there's a catch. Consider this monitor:
0 1 0
The best 1 by 2 subrectangle would include a pixels with brightnesses 0 and 1, which gives an average of 1/2. However, we can do better by taking the whole 1 by 3 rectangle, which gives an average of 1/3. While any rectangle with an even dimension can be divided up into 1 by 2 rectangles, odd by odd rectangles cannot. With both 1 by 2 and 1 by 3 rectangles, you can build any 1 by i rectangle for i greater than 2. With that, you can then make any j by i rectangle. Thus, we only have to search through all 1 by 2 and 1 by 3 subrectangles, which takes O(NM) time.
You can also consider the variant of requiring the answer subrectangle to by at least 2 by 2, but it's just more casework.
Note: This problem was previously asked in the Ladue Computing Contest that sadly never happened. I first heard this problem from Eric Zhang a few years ago who had written it for a programming contest at his high school. So yes, this problem has quite a storied history!