FloodDepth

Solution:

This java solution for the problem FloodDepth scored a 100%

Logic:

So the logic here is to imagine you are walking down the rocks..as you go down you try to set the minimum depth with how low you have reached, when you go up then at that point you must calculate the maximm depth so far and put it in a depths queue.

Also if you are climbing up higher than ever before then you need to actually reset the minimum node to null because its as good as entering a new valley. At this point reset the minimum depth to null and your highest altitude is also reset to this new high.

Keep doing that until you reach zero altitude, that is end of array!

And because we are collecting the depths in a PriorityQueue, whose comparator is set to maintain the elements in reverse order then in that case we have depths stored in descending order. so when you poll for the first time..you get your answer! maximum depth. Nevertheless if the size is zero..there was no water harvesting 🙂

I hope this makes sense with the algo I have written here, its the first pass..perhaps this can be enhanced with all optimised functions…I leave that to you coder!

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;

public class FloodDepth {

    public int solution(int[] A) {

        Integer minNode = null;
        Integer highNode = A[0];
        Queue<Integer> depths = new PriorityQueue<>(Collections.reverseOrder());

        for (int i = 1; i < A.length; i++) {
            //we are going up

            if (A[i] > A[i - 1]) {
                if (A[i] > highNode && minNode != null) {
                    //new high, highest altitude, so entering new valley
                    int depth = highNode - minNode;
                    depths.offer(depth);
                    highNode = A[i];
                    minNode = null;
                } else if (A[i] <= highNode && minNode != null) {
                    //going up, though we are still in the valley
                    if (A[i - 1] > minNode) {
                        int depth = A[i] - minNode;
                        depths.offer(depth);
                    } else {
                        int depth = A[i] - A[i - 1];
                        depths.offer(depth);
                    }
                }
                if (A[i] > highNode) {
                    //we need this for boundary conditions
                    highNode = A[i];
                }
            } else {
                //we are going down
, lets evaluate our min node.
                if ((minNode == null) || A[i] < minNode) {
                    minNode = A[i];
                }
            }
        }
        if (depths.size() > 0) {
            return depths.poll();
        }
        return 0;
    }

    public static void main(String[] args) {
        FloodDepth floodDepth = new FloodDepth();
        int depth = floodDepth.solution(new int[] {1, 3, 2, 1, 2, 1, 5, 3, 3, 4, 2});
        System.out.println(depth);

        depth = floodDepth.solution(new int[]{100000000, 1, 2, 99999999});
        System.out.println(depth);
    }
}