Arithmetic Slices动态规划算法详解

Arithmetic Slices 算术序列 Arithmetic Slices 算术序列 如果一个序列包含三个以上元素,而且连续两个元素之间的差值相等,这样的序列叫做算术序列 例如,这是一些算术序列 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 下面的不是算术序列。 1, 1, 2, 5, 7 输入一个起始坐标为0,长度为N的数组,该数组的一个切片用(P, Q)表示 ,P, Q 满足0 &l... 阅读详情

问题详见: 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, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

      The following sequence is not arithmetic.

1, 1, 2, 5, 7

      A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.
      A slice (P, Q) of array A is called arithmetic if the sequence:
      A[P], A[p + 1], …, A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

      The function should return the number of arithmetic slices in the array A.

Example:

A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

解题思路:

      由于题目中等差子序列的长度至少为3,所以如果所给数组长度小于3那么结果即为0,否则就进行动态规划。动态规划的思路是记录以当前遍历到的数组元素为子序列尾部元素的等差子序列的个数 dp[i] ,而 dp[i]=dp[i−1]+1 。这样遍历完以后将 dp[2] 到 dp[n−1] 加起来即为所求答案。整个算法复杂度为 O(n) 。具体算法如下:

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& A) {
        int n = A.size();
        if (n < 3) return 0;
        vector<int> dp(n, 0); 
        if (A[2]-A[1] == A[1]-A[0]) dp[2] = 1; 
        int result = dp[2];
        for (int i = 3; i < n; ++i) {
            if (A[i]-A[i-1] == A[i-1]-A[i-2])   dp[i] = dp[i-1] + 1;
            result += dp[i]; 
        }
        return result;
    }
};

其提交运行结果如下:
Arithmetic Slices

DM9000有线网卡驱动编写 DM9000有线网卡驱动编写 1.DM9000简介   DM9000 是一款完全集成的、性价比高、引脚数少、带有通用处理器接口的单芯片快速以太网控制器。 自 带一个 10/100M PHY 和 4K 双字的 SRAM ,DM9000A 为适应各种处理器提供了 8 位、16 位数据接口访问 内部存储器,DM9000 拥有自动协商功能,DM9000 特性如下:   1.集成自适应 10/100M 收发器。   2.内置 16k 字节的 SRAM。   3.支持硬件帧校验。   4.兼容 3.3V 和 5.0V 阅读详情

相关推荐

舵机PWM信号转互斥继电器开关信号

-----------------本文由“麦粒电子”撰写,并提供相应产品服务。---------------- 叙述 上次的PWM转继电器开关方案得到大家的不少好评(博客链接),但是发现有些玩家需要一路PWM同时去控制两路继电器开关通断,并且两者是互斥的,好去控制两种不同的负载。所以就继续升级了如下的模块。 正常的控制部件顺序这样:遥控器按钮拨动,遥控器接收机相应通道PWM值变化,PWM转开关模块检测PWM值发生变化从而驱动相应机械继电器触点吸合释放。本模块的两个继电器动作是互斥的,也就是不会同时吸合。

Mai_LiElectronic的博客 1631

LeetCode学习篇八—— 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:

chender_sysu的博客 267

(1)大蚂蚁即时通讯

即时通讯软件BigAnt(大蚂蚁)是杭州艾朴软件推出的一个”企业级”的即时通讯平台。该平台专为企业,其他企事业单位和政府单位而设计,目的就是为了增强内部沟通能力,提高内部员工的工作效率;同时服务器就直接置于单位内部,所有信息由单位自己管理,让单位以更安全、更可伸缩和更易于管理的方式灵活方便地使用协同和通信服务的功能! BigAnt(大蚂蚁)即时通讯软件包括服务器端和客户端。客户端功能包括单位

少安的砖厂 2234

力扣413-等差数列划分-numberOfArithmeticSlices-Java

如果一个数列至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。给你一个整数数组 nums,返回数组 nums 中所有为等差数组的 子数组个数。子数组是数组中的一个连续序列。...

weixin_43770899的博客 473

算法竞赛入门到进阶 --- 完整学习路线(含知识点讲解)

前言: 咳咳,接触算法竞赛也有一年了,却从来都没有进行过一次系统的学习,稀里糊涂的就这么搞了半年,如今,我决定认认真真的进行一次系统的复盘。嗯嗯,先把大体路线列出来,然后往后每天搞几篇,可能大三能完成,可能永远也不能写完这些,我只能慢慢来,一起共勉吧。冲了!(主要是太菜了,不系统学习下,怕是铜牌都拿不到呀!) 算法竞赛入门到进阶一、sort函数自定义排序二、c++容器的使用1.vector动态数组2.stack栈3.queue队列4.list链表5.set集合6.map映射三、二进制子集生成四、bfs.

qq_43514659的博客 2万+

安装Anaconda和Labelme的教程

1 登录Anaconda官网,选择Download https://www.anaconda.com/distribution/ 2 选择你想下载的Python版本,选择你的操作系统和位数,然后点击Download按钮就开始下载了 3 开始安装 下面两个框框都勾选 点击next,等待安装成功 打开Anaconda Navigator 点击红色圈圈的按钮 在cmd中输入 (1)创建虚拟环境 conda create -n labelme python=3.6 (2)激活虚拟环境 conda activa

qq_42178122的博客 3810

413. Arithmetic Slices

题目: An integer array is called arithmetic if it consists ofat least three elementsand 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 a...

weixin_49991368的博客 164

深入AOSP构建系统:从BUILD_FINGERPRINT的生成逻辑,看如何定制你的Android设备标识

本文深入解析AOSP构建系统中的设备指纹生成机制,详细介绍了BUILD_FINGERPRINT的生成逻辑及其在Android设备中的关键作用。通过分析多个指纹属性的区别与联系,以及如何定制设备标识,帮助开发者更好地理解和掌握Android系统构建过程。

weixin_27017211的博客 217

Arithmetic Slices

1. 解析 题目大意,计算满足以下条件的子序列个数: ①子序列连续且至少有3个元素 ②子序列相邻两个元素之间的间距相等(即等差序列) 2. 分析 整体上这道题并不是很难,只是需要我们找一下规律。例如:[1, 2, 3, 5, 7, 9, 11] 很容易看出存在两个等差序列: 1, 2, 3 间距为1 3, 5, 7, 9, 11 间距为2 对于n个元素的...

Czyaun的博客 299

真题库系列之 CCF GESP 2025年12月认证 Python 1级真题含正确答案与解析

学生通过计算机基础知识的学习,了解计算机的构成与操作,以及计算机的发 展历程。通过编程基础知识以及语句的掌握,可以独立完成简单功能的顺序结构、 分支结构、循环结构的程序。

iCloudEnd的博客 317

杰理AC696N系列芯片外部中断使用教程

本教程详细介绍了杰理AC696N芯片的GPIO引脚中断唤醒功能实现方法。主要内容包括: 功能特点: 支持任意GPIO引脚配置为中断源 可灵活选择上升沿或下降沿触发 提供自定义中断回调函数接口 核心实现: 通过port_wkup_interrupt_init()函数配置GPIO属性 使用request_irq()注册中断服务程序 提供中断使能/关闭接口 注意事项: 此功能在低功耗模式下无效 需注意引脚冲突和中断优先级设置 建议在回调函数中实现简洁的逻辑 应用场景: 按键检测 外部信号监测 事件触发等 本方案提

qq_42014563的博客 944

[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

Description 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 arithmeti...

Learning from the mistakes 288

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Week 8 - 338.Counting Bits & 413. Arithmetic Slices

338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an a...

aochengjizong4330的博客 125

第17周Arithmetic Slices

Arithmetic SlicesLeetcode algorithms problem 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 conse

m0_37856637的博客 245

446. Arithmetic Slices II - Subsequence

446. Arithmetic Slices II - Subsequence方法1: dynamic programming易错点:Complexity difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1, 3, 5, 7, 9 7,...

wilzxu的博客 222

LeetCode413 Arithmetic Slices| 动态规划

题目描述 Arithmetic Slices QuestionEditorial Solution My Submissions Total Accepted: 4238 Total Submissions: 7915 Difficulty: Medium Contributors: XiangyuLi926 A sequence of number is called arithm

tbsjianjian的博客 464

动态规划——413. Arithmetic Slices[Medium]

题目描述 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence:

xiegr的博客 408

Leetcode之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...

qq_35455503的博客 195

LeetCode#413 Arithmetic Slices

[Description]: 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 ar

eleven_mie11的博客 243

【Leetcode】413. 算数切片(Arithmetic Slices)

Leetcode - 413 Arithmetic Slices (Medium) 题目描述:给定一个数组,求数组中长度在 3 个以上(包含三个)的等差数列的数量。 A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself. 数组 等差数列...

厉兵秣码 217

leetcode 413 Arithmetic Slices

leetcode 413 Arithmetic Slices 给定一列整数,求含有的算数片个数。 算数片定义:至少包含3个整数,每个相邻整数的差值相等。eg: 1,2,3 算法 同样的,使用动态规划求解。找到状态n和状态n-1的关系。在这里就是找到n个整数包含的算数片的个数和前n-1个整数包含的算术片的关系。很明显,dp(n) = dp(n-1) +1dp代表前n个整数包含的算数片

SSDirector的博客 377
上一篇: Find Largest Value in Each Tree Row宽度优先遍历算法详解
下一篇: The Skyline Problem轮廓线问题算法详解
Poison520
博客等级 码龄10年 1粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值