Question Asked in My previous Interview #2
-
Maximum Profit in Job SchedulingProblem Statement: Example:
Output: |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 2 replies
-
Approach 1: Two Passes with (Kadane's Algorithm)Explanation:
Algorithm Steps:
Code: def maximumSum(arr): n = len(arr) if n == 1: return arr[0] # Step 1: Compute max sum ending at each index (left to right) left = [0] * n left[0] = arr[0] for i in range(1, n): left[i] = max(left[i-1] + arr[i], arr[i]) # Step 2: Compute max sum starting at each index (right to left) right = [0] * n right[-1] = arr[-1] for i in range(n-2, -1, -1): right[i] = max(right[i+1] + arr[i], arr[i]) # Step 3: Calculate the maximum sum maxSum = max(left) # Maximum without deletion for i in range(1, n-1): maxSum = max(maxSum, left[i-1] + right[i+1]) # Maximum with one deletion return maxSum Complexity:
Approach 2: Optimized Sliding Window with One PassExplanation:
This reduces the space complexity while maintaining the correctness of the algorithm. Code: def maximumSum(arr): n = len(arr) if n == 1: return arr[0] noDelete = arr[0] # Maximum sum without deletion oneDelete = 0 # Maximum sum with one deletion maxSum = arr[0] # Overall maximum sum for i in range(1, n): oneDelete = max(oneDelete + arr[i], noDelete) # Either extend oneDelete or use noDelete noDelete = max(noDelete + arr[i], arr[i]) # Standard Kadane's maxSum = max(maxSum, noDelete, oneDelete) # Update global max return maxSum Complexity:
Approach 3: Divide and ConquerExplanation:
Algorithm Steps:
Code: def maximumSum(arr): def divideAndConquer(left, right): if left == right: return arr[left] mid = (left + right) // 2 # Divide: Solve for left and right halves leftMax = divideAndConquer(left, mid) rightMax = divideAndConquer(mid + 1, right) # Conquer: Solve for crossing subarray leftCrossMax, rightCrossMax = float('-inf'), float('-inf') temp = 0 # Left crossing sum for i in range(mid, left - 1, -1): temp += arr[i] leftCrossMax = max(leftCrossMax, temp) temp = 0 # Right crossing sum for i in range(mid + 1, right + 1): temp += arr[i] rightCrossMax = max(rightCrossMax, temp) crossingSum = leftCrossMax + rightCrossMax # Conquer with one deletion oneDeleteMax = max(leftCrossMax, rightCrossMax) return max(leftMax, rightMax, crossingSum, oneDeleteMax) return divideAndConquer(0, len(arr) - 1) Complexity:
Approach 4: Prefix and Suffix SumExplanation:
Code: def maximumSum(arr): n = len(arr) prefixMax = [0] * n suffixMax = [0] * n # Compute prefixMax prefixMax[0] = arr[0] for i in range(1, n): prefixMax[i] = max(prefixMax[i-1] + arr[i], arr[i]) # Compute suffixMax suffixMax[-1] = arr[-1] for i in range(n-2, -1, -1): suffixMax[i] = max(suffixMax[i+1] + arr[i], arr[i]) # Calculate maximum sum maxSum = max(prefixMax) # Maximum without deletion for i in range(1, n-1): maxSum = max(maxSum, prefixMax[i-1] + suffixMax[i+1]) # Maximum with one deletion return maxSum Complexity:
Conclusion:
|
Beta Was this translation helpful? Give feedback.
Approach 1: Two Passes with (Kadane's Algorithm)
Explanation:
This approach extends Kadane's Algorithm to handle the case of one deletion. The idea is to:
forward
pass).backward
pass).Algorithm Steps:
left[i]
: Maximum subarray sum ending at index ii (standard Kadane's from left to right).right[i]
: Maximum subarray sum starting at index ii (Kadane's from right to left).