본문 바로가기

두두의 알고리즘

(145)
[해시] 릿코드 438 Medium 'Find All Anagrams in a String' (Python) 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 1. 순서 상관 없이 s 안에 연속으로 p의 문자열이 나열되어 있다면 첫번째 인덱스를 반환한다. 2. 순서가 상관이 없으므로 Counter 라이브러리 사용 3. 시간복잡도를 효율적으로 사용하기 위해 여러 변수를 선언해둠 from collections i..
[비트연산] 릿코드 231 Easy 'Power of Two' (Python) https://leetcode.com/problems/power-of-two/ Power of Two - 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. n이 2의 지수여야 하므로 n을 2진수로 바꾸면 모두 1이다 2. n에서 1을 빼면 10, 100000 처럼 나오므로 n-1과 n을 & 비트연산하면 무조건 0이 나올 것이다 3. n이 0보다 크고, n&(n-1)이 0이면 True가 나오게 한다. class Solution: def isPowerOfTwo(s..
[이분탐색] 릿코드 153 Medium 'Find Minimum in Rotated Sorted Array' (Python) https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Find Minimum in Rotated Sorted 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 1. 배열의 최소 요소를 반환하라고 했으므로 min 함수 사용 class Solution: def findMin(self, nums: List[int]) -> int: return min(nums)
[이분탐색] 릿코드 Medium 74 'Search a 2D Matrix' (Python) https://leetcode.com/problems/search-a-2d-matrix/ Search a 2D 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 1. 각 행에 target 값이 있으면 true class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for i in matrix: if target in i: return True retur..
[이분탐색] 릿코드 Medium 33 'Search in Rotated Sorted Array' (Python) https://leetcode.com/problems/search-in-rotated-sorted-array/ Search in Rotated Sorted 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 1. nums에서 target의 인덱스 값을 찾아라 2. target 값이 없으면 -1 class Solution: def search(self, nums: List[int], target: int) -> int: if target in nums: ..
[이분탐색] 릿코드 Medium 34 'Find First and Last Position of Element in Sorted Array' (Python) https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ Find First and Last Position of Element in Sorted 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 1. 파이썬의 bisect 라이브러리를 활용 2. target이 nums에 있다면 정답을 반환하고 없으면 [-1,-1]을 반환함 from bisect impor..
[해시] 릿코드 Medium 567 'Permutation in String' (Python) https://leetcode.com/problems/permutation-in-string/ Permutation in 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 순서 상관없이 s2 내에 연속해서 s1의 값이 들어있으면 됨 그 값이 몇개 있는지만 확인하면 되므로 Counter 라이브러리 이용 from collections import Counter class Solution: def checkInclusion(self, s1: str, s2..
[동적계획법] 릿코드 Medium 198 'House Robber' (Python) https://leetcode.com/problems/house-robber/ House Robber - 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. 더하는 숫자들이 이웃하지만 않으면 됨 2. 바로 직전의 돈과 두번째 전의 돈+현재 돈 중 큰 값을 구하면 됨 3. 집이 하나만 있을 수 있음 class Solution: def rob(self, nums: List[int]) -> int: answer = [] if len(nums)==1: return nu..

LIST