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

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

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

  2. 【leetcode】891. Sum of Subsequence Widths

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

  3. Sum of Subsequence Widths LT891

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

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

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

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

  6. All LeetCode Questions List 题目汇总

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

  7. leetcode hard

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

  8. Swift LeetCode 目录 | Catalog

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

  9. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

随机推荐

  1. [转]Win7下PEiD已停止工作

    转载自:http://www.programlife.net/peid-stop-working-under-win7.html PEID是一个很不错的工具,可查壳,查壳PE信息,借助插件还可以做一些 ...

  2. 三、oracle 用户管理一

    三.oracle 用户管理一 一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 ide ...

  3. 750A New Year and Hurry

    A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. 一名优秀的UI设计师应该具备哪些条件?

    想做好一个好的UI设计师除了应该具有一定的审美能力,还要了解整个产品的开发过程,因为目前国内的软件行业还不能对UI设计形成应有的重视度,所以对我们的要求就更高了,你要能作出夺人眼球的东西,还要站在用户 ...

  5. 给自己名字abel.这个好,怎么字母排序都第一

    给自己名字abel.这个好,怎么字母排序都第一

  6. part1:15-安装Linux系统到开发板

    1.Qtopia简介 Qtopia是Trolltech公司为采用嵌入式Linux操作系统的消费电子设备而开发的综合应用平台,Qtopia包含完整的应用层.灵活的界面用户.窗口操作系统.应用程序启动程序 ...

  7. 技术管理zz

    1.管理者最重要的是规划Roadmap 技术管理者并不能完全脱离技术.最少要把握最新技术的发展,了解团队当前技术现状和不足.用于规划的时间应该不少于50%的工作时间.具体而言,规划又分为业务规划和团队 ...

  8. 2018.08.20 loj#116. 有源汇有上下界最大流(模板)

    传送门 貌似就是转成无源汇,然后两遍最大流搞定? 其实第二遍跑最大流是自动加上了第一次的答案. 代码: #include<bits/stdc++.h> #define N 100005 # ...

  9. html使用技巧

    line-height: 27px;  /* lineheight和height保持一致就能达到文本垂直居中*/ .top_content li { list-style-image: url(../ ...

  10. 深浅 buffer

    var str = "深入浅出"; var buf = new Buffer(str, 'utf-8'); console.log(buf); 这种情况下是数字 var str = ...