목록SW/알고리즘 (9)
허허의 오늘은 뭐 먹지?
leetcode.com/problems/n-ary-tree-level-order-traversal/ N-ary Tree Level Order Traversal - 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의 기본적인 문제입니다. queue에 시작점을 넣어두고 해당 시작점과 연결된 지점들을 다시 넣어주고를 반복합니다. 반복할 때마다 1depth씩 내려갑니다. class Solution { public List levelOrder(Node ro..
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 Linked List문제에서 흔히 볼 수 있는 runner, walker문제입니다. runner는 한번에 두번씩, walker는 한번에 한번씩 움직입니다. 그래서 runner가 끝에 다다랐을 때, walker는 중간까지 올 수 있습니다. class Solution { public ..
x^n 결과를 구하는 문제입니다. leetcode.com/problems/powx-n/ Pow(x, n) - 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 이 문제는 x를 n번 루프를 돌려서 풀면.. 타임아웃이 나버립니다. 2^10이라고 10번 2를 곱해줄것이 아니라 2^10 = 2^4 * 2^4 * 2^2 2^4 = 2^2 * 2^2 2^2 해서 총 세번의 연산으로 최종 값을 구할 수 있습니다. 아래 코드는.. i를 n부터 시작해서 i를 반씩 나눠주며 계산합니..
이 문제는 전형적인 topological sort (위상정렬) 문제입니다. leetcode.com/problems/course-schedule/ Course Schedule - 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 위키에 topological sort에 대해서 잘 설명되어 있습니다. ko.wikipedia.org/wiki/%EC%9C%84%EC%83%81%EC%A0%95%EB%A0%AC 위상정렬 - 위키백과, 우리 모두의 백과사전 위키백과, 우리 모두의..
DP와 관련된 문제입니다. leetcode.com/problems/partition-equal-subset-sum/ Partition Equal Subset 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 input이 [5, 11, 5, 1] 인 경우, [11], [5,5,1]로 그룹을 묶으면 두 그룹의 합이 같으므로 true를 리턴해야 합니다. 전체 값의 합이 22이므로, 어떤 수들의 합이 11이 되는지를 보면 됩니다. 처음으로 풀어본 것은 아래와 같..
겹치는 구간을 합치는 문제입니다. leetcode.com/problems/merge-intervals/ Merge Intervals - 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 이 문제의 핵심은 처리해야 할 구간 배열에 대해 정렬을 먼저 하는 것입니다. 시작 시간을 기준으로 입력된 값을 정렬합니다. 먼저 병합된 결과값을 저장하는 변수를 선언하고 정렬된 배열의 전체를 처음부터 탐색합니다. 만약에 병합된 결과값의 마지막으로 들어간 값의 종료시간보다 탐색 중인 ..
binary search tree가 정상적인지 체크하는 문제입니다. 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 간단한 문제이나 recursion에 익숙하지 않으면 은근히 어려워 할 수도 있는 문제입니다. 왼쪽 substree의 모든 노드가 root보다 작아야 합니다. 오른쪽 subtree의 모든 노드는 root보다 ..
모든 Anagram을 찾는 문제입니다. https://leetcode.com/problems/find-all-anagrams-in-a-string/ Find All Anagrams in a String - 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 이 문제는 sliding window 알고리즘을 사용하는 대표적인 문제 중 하나입니다. 어떻게든 노가다로 문제를 풀 수는 있지만 sliding window를 사용하지 않는다면 좋은 성능으로 풀기 어려운 문제입니다...
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으로 나눴을 때 나머지가 0인 조합을 구하는 문제입니다. 수를 하나 입력을 때, 기존에 입력한 숫자들 중에 현재의 숫자를 가지고 0을 만들 수 있는 수가 몇 개 존재하는..