Giải pháp một dòng [Tôi không biết điều gì có nghĩa là "cách tốt hơn" cho bạn]
L = [1,2,3]
[L[i:i+j] for i in range[0,len[L]] for j in range[1,len[L]-i+1]]
L=[1,2,3,4]
[L[i:i+j] for i in range[0,len[L]] for j in range[1,len[L]-i+1]]
bạn lấy,
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều các số có số tiền lớn nhất. & NBSP;
Thuật toán Kadane từ:
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far
Giải thích: & nbsp; Ý tưởng đơn giản của thuật toán Kadane, là tìm kiếm tất cả các phân đoạn tiếp giáp tích cực của mảng [MAX_IVED_HERE được sử dụng cho việc này]. Và theo dõi phân đoạn liên tục tổng tối đa trong số tất cả các phân đoạn dương [MAX_SO_FAR được sử dụng cho việc này]. Mỗi lần chúng tôi nhận được một tổng dương so sánh nó với MAX_SO_FAR và cập nhật MAX_SO_FAR nếu nó lớn hơn MAX_SO_FAR & NBSP;
The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array [max_ending_here is used for this]. And
keep track of maximum sum contiguous segment among all positive segments [max_so_far is used for this]. Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 4
Program:
Python3
from
math
import
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
0[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
1[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
0[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
4 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
5[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far0
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far2
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far8
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far9
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 40
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 41
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 42
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 44
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 49
Maximum contiguous sum is 70
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 73
Maximum contiguous sum is 74
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 77
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 70
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Maximum contiguous sum is 72
Maximum contiguous sum is 74
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 ________ 26 & nbsp; & nbsp;[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Maximum contiguous sum is 78
Maximum contiguous sum is 79
Maximum contiguous sum is 7 Starting index 2 Ending index 60
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 7 Starting index 2 Ending index 62
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 64
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
0Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
3Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
9Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
2Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
5Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
8Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
1Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
4Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
7Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
00[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01Is
Output:
Maximum contiguous sum is 7
Cách tiếp cận khác:
Python3
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
4 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
5[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far0
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far2
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far8
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far9
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 40
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 41
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 42
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 44
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 49
Maximum contiguous sum is 70
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 70
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Maximum contiguous sum is 72
Maximum contiguous sum is 74
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 ________ 26 & nbsp; & nbsp;[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Maximum contiguous sum is 78
Maximum contiguous sum is 79
Maximum contiguous sum is 74
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 77
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Maximum contiguous sum is 78
Maximum contiguous sum is 79
Maximum contiguous sum is 7
Starting index 2
Ending index 6
0
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
Maximum contiguous sum is 7
Starting index 2
Ending index 6
2
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
Maximum contiguous sum is 7
Starting index 2
Ending index 6
4Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
Maximum contiguous sum is 7
Starting index 2
Ending index 6
7Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
from
0Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
from
3Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
Maximum contiguous sum is 7
Starting index 2
Ending index 6
7Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
from
9Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
math
2Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
math
5Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
math
8Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
import
1Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
import
4Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
import
7Maximum contiguous sum is 7
Starting index 2
Ending index 6
5
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
00[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01O[n] [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
IsDynamic Programming
Following is another simple implementation suggested by Mohit Kumar. The implementation handles the case when all numbers in the array are negative.
Python3
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
4 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
5
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
13Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far0
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far2
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far8
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far9
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 40
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 41
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 42
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 44
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 49
Maximum contiguous sum is 70
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Maximum contiguous sum is 78
Maximum contiguous sum is 79
Maximum contiguous sum is 7 Starting index 2 Ending index 60
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 7 Starting index 2 Ending index 62
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 64
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
0Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
3Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
9Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
2Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
5Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
8Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
1Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
4Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
7Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
00[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01Is
Output:
Maximum contiguous sum is 7
Cách tiếp cận khác:
Python3
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
13Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
4 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
5[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far0
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far2
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far8
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far9
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 40
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 41
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 42
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 44
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 49
Maximum contiguous sum is 70
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 73
Maximum contiguous sum is 74
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 77
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 70
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Maximum contiguous sum is 72
Maximum contiguous sum is 74
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 ________ 26 & nbsp; & nbsp;Maximum contiguous sum is 74
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
7[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 77
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 70
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Maximum contiguous sum is 72
Maximum contiguous sum is 74
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 ________ 26 & nbsp; & nbsp;Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 45
Maximum contiguous sum is 72
Maximum contiguous sum is 70
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far6
Maximum contiguous sum is 72
Maximum contiguous sum is 74
Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array [a] max_ending_here = max_ending_here + a[i] [b] if[max_so_far < max_ending_here] max_so_far = max_ending_here [c] if[max_ending_here < 0] max_ending_here = 0 return max_so_far4
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 ________ 26 & nbsp; & nbsp;[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
6Maximum contiguous sum is 78
Maximum contiguous sum is 79
Maximum contiguous sum is 7 Starting index 2 Ending index 60
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 7 Starting index 2 Ending index 62
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 64
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
0Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
3Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
9Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
2Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
5Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
8Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
1Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
4Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
7Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
00[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01Is
Cách tiếp cận khác:
Maximum contiguous sum is 7 Starting index 2 Ending index 60
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
2 Maximum contiguous sum is 7 Starting index 2 Ending index 62
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 64
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
0Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
3Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9Maximum contiguous sum is 7 Starting index 2 Ending index 67
Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9from
9Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
2Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
5Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9math
8Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
1Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
4Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9import
7Maximum contiguous sum is 7 Starting index 2 Ending index 65
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
9[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
00[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
01Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 434
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[2],
[2, 3],
[2, 3, 4],
[3],
[3, 4],
[4]]
06Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + [-2] Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + [-3] Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + [4] max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + [-1] max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + [-2] max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + [1] max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + [5] max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + [-3] max_ending_here = 436
Output:
Maximum contiguous sum is 7 Starting index 2 Ending index 6
Is
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [1]O[1]
Bây giờ hãy thử câu hỏi dưới đây & nbsp; đưa ra một loạt các số nguyên [có thể là một số yếu tố âm], hãy viết một chương trình C để tìm ra * sản phẩm tối đa * có thể bằng cách nhân các số nguyên liên tiếp trong mảng trong đó n ≤ mảng_size. Ngoài ra, in điểm bắt đầu của sản phẩm tối đa Subarray.
Given an array of integers [possibly some
elements negative], write a C program to find out the *maximum product* possible by multiplying ‘n’ consecutive integers in the array where n ≤ ARRAY_SIZE. Also, print the starting point of the maximum product subarray.
Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.