【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 ...
随机推荐
- scrapy之Request对象
我们在使用scrapy框架的时候,会经常疑惑,数据流是怎么样在各个组件中间传递的.最近经常用scrapy+selenium爬取淘宝,又因为今天周五心情好,本宝宝决定梳理一下这方面知识. scrapy中 ...
- CAS 认证
Central Authentication Service http session 保持机制 1. 用户向服务端发送密码,服务端生成与用户对应的 sessionid 2. 服务端将这个 sess ...
- 128、TensorFlow元数据MetaData
#tf.Session.run也接收一个可选的参数options #能够让你来配置训练时的参数 #run_metadata参数让你能够收集关于训练的元信息 #列如你可以使用这些可选项来追踪执行的信息 ...
- Invalid bound statement (not found)--spring boot集成mybatis
问题: {"timestamp":"2019-07-02T10:21:32.379+0000","status":500,"err ...
- Vagrant 手册之 Provisioning - File
原文地址 Provisioner 名字:"file" Vagrant 的 file provisioner 允许将文件或目录从主机上传到客户机. File provisioning ...
- 【转载】Spring boot学习记录(二)-配置文件解析
前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 Spring Boot使用了一个全局的配置文件application.prop ...
- Git004--版本回退
Git--版本回退 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...
- 关于eclipse中的maven插件问题
最近上课讲eclipse 中的maven插件 有一个坑确实比较坑,实际上就是一个配置的原因. 就是在eclipse中设置java 的buildpath的时候,一般不注意往往都设置成了jre的,这样的话 ...
- StringBuilder -字符串缓冲区,节约内层空间变长数组
package cn.learn; /* 字符串缓冲区 -缓冲可提高效率 java.lang.StringBuilder 字符串的底层是一个被final修饰的数组,不能改变,是一个常量 private ...
- 15、numpy——排序、条件刷选函数
NumPy 提供了多种排序的方法. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种排序算法的比较. 种类 速度 最坏情况 ...