200. Number of Islands
·
Algorithm/LeetCode
문제전형적인 bfs 탐색 문제였다. 0은 물, 1은 땅을 의미하는 그래프 배열이 주어진다. 땅이 근접하게 모여 있으면 하나의 섬이다. 섬의 갯수를 요구하는 문제였다.링크: https://leetcode.com/problems/number-of-islands/description/Solutionclass Solution { static int[][] dirArray = {{-1,0},{1,0},{0,-1},{0,1}}; public int numIslands(char[][] grid) { int m = grid.length; int n = grid[0].length; boolean[][]visited = new boolean[m][n]; int ..
994. Rotting Oranges
·
Algorithm/LeetCode
문제그래프가 주어지고, 값은 0,1,2 중 하나이다. 1은 신선한 토마토, 2는 썩은 토마토, 0은 해당 위치에 토마토가 없음을 표현하였다. 썩은 토마토는 전염시킬 수 있어서, 근접 노드에 신선한 토마토가 있으면 전염시킨다. 전염은 1분 단위로 진행된다. 모든 토마토를 전염시키는데 걸리는 시간을 요구한다. 만약 모든 토마토를 전염시킬 수 없다면 -1를 반환한다.링크: https://leetcode.com/problems/rotting-oranges/?envType=study-plan-v2&envId=leetcode-75Solutionclass Solution { public int orangesRotting(int[][] grid) { int[][] visited = grid; ..
417. Pacific Atlantic Water Flow
·
Algorithm/LeetCode
문제그래프 탐색 문제이다. 2차원 배열의 그래프가 주어진다. 그래프는 섬을 의미한다. 각 노드의 value는 섬의 높이다. 인근 노드와의 높이 차이로 물이 흐른다. 각 노드를 시작으로 물이 흐를 때 Pacific Ocean(태평양)과 Atlantic Ocean(대서양) 까지 닿는 노드들을 찾는 문제이다.링크: https://leetcode.com/problems/pacific-atlantic-water-flow/description/Solutionclass Solution { final int[][] d = {{-1, 0},{1, 0},{0, -1},{0, 1}}; public List> pacificAtlantic(int[][] heights) { int m = heights.l..
133. Clone Graph
·
Algorithm/LeetCode
문제원본 노드를 깊은 복사하는 문제이다. bfs를 사용했다.링크: https://leetcode.com/problems/clone-graph/Solution/*// Definition for a Node.class Node { public int val; public List neighbors; public Node() { val = 0; neighbors = new ArrayList(); } public Node(int _val) { val = _val; neighbors = new ArrayList(); } public Node(int _val, ArrayList _neighbors) { val = _..