Java solution beats 99%


  • 0
    V
    class Solution {
        private int computeDist(int[] a, int[] b) {
            return (int)Math.abs(a[0] - b[0]) + (int)Math.abs(a[1] - b[1]);
        }
        
        public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
            int ret = 0;
            int min = Integer.MAX_VALUE;
            int diff = 0;
            for (int i = 0; i < nuts.length; i++) {
                int treeDist = computeDist(tree, nuts[i]);
                int squirrelDist =computeDist(squirrel, nuts[i]);
                ret += treeDist;
                diff = squirrelDist - treeDist;
                if (diff < min) {
                    min = diff;
                }
            }
            return ret * 2 + min;
        }
    }
    '''

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.