Skip to content

Create 1266-Minimum-Time-Visiting-All-Points.py #4343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Yatin-aggarwal
Copy link

Problem Description:

Given an array points where points[i] = [xi, yi] represents coordinates on the 2D plane, return the minimum time required to visit all points in order. In one second, you can move:
1 unit horizontally,
1 unit vertically,
OR 1 unit diagonally (both horizontal and vertical at the same time).

Approach:

  • For each pair of consecutive points:
    • Compute the distance in x and y directions:
      • x = abs(x2 - x1)
      • y = abs(y2 - y1)
  • To reach from one point to the next:
    • Take min(x, y) diagonal steps (covers both axes).
    • Then take abs(x - y) straight steps (either horizontal or vertical).
  • Therefore, the total time (steps) needed for each pair is:
    • min(x, y) + abs(x - y)
    • Which simplifies to: max(x, y)
  • Repeat this process for all consecutive point pairs in the list using a simple loop.

Complexity:

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Let me know if you'd like it in a code comment style or in a rendered preview.

@Yatin-aggarwal Yatin-aggarwal changed the title Create 1266-Minimum-Time-Visiting-All-Points Create 1266-Minimum-Time-Visiting-All-Points.py Jul 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant