注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
有没有leetcode大神能够分析一下这道题的最优解是什么?
我的solution时间复杂度应该是O(N * M * M), 空间复杂度是O(N * M )
N = len(wordList)
M = len(beginWord)
答案上看到的空间复杂度是O(N * M * M),我的空间复杂度分析有问题的话请大神提点一下,如果有更优的解法欢迎讨论!
找工作不容易,大家互帮互助!
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
# Convert word list to a set for O(1) lookups
word_set = set(wordList)
if endWord not in word_set:
return 0
# Initialize id and not visited yet, enqueue it
if next_word in word_set and next_word not in begin_visited:
begin_visited[next_word] = current_steps + 1
begin_queue.append(next_word)
return 0 # If no valid transformation |