Original title Address
Leetcode also have this problem, directly sweep over the line, even the array is not open, feel like a certain kind of moving into the thought in the inside, to not be a moving title
Code:
1#include <cmath>2#include <cstdio>3#include <vector>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8 intMain () {9 /*Enter your code here. Read input from STDIN. Print output to STDOUT*/ Ten intN; One intCurr_score, Prev_score, curr_cnt; A intdir, Len; - intres =0; - thePrev_score =-1; -curr_cnt =0; -DIR =1; -scanf"%d", &n); + for(inti =0; I < n; i++) { -scanf"%d", &curr_score); + if(Curr_score >Prev_score) { A if(Dir <0) { atRes + = (1-curr_cnt) * (Len + (int) (curr_cnt <=0)); -curr_cnt =1; - } -Curr_cnt + =1; -Res + =curr_cnt; -DIR =1; in}Else if(Curr_score <Prev_score) { - if(dir >=0) toLen =0; +CURR_CNT-=1; -Res + =curr_cnt; theDIR =-1; *Len + =1; $}Else {Panax Notoginseng if(Dir <0) -Res + = (1-curr_cnt) * (Len + (int) (curr_cnt <=0)); thecurr_cnt =1; +Res + =curr_cnt; ADIR =0; the } +Prev_score =Curr_score; - } $ if(Dir <0) $Res + = (1-curr_cnt) * (Len + (int) (curr_cnt <=0)); - -cout << Res <<Endl; the - return 0;Wuyi}
hackerrank# Candies