Drill

ProblemsC# › data

The peak in each window

harddataSliding windowArraysC#

A monitoring chart smooths readings by asking, for each fixed-size window, what the loudest moment inside it was.

WindowMax(values: list<int>, window: int) → list<int>

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public List<int> WindowMax(List<int> values, int window) {
    
}

Worked examples

CallResult
WindowMax(new List<int> { 1, 3, 2, 5 }, 2)new List<int> { 3, 3, 5 }
WindowMax(new List<int> { 1, 2, 3 }, 3)new List<int> { 3 }
WindowMax(new List<int> { 5 }, 1)new List<int> { 5 }
WindowMax(new List<int> { 4, 1, 3 }, 2)new List<int> { 4, 3 }

Hint

For each starting index take the maximum of the next `window` values with an inner loop.

Reference solution in C#
public List<int> WindowMax(List<int> values, int window) {
    var result = new List<int>();
    if (window <= 0 || window > values.Count) return result;
    for (int i = 0; i + window <= values.Count; i++) {
        int m = values[i];
        for (int j = i + 1; j < i + window; j++) if (values[j] > m) m = values[j];
        result.Add(m);
    }
    return result;
}

The same problem in another language

More data problems in C#