We can improve the running time for solving the maximum subarray further by
applying the intuition behind the prefix summations idea to the computation of
the maximum itself. That is, what if, instead of computing a partial sum, St, for
t = 1, 2, . . . , n, of the values of the subarray from a1 to at, we compute a “partial
maximum,” Mt, which is the maximum summation of a subarray of A[1 : t] that
ends at index t?
Such a definition is an interesting idea, but it is not quite right, because it doesn’t
include the boundary case where we wouldn’t want any subarray that ends at t, in
the event that all such subarrays sum up to a negative number. So, recalling our
notation of letting sj,k denote the partial sum of the values in A[j : k], let us define
Mt = max{0, max
j≤t {sj,t} }.
In other words, Mt is the maximum of 0 and the maximum sj,k value where we
restrict k to equal t. This definition implies that ifMt > 0, then it is the summation
value for a maximum subarray that ends at t, and if Mt = 0, then we can safely
ignore any subarray that ends at t.
Note that if we know all the Mt values, for t = 1, 2, . . . , n, then the solution to
the maximum subarray problem would simply be the maximum of all these values.
So let us consider how we could compute these Mt values.
The crucial observation is that, for t ≥ 2, if we have a maximum subarray that
ends at t, and it has a positive sum, then it is either A[t : t] or it is made up of the
maximum subarray that ends at t − 1 plus A[t]. If this were not the case, then we
could make an even bigger subarray by swapping out the one we chose to end at
t − 1 with the maximum one that ends at t − 1, which would contradict the fact
that we have the maximum subarray that ends at t. In addition, if taking the value
เราสามารถปรับปรุงใช้เวลาการแก้ไข subarray สูงสุดที่เพิ่มเติมโดยการคำนวณของสัญชาตญาณเบื้องหลังความคิดออกนำหน้าสูงสุดเอง นั่นคือ ถ้า แทนที่จะคำนวณผลรวมบางส่วน St สำหรับt = 1, 2,..., n ค่า subarray จาก a1 ที่ เราคำนวณ "บางส่วนสูงสุด Mt ซึ่งเป็นผลรวมสูงสุดของ subarray ของ A [1: t] ที่สิ้นสุดที่ดัชนี tคำนิยามคือ ความคิดที่น่าสนใจ แต่มันไม่ถูกมาก เพราะมันไม่ได้รวมถึงกรณีขอบเขตที่เราไม่ต้อง subarray ที่ลงท้ายที่ tเหตุการณ์ที่ subarrays ดังกล่าวทั้งหมดรวมตัวเลขค่าลบ ดังนั้น นึกถึงเราเครื่องหมายการให้เอสเจ k แสดงผลรวมบางส่วนของค่าใน A [j: k], ให้เรากำหนดMt = {0 สูงสุดสูงสุดj≤t {sj, t } }ในคำอื่น ๆ Mt ไม่เกิน 0 ถึง sj สูงสุด k ค่าเราจำกัด k เท่ากับ t คำนิยามนี้หมายถึงการที่ ifMt > 0 แล้วมันเป็นการรวมค่า subarray สูงสุดที่สิ้นสุดที่ t และถ้า Mt = 0 แล้วเราสามารถที่ปลอดภัยละเว้น subarray ที่สิ้นสุดที่ tหมายเหตุว่า ถ้าเรารู้ค่า Mt ทั้งหมด t = 1, 2,..., n แล้ววิธีการแก้ปัญหา subarray สูงสุดเพียงจะสูงค่าเหล่านี้เพื่อให้เราพิจารณาว่าเราสามารถคำนวณค่าเหล่านี้ Mtการสังเกตที่สำคัญคือ สำหรับ t ≥ 2 ถ้าเรามี subarray ที่สูงสุดสิ้นสุด t และมันมีผลเชิงบวก แล้วมันเป็นอย่างใดอย่างหนึ่ง A [t: t] หรือจะเป็นการsubarray สูงสุดที่สิ้นสุดที่ t − 1 พลัส [t] ถ้าไม่กรณี แล้วเราสามารถทำให้ subarray ที่ยิ่งใหญ่ โดยสลับหนึ่งเราเลือกที่จะสิ้นสุดที่t − 1 สูงสุดหนึ่งที่สิ้นสุดที่ t − 1 ซึ่งจะขัดแย้งกับความจริงว่า เรามีค่าสูงสุดที่ subarray ที่สิ้นสุดที่ t นอกจากนี้ ถ้าใช้ค่า
การแปล กรุณารอสักครู่..

เราสามารถปรับปรุงเวลาที่ใช้ในการแก้สูงสุด subarray
ต่อไปโดยใช้สัญชาตญาณความคิดที่อยู่เบื้องหลังคำนำหน้าsummations
ในการคำนวณของสูงสุดของตัวเอง นั่นคือสิ่งที่ถ้าแทนการคำนวณผลรวมบางส่วนเซนต์สำหรับ
t = 1, 2, . . , n ของค่านิยมของ subarray จาก a1 อย่างที่เราคำนวณ
"บางส่วนสูงสุด" Mt ซึ่งเป็นผลบวกสูงสุดของ subarray ของได้ [1: t]
ที่? จบลงที่ t
ดัชนีดังกล่าวหมายเป็นความคิดที่น่าสนใจ
แต่มันเป็นสิ่งที่ไม่ถูกต้องนักเพราะมันไม่ได้รวมถึงกรณีเขตแดนที่เราไม่อยากsubarray ที่สิ้นสุดที่ t ใด ๆ
ในกรณีที่subarrays ดังกล่าวทั้งหมดรวมกันแล้วเป็นจำนวนลบ ดังนั้นเรานึกถึงสัญกรณ์ของการให้ SJ, k แสดงผลรวมบางส่วนของค่าใน [เจ: k] ให้เรากำหนด Mt = สูงสุด {0, สูงสุดj≤t {SJ, เสื้อ}}. ในคำอื่น ๆ Mt เป็นสูงสุดของ 0 และ SJ สูงสุดค่า k ที่เราจำกัด k จะเท่ากับเสื้อ คำนิยามนี้ก็หมายความว่า ifMt> 0 แล้วมันเป็นบวกค่าสำหรับsubarray สูงสุดที่สิ้นสุดที่ t และถ้า Mt = 0 แล้วเราสามารถละเว้นsubarray ที่สิ้นสุดที่ t ใด ๆ . โปรดทราบว่าถ้าเรารู้ทุกค่า Mt สำหรับ t = 1, 2, . . , n แล้ววิธีการแก้ปัญหาที่เกิดขึ้นสูงสุดsubarray ก็จะสูงสุดของค่าทั้งหมดเหล่านี้. เพื่อให้เราพิจารณาวิธีการที่เราสามารถคำนวณเหล่านี้ค่า Mt. สังเกตที่สำคัญก็คือว่าสำหรับ t ≥ 2 ถ้าเรามีสูงสุด subarray ที่จบลงที่t และมีผลรวมที่เป็นบวกแล้วมันเป็นอย่างใดอย่างหนึ่ง [t: t] หรือมันถูกสร้างขึ้นจากsubarray สูงสุดที่สิ้นสุดที่ t - 1 บวก [t] หากครั้งนี้มีกรณีที่ไม่แล้วเราสามารถทำให้ subarray ยิ่งใหญ่โดยสลับหนึ่งที่เราเลือกที่จะสิ้นสุดที่ที- 1 กับหนึ่งสูงสุดที่สิ้นสุดที่ t - 1 ซึ่งจะขัดแย้งกับความจริงที่ว่าเรามีสูงสุดsubarray ที่สิ้นสุดที่ t นอกจากนี้หากการค่า
การแปล กรุณารอสักครู่..
