CLEAN CODE: MIN STEPS IN INFINITE GRID

By: Saurav

2017-10-21 09:06:00 UTC

Interviewbit

You are in an infinite 2D grid where you can move in any of the 8 directions :

(x,y) to

(x+1, y),
(x - 1, y),
(x, y+1),
(x, y-1),
(x-1, y-1),
(x+1,y+1),
(x-1,y+1),
(x+1,y-1)
You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.

Example :

Input : [(0, 0), (1, 1), (1, 2)]
Output : 2
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).

This question is intentionally left slightly vague. Clarify the question by trying out a few cases in the “See Expected Output” section.

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

First Solution

class Solution
    # @param a : array of integers
    # @param b : array of integers
    # @return an integer
    def coverPoints(a, b)
        unless ((a.size < 1) and (b.size < 1))
            number_of_points = (a.size > b.size) ? b.size : a.size
            steps = 0
            (0..(number_of_points-2)).each do |index|
                comparison = compute((a[index+1] - a[index]).abs, (b[index+1] - b[index]).abs)
                min = comparison[0]
                max = comparison[1]
                steps += min + (max-min)
            end
            return steps
        end
        return 0
    end
    def compute(x_diff, y_diff)
        (x_diff >= y_diff) ? [y_diff, x_diff] : [x_diff, y_diff]
    end
end

Refactored Clean Code:

class Solution
    # @param a : array of integers
    # @param b : array of integers
    # @return an integer
    def specify_steps(a, b)
        unless ((a.size < 1) and (b.size < 1))
            number_of_points = (a.size > b.size) ? b.size : a.size
            return calculate_steps(number_of_points, a, b)
        end
        return 0
    end
            
    def calculate_steps(number_of_points, a, b, steps = 0)
        (0..(number_of_points-2)).each do |index|
            min_and_max = compute_max_min((a[index+1] - a[index]).abs, (b[index+1] - b[index]).abs)
            steps += min_and_max[0] + (min_and_max[1]-min_and_max[0])
        end
        return steps
    end
    
     def compute_max_min(x_diff, y_diff)
        (x_diff >= y_diff) ? [y_diff, x_diff] : [x_diff, y_diff]
    end

end

Turns out. Why do min + (max-min) when we can just focus on max as well as ruby's minmax function

Another version:

class Solution
    # @param a : array of integers
    # @param b : array of integers
    # @return an integer
    def coverPoints(a, b)
        unless ((a.size < 2) or (b.size < 2) or (a.size != b.size))
            return calculate_steps(a.size, a, b)
        end
        return 0
    end
            
    def calculate_steps(number_of_points, a, b)
        steps = 0
        (0..(number_of_points-2)).each do |index|
            min_and_max = [(a[index+1]-a[index]).abs, (b[index+1]-b[index]).abs].minmax
            steps += min_and_max[1]
        end
        return steps
    end
end

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)