Leetcode Problem 1191. K-Concatenation Maximum Sum
1191. K-Concatenation Maximum Sum
Leetcode Solutions
Kadane's Algorithm with Prefix and Suffix Sum Optimization
Calculate the maximum subarray sum for a single instance of the array using Kadane's algorithm.
Calculate the prefix sum maximum and suffix sum maximum.
Calculate the total sum of the array.
If k is 1, the result is simply the maximum subarray sum from step 1.
If the total sum is negative, the result is the maximum of the subarray sum from step 1 and the subarray sum spanning two instances of the array.
If the total sum is positive, the result is the maximum of the subarray sum from step 1, and the sum of the entire array multiplied by (k-2) plus the prefix and suffix sums.