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的更多相关文章

  1. [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  ...

  2. 891. Sum of Subsequence Widths

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

  3. [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  ...

  4. 【leetcode】891. Sum of Subsequence Widths

    题目如下: 解题思路:题目定义的子序列宽度是最大值和最小值的差,因此可以忽略中间值.首先对数组排序,对于数组中任意一个元素,都可以成为子序列中的最大值和最小值而存在.例如数组[1,2,3,4,5,6] ...

  5. 子序列宽度求和 Sum of Subsequence Widths

    2019-10-14 17:00:10 问题描述: 问题求解: 如果暴力求解,时间复杂度是exponational的,因为这里是子序列而不是子数组.显然,直接枚举子序列是不太现实的了,那么可以怎么做呢 ...

  6. Unique Letter String LT828

    A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. tornado框架设置

    路由 import tornado.ioloop #开启循环 让服务器一直等待请求的到来 import tornado.web #框架基本功能封装在此模块 #例子 class MainHendler( ...

  2. 李清华 201772020113《面向对象程序设计(java)》第十四周学习总结

    1.实验目的与要求 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及常用API: 2.实验内容和步骤 实验1: 导入第12章示例程序,测试程序并进行组内讨论. 测试程 ...

  3. Monkey测试简介

    1.Monkey测试简介monkey是安卓命令行工具,它向系统发送伪随机的用户事件,例如:按键的输入.触摸屏的输入.手势输入等操作来对设备上的程序进行压力测试,检测程序多久的时间会发生异常.因此,mo ...

  4. Mysql时间差计算

    Mysql如何计算两个时间字段的差值?可用函数 TIMESTAMPDIFF() ----------------------------- TIMESTAMPDIFF函数,有参数设置,可以精确到天(D ...

  5. thinkphp5.1明明密码的一致的 却说不一致的解决办法

    protected $rule = [       'password|密码'=>[                       'require',           'length:6,2 ...

  6. Https的前世今生

    1.年前会议 马上要过年了,公司业务上的需求也少了很多,这不,王小二他们召开了一场技术会议,盘点年前能干点啥. 只见C哥写了一份清单,其中一项是全站升级https. C哥说:https是一种趋势,但目 ...

  7. Navicat远程连接不上mysql解决方案

    一.can‘t connect to MySql server on ‘47.93.X.X’ 这是因为mysql端口被防火墙拦截,需用linux执行如下指令: 1.#/sbin/iptables -I ...

  8. Kivy / Buildozer VM Ubuntu不能连接到网络的问题解决

    从kivy网站下载下来的Buildozer VM镜像在进入虚拟机以后无论虚拟机里边的虚拟网络编辑器以及网络适配器网络连接作何设置都不能连接到网络,在终端里边使用ifconfig查看ip地址是127.0 ...

  9. 基础算法简单实现-python

    目录 Python(版本3.6+)-Anna-Lena Popkes

  10. Redis主从集群及哨兵模式

    本次实验环境准备用一台服务器模拟3台redis服务器,1主2从 主从集群搭建 第一步:安装Redis 安装Redis,参考前面安装Redis文章,保证单机使用没有问题. 第二步:配置服务器文件 定位到 ...