-
인터뷰 준비중인 취업 희망자 입니다. 준비중에 이런 문제를 보았는데요…
You’re given an array containing both positive and negative integers and required to find the subarray with the largest sum (O(N) a la KBL). Write a routine in C for the above.
위키피디아에 찾아 보면,
http://en.wikipedia.org/wiki/Maximum_subarray_problem다음과 같이 예제가 나와 있습니다.
“… For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.”
사실 문제 자체가 잘 이해가 안되는데요.. 쩝.. 꾸벅.. 죄송..TT
도대체 어떤 값을 구하라는건지 도통…그래서 위키피디아의 파이썬 예제를 보고 값을 나열해 보면요…
x max_ending_here max_so_far
-2 0 0
1 1 1
-3 0 1
4 4 4
-1 3 4
2 5 5
1 6 6
-5 1 6
4 5 6이렇게 나옵니다. the largest sum 이 6 인것은, 최종 max_so_far가 6이어서 그런가요? 그런데, contiguous subarray 라는 것이 무엇인지 전혀 감이 안잡힙니다. 어떻게, [4, −1, 2, 1]가 나오는 것인지요?