전체 글
-
[LeetCode] 542. 01 Matrix 문제 풀이Algorithm/문제풀이 2021. 9. 8. 15:57
https://leetcode.com/problems/01-matrix/ 01 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 문제 내용 2차원 배열에서 1값에서 가장 가까운 0까지의 거리를 모두 구하는 문제 문제 풀이 처음에는 모든 점을 탐색하면서 1을 찾고 0으로 BFS를 돌리는 방법을 생각했는데 시간초과가 걸린다. 가만히 생각하면 BFS 1번만 돌리면 모든 점의 거리를 구할 수 있다. 0인 지점을 미리 queue에 다 담아서 최단거리를 각각 ..
-
[LeetCode] 116. Populating Next Right Pointers in Each Node 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:31
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Populating Next Right Pointers in Each Node - 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 문제 내용 완전 바이너리 트리가 주어지는데 같은 depth를 왼쪽에서 오른쪽으로 연결된 링크드 리스트 처럼 연결시키는 문제 문제 풀이 Depth마다 연결하는 문제로 BFS를 이용하기에 딱이다. Depth..
-
[LeetCode] 617. Merge Two Binary Trees 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:28
https://leetcode.com/problems/merge-two-binary-trees/ Merge Two Binary Trees - 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 문제 내용 2개의 바이너리 트리가 주어지는데 2개를 합친 트리의 root를 return 하면 된다. 합칠 때는 규칙이 있는데, 한쪽이 nullptr 이면 덮어 쓰고, 둘다 노드가 존재하면 sum을 해서 값을 갱신하면 된다. 문제 풀이 조건에 맞춰서 재귀함수로 구현하면 깔끔하게 ..
-
[LeetCode] 695. Max Area of Island 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:25
https://leetcode.com/problems/max-area-of-island/ Max Area of Island - 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 문제 내용 grid 맵에 1로 표시된 island가 있는데 가장 큰 영역 찾기 문제 풀이 BFS써서 각 섬에서 영역을 구하고, 가장 큰 영역을 찾으면 된다. class Solution { public: int row,col; int dy[4] = {-1, 0, 1, 0}; int dx[4] ..
-
[LeetCode] 733. Flood Fill 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:23
https://leetcode.com/problems/flood-fill/ Flood Fill - 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 문제 내용 특정 지점에서 상하좌우 4방향으로 움직이면서 같은 색을 모두 칠하는 문제 문제 풀이 bfs를 이용해서 하나씩 색칠하면 된다. color가 같거나 target color가 아니면 skip 해야하는 조건이 필요하다. class Solution { public: vector floodFill(vector& imag..
-
[LeetCode] 19. Remove Nth Node From End of List 문제풀이Algorithm/문제풀이 2021. 9. 3. 21:37
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Remove Nth Node From End of List - 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 문제 내용 List의 끝에서 n번째 element를 제거하는 문제 근데 one pass 만에 하라고 한다. 문제 풀이 size를 구해서 size - n하면 제거할 node를 찾아서 했다. class Solution { public: ListN..
-
[LeetCode] 876. Middle of the Linked List 문제 풀이Algorithm/문제풀이 2021. 9. 3. 20:20
https://leetcode.com/problems/middle-of-the-linked-list/ Middle of the Linked List - 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 문제 내용 List 에서 중간 노드를 찾는 문제 문제 풀이 전체 리스트의 크기만 알면 그 절반을 구할 수 있다. class Solution { public: ListNode* middleNode(ListNode* head) { int size = 0; ListNode..
-
[LeetCode] 95. Unique Binary Search Trees II 문제 풀이Algorithm/문제풀이 2021. 9. 3. 01:57
https://leetcode.com/problems/unique-binary-search-trees-ii/ Unique Binary Search Trees 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 문제 내용 정수 n 이 주어질 때, [1, n] 의 node에서 binary search tree 로 구성할 수 있는 배열을 출력 Example 1: Input: n = 3 Output: [[1,null,2,null,3],[1,null,3,2],[2,1..