wy的leetcode刷题记录_Day45

声明

本文章的所有题目信息都来源于leetcode
如有侵权请联系我删掉!
时间:2022-11-18

前言

目录

  • wy的leetcode刷题记录_Day45
    • 声明
    • 前言
    • 891. 子序列宽度之和
      • 题目介绍
      • 思路
      • 代码
      • 收获

891. 子序列宽度之和

今天的每日一题是:891. 子序列宽度之和

题目介绍

一个序列的 宽度 定义为该序列中最大元素和最小元素的差值。

给你一个整数数组 nums ,返回 nums 的所有非空 子序列 的 宽度之和 。由于答案可能非常大,请返回对 109 + 7 取余 后的结果。

子序列 定义为从一个数组里删除一些(或者不删除)元素,但不改变剩下元素的顺序得到的数组。例如,[3,6,2,7] 就是数组 [0,3,1,6,2,2,7] 的一个子序列。

示例 1:
输入:nums = [2,1,3]
输出:6
解释:子序列为 [1], [2], [3], [2,1], [2,3],
[1,3], [2,1,3] 。 相应的宽度是 0, 0, 0, 1, 1, 2, 2 。 宽度之和是 6 。

示例 2:
输入:nums = [2
] 输出:0

思路

数学方法:贡献度。首先很多人估计会认为这个子序列跟原序列的顺序有关,其实不然,仔细看题目,题目要求的是宽度,也就是最大值与最小值之和,其实他与原序列的顺序无关,只是这个宽度的覆盖范围也就是贡献度有关,于是我们将其排序,计算其贡献度(最大值贡献度和最小值贡献度求得宽度贡献度)然后累加即可。

代码

class Solution {public:int sumSubseqWidths(vector<int>& nums) {sort(nums.begin(), nums.end());long long res = 0, mod = 1e9 + 7;long long x = nums[0], y = 2;for (int j = 1; j < nums.size(); j++) {res = (res + nums[j] * (y - 1) - x) % mod;x = (x * 2 + nums[j]) % mod;y = y * 2 % mod;}return (res + mod) % mod;}
};

收获

这题对于数学思维结构理解要求很高

wy的leetcode刷题记录_Day45相关推荐

  1. wy的leetcode刷题记录_Day15

    wy的leetcode刷题记录_Day15 目录 wy的leetcode刷题记录_Day15 2441. 与对应负数同时存在的最大正整数 题目介绍 思路 代码 收获 2442. 反转之后不同整数的数目 ...

  2. wy的leetcode刷题记录_Day66

    wy的leetcode刷题记录_Day66 声明 本文章的所有题目信息都来源于leetcode 如有侵权请联系我删掉! 时间:2023-4-10 前言 目录 wy的leetcode刷题记录_Day66 ...

  3. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  4. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  5. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  6. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  7. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  8. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  9. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

最新文章

  1. python时间变量_python的对象 变量
  2. 5.html基础标签:块级+行级元素+特殊字符+嵌套规则
  3. Java项目:在线考试系统(java+springboot+vue+jsp+mysql+maven)
  4. 习题4-11 兔子繁衍问题 (15 分)
  5. jsonp 200 进去error_同样是专科,400分考上的和200分就能进的,差距其实挺明显
  6. Modbus RTU 通信工具设计
  7. warning: function declaration isn’t a prototype解决方法
  8. 数据库-mysql基础操作之输入查询
  9. python异常处理的语法格式_Python异常处理
  10. 工业界求解NER问题的12条黄金法则
  11. 转)SQL 优化原则
  12. win10 + python3.6.1 + tensorflow1.10 + cuda9.0 + cudnn7.2
  13. TensorFlow经典入门示例MNIST(识别手写的数字图片)
  14. 通信网中的数据报子网和虚电路子网
  15. 啊哈C语言 第8章 游戏时间到了(第29讲)
  16. LU分解算法(串行、并行)
  17. PR(Adobe Premiere Pro)软件基础知识
  18. js:腾讯云播放器无法自动播放的问题
  19. 5G工业无线RTU TG511功能配置
  20. 大数据面试3分钟自我介绍_快手大数据岗位招聘面试题分享

热门文章

  1. Android TV框架 TIF(Android TV Input Framework)入门实践
  2. 通过中央气象台做天气预报
  3. Map根据Key值进行排序(升序加降序)
  4. Linux学习笔记11——远程拷贝命令scp
  5. U盘安装ubuntu20.04 Linux系统分区方案 Invalid Partition Table
  6. 微电网两阶段鲁棒优化问题(Matlab代码实现)
  7. 解决打开Android sdk manger D:\BaiduNetdiskDownload\开发工具\android' 不是内部或外部命令,也不是可运行的程序问题
  8. AE基础教程(14)——第14章 塌陷
  9. centos7重启或关机卡死
  10. painter qt 好恶心_Qt之有关于Painter的理解