|
| 1 | +export function* maxSumSubarray(array, k) { |
| 2 | + if (array.length === 0) { |
| 3 | + yield { |
| 4 | + type: "error", |
| 5 | + message: "Array cannot be empty", |
| 6 | + windowStart: -1, windowEnd: -1, currentSum: 0, maxSum: 0 |
| 7 | + }; |
| 8 | + return; |
| 9 | + } |
| 10 | + if (k > array.length) { |
| 11 | + yield { |
| 12 | + type: "error", |
| 13 | + message: `Window size ${k} cannot be greater than array length ${array.length}`, |
| 14 | + windowStart: -1, windowEnd: -1, currentSum: 0, maxSum: 0 |
| 15 | + }; |
| 16 | + return; |
| 17 | + } |
| 18 | + if (k <= 0) { |
| 19 | + yield { |
| 20 | + type: "error", |
| 21 | + message: "Window size must be greater than 0", |
| 22 | + windowStart: -1, windowEnd: -1, currentSum: 0, maxSum: 0 |
| 23 | + }; |
| 24 | + return; |
| 25 | + } |
| 26 | + let windowStart = 0; |
| 27 | + let windowEnd = 0; |
| 28 | + let currentSum = 0; |
| 29 | + let maxSum = Number.NEGATIVE_INFINITY; |
| 30 | + yield { |
| 31 | + type: "initialize", |
| 32 | + message: `Initializing: Calculating sum of first window [0, ${k - 1}]`, |
| 33 | + windowStart: 0,windowEnd: k - 1,currentSum: 0,maxSum: 0 |
| 34 | + }; |
| 35 | + for (let i = 0; i < k; i++) { |
| 36 | + currentSum += array[i]; |
| 37 | + yield { |
| 38 | + type: "expand", |
| 39 | + message: `Adding element at index ${i}: ${array[i]}. Current sum: ${currentSum}`, |
| 40 | + windowStart: 0,windowEnd: i,currentSum: currentSum,maxSum: maxSum |
| 41 | + }; |
| 42 | + } |
| 43 | + maxSum = currentSum; |
| 44 | + windowEnd = k - 1; |
| 45 | + |
| 46 | + yield { |
| 47 | + type: "window_ready", |
| 48 | + message: `First window sum: ${currentSum}. This is our initial maximum.`, |
| 49 | + windowStart: 0, windowEnd: k - 1, currentSum: currentSum, maxSum: maxSum |
| 50 | + }; |
| 51 | + for (let i = k; i < array.length; i++) { |
| 52 | + const removedElement = array[i - k]; |
| 53 | + const addedElement = array[i]; |
| 54 | + yield { |
| 55 | + type: "slide_start", |
| 56 | + message: `Sliding window: Removing element at index ${i - k} (${removedElement}), adding element at index ${i} (${addedElement})`, |
| 57 | + windowStart: i - k, windowEnd: i - 1, currentSum: currentSum, maxSum: maxSum, removedIndex: i - k, addedIndex: i |
| 58 | + }; |
| 59 | + currentSum = currentSum - removedElement + addedElement; |
| 60 | + windowStart = i - k + 1; |
| 61 | + windowEnd = i; |
| 62 | + yield { |
| 63 | + type: "slide_update", |
| 64 | + message: `Window moved: New sum = ${currentSum} (previous: ${currentSum + removedElement - addedElement}, removed: ${removedElement}, added: ${addedElement})`, |
| 65 | + windowStart: windowStart, windowEnd: windowEnd, currentSum: currentSum, maxSum: maxSum, removedIndex: i - k, addedIndex: i |
| 66 | + }; |
| 67 | + if (currentSum > maxSum) { |
| 68 | + maxSum = currentSum; |
| 69 | + yield { |
| 70 | + type: "new_max", |
| 71 | + message: `New maximum found! Sum: ${maxSum} at window [${windowStart}, ${windowEnd}]`, |
| 72 | + windowStart: windowStart, windowEnd: windowEnd, currentSum: currentSum, maxSum: maxSum |
| 73 | + }; |
| 74 | + } else { |
| 75 | + yield { |
| 76 | + type: "window_ready", |
| 77 | + message: `Current sum: ${currentSum} (not greater than max: ${maxSum})`, |
| 78 | + windowStart: windowStart, windowEnd: windowEnd, currentSum: currentSum, maxSum: maxSum |
| 79 | + }; |
| 80 | + } |
| 81 | + } |
| 82 | + yield { |
| 83 | + type: "done", |
| 84 | + message: `Algorithm completed! Maximum sum of subarray of size ${k} is ${maxSum}`, |
| 85 | + windowStart: windowStart, windowEnd: windowEnd, currentSum: currentSum, maxSum: maxSum |
| 86 | + }; |
| 87 | +} |
| 88 | + |
0 commit comments