Queries with Fixed Length

Sort by

recency

|

16 Discussions

|

  • + 0 comments

    linear time with sliding window

    int solve(const vector<int>& arr, int d) {
        deque<int> dQ;
        vector<int> slidingWindow;
        for (int i = 0; i < arr.size(); ++i) {
            if (!dQ.empty() and dQ.front() == i - d) dQ.pop_front();
            while (!dQ.empty() and arr[dQ.back()] <= arr[i]) dQ.pop_back();
            dQ.push_back(i);
            if (i >= d - 1) slidingWindow.push_back(arr[dQ.front()]);
        }
        return *min_element(slidingWindow.begin(), slidingWindow.end());
    }
    
  • + 0 comments

    Python DP ?, O(N*Q)

    def solve(arr, queries):
        def solve2(batches, d):
            a = [x for ba in batches for x in accumulate(reversed(ba), max)]
            b = [x for ba in batches for x in accumulate(ba, max)]
            return min(max(a[i-2*((i+1)%d)], b[i]) for i in range(d-1, n))
            
        return [solve2(list(batched(arr, d)), d) for d in queries]
    
  • + 0 comments

    Python, using heapq (a little troublesome since it is implemented using min heap instead of max heap) and dictionary for counting

    def solve(arr, queries):
        arr = [-1 * elem for elem in arr]
        answers = []
        for q in queries:
            subarray = arr[:q]
            heapify(subarray)
            valuecounter = defaultdict(lambda: 0)
            for elem in subarray:
                valuecounter[elem] += 1
            curmax = subarray[0]
            minofmax = curmax
            for index in range(q, len(arr)):
                newvalue = arr[index]
                oldvalue = arr[index - q]
                valuecounter[newvalue] += 1
                valuecounter[oldvalue] -= 1
                heappush(subarray, newvalue)
                while True:
                    curmax = subarray[0]
                    if valuecounter[curmax] != 0:
                        break
                    else:
                        heappop(subarray)
                minofmax = max(minofmax, curmax)
            answers.append(-1 * minofmax)
        return answers
    
  • + 0 comments

    Java, linear time complexity

    public static List<Integer> solve(List<Integer> arr, List<Integer> queries) {
            List<Integer> results = new ArrayList<>();
    
            for (int d : queries) {
                int minMax = Integer.MAX_VALUE;
    
                Deque<Integer> window = new LinkedList<>();
    
                for (int i = 0; i < d; i++) {
                    while (!window.isEmpty() && arr.get(i) >= arr.get(window.peekLast())) {
                        window.removeLast();
                    }
                    window.addLast(i);
                }
    
                for (int i = d; i <= arr.size(); i++) {
                    minMax = Math.min(minMax, arr.get(window.peekFirst()));
    
                    while (!window.isEmpty() && window.peekFirst() <= i - d) {
                        window.removeFirst();
                    }
    
                    if (i < arr.size()) {
                        while (!window.isEmpty() && arr.get(i) >= arr.get(window.peekLast())) {
                            window.removeLast();
                        }
                        window.addLast(i);
                    }
                }
    
                results.add(minMax);
            }
            return results;
        }
    
  • + 0 comments

    Monotonic two-sided queue in Python 3 (accepted):

    def solve(arr, queries):
        # Write your code here
        def get_val(arr, d):
            max_record = deque()
            ans = float('inf')
            for i in range(n):
                while max_record and arr[max_record[-1]] <= arr[i]:
                    max_record.pop()
                max_record.append(i)
                if max_record[0] <= i-d:
                    max_record.popleft()
                if i >= d-1:
                    ans = min(ans, arr[max_record[0]])  
            return ans 
        return [get_val(arr, q) for q in queries]