-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack1.cpp
More file actions
236 lines (207 loc) · 5.62 KB
/
stack1.cpp
File metadata and controls
236 lines (207 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// q1 - NGR | Nearest Greater to right | Next Largest Element
#include <stack>
vector<int> ngr(vector<int> arr)
{
stack<int> st;
vector<int> result;
for (int i = 0; i < arr.size(); i++)
{
while (!st.empty() && st.top() <= arr[i])
{
st.pop();
}
result.push_back(st.empty() ? -1 : st.top());
st.push(arr[i]);
}
return result;
}
// Intro to stack and identification
/*
all question can be solved with brute force O(n^2)
DSA rounds need you to identify which D.S or algorithm to use
to solve the question in O(best)
So identification is the most important thing
Identification
usually on array or heap (heap usually related to sorting)
if brute force solution is of O(n^2) and the inner loop has following patterns:
-> j= 0 to i j++
-> j= i-1 to 0 j--
-> j=i-1 to n j++
-> j=n to i j--
then it's likely it can be solved with stack
after identifying solve the variation
*/
#include <iostream>
#include <vector>
using namespace std;
int fibonacci(int n)
{
if (n == 0)
{
return n;
}
if (n == 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
// memoization - dp
int fibonacci_dp(int n, vector<int> &dp)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
if (dp[n] != -1)
return dp[n];
dp[n] = fibonacci_dp(n - 1, dp) + fibonacci_dp(n - 2, dp);
}
// bottom-up iterative
int fibonacci_it(int n)
{
vector<int> dp;
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// Bitwise right shift for division and bitwise left shift for division
/*
Shifting a number n right by k positions is equivalent to dividing n by 2 to the power k
(integer division).
Formula:
n>>k=[ n/2(to the power )K]
*/
/*
Similarly shifting is like multiplying n to 2 to the power k
*/
// You are given an array of integers nums and an integer k.
// Your task is to find the maximum sum of a subarray (contiguous elements) such that the length of the subarray is at most k.
// deep seek answer
/*
it's like a o(n2) sliding window that is efficient
cause of adding prefix window on top of it the inner loops works much better
*/
int maxSubarraySumAtMostK(vector<int> &nums, int k)
{
int n = nums.size();
int maxSum = INT_MIN;
// Compute prefix sums
vector<int> prefixSum(n + 1, 0);
for (int i = 0; i < n; i++)
{
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
// Use a deque to maintain the minimum prefix sum in the current window
deque<int> dq;
dq.push_back(0); // Start with the first prefix sum
for (int i = 1; i <= n; i++)
{
// Remove prefix sums outside the current window
while (!dq.empty() && dq.front() < i - k)
{
dq.pop_front();
}
// Update the maximum sum
if (!dq.empty())
{
int currentSum = prefixSum[i] - prefixSum[dq.front()];
if (currentSum > maxSum)
{
maxSum = currentSum;
}
}
// Maintain the deque in increasing order
while (!dq.empty() && prefixSum[i] <= prefixSum[dq.back()])
{
dq.pop_back();
}
dq.push_back(i);
}
return maxSum;
}
// Minimum Size Subarray Sum
int minSizeSubarraySum(vector<int> arr, int sum)
{
int minSize = INT_MAX;
int n = arr.size();
int windowSum = 0;
vector<int> prefixSum(n + 1, 0);
for (int i = 0; i < n; i++)
{
prefixSum[i + 1] = prefixSum[i] + arr[i];
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
windowSum = prefixSum[j + 1] - prefixSum[i];
if (windowSum == sum)
{
minSize = minSize < (i - j) ? minSize : (j - i + 1);
}
}
}
return minSize;
}
// wasn't prefix sliding window it was variable size sliding window question
int minSizeSubarraySum2(vector<int> arr, int sum)
{
int minSize = INT_MAX;
int n = arr.size();
int currentSum = 0;
int windowIndex = 0;
for (int i = 0; i < n; i++)
{
while (currentSum > sum)
{
currentSum -= arr[windowIndex];
windowIndex++;
}
if (currentSum < sum)
{
currentSum += arr[i];
}
if (currentSum == sum)
{
minSize = minSize < (i - windowIndex + 1) ? minSize : (i - windowIndex + 1);
currentSum += arr[i];
}
}
if (minSize == 0)
return -1;
return minSize;
}
// Minimum Moves to Equal Array Elements
/*
Given an array of integers nums, you can perform the following operation any number of times:
Choose any element and increment it by 1 (i.e., nums[i] += 1).
Find the minimum number of moves required to make all elements in the array equal.
-- heap of size arr.size()/2
-- to get the value that is mean
-- or is the element selected randomly
-- then loop?
-- wrong as no decrement is allowed
*/
#include <algorithm>
int movesAllowed(vector<int> arr)
{
int maxElement = *max_element(arr.begin(), arr.end());
int moves = 0;
for (int num : arr)
{
moves += maxElement - num;
}
return moves;
}
// key takeaway - no need to perform actual calculation or avoid actual calculation wherever possible
/*
Minimize Maximum Difference After Operations
Problem Statement:
Given an array nums of size n and an integer k, you are allowed
to increment or decrement each element by at most k.
Find the minimum possible value of the difference between
the maximum and minimum elements of the array after applying the operation to any elements.
*/