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; ..
452. Minimum Number of Arrows to Burst Balloons
·
Algorithm/LeetCode
문제그리디 알고리즘 문제였다. 풍선의 위치(지름)가 범위로 표현되어 주어진다. 여러 풍선이 주어지고, 최대한 적은 갯수의 화살로 모든 풍선을 터트리는 문제였다. 풍선은 겹칠 수 있다(위치 범위가 일부 겹칠 수 있다). 그리디 알고리즘이란 Greedy(탐욕, 욕심쟁이) 이름 처럼 지금 당장 최적의 답을 선택하는 과정을 반복하여 결과를 도출하는 알고리즘이다. 링크: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloonsSolution 1class Solution { public int findMinArrowShots(int[][] points) { int answer = 1; Arrays.sort(poi..
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..
207. Course Schedule
·
Algorithm/LeetCode
문제There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.Return true if you can finish all courses. Otherwise, return fal..
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 = _..
11. Container With Most Water
·
Algorithm/LeetCode
문제각 위치의 높이를 나타내는 정수 배열이 주어진다. 두 위치 간 높이에 따른 최대 부피를 계산하는 문제이다.Constraints:n == height.length2 0 링크https://leetcode.com/problems/container-with-most-water/Solution 1class Solution { public int maxArea(int[] height) { int max = 0; int n = height.length; for(int i=0;i문제를 읽고 직관적으로 생각한 풀이 방식이다.시간 복잡도: O(n^2)타임 초과 발생!Solution 2class Solution { public int maxArea(int[] height..
15. 3Sum
·
Algorithm/LeetCode
문제 링크https://leetcode.com/problems/3sum/description/ 해결 1.class Solution { public List> threeSum(int[] nums) { int n= nums.length; List> answer=new ArrayList(); //중복을 방지하고자 임시로 사용할 Set을 선언하였다. Set> temp=new HashSet(); // 배열을 먼저 정렬하여 중복 및 순서 문제를 해결 Arrays.sort(nums); for(int i=0;i set = new HashSet(); for(int j=i+1;j triplet = Arrays..