Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- binary search
- python 릿코드
- LeetCode
- 상가수익률계산기
- 릿코드풀기
- 코틀린기초
- 알고리즘풀기
- 파이썬 알고리즘
- leetcode 풀기
- 파이썬알고리즘
- python Leetcode
- 릿코드 파이썬
- leetcode풀기
- 파이썬알고리즘풀기
- 릿코드풀이
- 릿코드 풀기
- 파이썬릿코드
- 파이썬릿코드풀기
- python 알고리즘
- python sorted
- 릿코드
- 파이썬 프로그래머스
- 알고리즘풀이
- 잇츠디모
- 파이썬 알고리즘 풀기
- 파이썬 릿코드
- leetcode풀이
- python priority queue
- python zip_longest
- python xor
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 1436. Destination City 본문
반응형
1436. Destination City
제목
문제)
솔루션1) - hash 자료구조
direct path 두 도시를 key, value 형태의 hash로 저장합니다.
destination 중 hash의 key에 없다는 의미는 출발해서 도착하는 도시가 없다는 것이므로 그것이 해에 해당합니다.
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
d = {}
for path in paths:
d[path[0]] = path[1]
for dest in d.values():
if dest not in d:
return dest
return None
솔루션2) - set 사용
출발하는 도시와 도착하는 도시를 set 자료구조로 데이터를 유지합니다.
도착하는 도시에서 출발하는 도시를 빼서 남는 도시가 정답입니다.
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
start_cities, dest_cities = map(set, zip(*paths))
return (dest_cities - start_cities).pop()
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 1002. Find Common Characters (0) | 2021.11.23 |
---|---|
LeetCode 풀기 - 1812. Determine Color of a Chessboard Square (0) | 2021.11.21 |
LeetCode 풀기 - 1295. Find Numbers with Even Number of Digits (0) | 2021.11.21 |
LeetCode 풀기 - 21. Merge Two Sorted Lists (0) | 2021.11.20 |
LeetCode 풀기 - 102. Binary Tree Level Order Traversal (0) | 2021.11.20 |
Comments