Skip to content

Commit 013f6ce

Browse files
committed
Stack Sliding Window
1 parent 543ba25 commit 013f6ce

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

239.MaxSlidingWindow.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 239. Sliding Window Maximum
2+
3+
// You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
4+
5+
// Return the max sliding window.
6+
7+
8+
//c# incomplete solution.
9+
//need deque implementation
10+
public class Solution {
11+
public int[] MaxSlidingWindow(int[] nums, int k) {
12+
int[] res = new int[nums.Length-k+1];
13+
Deque<int> max = new Deque<int>();
14+
for (int i = 0; i < k; i++)
15+
{
16+
if (max.Count > 0 && max.PeekFirst() < nums[i])
17+
{
18+
max.Clear();
19+
}
20+
max.AddFirst(nums[i]);
21+
}
22+
res[0] = max.PeekFirst();
23+
for (int i = k; i < nums.Length; i++)
24+
{
25+
if (max.Count > 0 && max.PeekFirst() == nums[i-k])
26+
max.PopFirst();
27+
if (max.Count > 0 && max.Peek() < nums[i])
28+
{
29+
max.Clear();
30+
}
31+
while(max.Count > 0 && max.PeekLast > nums[i])
32+
max.PopLast();
33+
max.Enqueue(nums[i]);
34+
res[i-k+1] = max.PeekFirst();
35+
}
36+
return res;
37+
}
38+
}
39+
40+
41+
////c++ dequeue soln
42+
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
43+
44+
vector <int> ans;
45+
46+
deque<int> dq;
47+
48+
int i=0,j=0;
49+
50+
while(j<nums.size())
51+
{
52+
while(!dq.empty() && dq.back()<nums[j])
53+
{
54+
dq.pop_back(); //coz we dont need elemnt lesser than it in front of queue
55+
// to better understanding dry run on nums={1,3,1,2,0,5} & k=3
56+
}
57+
58+
dq.push_back(nums[j]);
59+
60+
//if window size not achieved
61+
if(j-i+1<k)
62+
{
63+
j++;
64+
}
65+
else
66+
{
67+
68+
ans.push_back(dq.front());
69+
70+
//calculation for i
71+
if(nums[i]==dq.front())
72+
{
73+
dq.pop_front();
74+
}
75+
76+
i++;
77+
j++;
78+
}
79+
}
80+
81+
return ans;

42.Trap.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 42. Trapping Rain Water
2+
3+
// Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
4+
5+
// Example :
6+
// Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
7+
// Output: 6
8+
// Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
9+
10+
public class Solution {
11+
public int Trap(int[] height) {
12+
int[] maxleft = new int[height.Length];
13+
int[] maxright = new int[height.Length];
14+
int res = 0;
15+
maxleft[0] = height[0];
16+
maxright[height.Length-1] = height[height.Length-1];
17+
for(int i=1;i<height.Length;i++){
18+
maxleft[i] = Math.Max(maxleft[i-1],height[i]);
19+
}
20+
for(int i=height.Length-2;i>=0;i--){
21+
maxright[i] = Math.Max(maxright[i+1],height[i]);
22+
}
23+
for(int i=0;i<height.Length;i++){
24+
res += Math.Min(maxleft[i],maxright[i]) - height[i];
25+
}
26+
return res;
27+
}
28+
}

496.NextGreaterElement.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 496. Next Greater Element I
2+
3+
// The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
4+
5+
// You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
6+
7+
// For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
8+
9+
// Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
10+
11+
12+
public class Solution {
13+
public int[] NextGreaterElement(int[] nums1, int[] nums2)
14+
{
15+
Dictionary<int, int> Map = new Dictionary<int, int>();
16+
17+
Stack<int> st = new Stack<int>();
18+
for (int i = 0; i < nums2.Length; i++)
19+
{
20+
while (st.Count > 0 && st.Peek() < nums2[i])
21+
{
22+
Map.Add(st.Pop(), nums2[i]);
23+
}
24+
st.Push(nums2[i]);
25+
}
26+
int[] res = new int[nums1.Length];
27+
for (int i = 0; i < nums1.Length; i++)
28+
{
29+
res[i] = Map.GetValueOrDefault(nums1[i], -1);
30+
}
31+
return res;
32+
}
33+
}

0 commit comments

Comments
 (0)