@@ -214,6 +214,7 @@ class SlidingWindow {
214
214
return maxLength
215
215
};
216
216
217
+ // NOTE: It is advised to solve hard question like this in python or even C++
217
218
minWindow(s, t) { // minimum window substring
218
219
if (s.length < t.length) return "";
219
220
@@ -280,64 +281,70 @@ class Stack {
280
281
}
281
282
282
283
class BinarySearch{
283
- findMin(nums) { // find minimum in rotated sorted array
284
- let left = 0;
285
- let right = nums.length - 1;
286
-
287
- if (nums[left] < nums[right] || nums.length === 1) { // If the array is not rotated or has one element, return the first element
288
- return nums[left];
289
- }
290
-
291
- while (left <= right) {
292
- let mid = Math.floor((left + right) / 2);
293
-
294
- if (nums[mid] > nums[mid + 1]) { // Check if mid element is the smallest
295
- return nums[mid + 1];
296
- }
297
-
298
- if (nums[mid - 1] > nums[mid]) { // Check if the element before mid is the smallest
299
- return nums[mid];
300
- }
301
-
302
- if (nums[mid] > nums[left]) { // Adjust search range
303
- left = mid + 1;
304
- } else {
305
- right = mid - 1;
284
+ findMin = function(nums) { // Find minimum in a rotated sorted array
285
+ let left = 0, right = nums.length-1;
286
+ while (left < right){
287
8000
+ const mid = Math.floor((left+right)/2)
288
+ if (nums[right] < nums[mid]){ // If mid is greater than minimum is on the right
289
+ left = mid +1;
290
+ }else{
291
+ right = mid;
306
292
}
307
293
}
308
- }
294
+ return nums[left]
295
+ };
309
296
310
- search(nums, target) { // search in rotated sorted array
311
- let left = 0;
312
- let right = nums.length - 1;
313
-
314
- while (left <= right) {
315
- let mid = Math.floor((left + right) / 2);
316
-
317
- if (nums[mid] === target) { // Check if the target is found at the mid index
318
- return mid;
319
- }
320
-
321
- if (nums[left] <= nums[mid]) { // If the left part is sorted
322
- if (target >= nums[left] && target < nums[mid]) { // Check if target is in the left part
323
- right = mid - 1; // Target is in the left part
324
- } else {
325
- left = mid + 1; // Target is in the right part
297
+ search = function(nums, target) { // search target in a rotated sorted array
298
+ let left = 0, right = nums.length -1;
299
+ while (left <= right ){
300
+ const mid = Math.floor((left + right )/2);
301
+ if ( nums[mid] === target) return mid;
302
+ if ( nums[left] <= nums[mid]) {
303
+ if (nums[left] <= target && target < nums[mid]){
304
+ right = mid;
305
+ }else {
306
+ left = mid +1;
326
307
}
327
- }
328
- else { // If the right part is sorted
329
- if (target > nums[mid] && target <= nums[right]) { // Check if target is in the right part
330
- left = mid + 1; // Target is in the right part
308
+ }else{
309
+ if (nums[mid] < target && target <= nums[right]){
310
+ left = mid+1;
331
311
} else {
332
- right = mid - 1; // Target is in the left part
312
+ right = mid;
333
313
}
334
314
}
335
315
}
336
-
337
- return -1; // Target not found
338
- }
316
+ return -1;
317
+ };
339
318
}
340
319
341
320
class LinkedList{
321
+ reverseList = function(head) {
322
+ let prev = null
323
+ let curr = head
324
+ while (curr !== null){
325
+ let next = curr.next;
326
+ curr.next = prev;
327
+ prev = curr;
328
+ curr = next;
329
+ }
330
+ return prev;
331
+ };
342
332
333
+ mergeTwoLists = function(list1, list2) {
334
+ let dummy = new ListNode(-1);
335
+ let curr = dummy;
336
+ while (list1 !== null && list2 !== null){
337
+ if ( list1.val <= list2.val){
338
+ curr.next = list1
339
+ list1 = list1.next
340
+ }else{
341
+ curr.next = list2
342
+ list2 = list2.next
343
+ }
344
+ curr = curr.next
345
+ }
346
+ curr.next = list1 !== null ? list1 :list2
347
+ return dummy.next;
348
+ };
349
+
343
350
}
0 commit comments