-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'''不排序,把所有子数组找出来,然后把极差加起来,这是最麻烦的。 | ||
其次是排序之后二重循环 | ||
O(N)的是等比数列 | ||
''' | ||
|
||
class Solution: | ||
def sumSubseqWidths(self, A): | ||
""" | ||
:type A: List[int] | ||
:rtype: int | ||
""" | ||
A.sort() | ||
res=0 | ||
n=len(A) | ||
|
||
for i in range(n): | ||
pos=(1<<i) | ||
neg=(1<<(n-1-i)) | ||
res=(res+(pos-neg)*A[i])%(10**9+7) | ||
return res | ||
|
||
|
||
|
||
A=[2,1,3] | ||
s=Solution() | ||
print(s.sumSubseqWidths(A)) |