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 ...
随机推荐
- spring boot 接口返回值去掉为null的字段
现在项目都是前后端分离的,返回的数据都是使用json,但有些接口的返回值存在 null或者"",这种字段不仅影响理解,还浪费带宽,需要统一做一下处理,不返回空字段,或者把NULL转 ...
- zabbix使用SNMPV3协议监控交换机
SNMPV3是简单网络管理协议的第三版,因为其安全性更高,现在的中低端交换机已普遍支持该协议,所以在生产环境中我们应该采用SNMPV3对交换机.路由器进行管理. 首先在交换机上要配置SNMPV3协议, ...
- ---- 关于Android蓝牙搜索到设备的图标显示和设备过滤
根据: https://www.douban.com/note/637446089/http://bbs.16rd.com/blog-23795-3446.html 以下摘自原文: (Android主 ...
- V-REP与C++初步通信测试
打开vrep,在上方操作栏找到help选项打开,选择help topics.此时浏览器打开了vrep的操作手册user manual. 在user manual左侧目录中找到writing code ...
- Why Everyone Should Lift Weights
Why Everyone Should Lift Weights by James Clear I'll say it plain and simple: you should be lifting ...
- R数据导入导出(一): read.table()和read.csv()的区别
之前也参考过一些资料,虽然是这么简单的两个buildin,还是仔细对比了一下,我有两张txt,都是从cube中导出的,就意味着每一列的列数是不一样的.R语言官方文档中有这样一句话不知道大家注意到了没有 ...
- numpy统计分布显示
#导包 import numpy as np #导入鸢尾花数据 from sklearn.datasets import load_iris data = load_iris() pental_len ...
- go调查内存泄漏
curl x.x.x.x/debug/pprof/heap > base.heap 过段时间 curl x.x.x.x/debug/pprof/heap > current.heap go ...
- Neural Network Virtual Machine
深度学习Stack 为什么提出NNVM? 深度学习框架现状 - “碎片化” 目前,深度学习应用框架呈现出高度的“碎片化(fragmentation)”倾向,这主要是由于下述两个原因: 1. 深度学习正 ...
- RFCN配置参数
最近一直被人问这个,索性画张图,省得一遍一遍解释.