본문 바로가기

전체 글

[leetcode] 221. Maximal Square 문제 링크: https://leetcode.com/problems/maximal-square/description/ 1로만 채워진 가장 큰 정사각형을 구하는 문제이다. Approach 1: Brute Force 가장 단순한 방법은 matrix 의 모든 cell 을 순회하며, 각 cell 이 형성할 수 있는 가장 큰 정사각형을 구하는 방식이다. 방법은 아래와 같다. 1. matrix 를 순회하며, 각 cell 을 left uppermost 로 하는 가장 큰 정사각형을 구한다. 2. cell 이 1이면 다음 계산을 수행한다. - 일시적으로 row, col 을 1씩 증가 시켜 해당 row 와 col 에 속한 cell 들이 전부 1인지 확인한다. - 전부 1이면, 대각선 아래방향으로 이동한다. - 0이 하나라도.. 더보기
[leetcode] 815. Bus Routes 문제 링크 LeetCode - The World's Leading Online Programming Learning Platform 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 최소 버스 수를 구하는 문제입니다. Tip > 문제에 "최소" 라는 말이 있다면, BFS 나 Heap 을 후보군으로 떠올려 봅시다. Approach : BFS 사용할 샘플 예제는 아래와 같습니다. Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6 A.. 더보기
[leetcode] 873. Length of Longest Fibonacci Subsequence 문제 링크 LeetCode - The World's Leading Online Programming Learning Platform 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 주어진 배열의 Subsequence 중 Fibonacci 한 배열의 최장 길이를 구하는 문제이다. Fibonacci 라 하면, 3개의 수를 가지고 규칙이 성립되는 지 확인해야 하니까 어떻게 코드를 짜야할 지 막막할 수 있다. (3개의 수라고 말한 이유는 a+b=c 가 성립되면, a,b,c 이 3개의 수.. 더보기
[leetcode] 870. Advantage Shuffle https://leetcode.com/problems/advantage-shuffle/description/ LeetCode - The World's Leading Online Programming Learning Platform 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 주어진 nums1 배열에서 조건에 맞는 permutation 을 찾아 반환하는 문제이다. 사용할 샘플 예제는 아래와 같다. nums1 = [12, 24, 8, 32], nums2 = [13, 25, 32,.. 더보기
[leetcode] 916. Word Subsets 916. Word Subsets LeetCode - The World's Leading Online Programming Learning Platform 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 이 문제는 words2 의 모든 문자열이 포함되어 있는 words1 의 문자열을 반환하는 문제이다. (subset) 나의 시도 이 풀이를 봤을 때 이중 루프 돌며, words1 과 word2 의 hashmap 을 비교하는 것밖에 생각나지 않았다. 참고로 문제 조건이 아래와 같았기 .. 더보기
[Algorithm] 217-Contains-Duplicate https://leetcode.com/problems/contains-duplicate/ Contains Duplicate - 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. HashSet 이용한 풀이 class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ inputNums = set() for i in (nums): input.. 더보기
[Algorithm] 213-house-robber2 https://leetcode.com/problems/house-robber-ii/ House Robber 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 1. 시간복잡도 - O(n) : 똑같은 알고리즘(helper function) 2번 호출할 뿐임 2. 공간복잡도 - O(1) : don't need any extra data structure. just 변수 2개만 사용하고 있음. class Solution(object): def rob(self, n.. 더보기
[Algorithm] 70. Climbing Stairs 문제 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 내 풀이 class Solution(object): def climbStairs(self, n): if n == 1: return 1 d = [0] * (n) d[n-1] = 1 d[n-2] = 2 for i in range(n-3,-1,-1): d[i] = d[i+1] + d[i+2] return d.. 더보기