Skip to content

Commit

Permalink
Sum of Subsequence Widths
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Aug 22, 2018
1 parent f420369 commit 6fdf117
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lc891.py
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))

0 comments on commit 6fdf117

Please sign in to comment.