본문 바로가기

반응형

Java/Java 알고리즘 LeetCode

(60)
[LeetCode- Ch9. 백트래킹] 2. 순열 # https://leetcode.com/problems/permutations/ Permutations - 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. 배열에 [1,2,3]이 존재한다면 -> 1부터 시작해 배열의 길이만큼 깊이 우선 탐색 2. 배열의 길이만큼 깊이 우선 탐색시 -> 마지막 값 삭제 후, 다시 깊이 우선 탐색 수행 3. 현재 배열의 길이와 주어진 배열의 길이가 같을 때, 해당 리스트를 결과에 담는다. class Solution { //순열 :..
[LeetCode- Ch9. 백트래킹] 1. 생성된 괄호 # https://leetcode.com/problems/generate-parentheses/ Generate 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 1. 특정 조건에서 반대로 돌 수 있도록 조건 생성 2. left와 right 모두 소진시 리스트에 넣는다. class Solution { static int count=0; public List generateParenthesis(int n) { //완성된 괄호의 개수가 n개인 가능..
[LeetCode- Ch8. 동적계획법] 4. 가장 긴 증가하는 서브시퀀스 https://leetcode.com/problems/longest-increasing-subsequence/ Longest Increasing Subsequence - 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/643 [Ch.10 - DP] 03. 최대 부분 증가수열 [+LIS] 3. 최대 부분 증가수열 설명 N개의 자연수로 이루어진 수열이 주어졌을 때, 그 중에서 가장 길게 증가하는(작은 수에..
[LeetCode- Ch8. 동적계획법] 3. 동전 교환 # https://leetcode.com/problems/coin-change/submissions/ Coin Change - 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/662 [Ch.10 - DP] 05. 동전교환 [+냅색 알고리즘] 5. 동전교환(냅색 알고리즘) 냅색 알고리즘 : 담을 수 있는 무게가 정해진 백팩에 가장 비싼 금액의 물건으로 채우는 알고리즘 설명 다음과 같이 여러 ..
[LeetCode- Ch8. 동적계획법] 2. 계단 오르기 # https://leetcode.com/problems/climbing-stairs/ Climbing Stairs - 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칸씩 오를 수 있다. 0칸 -> 경우의 수 0 1칸 -> 경우의 수 1 2칸 -> 경우의 수 2 .... n칸 -> n-1칸의 경우의 수 + n-2칸의 경우의 수 class Solution { public int climbStairs(int n) { //계단은 1칸이나 2칸씩 오를 수 있다...
[LeetCode- Ch8. 동적계획법] 1. 유일한 경로 https://leetcode.com/problems/unique-paths/ Unique Paths - 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 문제 조건 (0,0)부터 (n,m)까지 유일한 경로의 개수를 구한다. 문제 해결 순서 1. 한번에 이동가능한 경로를 이동경로 배열에 추가 2. 그 외의 경로 두 경로의 합으로 추가 class Solution { public int uniquePaths(int m, int n) { int[][] arr = new ..
[LeetCode- Ch7. DFS & BFS] 7. 구슬 미로 https://www.lintcode.com/problem/787/ LintCode 炼码 www.lintcode.com BFS 기본 방향 설정 & 배열 사이즈 변수 설정 맞는 조건 탐색 큐 생성 조건 체크해서 큐 넣기 마이너스 좌표 체크 m*n 범위 체크 grid[x][y]값 체크 문제 조건 1. 구슬은 벽이 없으면 한 방향으로 쭉 이동한다. 2. 이동한 다음에는 다른 방향으로 이동 가능하다. 3. 시작지점에서 목적지점까지 갈 수 있는지 여부 리턴 문제 풀이 순서 - BFS 1. Queue에서 poll 한후 2. 4방향으로 체크시작 3. 공식: 조건체크 부분 수행 4. (3,4)까지 도착한후 에러 체크 한다. While문에 서 올린 좌표를 –로 원상복귀 visited[x][y]==true인지 체크 후 방..
[LeetCode- Ch7. DFS & BFS] 6. 유효하지 않은 괄호 제거 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid 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 1. 문자열에서 쌍으로 존재하는 괄호만 리턴하는데, 가능한 경우의 수 모두를 담아서 리턴 2. 유효하지 않는 괄호는 제거한다. 문제 해결 순서 Queue에 저장 케이스별로 유효성 체크 쌍으로 존재하는지 체크해, 해당하는 문자열을 새로운 큐에 저장 유효한 괄호 확인..

반응형