본문 바로가기

탐색

(9)
[DFS] 릿코드 200 Medium 'Number of Islands' (Python) https://leetcode.com/problems/number-of-islands/ Number of Islands - 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. 상하좌우로 1이 연결되어 있으면 하나의 섬이라고 본다. 2. DFS 알고리즘으로 해결한다. class Solution: def numIslands(self, grid: List[List[str]]) -> int: answer = 0 q = [] dx = [0,0,-1,1] dy = [-1,..
[이분탐색] 릿코드 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: ..
[BFS] 릿코드 Easy 733 'Flood Fill' (Python) 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 주어진 image [sr][sc]에서 위, 아래, 왼쪽, 오른쪽에 있는 값이 처음 image [sr][sc]와 동일하면 newColor로 변경한다. 변경한 값의 위, 아래, 왼쪽, 오른쪽 값도 변경한다. (반복) class Solution: def floodFill(self, image: List[List[int]], ..
[이분탐색] 릿코드 Easy 278 'First Bad Version' (Python) https://leetcode.com/problems/first-bad-version/ First Bad Version - 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 원소의 값이 크기 때문에 이분 탐색으로 정답을 찾아야 함 # The isBadVersion API is already defined for you. # @param version, an integer # @return an integer # def isBadVersion(version): cla..
[이분탐색] 릿코드 Easy 704 'Binary Search' (Python) https://leetcode.com/problems/binary-search/ Binary Search - 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: def search(self, nums: List[int], target: int) -> int: def binary_search(array,target,start,end): if start>end: return None mid = (start+e..
[이분탐색] 난이도2, Zoho 인터뷰 '정렬된 배열에서 특정 수의 개수 구하기' (Python) N개의 원소를 포함하고 있는 수열이 오름차순으로 정렬되어 있습니다. 이때 이 수열에서 x가 등장하는 횟수를 계산하세요. 예를 들어 수열 {1,1,2,2,2,2,3}이 있을 때 x=2라면, 현재 수열에서 값이 2인 원소가 4개이므로 4를 출력합니다. 단, 이 문제는 시간 복잡도 O(logN)으로 알고리즘을 설계하지 않으면 '시간 초과' 판정을 받습니다. 첫째 줄에 N과 x가 정수 형태로 공백으로 구분되어 입력됩니다. (1
[이분탐색] 프로그래머스 L4 '가사 검색' (Python) https://programmers.co.kr/learn/courses/30/lessons/60060 코딩테스트 연습 - 가사 검색 programmers.co.kr 물음표(?)를 와일드카드로 표현하고 길이가 같아야 한다고 해서 처음엔 정규표현식을 사용해서 풀었다. 한 달 뒤에는 조건문을 활용하여 문제를 풀었다. #211017 -> 효율성 테스트 1,2,3,4 실패 #정규표현식 활용 import re def solution(words, queries): answer = [0] * len(queries) words = sorted(words) p = [0] * len(queries) for i in range(len(queries)): queries[i] = queries[i].replace("?",".")..

LIST