본문 바로가기
반응형

dfs5

#33 [파이썬] 프로그래머스: 여행경로 https://programmers.co.kr/learn/courses/30/lessons/43164 코딩테스트 연습 - 여행경로 [["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL","SFO"]] ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"] programmers.co.kr from collections import defaultdict def dfs(dic, route, n): if len(route) == n+1: return route for i, r in enumerate(dic[route[-1]]): dic[route[-1]].pop(i) answer = dfs(dic, route+[r],.. 2022. 3. 12.
#32 [파이썬] LeetCode: Number of Provinces https://leetcode.com/problems/number-of-provinces/submissions/ Number of Provinces - 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 코드 from typing import List class Solution: def dfs(self, i: int, isConnected: List[List[int]]): isConnected[i][i] = 0 for j in range(len(isConnected).. 2022. 2. 12.
#31 [파이썬] LeetCode: Number of Islands 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 코드 class Solution: def dfs(self, grid: List[List[str]], i: int, j: int): if i = len(grid) or j = len(grid[0]) or grid[i][j] == '0': return grid[i.. 2022. 2. 12.
#24 [파이썬] 프로그래머스: 타겟 넘버 https://programmers.co.kr/learn/courses/30/lessons/43165 코딩테스트 연습 - 타겟 넘버 n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다. -1+1+1+1+1 = 3 +1-1+1+1+ programmers.co.kr def solution(numbers, target): answer = 0 def dfs(i, sum): nonlocal answer if i == len(numbers) and target == sum: answer += 1 return if i == len(numbers): return dfs(i + 1,.. 2021. 6. 27.
728x90
반응형