Created
October 17, 2023 01:26
-
-
Save primaryobjects/1f690dd6af798577a70bea2422ea37de to your computer and use it in GitHub Desktop.
Minimum time visiting all points https://leetcode.com/problems/minimum-time-visiting-all-points/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int MinTimeToVisitAllPoints(int[][] points) { | |
| int seconds = 0; | |
| int cx = points[0][0]; | |
| int cy = points[0][1]; | |
| for (int i=1; i<points.Length; i++) | |
| { | |
| int[] coord = points[i]; | |
| int x = coord[0]; | |
| int y = coord[1]; | |
| while (cx != x || cy != y) | |
| { | |
| if (x != cx && y != cy) | |
| { | |
| // Reduce for diagonal move. | |
| seconds--; | |
| } | |
| if (x < cx) | |
| { | |
| cx--; | |
| seconds++; | |
| } | |
| else if (x > cx) | |
| { | |
| cx++; | |
| seconds++; | |
| } | |
| if (y < cy) | |
| { | |
| cy--; | |
| seconds++; | |
| } | |
| else if (y > cy) | |
| { | |
| cy++; | |
| seconds++; | |
| } | |
| } | |
| } | |
| return seconds; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment