题目如下:

解题思路:题目定义的子序列宽度是最大值和最小值的差,因此可以忽略中间值。首先对数组排序,对于数组中任意一个元素,都可以成为子序列中的最大值和最小值而存在。例如数组[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. Jsoup学习和使用

    我们先看一下百度百科简介 它是java的HTML解析器 用HttpClient获取到网页后 具体的网页提取需要的信息的时候 ,就用到Jsoup,Jsoup可以使用强大的类似选择器,来获取需要的数据. ...

  2. p5339 [TJOI2019]唱、跳、rap和篮球

    分析  代码 #include<bits/stdc++.h> using namespace std; #define int long long ; ; ],inv[],G,cc[][] ...

  3. Entity Framework Code First数据库连接 转载 https://www.cnblogs.com/libingql/p/3351275.html

    Entity Framework Code First数据库连接   1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器 ...

  4. note3

    awk awk “样式” 文件: 把符合样式的数据行显示出来.awk { 操作 } 文件: 对每一行都执行{}中的操作.awk " 样式 { 操作 }" 文件: 对符合样式的数据行 ...

  5. Mac010--IDEA安装及应用

    Mac--IDEA安装及应用 应用IDEA,首先确保已安装如下环境: JDK:JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库(安装 & 配置环境变 ...

  6. XML学习——java解析xml文件

    递归获取每个标签 package test; import java.io.File; import java.util.List; import org.dom4j.Document; import ...

  7. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  8. ‘’‘安装PyMouse,个人日志'''

    管理员启动CMD 1.直接pip install pymouse,成功安装, 2.安装PyHook: https://www.lfd.uci.edu/~gohlke/pythonlibs/ (向下滑动 ...

  9. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  10. L The Digits String(没有写完,有空补)

    链接:https://ac.nowcoder.com/acm/contest/338/L来源:牛客网 Consider digits strings with length n, how many d ...