Загрузка страницы

GOOGLE - BATTLESHIPS IN A BOARD (LeetCode)

Battleships in a Board LeetCode coding solution. One of Google's most commonly asked interview questions according to LeetCode.

Coding Interviews Battleships in a Board (LeetCode) question and explanation.

This question is a commonly asked by the following companies: Google.

Link to problem: https://leetcode.com/problems/battleships-in-a-board/description/

Intuition behind solution:
- Solution #1: Traverse the matrix looking for 'X's. Once you find an X, increment your count and call a recursive function to "sink" the entire battleships by constantly visiting its 4 neighboring cells (up, down, left, and right). This will prevent you from counting the same battleship multiple times. Once you have traversed the entire matrix, simply return your count.
- Solution #2: Traverse the matrix using the same logic as the first solution however, only count the first 'X' you find in any battleship. This can be done by realizing how we are traversing the matrix (top to bottom and left to right) and understanding that battleships strictly exist horizontally or vertically. Therefore we ensure only counting the first 'X' of any battleship by checking the cell above us and the cell to the left of us to see if either of them are 'X's. Once we finish traversing the matrix we return our count again.

SOCIAL
----------------------------------------------------------------------------------------------------------------
Support me on Patreon: https://www.patreon.com/KevinNaughtonJr
Follow me on Twitter: https://twitter.com/KevinNaughtonJr
Follow me on Instagram: https://www.instagram.com/programeme
Follow me on GitHub: https://github.com/kdn251

MUSIC
----------------------------------------------------------------------------------------------------------------
Evil Morty Remix by Young Russell - https://soundcloud.com/yungrussell/evil-morty-remix

Видео GOOGLE - BATTLESHIPS IN A BOARD (LeetCode) канала Kevin Naughton Jr.
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
30 октября 2018 г. 8:41:09
00:10:31
Яндекс.Метрика