Skip to content

Commit 2c7e35a

Browse files
committed
#239: description and solution update
1 parent 100ad7a commit 2c7e35a

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

c-sharp/Problems/sliding-window/SlidingWindowMaximum.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,46 @@
33
* Sliding Window Maximum
44
**
55
* You are given an array of integers nums,
6-
* there is a sliding window of size k which is moving from the very left of the array to the very right.
6+
* there is a sliding window of size k
7+
* which is moving from the very left of the array to the very right.
78
* You can only see the k numbers in the window.
89
* Each time the sliding window moves right by one position.
9-
*
10+
*
1011
* Return the max sliding window.
12+
*
13+
* Example 1:
14+
* Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
15+
* Output: [3,3,5,5,6,7]
16+
* Explanation:
17+
* Window position Max
18+
* --------------- -----
19+
* [1 3 -1] -3 5 3 6 7 3
20+
* 1 [3 -1 -3] 5 3 6 7 3
21+
* 1 3 [-1 -3 5] 3 6 7 5
22+
* 1 3 -1 [-3 5 3] 6 7 5
23+
* 1 3 -1 -3 [5 3 6] 7 6
24+
* 1 3 -1 -3 5 [3 6 7] 7
1125
*
26+
* Example 2:
27+
* Input: nums = [1], k = 1
28+
* Output: [1]
29+
*
30+
* Constraints:
31+
* • 1 <= nums.length <= 10^5
32+
* • -10^4 <= nums[i] <= 10^4
33+
* • 1 <= k <= nums.length
34+
*
35+
* Hint 1:
36+
* How about using a data structure such as deque (double-ended queue)?
37+
*
38+
* Hint 2:
39+
* The queue size need not be the same as the window’s size.
40+
*
41+
* Hint 3:
42+
* Remove redundant elements and the queue should store only elements that need to be considered.
43+
**
1244
* https://leetcode.com/problems/sliding-window-maximum/
13-
*/
45+
***/
1446

1547
using System.Collections.Generic;
1648

@@ -20,8 +52,8 @@ public class SlidingWindowMaximum
2052
{
2153
public int[] MaxSlidingWindow( int[] nums, int k )
2254
{
23-
List<int> result = new();
24-
LinkedList<int> queue = new();
55+
List<int> result = [];
56+
LinkedList<int> queue = [];
2557

2658
int left = 0;
2759
int right = 0;
@@ -51,6 +83,6 @@ public int[] MaxSlidingWindow( int[] nums, int k )
5183
right++;
5284
}
5385

54-
return result.ToArray();
86+
return [.. result];
5587
}
5688
}

0 commit comments

Comments
 (0)