首页 > 其他分享 >[LeetCode] 891. Sum of Subsequence Widths

[LeetCode] 891. Sum of Subsequence Widths

时间:2022-11-19 14:59:43浏览次数:72  
标签:891 nums int pow Sum Subsequence widths 序列 LeetCode

The width of a sequence is the difference between the maximum and minimum elements in the sequence.

Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 109 + 7.

A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].

Example 1:

Input: nums = [2,1,3]
Output: 6
Explanation: The 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.

Example 2:

Input: nums = [2]
Output: 0

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

子序列宽度之和。

一个序列的 宽度 定义为该序列中最大元素和最小元素的差值。
给你一个整数数组 nums ,返回 nums 的所有非空 子序列 的 宽度之和 。由于答案可能非常大,请返回对 109 + 7 取余 后的结果。
子序列 定义为从一个数组里删除一些(或者不删除)元素,但不改变剩下元素的顺序得到的数组。例如,[3,6,2,7] 就是数组 [0,3,1,6,2,2,7] 的一个子序列。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sum-of-subsequence-widths
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道hard题,但是如果你对数学很敏感,感觉也不是很难。我参考了这个帖子,总结的很详细。

因为题目请你找的是子序列,所以顺序就并不是那么重要。问题的关键在于你如何确定某个数字 nums[i] 是多少个子序列的最大值/最小值,从而确定这个数字 nums[i] 的贡献值。其余部分参见引用贴,写的真的很好。

时间O(nlogn) - sort

空间O(n)

Java实现

 1 class Solution {
 2     public int sumSubseqWidths(int[] nums) {
 3         Arrays.sort(nums);
 4         int MOD = (int) Math.pow(10, 9) + 7;
 5         int len = nums.length;
 6         long res = 0;
 7         long[] pow = new long[len];
 8         pow[0] = 1;
 9         for (int i = 1; i < len; i++) {
10             pow[i] = (pow[i - 1] << 1) % MOD;
11         }
12 
13         for (int i = 0; i < len; i++) {
14             res = (res + (pow[i] - pow[len - i - 1]) * nums[i] % MOD) % MOD;
15         }
16         return (int) res;
17     }
18 }

 

LeetCode 题目总结

标签:891,nums,int,pow,Sum,Subsequence,widths,序列,LeetCode
From: https://www.cnblogs.com/cnoodle/p/16906084.html

相关文章

  • 1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10......
  • 891. 子序列宽度之和
    891.子序列宽度之和题解:对于每个数a而言,其对res的贡献在于a*a作为最大值的次数-a*a作为最小值的次数先将数组排序a作为最大值的次数:a的下标为i,比a小的数......
  • POJ 1845Sumdiv(数论)
    SumdivTimeLimit:1000MS MemoryLimit:30000KTotalSubmissions:20041 Accepted:5060DescriptionConsidertwonaturalnumbersAandB.LetSbethesumof......
  • LeetCode 891 -- 贡献fa
    题目描述子序列宽度之和思路ref代码相似题子数组范围和......
  • 891. 子序列宽度之和 ----- 贡献值法
    一个序列的宽度定义为该序列中最大元素和最小元素的差值。给你一个整数数组nums,返回nums的所有非空子序列的宽度之和。由于答案可能非常大,请返回对109+7取......
  • 38:列表_排序_revered逆序_max_min_sum
    ###列表排序###修改原列表,不建新列表的排序>>>a=[20,10,30,40]>>>id(a)46017416>>>a.sort()#默认是升序排列>>>a[10,20,30,40]>>>a=[10,20,30,40]>>......
  • dp好题CF1183H Subsequences (hard version)
    CF1183HSubsequences(hardversion)考虑dp计算本质不同方案数dp[i][j]表示在前i个字符中,长度为j的本质不同的子串数跑pre[i]表示de字母出现的上一个位置pre数组我属......
  • 【补题】The 2022 SDUT Summer Trials
    比赛链接The2022SDUTSummerTrialsA.Ginger'snumber样例恶臭(恼)签到题简单分解因数就会发现要求的就是\(gcd\),直接算即可,时间复杂度\(\Theta(Tlog(max(x,y)))\)......
  • 《Weakly Sumpervised cell instance segmentation by propagating from detection re
    1.介绍非侵入式的显微镜(共焦距)细胞技术广泛的用于细胞计数和形状分析,不需要对切片进行上色。对单独细胞的分割任务是细胞图像分析中的重要一环。然而,细胞的分割及其困难,......
  • PTA_Maximum Subsequence Sum
    Givenasequenceof K integers{ N1​, N2​,..., NK​ }.Acontinuoussubsequenceisdefinedtobe{ Ni​, Ni+1​,..., Nj​ }where 1≤i≤j≤K.......