891. Sum of Subsequence Widths
Given an array of integers
A, consider all non-empty subsequences ofA.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
Approach #1: Math. [Java]
class Solution {
public int sumSubseqWidths(int[] A) {
int n = A.length;
Arrays.sort(A);
long p = 1;
long mod = 1000000007;
long ans = 0;
for (int i = 0; i < n; ++i) {
ans = (ans + (A[i] - A[n-i-1]) * p) % mod;
p = (p << 1) % mod;
}
return (int)((ans + mod) % mod);
}
}
Reference:
https://zxi.mytechroad.com/blog/math/leetcode-891-sum-of-subsequence-widths/
891. Sum of Subsequence Widths的更多相关文章
- [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 LT891
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the ...
- [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 ...
- 子序列宽度求和 Sum of Subsequence Widths
2019-10-14 17:00:10 问题描述: 问题求解: 如果暴力求解,时间复杂度是exponational的,因为这里是子序列而不是子数组.显然,直接枚举子序列是不太现实的了,那么可以怎么做呢 ...
- 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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
随机推荐
- Nginx+Keepalived实现站点高可用[z]
http://segmentfault.com/a/1190000002881132
- 【转】MEF程序设计指南一:在应用程序中宿主MEF
在应用程序中宿主MEF其实非常简单,只需要创建一个组合容器对象(CompositionContainer)的实例,然后将需要组合的部件(Parts)和当前宿主程序添加到容器中即可.首先需要添加MEF框 ...
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- predict_proba 的使用
- linux磁盘管理(RHEL)
IDE硬盘名称格式为/dev/hdXY,其中X为a-z的小写字母,Y为数字1-4(一块硬盘最多能分4个主分区).如hda1,表示第一块硬盘的第一个分区.hdb3表示第二块硬盘的第三个分区.还有如Pri ...
- 针对程序员的podcast
身为程序员们,必须要懂得合理的利用琐碎时间来提炼自身,或许上下班途中或骑行或徒步或...时,以下这些Podcasts对你有些许作用: The Hanselminutes podcast by Scot ...
- 2018.08.21 NOIP模拟 unlock(模拟+找规律)
unlock 描述 经济危机席卷全球,L国也收到冲击,大量人员失业. 然而,作为L国的风云人物,X找到了自己的新工作.从下周开始,X将成为一个酒店的助理锁匠,当然,他得先向部门领导展示他的开锁能力. ...
- returning into 语句
returning into 语句用于执行完语句后返回的值,具体返回执行之前或者之后的结果,多用于在存储过程中 如下所述:delete语句的returning into语句返回的是delete之前的结 ...
- @Autowired 和 @Qualifier
一 无冲突 bean工厂 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- Swift1.2与Xcode6.3 beta
Xcode6.3和Swift1.2都已经发布.这次发布增强了Swift编译器也给Swift增加了一些新的特性.详细内容可以看这里.这里主要关注比较重要的内容. 编译器的改进 Swift1.2的编译器更 ...