| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- Python Implementation
- Generator
- 파이썬
- 밴픽
- 43. Multiply Strings
- Protocol
- Python Code
- Convert Sorted List to Binary Search Tree
- LeetCode
- Substring with Concatenation of All Words
- t1
- Class
- 315. Count of Smaller Numbers After Self
- shiba
- DWG
- 715. Range Module
- attribute
- Python
- Regular Expression
- 프로그래머스
- 30. Substring with Concatenation of All Words
- 시바견
- 운영체제
- Decorator
- 컴퓨터의 구조
- concurrency
- data science
- iterator
- kaggle
- 109. Convert Sorted List to Binary Search Tree
Archives
- Today
- Total
Scribbling
LeetCode: 127. Word Ladder 본문
아래 문제와 거의 같다.
https://focalpoint.tistory.com/101
LeetCode: 126. Word Ladder II - 시바견의 끄적임
개인적으로 어려웠던 문제... A. 내가 푼 방법 일단 내가 푼 방법은 아래와 같다. 1) 모든 word에 대해 graph를 그린다. (word간 글자가 1개만 차이나는 경우 연결) 2) 다익스트라 알고리즘으로 최단 경
focalpoint.tistory.com
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordSet = set(wordList)
wordSet.discard(beginWord)
if endWord not in wordSet:
return 0
def neighbor(word):
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
candidate = word[:i] + c + word[i+1:]
if candidate in wordSet:
yield candidate
q = [[beginWord]]
while q:
temp_q = []
for i in range(len(q)):
last_word = q[i][-1]
if last_word == endWord:
return len(q[i])
deletes = []
for next_word in neighbor(last_word):
temp_q.append(q[i]+[next_word])
deletes.append(next_word)
wordSet -= set(deletes)
q = temp_q
return 0'Computer Science > Coding Test' 카테고리의 다른 글
| LeetCode: 128. Longest Consecutive Sequence (0) | 2021.11.08 |
|---|---|
| 프로그래머스: 방의 개수 (0) | 2021.11.08 |
| 프로그래머스: 가장 먼 노드 (0) | 2021.11.05 |
| LeetCode: 124. Binary Tree Maximum Path Sum (0) | 2021.11.04 |
| LeetCode: 188. Best Time to Buy and Sell Stock IV (0) | 2021.11.04 |