Sunday, April 16, 2023

solving hackerrank Queries with Fixed Length

 first solution is

public static List<Integer> solve(List<Integer> arr, List<Integer> queries) {
List result = new ArrayList();
queries.forEach(k -> {
List<List<Integer>> partitionedList = new ArrayList<>();
for (int s = 0; s <= arr.size() - k; s++) {
partitionedList.add(arr.subList(s, s+k));
}
List<Integer> maxes = partitionedList.stream().map(m -> Collections.max(m)).collect(toList());
result.add(Collections.min(maxes));
});
return result;
}
 
which is working fine but some is getting timeouts
 
now optimizations. 

No comments:

odd string diff

 https://leetcode.com/problems/odd-string-difference/ Beats 19.92% of users with Java   class Solution { public String oddString ( S...