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..
Problem2
·
Algorithm/Socar
문제설명 - int[] numbers가 있다. - 각 요소들은 인접한 요소와 값의 차가 k이하가 되어야 한다. - k 이하가 안되면 swap을 해서라도 k이하가 되게 만들어야 한다. - 최소 swap 횟수를 구한다. - numbers의 길이는 최대 8이다. 입출력 정보 input - int k - int[] numbers output int answer(최소 swap 횟수) Code Solution.class package jason.socar.problem2; import java.util.*; public class Solution { static Set answerSet = new HashSet(); static int sum=0; public int solution(int k, int[] numb..
Problem1
·
Algorithm/Socar
문제 설명 - 각 섬(=노드) 사이를 자전거 타고 활보한다. k 시간에 정차 할 수 있는 섬들은? - 간선의 값은 이동거리를 의미한다. - 섬은 중복해서 이동할 수 있다. - 시작 노드는 0 이다. 입출력 정보 Input - int n(노드의 수): 0~n-1까지 노드가 존재 - int k(도착시간) - int[][] roads(간선 정보) int[][] roads={{5, 4, 6}, {5, 2, 5}, {0, 4, 2}, {2, 3, 3}, {1, 2, 7}, {0, 1, 3}}; //roads[0]= {5,4,6}; //노드 5와 노드 4 사이 간선의 길이는 6 Output - int[] answer(k 시간에 도착할 수 있는 노드들의 집합) Code Solution2.class package ja..
11053. 가장 긴 증가하는 부분 수열(Java)
·
Algorithm/BaekJoon
문제 링크 https://www.acmicpc.net/problem/11053 11053번: 가장 긴 증가하는 부분 수열 수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이 www.acmicpc.net 문제 풀이 DP 문제입니다. n의 범위를 고려해서 이중 for문을 사용했습니다. 코드 package jason.baekjoon.problem11053; import java.util.Scanner; public class Main { public static void main(String[] args) { S..