【leetcode】891. Sum of Subsequence Widths
题目如下:

解题思路:题目定义的子序列宽度是最大值和最小值的差,因此可以忽略中间值。首先对数组排序,对于数组中任意一个元素,都可以成为子序列中的最大值和最小值而存在。例如数组[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的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- [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 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 891. Sum of Subsequence Widths
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the ...
随机推荐
- vue定义组件
1.定义组件 2.在App.vue里引入Home组件 home代码 <template> <div>home</div> </template> < ...
- 不同vlan之间相互通信
不同VLAN之间相互通信的两种方式 (单臂路由.三层交换) 试验目的: 1.通过单臂路由实现不同VLAN之间的通信 2.通过三层交换路由功能实现不同VLAN之间的通信 网络拓扑图: 1.单臂路由实 ...
- Bootstrap,Bagging and Random Forest Algorithm
Bootstrap Method:在统计学中,Bootstrap从原始数据中抽取子集,然后分别求取各个子集的统计特征,最终将统计特征合并.例如求取某国人民的平均身高,不可能测量每一个人的身高,但却可以 ...
- python args参数理解
文件名:while.py 代码:import sysprint(sys.path)print(sys.argv) 执行结果: ['D:\\python_basic\\s01', 'D:\\python ...
- numpy.meshgrid的理解以及3D曲面图绘制(梯度下降法实现过程)
相关概念: 1.x向量和y向量 import numpy as np import matplotlib.pyplot as plt x = np.array([[0,1,2,3], [0,0,0,0 ...
- Ubuntu下的图形化多线程下载器XDM
目录 1.下载 2.安装 3.浏览器支持 使用Ubuntu下载东西经常过于缓慢,因此需要多进程下载器. 1.下载 下载链接:http://xdman.sourceforge.net/#download ...
- ubuntu下修改子网掩码
1.修改网络配置 修改 /etc/netplan/01-network-manager-all.yaml 文件 vi /etc/netplan/01-network-manager-all.yaml ...
- Linux服务正常启动,Linux服务器能访问,但是外部机器不能访问
公司用到了jenkins,就在自己虚拟机里面部署了一个jenkins.部署成功之后,在Linux虚拟机里面能正常访问,但是外部真实机却不能访问.当时的第一反应就是觉得应该是权限问题,猜测会不会是jen ...
- Codeforces 1156E Special Segments of Permutation(单调栈)
可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...
- UIViewController push或presentViewController 弹出方式
//导航控制器数量 add xjz 判断是push还是present出来的 NSArray *viewcontrollers = self.navigationController.viewContr ...