You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -148,36 +153,49 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
148
153
if(arr.Length==0)
149
154
return"{}";
150
155
151
-
if(arr.Length==1)
152
-
return$"{{ {{{arr[0]}, {DEFAULT_NGE}}} }}";
153
-
// For an interpolated string, use "{{" for a single '{' and "}}" for a single '}'
156
+
varbuilder=newStringBuilder(" }");
157
+
// Using the helper here instead of writing out the pattern multiple times
158
+
// to be consistent
159
+
PrependPair(arr[^1],DEFAULT_NGE,builder);
160
+
// The last element, which may also be the first, and if it is:
154
161
155
-
// Remember that the Array.Clone method returns an object[], must cast it to appropriate type
156
-
vararrNGE=(int[])arr.Clone();
162
+
// Main logic requires array length of at least two
163
+
if(arr.Length==1)
164
+
{
165
+
builder[0]='{';
166
+
returnbuilder.ToString();
167
+
}
168
+
//// From back when we were using a paralell array to store NGEs
169
+
//// Remember that the Array.Clone method returns an object[], must cast it to appropriate type
170
+
//var arrNGE = (int[])arr.Clone();
171
+
//// By definition
172
+
//arrNGE[^1] = DEFAULT_NGE;
157
173
158
-
varlocalMaxes=newStack<int>();
159
-
// remains empty until the right element of a pair is actually larger than the left element of a pair
160
174
161
-
// By definition
162
-
arrNGE[^1]=DEFAULT_NGE;
175
+
varlocalMaxes=newStack<int>();
176
+
// remains empty until the right element of a pair is actually larger than the left element of a pair
163
177
164
-
// Will refer to result of last comparison to determine if arr[i + 1] is determined to be a new local maximum
165
-
varlastResult=CompResult.NoNotLessThan;
178
+
// Will refer to result of last comparison to determine if arr[i + 1] is determined to be a new local maximum
179
+
varlastResult=CompResult.NoNotLessThan;
166
180
// NoNotLessThan is correct for the "comparison" between arr[^1] and the non-element to its right, since arr[^1] will be a local max if arr[i] < arr[i + 1]
167
181
// Default is reserved to essentially mean uninitialized
168
182
169
183
varthisResult=CompResult.Default;
170
184
171
-
// walk array from right to left
172
-
// i starts at next-to-last index
173
-
// compare the elements in pairs
174
-
for(vari=arr.Length-2;i>=0;--i)
185
+
186
+
// walk array from right to left
187
+
// i starts at next-to-last index
188
+
// compare the elements in pairs
189
+
for(vari=arr.Length-2;i>=0;--i)
175
190
{
176
191
if(arr[i]<arr[i+1])
177
192
// Immediately adjacent element is NGE
178
193
{
179
194
thisResult=CompResult.YesLessThan;
180
-
arrNGE[i]=arr[i+1];
195
+
PrependPair(arr[i],arr[i+1],builder);
196
+
197
+
// From back when we were using a paralell array to store NGEs
198
+
//arrNGE[i] = arr[i + 1];
181
199
182
200
if(lastResult==CompResult.NoNotLessThan)
183
201
{
@@ -195,7 +213,11 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
195
213
{
196
214
if(arr[i]<localMaxes.Peek())
197
215
{
198
-
arrNGE[i]=localMaxes.Peek();
216
+
PrependPair(arr[i],localMaxes.Peek(),builder);
217
+
218
+
// From back when we were using a paralell array to store NGEs
219
+
//arrNGE[i] = localMaxes.Peek();
220
+
199
221
// Leave the NGE that we just used where we found it in localMaxes, it is still a candidate to be an NGE again in the future
200
222
foundNGE=true;
201
223
}
@@ -209,46 +231,52 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
209
231
if(localMaxes.Count==0)
210
232
// No candidate NGEs exist, or arr[i] was greater than each candidate NGE
211
233
{
212
-
arrNGE[i]=DEFAULT_NGE;
213
-
}
234
+
PrependPair(arr[i],DEFAULT_NGE,builder);
214
235
215
-
// Not pushing onto localMaxes here, since we won't know until next iteration whether the current arr[i] is a local max
216
-
}
236
+
// From back when we were using a paralell array to store NGEs
237
+
//arrNGE[i] = DEFAULT_NGE;
238
+
}
239
+
240
+
// Not pushing onto localMaxes here, since we won't know until next iteration whether the current arr[i] is a local max
241
+
}
217
242
218
243
// Advance state of tracking for next loop iteration
219
244
lastResult=thisResult;
220
245
thisResult=CompResult.Default;
221
246
}
222
247
223
-
// Now construct the string left to right (or print it out as you go...but not since we're doing fixup at the end of the string building...and we're headed back to build the string as we go in the next version anyway)
248
+
// Now construct the string left to right (or print it out as you go...but not since we're doing fixup at the end of the string building...and we're headed back to build the string as we go in the next version anyway)
// // BUG: array index var for this loop is k, not i
263
+
// // But this loop is going away in the next version anyway
264
+
//}
265
+
266
+
// // Drop the last comma
267
+
// // param1: remove starting from what index
268
+
// // param2: how many to remove
269
+
// // that is: builder.Remove(where, howmany);
270
+
// builder.Remove(builder.Length - 2, 1);
240
271
241
-
// Drop the last comma
242
-
// param1: remove starting from what index
243
-
// param2: how many to remove
244
-
// that is: builder.Remove(where, howmany);
245
-
builder.Remove(builder.Length-2,1);
272
+
//// Add the final curly brace
273
+
//builder.Append('}');
246
274
247
-
// Add the final curly brace
248
-
builder.Append('}');
249
275
276
+
// Overwrite the final leading comma inserted by PrependPair
277
+
builder[0]='{';
250
278
251
-
returnbuilder.ToString();
279
+
returnbuilder.ToString();
252
280
253
281
254
282
// Go ahead and finish writing this, test that this solution works, then tomorrow adapt it to build the string right to left during initial traversal of the array (instead of cloning the array)
@@ -261,19 +289,26 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
0 commit comments