Top 150 InterviewЛегко

Swim In Rising Water

Detailed guide and Python implementation for the 'Swim In Rising Water' problem.

Постановка задачи

Легко

You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j). Rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if both elevations in the squares are at most t.

You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (n-1, n-1)?

Write a function swimInWater(grid: List[List[int]]) -> int.

Ограничения
  • n == len(grid) == len(grid[i])
  • 1 <= n <= 50
  • 0 <= grid[i][j] < n^2
  • Each value grid[i][j] is unique

Примеры

Example 1
Input
grid = [[0,2],[1,3]]
Output
3
Explanation

At time 3, you are permitted to swim all the way from (0,0) to (1,1). The path is 0 -> 1 -> 3.

Example 2
Input
grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output
16
Explanation

The final path is 0-1-2-3-4-5-16-15-14-13-12-11-10-9-8-7-6. The maximum height along this path is 16.

Need a Hint?
Consider using Advanced Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Открыть в редакторе
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Рекомендуемые ресурсы Python

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Python Try/Except и обработка ошибок

Предотвратите сбой ваших скриптов Python. Узнайте, как блокировать try, кроме, наконец, и как правильно создавать пользовательские исключения.

Посмотреть ресурс
Практическое руководство

Как перевернуть строку в Python

Узнайте, как перевернуть строку в Python с помощью нарезки, функции Reverse() и конкатенации циклов, с помощью визуальных примеров кода.

Посмотреть ресурс
Шпаргалка

Шпаргалка по строковым методам Python

Полное справочное руководство по манипулированию строками в Python. Мастер форматирования, поиска, разделения, замены и проверки свойств строк.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс