Sum of Subsequence Widths LT891
Given an array of integers A
, consider all non-empty subsequences of A
.
For any sequence S, let the width of S be the difference between the maximum and minimum element of S.
Return the sum of the widths of all subsequences of A.
As the answer may be very large, return the answer modulo 10^9 + 7.
Example 1:
Input: [2,1,3]
Output: 6
Explanation:
Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
The sum of these widths is 6.
Note:
1 <= A.length <= 20000
1 <= A[i] <= 20000
Idea 1. 刚开始想subset穷举, sort the array and get all the pair (0<= i < j <= n-1, A[i] < A[j]) such that (A[j] - A[i]) * 2^(j-i-1), saw an amazing online soloution, consider the contribution for each element, assume sequence is like A[0]...A[i-1]A[i]A[i+1]...A[n-1], on the left, there are i numbers < A[i], 2^(i) subsequence where A[i] is the maximu, on the right, there are n-1-i numbers > A[i], 2^(n-1-i) subsequence A[i] as minimum, hence we have
res = A[i]*2^(i) - A[i]*2^(n-1-i)
another trick to save compute 2^(n-1-i) and 2^(i) separately, sum(A[n-1-i]*2^(n-1-i)) = sum(A[n-1-i]*2^(i))
1 << i
(c=1 << 1) incrementely
Time complexity: O(nlogn)
Space complexity: O(1)
class Solution {
public int sumSubseqWidths(int[] A) {
long res = 0;
long mod = (long)1e9+7;
long c = 1;
int n = A.length; Arrays.sort(A); for(int i = 0; i < A.length; ++i, c = (c << 1)%mod) {
res = (res + (A[i] - A[n - 1 - i]) * c + mod)%mod;
} return (int)(res);
}
}
Sum of Subsequence Widths LT891的更多相关文章
- [Swift]LeetCode891. 子序列宽度之和 | Sum of Subsequence Widths
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the ...
- 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] 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】891. Sum of Subsequence Widths
题目如下: 解题思路:题目定义的子序列宽度是最大值和最小值的差,因此可以忽略中间值.首先对数组排序,对于数组中任意一个元素,都可以成为子序列中的最大值和最小值而存在.例如数组[1,2,3,4,5,6] ...
- 子序列宽度求和 Sum of Subsequence Widths
2019-10-14 17:00:10 问题描述: 问题求解: 如果暴力求解,时间复杂度是exponational的,因为这里是子序列而不是子数组.显然,直接枚举子序列是不太现实的了,那么可以怎么做呢 ...
- Unique Letter String LT828
A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- matlab-逻辑回归二分类(Logistic Regression)
逻辑回归二分类 今天尝试写了一下逻辑回归分类,把代码分享给大家,至于原理的的话请戳这里 https://blog.csdn.net/laobai1015/article/details/7811321 ...
- 搭建IntelliJ IDEA授权服务器
地址:https://blog.csdn.net/maozexijr/article/details/79072287 https://www.jianshu.com/p/754d8f907f2 ...
- scala 随笔
创建map,并向map添加元素 val idMap = Map( "group_id" -> "GID", "sim_id" -> ...
- 设计CUBE by kylin
1.新增或者选择一个已有项目(project)后,将hive数据导入到kylin中,web界面的Model→Data source,导入数据 2.创建数据模型,数据模型是Cube的基础,它主要用于描述 ...
- 关联tomcat源代码
1.进入tomcat官网下载对应版本源代码文件 2. 3..ctrl+鼠标左键 点击Cookie对象 4. 5.
- Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如: public class MyJobFactory extends org.s ...
- 蒙特卡洛树,AMAF,Rave浅析
蒙特卡洛树搜索: MCTS使用蒙特卡洛模拟来估计每个节点的价值. 其默认策略为贪婪算法, 即每次选择价值最高的节点进行模拟, 在每次模拟得到结果后, 将结果反馈回每个上级节点, 更新节点价值. 通常来 ...
- CentOS7.0 采用压缩包 安装Nginx 1.7.4和添加Tomcat为系统服务 nginx结合tomcat
CentOS7.0 采用压缩包 安装Nginx 1.7.4 一.安装准备首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc ...
- springboot security
Authority 权限Credential 证书Grant 授予 Authentication 身份验证 以下,我们将通过四步,逐步实现spring-security的userna ...
- Java学习--泛型
个人理解,所谓的泛型就是将数据类型像参数(称为类型参数或者泛型参数)一样传入类,接口或者方法中,这个类型参数可以当作普通的数据类型,进行变量的声明(成员变量,局部变量(包括方法参数)),指明返回值类型 ...