Дан массив целых чисел 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 { public int findNumberOfLIS(int[] nums) { int n = nums.length; int[] length = new int[n]; int[] count = new int[n]; Arrays.fill(length, 1); Arrays.fill(count, 1);
for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { 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]; } } } }
int maxLength = Arrays.stream(length).max().getAsInt(); int result = 0;
for (int i = 0; i < n; i++) { if (length[i] == maxLength) { result += count[i]; } }
Дан массив целых чисел 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 { public int findNumberOfLIS(int[] nums) { int n = nums.length; int[] length = new int[n]; int[] count = new int[n]; Arrays.fill(length, 1); Arrays.fill(count, 1);
for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { 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]; } } } }
int maxLength = Arrays.stream(length).max().getAsInt(); int result = 0;
for (int i = 0; i < n; i++) { if (length[i] == maxLength) { result += count[i]; } }
The Channel name and bio must be no more than 255 characters long ‘Ban’ on Telegram Administrators With the administration mulling over limiting access to doxxing groups, a prominent Telegram doxxing group apparently went on a "revenge spree." Telegram offers a powerful toolset that allows businesses to create and manage channels, groups, and bots to broadcast messages, engage in conversations, and offer reliable customer support via bots.
from us