본문 바로가기

728x90
반응형

스터디/알고리즘

(20)
[medium] 200. Number of Islands 문제 https://leetcode.com/problems/number-of-islands/ Number of Islands - 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 풀이방법 섬 개수를 세는 유명한 BFS 문제. M*N 배열을 탐색하면서 중복 방문을 허용하지 않도록 하면 된다. 붙어있는 land를 체크하기 위해서 좌,우,위,아래의 이웃한 셀들을 체크한다 grid[row][col]=="0" 인 경우, 해당 셀은 water를 뜻하므로 체크할 필요 없다 vi..
[medium] 869. Reordered Power of 2 문제 https://leetcode.com/problems/reordered-power-of-2/ Reordered Power of 2 - 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 풀이방법 문제는 간단한데 풀이방법이 딱 떠오르지 않아서 다른 사람들의 풀이를 참고했다. n을 나누거나 하는 방식으로 접근하는게 아니라 n의 범위가 1이상 10^9 이하에 초점을 맞춰야 했다. 주어진 n의 범위에 해당하는 2의 제곱수는 2^0, 2^1, ..., 2^31 이므로 이..
[easy] 13. Roman to Integer 문제 https://leetcode.com/problems/roman-to-integer/ Roman to Integer - 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 풀이방법 로마숫자를 변환하는 문제이다. "IV" 와 "VI" 처럼 "I"가 "V"보다 앞에 나왔는지 뒤에 나왔는지에 따라서 -가 될지 +가 될지가 달라지는데, 이를 고려해야 한다. 그래서 prev와 prevcnt로 이전에 나온 심볼의 종류와 횟수를 저장했다가, cur에서 새로운 심볼이 나올때 ..
[hard] 30. Substring with Concatenation of All Words 문제 https://leetcode.com/problems/substring-with-concatenation-of-all-words/ Substring with Concatenation of All Words - 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 풀이방법 silding window 방법으로 시도했으나 접근방법이 잘못되서 계속 예외케이스들이 나와서 실패. 결국 다른사람 풀이 보고 이해해서 풀었다. words의 단어들의 길이는 모두 동일하고, word..
[medium] 98. Validate Binary Search Tree 문제 https://leetcode.com/problems/validate-binary-search-tree/ Validate Binary Search Tree - 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 풀이방법 root부터 시작해서 최소값, 최대값을 저장하면서 children이 범위 내에 있는지 확인하면 되는 문제이다. right children을 탐색할 때는 최대값을 업데이트하고, left children을 탐색하다가 최소값을 업데이트 한다. 최소값,..
[medium] 916. Word Subsets 문제 https://leetcode.com/problems/word-subsets/ Word 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 풀이방법 words1의 단어들 중 words2의 모든 단어를 subset으로 가질 수 있는 단어들을 찾는 문제이다. 시간 절약을 위해 words2에 대한 전처리가 매우 중요했다. words2의 단어들 중에 중복되는게 있을 수 있으므로 set으로 변환했다가 다시 list로 변환함으로써 중복을 제거한다. wor..
[medium] 240. Search a 2D Matrix II 문제 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D 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 풀이방법 문제 보자마자 DFS로 중복방문 하지 않도록 하면서 풀면 되겠다는 생각이 들었다 1. [0,0] 위치부터 DFS 탐색을 시작한다 2. 탐색은 아래 과정을 반복한다 - 방문했던 곳이면 패스하고, 방문하지 않았던 곳이면 visited 를 True로 바꾸고 해당지점의 숫자..
[hard] 629. K Inverse Pairs Array 문제 https://leetcode.com/problems/k-inverse-pairs-array/ K Inverse Pairs Array - 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 풀이방법 숫자제한을 보니 제일 먼저 떠오르는 브루트포스로 풀면 시간초과가 날 수 밖에 없는 문제라서 DP로 접근해야겠다고 생각했다 그런데 DP 규칙을 어떻게 만들어야하는지... 역시 hard... 그래서 https://leetcode.com/problems/k-inverse-..

728x90
반응형