Здесь i, j представляет индексную позицию входного списка, такую что i < j < n
Элементы от 0 до i должны быть в порядке возрастания
Элементы от i до j должны быть в порядке убывания
Элементы от j до последнего индекса должны быть в порядке возрастания
Пример:
Код: Выделить всё
input [2, 1, 3, 3, 1, 2, 1, 2, 3]
valid output sequence with max selected items is [1,2,3,2,1,2,3]
size of this output sequence is 7, so return the value 7
Код: Выделить всё
increasing from position 0 to 2 => [1,2,3]
decreasing from position 2 to 4 => [3,2,1]
again increasing from position 4 to last index => [1,2,3]
Код: Выделить всё
input [5, 5, 2, 1, 3, 4, 5]
valid output sequence with max selected items is [1, 3, 5, 4, 2, 5]
size of this output sequence is 7, so return the value 6
Код: Выделить всё
increasing from position 0 to 2 => [1,3,5]
decreasing from position 2 to 4 => [5,4,2]
again increasing from position 4 to last index => [2,5]
Код: Выделить всё
public static int solve(List list) {
int n = list.size();
TreeMap map = new TreeMap();
int min = Integer.MAX_VALUE;
for(int e : list) {
min = Math.min(min, e);
map.put(e, map.getOrDefault(e, 0)+1);
}
int result = 1;
map.put(min, map.getOrDefault(min,0)-1);
if(map.get(min)
Подробнее здесь: [url]https://stackoverflow.com/questions/79163186/find-maximum-length-of-up-then-down-and-up-sequence-of-numbers[/url]