题目如下:

解题思路:题目定义的子序列宽度是最大值和最小值的差,因此可以忽略中间值。首先对数组排序,对于数组中任意一个元素,都可以成为子序列中的最大值和最小值而存在。例如数组[1,2,3,4,5,6],对于元素3来说,由左边[1,2]组成的所有子序列都可以以3为最大值的,而右边[4,5,6]组成的所有子序列都可以以3为最小值。根据排列组合的原理:[1,2]可以组成的子序列个数为C(2,1) + C(2,2) ,而[4,5,6]可以组成的子序列个数为C(3,1) + C(3,2) + C(3,3) ,同样根据组合计算定律,C(n,1) + C(n,2) + ...... + C(n,n) = 2^n - 1。因为以3为最大值是做被减数,以3为最小值是做减数,因此以3作为最大/最小值的所有子序列的和宽度和就是:(2 ^ 2 - 1)* 3 - (2 ^ 3-1)*3 。同理,也可以求出其他元素的宽度和,并最终求出总宽度和。根据组合的对称性,C(n,m) = C(n,n-m),因此只需要遍历一半的数组长度即可。

代码如下:

class Solution(object):
def sumSubseqWidths(self, A):
"""
:type A: List[int]
:rtype: int
"""
A.sort()
res = 0
l = 1
r = pow(2, len(A) - 1)
for i in range(len(A)/2): res += l * A[i]
res -= r * A[i] res += r * A[len(A)-i-1]
res -= l * A[len(A) - i - 1] l *= 2
r /= 2
return res % (pow(10,9) + 7)

【leetcode】891. Sum of Subsequence Widths的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  5. [LeetCode] 891. Sum of Subsequence Widths 子序列宽度之和

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

  6. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  7. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  8. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  9. 891. Sum of Subsequence Widths

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

随机推荐

  1. [CSP-S模拟测试]:折纸(模拟)

    题目描述 小$s$很喜欢折纸.有一天,他得到了一条很长的纸带,他把它从左向右均匀划分为$N$个单位长度,并且在每份的边界处分别标上数字$0\sim n$.然后小$s$开始无聊的折纸,每次他都会选择一个 ...

  2. ANTLR4加载csv数据

    实现功能: 编写一个自定义的监听器,将逗号分隔符文件(csv)中的数据加载到一种数据结构--“由Map组成的List”中. antlr4文件: grammar CSV; file : hdr row+ ...

  3. react-native 异常处理 Execution failed for task ':app:mergeDebugResources'.

    1.react-native run-android 失败 * What went wrong:Execution failed for task ':app:mergeDebugResources' ...

  4. ArcGIS API for JavaScript(4.x)-加载天地图

    ArcGIS API for JavaScript(3.x)如何加载天地图<ArcGIS API for Javascript 加载天地图(经纬度投影) - 张凯强 - 博客园>这篇文章已 ...

  5. delphi idhttpsever

    http://blog.csdn.net/chelen_jak/article/details/50203809 delphi idhttpsever 2015-12-07 11:36 216人阅读  ...

  6. python web自动化测试框架搭建(功能&接口)——功能测试模块

    功能测试使用selenium,模块有: 1.futil: 公共方法,如元素高亮显示 # coding=utf-8 """高亮显示元素""" ...

  7. java web项目启动加载顺序

    转载:https://www.cnblogs.com/writeLessDoMore/p/6935524.html web.xml加载过程(步骤):       1.启动WEB项目的时候,容器(如:T ...

  8. HDU 6583 Typewriter 题解

    ——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...

  9. js 函数 写法

    // function ckeckName(){}; // function checkUser(){}; // function checkPassWorld(){}; // var checkNa ...

  10. 基于Annotation的IOC 初始化

    从Spring2.0 以后的版本中,Spring 也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5 中引入的一个新特性,用于简化Bean 的配置,可以取代 ...