Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created October 17, 2023 01:26
Show Gist options
  • Select an option

  • Save primaryobjects/1f690dd6af798577a70bea2422ea37de to your computer and use it in GitHub Desktop.

Select an option

Save primaryobjects/1f690dd6af798577a70bea2422ea37de to your computer and use it in GitHub Desktop.
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