leetcode 413 Arithmetic Slices(等差数列划分)

模型可解释性分析:基于SHAP的深度探索 SHAP模型可解释性分析摘要 本文系统性地介绍了SHAP(SHapley Additive exPlanations)方法在机器学习模型可解释性分析中的应用。SHAP基于博弈论的Shapley值理论,为特征重要性评估提供了统一框架。文章详细阐述了四种核心算法:Kernel SHAP(模型无关方法)、Tree SHAP(针对树模型的高效算法)、Deep SHAP(适用于深度学习)和Linear SHAP(线性模型专用)。通过Python实现展示了从数据准备到SHAP分析的全流程,包括摘要图、依赖图等可视化技术 阅读详情
  1. Arithmetic Slices
    Medium

An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.

Given an integer array nums, return the number of arithmetic subarrays of nums.

A subarray is a contiguous subsequence of the array.

Example 1:

Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.

Example 2:

Input: nums = [1]
Output: 0

Constraints:

1 <= nums.length <= 5000
-1000 <= nums[i] <= 1000

给出nums数组,判断里面有多少个等差数列子数组。
等差数列最短长度为3, 满足nums[i] - nums[i-1] = nums[i-1] - nums[i-2]。
子数组应是连续的。

思路:
DP
定义和nums等长的dp数组,dp[i]表示以nums[i]结尾,有多少个等差数列子数组。
下面看dp[i]的递推。

比如[1,2,3,4]
等差数列至少要3个元素,所以从“3”开始看,它满足nums[i] - nums[i-1] = nums[i-1] - nums[i-2],所以dp[2] = 1。
然后到"4"的位置,仍然满足nums[i] - nums[i-1] = nums[i-1] - nums[i-2],
这时[2,3,4]就是一个等差数列,

除此之外,4还是前面等差子数组[1,2,3]的延伸,它们一起构成了一个新的子数组[1,2,3,4], 假设现在是位置i, 那么这个以nums[i]为结尾的延伸子数组的个数 和 前面的个数dp[i-1]是一样的。

延伸数组的个数dp[i-1] 和当前子数组([2,3,4])的个数相结合,得到dp[i] = dp[i-1] + 1

如果在当前位置i不满足等差数列的条件,那么dp[i] = 0

返回的结果是以每个nums[i]为结尾的等差子数组的和,即对所有dp[i]求和。

    public int numberOfArithmeticSlices(int[] nums) {
        if(nums.length <= 2) return 0;
        int result = 0;
        int n = nums.length;
        int[] dp = new int[n];
        
        for(int i = 2; i < n; i++) {
            if(nums[i] - nums[i-1] == nums[i-1] - nums[i-2]) {
                dp[i] = dp[i-1] + 1;
                result += dp[i];
            }
        }
        return result;
    }

可以看到dp[i]只和它前面一个元素dp[i-1]有关系,所以不需要定义和nums一样长的数组,一个数字就够了。

    public int numberOfArithmeticSlices(int[] nums) {
        if(nums.length <= 2) return 0;
        int result = 0;
        int n = nums.length;
        //int[] dp = new int[n];
        int dp = 0;
        
        for(int i = 2; i < n; i++) {
            if(nums[i] - nums[i-1] == nums[i-1] - nums[i-2]) {
                //dp[i] = dp[i-1] + 1;
                //result += dp[i];
                dp ++;
                result += dp;
            } else {
                dp = 0;
            }
        }
        return result;
    }
多模态论文笔记——BLIP 本文详细介绍这几年AIGC火爆的隐藏功臣,多模态模型:BLIP。 阅读详情

相关推荐

自动驾驶仿真(五)—— 基于Carsim、Prescan、Simulink的联合仿真

自动驾驶仿真五——基于Carsim、Prescan、Simulink的联合仿真1. 联合仿真流程2. CarSim联合仿真配置要点3. Prescan联合仿真配置要点4. Carsim、Prescan、Simulink联合仿真结果5. 参考学习的书目教材 博主前面的博文已经介绍了Carsim和Prescan各自与Matlab/Simulink的相关联合仿真应用实例,并且论述过几款软件各自的优势及用法,CarSim适合动力学模型的建立,PreScan适合行车场景的搭建,Simulink适合控制算法的设计及信号

zhudj08的博客 1万+

Leetcode 413.等差数列划分(Arithmetic Slices)

Leetcode 413.等差数列划分 1 题目描述(Leetcode题目链接)   如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 以下数列不是等差数列。 1, 1, 2, 5, 7   数组 A 包含 N 个数,且索引从0开始。数组 A 的一个子数组划...

qq_39378221的博客 196

LeRobot SO-ARM100 学习笔记(2) diffusion module

对 lerobot 中的 diffusion policy 进行基本的代码阅读

mikhailbran的博客 1586

leetcode 413. Arithmetic Slices 等差数列划分

因为是求等差数列,可以想到满足num[i]-num[i-1]=num[i-1]-num[i-2]

ln_ydc的专栏 250

IT运维必备 开源Urbackup 备份系统部署教程 实战操作

手动添加ip方式检查是否在线 ,linux需要按上述步骤6配置server端地址 不然一直不会进去状态栏。安装依赖sudo apt install -y libcrypto++-dev。补充客户端手动触发备份命令 (一般在服务端操作即可,特殊情况客户端进行操作)修改ubuntu主机名 建议修改为ip 方便上线以ip命名上线。默认客户端安装完毕大概几分钟之内服务端会自动上线。配置客户端指向server端地址。(2)ubuntu 客户端设置。新起一个远程窗口查看配置状态,配置客户端备份文件目录。

qq_34981755的博客 1278

切比雪夫(最小区域法)直线拟合算法

切比雪夫,最小区域法直线拟合

闪电彬彬的博客 3961

【LeetCode】413. Arithmetic Slices 等差数列划分

【LeetCode】413. Arithmetic Slices 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/arithmetic-slices/description/ 题目描述: A sequence of number is called arithmetic if it consist...

负雪明烛 2008

R数据分析:潜在剖面分析LPA的做法与解释

Latent Profile Analysis (LPA) tries to identify clusters of individuals (i.e., latent profiles) based on responses to a series of continuous variables (i.e., indicators). LPA assumes that there are unobserved latent profiles that generate patterns of resp.

Codewar的博客 1万+

leetcode 413. 等差数列划分(Arithmetic Slices)

目录 题目描述: 示例: 解法: 题目描述: 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 以下数列不是等差数列。 1, 1, 2, 5, 7...

weixin_30675967的博客 100

笔试题-2023-思特威-数字电路设计(CIS)【纯净题目版】

10 笔试题-2023-思特威-数字电路设计(CIS)【纯净题目版】

w0shishabi的博客 1471

TC3XX - MCAL知识点(二十四):UART MCAL配置及代码实战

UART驱动程序负责按照UART协议提供通信服务。ASCLIN模块为异步通信提供硬件支持,实现UART协议。UART驱动程序提供了配置、初始化、数据传输、接收等功能,还提供了中止传输和中止接收 abort等可选特性。软硬件匹配关系如下:UART驱动程序使用ASCLIN进行数据的传输和接收。驱动程序使用的关键硬件功能特性是:1、全双工异步工作模式2、支持半双工工作模式3、16字节的TXFIFO4、16字节的RXFIFO5、2 ~ 16位数据帧6、校验位生成/检查7、一个或两个停止位。

梦想技术家 1831

[Swift]LeetCode413. 等差数列划分 | Arithmetic Slices

原文地址:https://www.cnblogs.com/strengthen/p/10330630.html A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements...

weixin_30685047的博客 174

《MySQL 入门教程》第 03 篇 管理数据库

本篇主要介绍 MySQL 数据库的创建、查看、选择和删除操作,包括使用 mysql 命令行和 MySQL Workbench 图形工具两种方式。

Tony.Dong的专栏 3520

[leetcode]413. Arithmetic Slices 等差数列划分

题目链接:https://leetcode.com/problems/arithmetic-slices/ A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive element

xiaocong1990的博客 379

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

(Java) LeetCode 413. Arithmetic Slices —— 等差数列划分

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequ...

anzeng0652的博客 238

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode 413. Arithmetic Slices(等差数列划分)

一个等差数列至少包含三个元素 A = [0, 1, 2, 3, 4] return: 6, for 3 arithmetic slices in A: [0, 1, 2], [1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3, 4], [ 1, 2, 3, 4], [2, 3, 4] dp[i] 表示以 A[i] 为结尾的等差递增子区间的个数。 当 A[i] - ...

肉丸不肉 437

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Leetcode-413 Arithmetic Slices(等差数列划分)

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int f(int len) 6 { 7 int sum = 0; 8 len -= 2; 9 ...

weixin_30512043的博客 83

[leetcode]413. 等差数列划分(Arithmetic Slices )C++代码实现

1,题目描述 2,题目分析 开始想到的是如果A[i]到A[j]为一个等差数列,即dp[i][j] = 1,那么如果A[j+1]-A[j]等于该等差数列的差,那么A[i]到A[j+1]也为一个等差数列,但是运行的时候超出内存限制了。 3,代码实现 /* * @lc app=leetcode.cn id=413 lang=cpp * * [413] 等差数列划分 */ cla...

博客 390

leetcode 413. Arithmetic Slices | 413. 等差数列划分(Java)

题目 https://leetcode.com/problems/arithmetic-slices/ 题解 一次遍历搞定,直接看草稿。 class Solution { public int numberOfArithmeticSlices(int[] nums) { if (nums.length < 3) return 0; int sum = 0; int cnt = 0; int dif = nums[1] - nu

寒泉 4万+

LeetCode——413. 等差数列划分(Arithmetic Slices)[中等]——分析及代码(Java)

LeetCode——413. 等差数列划分[Arithmetic Slices][中等]——分析及代码[Java]一、题目二、分析及代码1. 直接计数(1)思路(2)代码(3)结果三、其他 一、题目 如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。 例如,[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。 给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。 子数组 是数组中的一个连续序列。 示例

江南土豆的博客 321

Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices)

Leetcode之动态规划(DP)专题-413. 等差数列划分(Arithmetic Slices) 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 以下数列不是等差数列。 1, 1, 2, 5, 7 数组 A 包含 ...

denghan1065的博客 270

[leetcode]413.C++实现 等差数列划分(Arithmetic Slices)

(一)题目描述 (二)思想方法 题目理解: (1)输入数组不一定是等差数组 (2)P+1<Q,说明数组至少包含三个元素, (3)等差子数组按原数组顺序 方法: {1,2,3} {1,2,3} ...

guaiguaitinghua的博客 584

java等差数列排序_(Java) LeetCode 413. Arithmetic Slices —— 等差数列划分

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1,...

weixin_39849153的博客 244

LeetCode 413.Arithmetic Slices 解题报告

LeetCode 413. Arithmetic Slices 解题报告

骆小坑的填坑之旅 5359

[LeetCode] 413. Arithmetic Slices

原题链接: https://leetcode.com/problems/arithmetic-slices/ 1. 题目介绍 A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive ele...

Ber03的博客 284

LeetCode 413. Arithmetic Slices 题解(C++)

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

prayyojay的博客 1007
上一篇: ubuntu 20.04 pangolin安装 cmakeLists
下一篇: ceres库 拟合曲线 (含高斯牛顿法)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值