數組平衡點


一個序列的平衡點是這樣的,它的左邊的所有的元素的和應該等於右邊的所有的元素的和,比如在下面的序列A:

 

A[0] = -7   A[1] =  1   A[2] = 5
A[3] =  2   A[4] = -4   A[5] = 3
A[6] =  0

 

3是一個平衡點因為:

  • A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

6也是一個平衡點因為:

  • A[0] + A[1] + A[2] + A[3] + A[4] + A[5] = 0

(零個元素的和是零) 索引7不是平衡點,因為它不是序列A的有效索引。

如果你仍然不是很清楚,那么這里給出了明確的定義:0 ≤ k < n 並且 sum[i=0]k-1A[i] = sum[i=k+1]n-1 A[i]。時, 整數k是序列A[0], A[1], ..., A[n−1]$ 的平衡點,這里我們假定零個元素的和為零。

請寫一個函數

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

返回給定序列的平衡點(任意一個)如果沒有平衡點則返回−1,假設這個序列可達到非常大。

假定:

  • N 是 [0..10,000,000] 內的 整數;
  • 數組 A 每個元素是取值范圍 [−2,147,483,648..2,147,483,647] 內的 整數 .

復雜度:

  • 最壞-情況下,期望的時間復雜度是 O(N);
  • 最壞-情況下,期望的空間復雜度是 O(N), 輸入存儲除外 (不計輸入參數所需的存儲空間).

輸入數組中的元素可以修改

C#給出兩個思路一樣只是寫法有點不同的方法:

方法一:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 class Solution {
 5   public int equi ( int[] A ) {
 6     if (A.Length <= 2)
 7       return -1;
 8     long summary = A.Sum();
 9     long left = 0;
10 
11     for (int i = 1; i < A.Length; i++)
12     {
13        left += A[i - 1];
14        if (A[i] == left && summary - left - A[i] == A[i])
15             return i;
16      }
17      return -1;
18   }
19 }

方法二:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 class Solution {
 5   public int equi ( int[] A ) {
 6     if (A.Length == 0)
 7                 return -1;
 8      long summary = A.Sum();
 9             
10      long sum_left = 0;
11      for (int i = 0; i < A.Length; i++)
12      {
13           long sum_right = summary - sum_left - A[i];
14           if (sum_left == sum_right)
15           {
16                return i;
17            }
18            sum_left += A[i];
19       }
20        return -1;
21   }
22 }

這兩種寫法優劣請大家一起來評判一下。

========================華麗的分割線==========================

測試網站上給出的評判:

方法一:

 

Analysis
 

 

test time result
example 
Test from the task description
0.080 s. OK
simple 0.080 s. OK
extreme_large_numbers 
Sequence with extremly large numbers testing arithmetic overflow.
0.070 s. RUNTIME ERROR 
tested program terminated unexpectedly 
stdout:
Unhandled Exception: System.OverflowException: Number overflow.
  at System.Linq.Enumerable.<Sum>m__5E (Int32 a, Int32 b) [0x00000] 
  at System.Linq.Enumerable.Sum[Int32,Int32] (IEnumerable`1 source, System.Func`3 selector) [0x00000] 
  at System.Linq.Enumerable.Sum (IEnumerable`1 source) [0x00000] 
  at Solution.equi (System.Int32[] A) [0x00000] 
  at SolutionWrapper.run (System.String input, System.String output) [0x00000] 
  at SolutionWrapper.Main (System.String[] args) [0x00000] 
overflow_tests 0.080 s. RUNTIME ERROR 
tested program terminated unexpectedly 
stdout:
Unhandled Exception: System.OverflowException: Number overflow.
  at System.Linq.Enumerable.<Sum>m__5E (Int32 a, Int32 b) [0x00000] 
  at System.Linq.Enumerable.Sum[Int32,Int32] (IEnumerable`1 source, System.Func`3 selector) [0x00000] 
  at System.Linq.Enumerable.Sum (IEnumerable`1 source) [0x00000] 
  at Solution.equi (System.Int32[] A) [0x00000] 
  at SolutionWrapper.run (System.String input, System.String output) [0x00000] 
  at SolutionWrapper.Main (System.String[] args) [0x00000] 
one_large 
one large number at the end of the sequence
0.070 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 0
sum_0 
sequence with sum=0
0.070 s. OK
single 
single number
0.070 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 0
empty 
Empty array
0.060 s. OK
combinations_of_two 
multiple runs, all combinations of {-1,0,1}^2
0.080 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 0
combinations_of_three 
multiple runs, all combinations of {-1,0,1}^3
0.080 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 0
small_pyramid 0.070 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 42
large_long_sequence_of_ones 0.130 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 50000
large_long_sequence_of_minus_ones 0.110 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 50002
medium_pyramid 0.090 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 402
large_pyramid 
Large performance test, O(n^2) solutions should fail.
0.160 s. WRONG ANSWER 
got -1, but equilibrium point exists, for example on position 898

方法二:

Analysis
 
Detected time complexity:
O(N)

 

test time result
example 
Test from the task description
0.100 s. OK
simple 0.090 s. OK
extreme_large_numbers 
Sequence with extremly large numbers testing arithmetic overflow.
0.070 s. RUNTIME ERROR 
tested program terminated unexpectedly 
stdout:
Unhandled Exception: System.OverflowException: Number overflow.
  at System.Linq.Enumerable.<Sum>m__5E (Int32 a, Int32 b) [0x00000] 
  at System.Linq.Enumerable.Sum[Int32,Int32] (IEnumerable`1 source, System.Func`3 selector) [0x00000] 
  at System.Linq.Enumerable.Sum (IEnumerable`1 source) [0x00000] 
  at Solution.equi (System.Int32[] A) [0x00000] 
  at SolutionWrapper.run (System.String input, System.String output) [0x00000] 
  at SolutionWrapper.Main (System.String[] args) [0x00000] 
overflow_tests 0.070 s. RUNTIME ERROR 
tested program terminated unexpectedly 
stdout:
Unhandled Exception: System.OverflowException: Number overflow.
  at System.Linq.Enumerable.<Sum>m__5E (Int32 a, Int32 b) [0x00000] 
  at System.Linq.Enumerable.Sum[Int32,Int32] (IEnumerable`1 source, System.Func`3 selector) [0x00000] 
  at System.Linq.Enumerable.Sum (IEnumerable`1 source) [0x00000] 
  at Solution.equi (System.Int32[] A) [0x00000] 
  at SolutionWrapper.run (System.String input, System.String output) [0x00000] 
  at SolutionWrapper.Main (System.String[] args) [0x00000] 
one_large 
one large number at the end of the sequence
0.070 s. OK
sum_0 
sequence with sum=0
0.080 s. OK
single 
single number
0.070 s. OK
empty 
Empty array
0.060 s. OK
combinations_of_two 
multiple runs, all combinations of {-1,0,1}^2
0.070 s. OK
combinations_of_three 
multiple runs, all combinations of {-1,0,1}^3
0.070 s. OK
small_pyramid 0.070 s. OK
large_long_sequence_of_ones 0.100 s. OK
large_long_sequence_of_minus_ones 0.120 s. OK
medium_pyramid 0.090 s. OK
large_pyramid 
Large performance test, O(n^2) solutions should fail.
0.160 s. OK


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM