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 ...
随机推荐
- Spring MVC 集成Disconf
1.Disconf:Distributed Configuration Management Platform(分布式配置管理平台),专注于各种「分布式系统配置管理」的「通用组件」和「通用平台」, 提 ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- orcal - 伪列
数据伪劣 行号 ROWNUM SELECT ROWNUM, empno,ename,sal from emp; 取出第一行数据 SELECT ROWNUM, empno,ename,sal from ...
- python 网站 监控返回值
import requests try: code = requests.get("https://api.sudaizhijia.cm/").status_code print( ...
- K2项目开发流程
(自己的学习资料) K2项目开发流程: 1.在VS2013中设计流程,并在K2 Workspce中测试流程 首先是新建新建一个k2的Process文件..kprx后缀. 在里面创建所需要的流程.由于我 ...
- nodeJs 代码热更新
在开发node过程中,每次修改代码都需要重新启动服务,是一件很抓狂的事情 使用nodemon热加载可以帮我们很好的解决这一问题 1. 安装 npm install nodemon -g 2. 修改np ...
- Java 8 日期时间 API
转自:https://www.runoob.com/java/java8-datetime-api.html Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与 ...
- Navicat远程连接不上mysql解决方案
一.can‘t connect to MySql server on ‘47.93.X.X’ 这是因为mysql端口被防火墙拦截,需用linux执行如下指令: 1.#/sbin/iptables -I ...
- Dostoevsky: Better Space-Time Trade-Offs for LSM-Tree Based Key-Value Stores via Adaptive Removal of Superfluous Merging 阅读笔记
Introduction 主流的基于LSM树的KV存储都在两方面进行权衡,一方面是写入更新的开销,另一方面是查询和存储空间的开销.但它们都不是最优的,问题在于这些存储系统在LSM树的每一个level上 ...
- js前台计算两个日期的间隔时间
js前台计算两个日期的间隔时间(时间差)原创 2017年08月28日 16:09:43 标签:javascript 1144在后台传来两个时间字段,从中解析出两个字符串类型的日期格式 需要在前台解析出 ...