и посмотреть медиа
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.
Задача: 861. Score After Flipping Matrix Сложность: hard Дан целочисленный массив nums и целое число k. Верните длину самой короткой непустой подмассива nums, сумма которого составляет как минимум k. Если такого подмассива нет, верните -1. Подмассив — это непрерывная часть массива. Пример: Input: nums = [1], k = 1 Output: 1 👨💻 Алгоритм: 1⃣Создайте "моноочередь" индексов P: дек индексов x_0, x_1, ..., так чтобы P[x_0], P[x_1], ... увеличивались. 2⃣При добавлении нового индекса y, удалите x_i из конца дека, чтобы P[x_0], P[x_1], ..., P[y] увеличивались. 3⃣Если P[y] >= P[x_0] + K, то (как описано ранее) мы больше не рассматриваем этот x_0 и удаляем его из начала дека. 😎 Решение: var shortestSubarray = function(A, K) { const N = A.length; const P = new Array(N + 1).fill(0); for (let i = 0; i < N; ++i) { P[i + 1] = P[i] + A[i]; } let ans = N + 1; const monoq = []; for (let y = 0; y < P.length; ++y) { while (monoq.length > 0 && P[y] 0 && P[y] >= P[monoq[0]] + K) { ans = Math.min(ans, y - monoq.shift()); } monoq.push(y); } return ans < N + 1 ? ans : -1; }; Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!