Codility MaxAbsSliceSum Solution

1.题目(HARD)

A non-empty zero-indexed 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. Thesum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].

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:

int solution(int A[], int N);

that, given a non-empty zero-indexed 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), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

2.题目分析

这个题目看上去很简单。

slice求和,prefix的经典解法。不过,这个要求的是和的绝对值。这就有一点生硬了。贪心算法或者蠕虫都不太实用。

有点着急呢。。眼看答案就在眼前,可是就是想不出。过了20分钟,我有点开窍了。

首先是看到了时间复杂度为O(N*logN).这感觉是要排序啊,不过对元数据排序貌似没有用啊。突然意识到,可以对prefix排序。

瞬间感觉自己很牛逼。

你想,prefix任何两者的差,即是这一段slice的和,那么我们现在其实找的就是这段和的最小绝对值。我们将prefix排序以后,那么就是大小最相近的两个prefix的值会成为相邻。对其做差,就是找到一段最小值。我们顺序遍历这个prefix数组,找到所有的相邻项相差的最小值,不就是我们要找的那个绝对值吗?(因为已经从大到小排序了)。

当然,还是要做一些其他的处理,比如找到prefix的绝对值最小值,和数组的绝对值的最小值,同得出的结论进行比较。

就必然得出了最后的答案。

3. 想通了代码也就几十行不到,大家自己想吧!

时间: 2024-12-22 13:34:59

Codility MaxAbsSliceSum Solution的相关文章

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

Codility NumberSolitaire Solution

1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from 0 to N − 1. There is a number written on each square. A non-empty zero-indexed array A of N integers contains the numbers written on the squares. More

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

the solution of CountNonDivisible by Codility

question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn); Then  we iterate array A to get the ith non-di

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

codility上的问题(34) Fluorum 2014

好久没写codility的题了,一来没时间,二来有的题目不太好分析.这个题比较有意思,我还没有给出非常严格的证明. 给定一棵树(无向无环图),从一个节点出发,每次选择一个节点,从起点到目的节点的路径上没经过的节点尽可能多,直到遍历完所有的节点.如果起点到两个目的节点的路径中没经过的节点同样多,则选择标号较小的节点作为目的节点.如此继续,直到遍历所有的节点,求按顺序选择了哪些目的节点? 例如从2 开始,第一次选择0,然后1,0变为经历过的节点. 然后从0开始,第二次选择6, 然后4,6变为经历过的

*[codility]Country network

https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave