LeetCode Entry

1266. Minimum Time Visiting All Points

03.12.2023 easy 2023 kotlin

Path coordinates distance in XY plane

1266. Minimum Time Visiting All Points easy blog post substack youtube image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/426

Problem TLDR

Path coordinates distance in XY plane

Intuition

For each pair of points lets compute diagonal distance and the remainder: time = diag + remainder. Given that remainder = max(dx, dy) - diag, we derive the formula.

Approach

Let’s use some Kotlin’s API:

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code


  fun minTimeToVisitAllPoints(points: Array<IntArray>): Int =
    points.asSequence().windowed(2).sumBy { (from, to) ->
      max(abs(to[0] - from[0]), abs(to[1] - from[1]))
    }