Find Longest Series solution
This commit is contained in:
56
src/main/java/com/algos/stepik/FindLongestSeries.java
Normal file
56
src/main/java/com/algos/stepik/FindLongestSeries.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package com.algos.stepik;
|
||||||
|
|
||||||
|
//У вас есть переменная data которая содержит входные пользовательские данные.
|
||||||
|
//
|
||||||
|
// data - список из элементов целых числел.
|
||||||
|
//
|
||||||
|
// Напишите код, который находит наибольшую серию подряд идущих элементов, расположенных по возрастанию.
|
||||||
|
//
|
||||||
|
// Результат записать в переменную result.
|
||||||
|
//
|
||||||
|
// Sample Input 1:
|
||||||
|
//
|
||||||
|
// [1, 2, 3, 4, 4, 3, 4, 6, 8, 10]
|
||||||
|
//
|
||||||
|
// Sample Output 1:
|
||||||
|
//
|
||||||
|
// 5
|
||||||
|
//
|
||||||
|
// Sample Input 2:
|
||||||
|
//
|
||||||
|
// [1, 2, 3, 4, 4, 3, 5, 5, 8, 10]
|
||||||
|
//
|
||||||
|
// Sample Output 2:
|
||||||
|
//
|
||||||
|
// 4
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FindLongestSeries {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
List<Integer> list1 = List.of(1, 2, 3, 4, 4, 3, 4, 6, 8, 10);
|
||||||
|
List<Integer> list2 = List.of(1, 2, 3, 4, 4, 3, 5, 5, 8, 10);
|
||||||
|
|
||||||
|
System.out.println(findLongestSeries(list1));
|
||||||
|
System.out.println(findLongestSeries(list2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int findLongestSeries(List<Integer> data) {
|
||||||
|
|
||||||
|
int max = 0;
|
||||||
|
int counter = 1;
|
||||||
|
|
||||||
|
for(int i = 1; i < data.size(); i++) {
|
||||||
|
if (data.get(i) - data.get(i - 1) > 0) {
|
||||||
|
counter++;
|
||||||
|
} else {
|
||||||
|
max = Math.max(max, counter);
|
||||||
|
counter = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return Math.max(max, counter);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user