Trước hết cảm ơn các anh chị / bạn đã tham gia giải đáp các vấn đê cho em thời gian qua. Em ko có đủ năng lực để giải đáp vấn đề của người khác toàn đi hỏi, thật ngại, xin thư lỗi ạ.
Đề này bảo mình tìm tổng nhỏ nhất đúng ko ạ? em thắc mắc vì sao có (1, 3) mà ko có cặp (0,4) , (1,2) …vậy ạ. Em đọc tiếng anh còn kém lắm mong mọi người giải đáp giúp
A non-empty array A of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + … + A[Q].
A min abs slice is a slice whose absolute sum is minimal.
For example, array A such that:
A[0] = 2
A[1] = -4
A[2] = 6
A[3] = -3
A[4] = 9
contains the following slices, among others:
(0, 1), whose absolute sum = |2 + (−4)| = 2
(0, 2), whose absolute sum = |2 + (−4) + 6| = 4
(0, 3), whose absolute sum = |2 + (−4) + 6 + (−3)| = 1
(1, 3), whose absolute sum = |(−4) + 6 + (−3)| = 1
(1, 4), whose absolute sum = |(−4) + 6 + (−3) + 9| = 8
(4, 4), whose absolute sum = |9| = 9
Both slices (0, 3) and (1, 3) are min abs slices and their absolute sum equals 1.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A consisting of N integers, returns the absolute sum of min abs slice.
For example, given:
A[0] = 2
A[1] = -4
A[2] = 6
A[3] = -3
A[4] = 9
the function should return 1, as explained above.
Assume that:
N is an integer within the range [1…100,000];
each element of array A is an integer within the range [−10,000…10,000].
Complexity:
expected worst-case time complexity is O(N*log(N));
expected worst-case space complexity is O(N) (not counting the storage required for input arguments).