и посмотреть медиа
JavaScript | LeetCode
и посмотреть медиа
LeetCode tasks and solutions in JavaScript. Ideal for interview preparation and improving algorithm skills.
LeetCode tasks and solutions in JavaScript. Ideal for interview preparation and improving algorithm skills.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
The channel is dedicated to solving LeetCode tasks in JavaScript. It contains code examples for popular algorithmic problems frequently encountered in interviews. The materials will help master data structures, sorting, dynamic programming, and other key topics. Suitable for junior and middle developers looking to improve their skills.
Задача: 126.Word Ladder II Сложность: hard Последовательность преобразований от слова beginWord до слова endWord с использованием словаря wordList — это последовательность слов beginWord -> s1 -> s2 -> ... -> sk, для которой выполняются следующие условия: Каждая пара соседних слов отличается ровно одной буквой. Каждое si для 1 "dot" -> "dog" -> "cog" "hit" -> "hot" -> "lot" -> "log" -> "cog" 👨💻 Алгоритм: 1️⃣Сохранение слов из списка слов (wordList) в хэш-таблицу (unordered set) для эффективного удаления слов в процессе поиска в ширину (BFS). 2️⃣Выполнение BFS, добавление связей в список смежности (adjList). После завершения уровня удалять посещенные слова из wordList. 3️⃣Начать с beginWord и отслеживать текущий путь как currPath, просматривать все возможные пути, и когда путь ведет к endWord, сохранять путь в shortestPaths. 😎 Решение: var findLadders = function(beginWord, endWord, wordList) { let adjList = {}; let currPath = [endWord]; let shortestPaths = []; let wordSet = new Set(wordList); function findNeighbors(word) { let neighbors = []; let charList = Array.from(word); for (let i = 0; i < charList.length; i++) { let oldChar = charList[i]; for (let c = 'a'.charCodeAt(0); c { currPath.push(neighbor); backtrack(neighbor); currPath.pop(); }); } function bfs() { let queue = [beginWord]; wordSet.delete(beginWord); while (queue.length) { let current = queue.shift(); let neighbors = findNeighbors(current); neighbors.forEach(neighbor => { if (!adjList[neighbor]) adjList[neighbor] = []; adjList[neighbor].push(current); if (!wordSet.has(neighbor)) { queue.push(neighbor); wordSet.delete(neighbor); } }); } } bfs(); backtrack(beginWord); return shortestPaths; }; Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!