await Promise.all([
'https://learn.javascript.ru/article/promise-chaining/one.js',
'https://learn.javascript.ru/article/promise-chaining/two.js',
'https://learn.javascript.ru/article/promise-chaining/three.js',
].map(loadScript));
one();
two();
three();
function siftDown(arr, i) {
let chi = i * 2 + 1;
while (chi < arr.length) {
const minChildIdx = chi < arr.length - 1 && arr[chi + 1] < arr[chi] ? chi + 1 : chi;
const chv = arr[minChildIdx];
if (arr[i] <= chv) break;
arr[minChildIdx] = arr[i];
arr[i] = chv;
i = minChildIdx;
chi = i * 2 + 1;
}
}
function heapify(arr) {
for (let i = Math.floor(arr.length / 2); i >= 0; --i) {
siftDown(arr, i);
}
}
function findMinWithHeap(arr) {
const heap = arr.flat();
heapify(heap);
return heap[0];
}
console.log(findMinWithHeap([[2, 1, 2], [0, 4, 5] ]));
function sortTickets(tickets) {
const map = new Map();
const transits = new Set();
tickets.forEach((t) => {
map.set(t.from, t);
transits.add(t.to);
});
const start = tickets.find(t => !transits.has(t.from));
const result = [];
for (let t = start; t; t = map.get(t.to)) {
result.push(t);
}
return result;
}
const sorted = sortTickets([
{from: 'Sochi', to: 'Paris'},
{from: 'Moscow', to: 'Sochi'},
{from: 'London', to: 'Moscow'},
{from: 'Berlin', to: 'Kamchatka'},
{from: 'Paris', to: 'Berlin'},
]);