Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of problem is this? Help categorise

Tags:

algorithm

Recently, I came across this problem and I was intrigued as to whether 1) I could categorise this type of problem to a specific name or 2) what the best way to solve this problem was. It's a bit finicky so bear with me as I'll try to explain it the best I can (apologies in advance for the poor drawing).

You are given an n x n matrix which represents plots of soil. Each entry has a numeric value which represents the amount of water required to make that plot fertile. You are able to choose a specific co-ordinate about which the rain falls (with intensity I) and the area of rainfall depends on the parameter d (which is always even). Big d means more area..

Here comes the finicky part.

The rain falls in the way illustrated in the image below. The outskirt (represented by the dotted green lines) receive half the rainfall ( I/2 ) to their counterpart. So in this case, it would receive 4 mm. Hence, by deciding to pour rain about the point (3,3) there would be 7 plots of soil on the boundary (7 plots which are less than or equal to 8 / 2 = 4) and 10 plots of soil on the inner (10 plots which are less than or equal to 8) leading to 17 plots of soil in total that become fertile. enter image description here

The question asks, given the arguments for (n x n: 2d int array, d: int, I: int), what is the maximum number of plots of soil that can be made fertile?

n.b. there are no restrictions on where the co-ordinates may be i.e. it can be on the edge of the terrain.

I solved it in the following way:

Basically brute force but with some efficiency tweaks.

Efficiency tweak 1: Based on the value of d, there is a limit [d * (d / 2 + 1)] as to how many plots of soil it encompasses. If any point satisfies the max then stop searching.

Efficiency tweak 2: If the value of d is 10, for example, then the radius of that will be 5 meaning at best it will completely cover sqrt(5) by sqrt(5) inside the inner area. In other words, it will cover at most 2 x 2 completely and hence, we can start searching from the point (2, 2) instead of from (0, 0) which will unnecessarily water vacant land. Similarly, we can end searching at the diagonally opposite point which will be (n - 2, n - 2).

This solution feels non-optimal because as d and n x n get bigger, there is a lot of overlap in the land being calculated.

like image 602
220284 Avatar asked Nov 25 '25 19:11

220284


2 Answers

The brute force solution has O(n^2 * d^2) time complexity.

An effective optimization is to use a sliding window approach. Once you know the irrigation value for a given point, you can compute the value for the next adjacent point just by looking at the squares where the two regions differ. This allows you to reduce the complexity to O(n^2 * d).

Sliding window illustration

In fact, by taking advantage of the geometry of the problem, you can further optimize this approach to achieve optimal O(n^2) time complexity. (Hint: Think about moving the window diagonally.) Details are left as an exercise to the reader.

Further reading:

  • https://www.baeldung.com/cs/sliding-window-algorithm
  • What is Sliding Window Algorithm? Examples?
like image 192
augurar Avatar answered Nov 28 '25 17:11

augurar


If the boundary cells received the full rainfall amount, the problem would be a lot simpler. In such a case you could transform the problem into a sliding window 2D maximum subarray problem:

  1. For all cells, subtract the rainfall amount.
  2. Mark fertile cells as 1, and non-fertile cells as 0.
  3. Solve the sliding window 2D maximum subarray problem on the resulting binary matrix.

The fact that the region isn't rectangular and the boundary cells receive only half the rainfall makes the problem more difficult, but I would still classify this problem as a variation of the maximum subarray problem and researching algorithms in that space would be a good starting point.

like image 25
wookie919 Avatar answered Nov 28 '25 17:11

wookie919



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!