и посмотреть медиа
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.
Задача: 907. Sum of Subarray Minimums Сложность: medium Учитывая массив целых чисел arr, найдите сумму min(b), где b находится в каждом (смежном) подмассиве arr. Поскольку ответ может быть большим, верните ответ по модулю 109 + 7. Пример: Input: arr = [3,1,2,4] Output: 17 👨💻 Алгоритм: 1⃣Использовать монотонный стек для нахождения ближайшего меньшего элемента слева и справа для каждого элемента массива. 2⃣Использовать эту информацию для вычисления количества подмассивов, где каждый элемент является минимальным. 3⃣Вычислить сумму минимальных значений для всех подмассивов и вернуть результат по модулю 10^9 + 7. 😎 Решение: var sumSubarrayMins = function(arr) { const MOD = 1e9 + 7; const n = arr.length; const left = new Array(n).fill(0); const right = new Array(n).fill(0); const stack = []; for (let i = 0; i < n; i++) { while (stack.length && arr[stack[stack.length - 1]] > arr[i]) { stack.pop(); } left[i] = stack.length ? i - stack[stack.length - 1] : i + 1; stack.push(i); } stack.length = 0; for (let i = n - 1; i >= 0; i--) { while (stack.length && arr[stack[stack.length - 1]] >= arr[i]) { stack.pop(); } right[i] = stack.length ? stack[stack.length - 1] - i : n - i; stack.push(i); } let result = 0; for (let i = 0; i < n; i++) { result = (result + arr[i] * left[i] * right[i]) % MOD; } return result; }; Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!