Java Clear Code with Detailed Explanations
Thanks @shawngao for the original post.\n\n My thinking process :\nIt is required to return true if it is possible to escape.\nIn other words, we need to return
Thanks @shawngao for the original post.\n\n* My thinking process :\nIt is required to return true if it is possible to escape.\nIn other words, we need to return false when it is impossible and for the other scenarios, we return true.\nWhen is it impossible?\nIf a ghost can reach the target earlier than me, I am impossible to escape, return false.\nIf I can reach the target earlier than all ghosts, I am possible to escape return true.\nSince ghosts and I move based on a strictly horizontal or vertical path, we apply the Manhattan Distance.\n\n* Math related:\nRed : Manhattan Distance, which equals to Blue, Yello\nGreen : Straight-line Distance\n![image](https://s3-lc-upload.s3.amazonaws.com/users/mengcareers/image_1526693058.png)\n\nThe complete code is as below :\n\n```\n    public boolean escapeGhosts(int[][] ghosts, int[] target) {\n        int myDist = Math.abs(target[0] - 0) + Math.abs(target[1] - 0);\n        int ghostDist = Integer.MAX_VALUE;\n        for (int[] ghost : ghosts) {\n            ghostDist = Math.abs(target[0] - ghost[0]) + Math.abs(target[1] - ghost[1]);\n            if (ghostDist <= myDist) {\n                return false;\n            }\n        }\n        return true;\n    }\n```
[]