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. linux的ssh服务

    1.检查是否安装ssh > rpm -qa|grep ssh 2.安装ssh服务 > yum install ssh 配置 /etc/ssh/sshd_config 端口 22 3.启动s ...

  2. kafka 报Failed to load class "org.slf4j.impl.StaticLoggerBinder".[z]

    转:http://blog.chinaunix.net/uid-25135004-id-4172954.html 测试kafka    producer发送消息 和  consumer 接受消息报错 ...

  3. maven的pom.xml样例

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. Java数据结构和算法(七)B+ 树

    Java数据结构和算法(七)B+ 树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 我们都知道二叉查找树的查找的时间复杂度是 ...

  5. Asp.Net 用户验证(自定义IPrincipal和IIdentity)

    http://www.cnblogs.com/JimmyZhang/archive/2008/12/07/1349457.html

  6. Vue2.0+ElementUI+PageHelper实现的表格分页

    Vue2.0+ElementUI+PageHelper实现的表格分页 前言 最近做了一些前端的项目,要对表格进行一些分页显示.表格分页的方法有很多,从宏观上来说分为物理分页和逻辑分页,由于逻辑分页(即 ...

  7. Largest product from 3 integers

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  8. Linux服务器部署系列之二—MySQL篇

    MySQL是linux环境中使用最广泛的数据库之一,著名的“LAMP黄金组合”就要用到MySQL.关于MySQL的优点及作用,我就不多讲了,网上很多这样的文章. 今天我们要谈的是MySQL服务器的部署 ...

  9. Netty学习第三节Netty的入门级学习

    1.原生NIO存在哪些缺陷     (1)NIO的类库和API繁杂,使用也比较麻烦,需要熟练掌握selector.ServerSocketChannel.SocketChannel.ByteBuffe ...

  10. HTML中submit和button的区别

    submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理(包括输入验证)后再提交的话,通常都必须把submit改成butt ...