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 <= 200001 <= 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 ...
随机推荐
- Tools:实现vmware虚拟机开机自启动
[来自同事笔记分享] 背景:很多时候宿主机会因为各种原因导致关机或重启,但是里面配置的各个虚拟机不会随宿主机启动而启动,而是需要人为的再去一个一个的操作,无疑会对工作造成一定的影响 因此,正文来了: ...
- js 向上、向下取整
// 1.只保留整数部分(丢弃小数部分) parseInt(5.1234);// 5// 2.向下取整(<= 该数值的最大整数)和parseInt()一样Math.floor(5.1234);/ ...
- SQL注入--SQLMap过WAF
单引号被过滤情况: 空格.等号未被过滤情况: select被过滤情况: 以此类推,当sqlmap注入出现问题时,比如不出数据,就要检查对应的关键词是否被过滤. 比如空格被过滤可以使用space2com ...
- --- rk3399/3288 系列平台接mipi 的dts 数据 panel-init-sequence = [] 命令的整法
https://blog.csdn.net/Shushan1/article/details/87858434 mipi 屏的数据手册 dts sample: &dsi { status = ...
- FLV 格式导入 视频
- java List<Map<String,Object>遍历的方法
public static void main(String[] args) { List<HashMap<String, Object>> list = new ArrayL ...
- 微信小程序页面导航功能
页面导航功能无论是在app和web中都是一个极其常见的功能,如首字母导航,tabs导航等等.但是由于微信小程序无法都dom节点进行操作,所以怎么才能在小程序中快速的导航到用户的想要到达的地方呢. 那么 ...
- 关于css 的兼容设置 ----笔记
在开发网页的时候,由于浏览器的差异,会造成css 代码需要写不同的样式,才能适配大部分浏览器,这很烦,但是又不得不做,我把其中用得比较多的那些,做了一个归纳,放在这里,开发的时候,直接打开这里,复制粘 ...
- 【读书笔记】segment routing mpls数据平面-1
- Analysis of Servlet
@WebServlet("/cdiservlet") public class NewServlet extends HttpServlet { private Message m ...