и посмотреть медиа
Kotlin | LeetCode
и посмотреть медиа
LeetCode problems and solutions in Kotlin.
LeetCode problems and solutions in Kotlin.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
LeetCode problem solutions in Kotlin. Optimized code for mobile developers and algorithm specialists.
Задача: 673. Number of Longest Increasing Subsequence Сложность: medium Дан массив целых чисел nums, верните количество самых длинных строго возрастающих подпоследовательностей. Пример: Input: n = 1, presses = 1 Output: 2 Explanation: Status can be: - [off] by pressing button 1 - [on] by pressing button 2 👨💻 Алгоритм: 1⃣Объявите два массива динамического программирования length и count, и инициализируйте их значениями length[i]=1 и count[i]=1. Итерируйте i от 0 до n−1. Для каждого i итерируйте j от 0 до i−1 и, если nums[j] < nums[i], обновите length[i] и count[i] в зависимости от значений length[j] и count[j]. 2⃣Найдите максимальное значение в массиве length и сохраните его в переменной maxLength. Инициализируйте переменную result = 0. 3⃣Итерируйте i от 0 до n−1 и, если length[i] = maxLength, добавьте count[i] к result. Верните result. 😎 Решение: class Solution { fun findNumberOfLIS(nums: IntArray): Int { val n = nums.size val length = IntArray(n) { 1 } val count = IntArray(n) { 1 } for (i in 0 until n) { for (j in 0 until i) { if (nums[j] < nums[i]) { if (length[j] + 1 > length[i]) { length[i] = length[j] + 1 count[i] = 0 } if (length[j] + 1 == length[i]) { count[i] += count[j] } } } } val maxLength = length.maxOrNull() ?: 0 var result = 0 for (i in 0 until n) { if (length[i] == maxLength) { result += count[i] } } return result } } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!