Walls and Gates (Java)

You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle.0 - A gate.INF - Infinity means an empty…

Number of Connected Components in an Undirected Graph (Java)

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number…

Find the number of Unique Paths (Java)

A robot is located at the top-left corner of a m x n grid. It can only move either down or right at any point in time. The robot is…

Return matrix in Spiral order (Java)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, given the following matrix: [ [ 1,…

Shortest Distance from All Buildings (Java)

Java Solution This problem can be solved by BFS. We define one matrix for tracking the distance from each building, and another matrix for tracking the number of buildings that…

Search a 2D Matrix (Java)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has properties: 1) Integers in each row are sorted from left to right.…

Minimum Path Sum in Matrix (Java)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Java…

Tic-Tac-Toe (Java)

Design a Tic-tac-toe game that is played between two players on a n x n grid. Java Solution 1 - Naive We can simply check the row, column, and diagonals…

Sudoku Solver (Java)

Write a program to solve a Sudoku puzzle by filling the empty cells. Java Solution public void solveSudoku(char[][] board) { helper(board); } private boolean helper(char[][] board){ for(int i=0; i<9; i++){…

Find if the word exists in the grid (Java)

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are…