This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Реализует очередь с помощью двух стеков. | |
* | |
* @example | |
* const q = new MyQueue(); | |
* q.add(10); | |
* q.add(12); | |
* q.add(15); | |
* q.add(17); | |
* // 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Обходит граф в ширину, проверяя существует ли маршрут | |
* между указанными нодами. | |
* Предполагается, что граф представлен в виде списка смежности, | |
* проще говоря, массива где индексами являются элементы, в значениями | |
* массивы с дочерними элементами. | |
* | |
* @param {Array} graph | |
* @param {Number} start | |
* @param {Number} end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Представление графа в виде списка смежности: | |
* индексу соответствует родительскому элементу, | |
* а значению — массив с дочерними элементами. | |
*/ | |
const graph = [ | |
null, | |
null, | |
null, | |
[8,10], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Конструктор узла дерева. | |
* Каждый узел имеет 2 ссылки: левую и правую части поддерева. | |
*/ | |
class Node { | |
constructor(data) { | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Рекурсивно проходится по бинарному дереву от корня, | |
* пока не найдет подходящее место для нового узла. | |
* Каждая вставка требует обход дерева, т.е. O(log N), | |
* поэтому время работы для создания бинарного дерева O(n * log N) | |
*/ | |
function insertNodeIntoBST(root, node) { | |
if (root.data < node.data) { | |
if (root.right === null) { | |
root.right = node; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Находит количество всех возможных вариантов | |
* как можно добраться до n-й ступени по лестнице, | |
* если можно пройти 1, 2 или 3 ступени за один шаг. | |
* | |
* @param {number} n | |
* @returns {number} | |
*/ | |
function countWays(n) { | |
if (n < 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Находит количество всех возможных вариантов | |
* как можно добраться до n-й ступени по лестнице, | |
* если можно пройти 1, 2 или 3 ступени за один шаг. | |
* | |
* Используется кеш, ссылка на который передается вторым аргументом | |
* через все рекурсивные вызовы функции. | |
* | |
* @param {number} n | |
* @param {array<number>} cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Сливает два отсортированных массива. | |
* Результат окажется в первом массиве, | |
* в котором есть дополнительное место для второго массива. | |
* | |
* @example | |
* const a = [1,3,4]; | |
* const b = [2,3,5]; | |
* merge(a, b); | |
* // [1,2,3,3,4,5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Проверяет является ли переданная строка | |
* перестановкой палиндрома. | |
* Работает за O(n) и требует O(n) дополнительной памяти. | |
* | |
* @param {string} str | |
* @returns {boolean} | |
*/ | |
function isPalindromPermutation(str) { | |
const counters = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Проверяет является ли переданная строка | |
* перестановкой палиндрома. | |
* Работает за O(n) и не требует дополнительной памяти, | |
* использует битовый вектор для хранения счетчиков. | |
* | |
* @param {string} str | |
* @returns {boolean} | |
*/ | |
function isPalindromePermutation(str) { |