当前位置:网站首页>Li Kou 413 division of equal difference sequence dynamic programming
Li Kou 413 division of equal difference sequence dynamic programming
2022-07-19 13:03:00 【Tension √】
Title Description
If a sequence of numbers There are at least three elements , And the difference between any two adjacent elements is the same , The sequence is called the arithmetic sequence .
for example ,[1,3,5,7,9]、[7,7,7,7] and [3,-1,-5,-9] They're all arithmetic sequences .
Give you an array of integers nums , Returns an array of nums The number of subarrays of all arithmetic arrays in .
Subarray Is a continuous sequence in an array .
Their thinking
There is no idea when looking at this topic , Even read the wrong title : The subarray is required to be a continuous sequence in the array , therefore (1,3,5) Not at all (1,2,3,4,5) Subarray . Create an array arr To hold End with each element Subarray , Because the subarray must satisfy three elements , So from the third of the array ( The index for 2) Start searching for .
Let's say the array is (1,2,3,4,5,6,7)
- 3 ---> arr[ 2 ] = 1; (1,2,3)
- 4 ---> arr[ 3 ] = 2; (1,2,3,4); (2,3,4)
- 5 ---> arr[ 4 ] = 3; (1,2,3,4,5); (2,3,4,5); (3,4,5)
- 6 ---> arr[ 5 ] = 4; (1,2,3,4,5,6); (2,3,4,5,6); (3,4,5,6); (4,5,6)
- 7 ---> arr[ 6 ] = 5; (1,2,3,4,5,6,7); (2,3,4,5,6,7); (3,4,5,6,7); (4,5,6,7); (5,6,7)
It is found that the number of subarrays found at the end of each element is exactly one more than the number of subarrays found at the end of the previous element , So the array (1,2,3,4,5,6,7) The number of subarrays of is to find the number of subarrays at the end of each element for summation . Therefore, the number of subarrays of this array is :1+2+3+4+5=15
So the state transition equation is very clear
arr [ i ] = arr [ i -1 ] +1
And then arr Sum the array .
I / O example

Code
class Solution {
public int numberOfArithmeticSlices(int[] nums) {
int len = nums.length;
int[] arr = new int[len];
for(int i = 2; i < len; i++){
if(nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]){
arr[i] = arr[i - 1] + 1;
}
}
int sum = 0;
for(int cnt :arr){
sum += cnt;
}
return sum;
}
}边栏推荐
- Is the career direction of test / development programmers over 35 a turning point in the workplace?
- Binary tree 2-symmetry recursion problem
- Nitrogen heterocyclic molecule modified uio-66-nh2 | polyethyleneimine modified uio-66-nh2| [email protected] @Zif67 nanomaterial
- MatrixCube揭秘 101——MatrixCube的功能与架构
- Interview difficulties: difficulties in implementing distributed session, this is enough!
- Redis logical cluster creation
- 力扣413-等差数列划分——动态规划
- 力扣198-213 打家劫舍Ⅰ、Ⅱ——动态规划
- 506. Relative ranking
- Copper sulfide nanoparticles /zif-8 Composites( [email protected] Support) | uio-66/coso composite | zif-67 nanocrystalline sur
猜你喜欢

Mycat2 builds MySQL master-slave separation

时序逻辑与组合逻辑的reg

10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!

Wrong again, byte alignment and the use of pragma pack

AI is the designer who knows you best? Let users generate digital clothing "by heart" Adidas ozworld

Preparation Notes: Matplotlib learning notes a

Which domestic API tool is better? It turned out to be it

通货收缩的市场何时反转?我们该如何操作?2020-03-13

The difference and use between get request and post request

jvm自学总结
随机推荐
Preparation Notes: Matplotlib learning notes a
How to invest scientifically and rationally when the global financial crisis strikes? 2020-03-17
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
Enrollment publicity - Jiangnan University
VMware imports ova/ovf virtual machine files
MYCAT divides the database and table according to the hash value of the string range
S32K148_ Can drive (bare metal development)
又错了,字节对齐及#pragma pack的使用
MOF customized product | n-k2ti4o9/g-c3n4/uio-66 ternary composite | paper based au-aginse2-zif-8 Nanocomposite
C语言进阶——字符函数和字符串函数
Go unit test
[error record /selectpicker] the display position of dropdown menu is offset
【错误记录/selectpicker】dropdown menu显示位置出现偏移
Flask source code analysis (III): Context
懒到骨子里了,我在CSDN写文章都懒得自己写了,基于selenium模拟写文章
Minimum exchange times
Cadmium sulfide supported mil-125 (TI) | streptavidin (SA) - zirconium porphyrin MOF composite (PCN- [email protected] )|Shell core
Ultrasonic sensor (chx01) learning notes Ⅲ - I2C reading and writing operation
The latest Jilin construction safety officer simulation question bank and answers in 2022
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元