207. Course Schedule
·
Algorithm
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 특정 과목을 수강하기 위해 반드시 선수강해야 하는 과목의 조합이 입력으로 주어진다. 해당 조합이 유효한지를 판단하는 문제다. 예를 들어서 [[1,0], [2,1]]이라고 주어지면 1번 과목은 0번을 선수강해야, 2번 과목은 1번 과목을 선수강해야 하는 조건이다. 문제없기 때문에 True 반환한다. [[1,0], [0,1]]의 경우는 False를 반환해야 한다. 1번 과목은 0번을 선수..
73. Set Matrix Zeroes
·
Algorithm
Set Matrix Zeroes - 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차원 배열에서 (row, col)에 해당하는 값이 0이라면 해당 row와 col을 배열 끝까지 0의 값으로 채우는 문제다. 1. 구현 from typing import * def set_row(board, row): board[row] = [1] * len(board[row]) def set_col(board, col): for row in range(l..
128. Longest Consecutive Sequence
·
Algorithm
Longest Consecutive Sequence - 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가 주어진다. 이 List의 정수들이 연속되게 증가, 혹은 감소하는 조합의 최대 길이를 반환하는 문제다. 시간 복잡도 제한은 O(n)이다. 예시는 다음과 같다. Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [..
11. Container With Most Water
·
Algorithm
Container With Most Water - 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로 주어진다. 두 개의 값을 골라서 가장 많은 양의 물을 담을 수 있는 컨테이너의 세로 벽 후보를 고르고, 그때 담을 수 있는 물의 양을 반환하는 문제다. 1. Brute Force from typing import * class Solution: def maxArea(self, height: List[int]) -> int: set1 = ..
@cache
·
Programming
96. Unique Binary Search Trees Unique Binary Search 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 노드.. doljae.tistory.com 최근에 이런 문제를 풀었었다. 트리 문제가 그러하듯 left, right에 함수 달고 반환 값을 조합해서 구할 수 있는 문제였다. 방향까진 생각했으나 코드까지는 작성 못했던 걸 풀이를 참고해서(복붙 해서) 작성해봤고 결과는 다음과 같다. class Solution: ..
96. Unique Binary Search Trees
·
Algorithm
Unique Binary Search 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 노드의 개수가 주어진다. 주어진 노드를 모두 사용해서 만들 수 있는 이진트리의 형태의 가짓수를 반환하는 문제다. 1. DFS 처음에 DFS로 해결할 수 있는지를 생각해보았다. 결과적으론 불가능하다고 판단했다. 루트 노드를 기준으로 DFS는 left subtree, right subtree를 한 재귀에서 위 문제에서 확인해야 하는 상태를 볼 수 없다. 2. DP cl..