본문 바로가기

반응형

Java/Java 알고리즘 LeetCode

(60)
[LeetCode- Part. 1] 6. 최소 경로 합 # https://leetcode.com/problems/minimum-path-sum/ Minimum Path Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 경로를 따라 모든 숫자의 합을 최소화하는 왼쪽 상단에서 오른쪽 하단까지의 경로를 찾기 아래 또는 오른쪽으로 만 이동 가능 (1) DFS - 맵 public class Solution { public int minPathSum(int[][] grid) { Map visited = new HashMap..
[LeetCode- Part. 1] 5. 단어 나누기 # (+DP) https://leetcode.com/problems/word-break/ Word Break - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자 s와 문자열 리스트 wordDict이 주어지면, wordDict에 있는 단어로 모두 분리되면 true 리턴 여러번 재사용 가능 DP 이용 1차시도 -> 실패 import java.util.*; class Solution { public boolean wordBreak(String s, List wordDict) {..
[LeetCode- Part. 1] 4. 나선형 매트릭스 생성 https://leetcode.com/problems/spiral-matrix-ii/submissions/ Spiral Matrix II - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 참고자료 https://and-some.tistory.com/899 [LeetCode- Ch3. 배열] 7. 나선형 매트릭스 # https://leetcode.com/problems/spiral-matrix/ Spiral Matrix - LeetCode Level up your..
[LeetCode- Part. 1] 3. 총길이 60초짜리 음악 쌍 https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/ Pairs of Songs With Total Durations Divisible by 60 - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 노래의 길이를 나타내는 배열 -> 총 길이 60초로 나눌 수 있는 노래 쌍의 수 반환 두 개 합쳐서 %60==0이면 answer++ 1. 이중 for문 : O(n^2) 2. ..
[LeetCode- Part. 1] 2. 2행N열 재구성 (+ 2차원 배열 정렬) https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/ Reconstruct a 2-Row Binary Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. colsum배열을 정렬한다. : colsum이 크고, 인덱스가 작은순으로 정렬 즉, 1열 내림차순 정렬하되 같으면, 0열이 작은순 정렬 (1) Comparator 오버라이딩 Arrays.sort(newarr, new Comparato..
[LeetCode- Part. 1] 1. 가장 바깥 괄호제거 (+ StringBuilder, substring) https://leetcode.com/problems/remove-outermost-parentheses/ Remove Outermost Parentheses - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { public String removeOuterParentheses(String s) { //10 int cnt=0; String str=""; String answer=""; for(char c: s.toCharArray())..
[LeetCode- Ch9. 백트래킹] 4. 핸드폰 숫자의 문자 조합 https://leetcode.com/problems/letter-combinations-of-a-phone-number/submissions/ Letter Combinations of a Phone Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. DFS 이용 2. 이중 for문 이용 : 뒤부터 시작 class Solution { static int[] digitarr; static int tmp=0; public List letterComb..
[LeetCode- Ch9. 백트래킹] 3. 부분집합 https://leetcode.com/problems/subsets/submissions/ Subsets - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 2^n개의 부분집합을 모두 나열 2. 배열의 길이만큼 돌면서 DFS 수행 3. 배열의 길이보다 크면 리턴하고, 나머지는 해당 배열값을 current에 넣고, list에 존재하지 않으면 삽입 4. 모두 돌기위해서, 다음값을 더한 current와 다음값을 제거한 current로 나눠 DFS 수행 class ..

반응형