и посмотреть медиа
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.
Задача: 1464. Maximum Product of Two Elements in an Array Сложность: easy Дан массив целых чисел nums, выберите два разных индекса i и j этого массива. Верните максимальное значение (nums[i]-1)*(nums[j]-1). Пример: Input: nums = [3,4,5,2] Output: 12 Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 👨💻 Алгоритм: 1⃣Инициализируйте biggest = 0 и secondBiggest = 0. 2⃣Итерируйте по каждому элементу массива nums: Если текущий элемент больше biggest, обновите secondBiggest = biggest и biggest = текущий элемент. Иначе обновите secondBiggest, если текущий элемент больше secondBiggest. 3⃣Верните (biggest - 1) * (secondBiggest - 1). 😎 Решение: class Solution { maxProduct(nums) { let biggest = 0; let secondBiggest = 0; for (let num of nums) { if (num > biggest) { secondBiggest = biggest; biggest = num; } else if (num > secondBiggest) { secondBiggest = num; } } return (biggest - 1) * (secondBiggest - 1); } } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!