Queries with Fixed Length

Sort by

recency

|

33 Discussions

|

  • + 0 comments

    Java 8

        public static List<Integer> solve(List<Integer> arr, List<Integer> queries) {
            List<Integer> results = new ArrayList<>(queries.size());
            
            for ( Integer query: queries ) {
                if (query == 1){
                    // Special case. Just need to find the min value.
                    results.add(arr.stream()
                        .mapToInt(Integer::intValue).min().getAsInt());
                } else {
                    int maxIdx = -1;
                    int minOfMaxs = Integer.MAX_VALUE ;
                    
                    for (int i=0; i<=arr.size()-query; ++i) {
                        if ( maxIdx<i) 
                            // The max value dropped out of the query windows
                            maxIdx = windowMaxIdx(arr, i, query);
                        else if ( arr.get(i+query-1)>arr.get(maxIdx) )
                            // New max value entered the query window
                            maxIdx = i+query-1;
                        
                        minOfMaxs = Math.min(arr.get(maxIdx), minOfMaxs);
                    }
                    
                    results.add(minOfMaxs);
                }
            }
            return results ;
        }
        
        /**
         * Calulates the index of the max value within the window
         */
        private static int windowMaxIdx(List<Integer> arr, int idx, int window) {
            int maxIdx = idx;
            for ( int i=idx+1; i<idx+window;++i ) {
                if (arr.get(i) > arr.get(maxIdx))
                    maxIdx=i;
            }
            return maxIdx;
        }
        
    
  • + 0 comments

    Here is Hackerrank Queries with fixed length problem solution in Python Java c++ c and javascript

  • + 0 comments
    public static List<Integer> solve(List<Integer> arr, List<Integer> queries) {
            // Write your code here
            int i, j, n = arr.size();
            List <Integer> result = new ArrayList<>();
            for (Integer query : queries) {
                List <Integer> max = new ArrayList<>();
                for (i = 0; i <= n - query; i++) {
                    max.add(Collections.max(arr.subList(i, i+query)));
                }
                result.add(Collections.min(max));
            }
            return result;
        }
    
  • + 0 comments
    from collections import deque
    
    def min_of_max_in_subarrays_with_size(d, arr):
        # use sliding window to iterate through array
        dq = deque()
        candidtaes = []
        for i in range(len(arr)):
            # remove out-of-window index
            if dq and dq[0] <= i-d:
                dq.popleft()
            # before adding the current index, make sure to remove all indexes
            # indicating to value not larger than that of the current i
            # this way can make sure the maximum is in the front of queue
            while dq and arr[dq[-1]] <= arr[i]:
                dq.pop()
            dq.append(i)
            # add to max subarrays
            if i >= d-1:
                candidtaes.append(arr[dq[0]])
        return min(candidtaes)
    
    def solve(arr, queries):
        result = []  # the min of max subarrays with size d
        for d in queries:
            result.append(min_of_max_in_subarrays_with_size(d, arr))
        return result
    
  • + 0 comments

    C++20

    vector<int> solve(vector<int> arr, vector<int> queries) {
        vector<int> results;
        int N = arr.size();
        for(auto q : queries){
            auto it = max_element(arr.begin()+ 0, arr.begin() + 0 + q );
            int max_e = *it;
            int min_qu = *it;
            cout << max_e << ", ";
            for(int i=1; i<N-q+1; i++){
                if(arr[i+q-1] > max_e) max_e = arr[i+q-1];
                if(arr[i-1] == max_e){
                    auto it = max_element(arr.begin()+ i, arr.begin() + i + q );
                    max_e = *it;
                }
                            
                cout << max_e << ", ";
                min_qu = min(min_qu, max_e);
            }
            
            results.push_back(min_qu);
        }
        return results;
    }