135 need to use prefix array and suffix array method to solve the probelm
This commit is contained in:
		
							
								
								
									
										90
									
								
								135-231130-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								135-231130-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | #include<stdcpp.h> | ||||||
|  | using namespace std; | ||||||
|  |  | ||||||
|  | class Solution{ | ||||||
|  | public: | ||||||
|  |     int candy(vector<int>& ratings){ | ||||||
|  |         const int length = ratings.size(); | ||||||
|  |         const int size = 2e4+10; | ||||||
|  |         vector<int>pref(length, 1); | ||||||
|  |         vector<int>suff(length, 1); | ||||||
|  |         for(int i = 1 ; i < length ;i++){ | ||||||
|  |             if(ratings[i] > ratings[i-1]) | ||||||
|  |                 pref[i] = pref[i-1] + 1; | ||||||
|  |         } | ||||||
|  |         for(int i = length - 2 ; i >= 0 ; i--){ | ||||||
|  |             if(ratings[i] > ratings[i+1]) | ||||||
|  |                 suff[i] = suff[i+1] + 1; | ||||||
|  |         } | ||||||
|  |         int sum = 0; | ||||||
|  |         for(int i = 0 ; i < length ;i++) | ||||||
|  |         sum+=max(pref[i],suff[i]); | ||||||
|  |         return sum; | ||||||
|  |     } | ||||||
|  |     int candy1(vector<int>& ratings){ | ||||||
|  |         const int length = ratings.size(); | ||||||
|  |         const int size = 2e4+10; | ||||||
|  |         int comparision[size] = {}; | ||||||
|  |         if(ratings[1] > ratings[0]) | ||||||
|  |             comparision[0] = 1; | ||||||
|  |         else if (ratings[1] == ratings[0]) | ||||||
|  |             comparision[0] = 0; | ||||||
|  |         else | ||||||
|  |             comparision[0] = -1; | ||||||
|  |         int rlt[size] = {}; | ||||||
|  |         for(int i = 1 ; i < length ; i++){ | ||||||
|  |             if(ratings[i] < ratings[i-1]){ | ||||||
|  |                 comparision[i] = -1; | ||||||
|  |                 if(rlt[i-1] == 0) { | ||||||
|  |                     rlt[i] = 2; | ||||||
|  |                     // rlt[i-1] = 1; | ||||||
|  |                 } | ||||||
|  |                 else    rlt[i] = rlt[i-1] + 1; | ||||||
|  |             } | ||||||
|  |             else if(ratings[i] == ratings[i - 1]){ | ||||||
|  |                 comparision[i] = 0; | ||||||
|  |             } | ||||||
|  |             else{ | ||||||
|  |                 comparision[i] = 1; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         for(int i = 1 ; i < length ; i++){ | ||||||
|  |             if(rlt[i] == 0){ | ||||||
|  |                 if(comparision[i] == 0) | ||||||
|  |                     rlt[i] = 1; | ||||||
|  |                 else { | ||||||
|  |                     // if(comparision[i-1] == -1) | ||||||
|  |                     //     rlt[i] = 2; | ||||||
|  |                     // else | ||||||
|  |                     //     rlt[i] = rlt[i-1] + 1; | ||||||
|  |                     if(comparision[i-1] == 1 && rlt[i-1] == 0) | ||||||
|  |                         rlt[i] = 2; | ||||||
|  |                     else if(comparision[i-1] == 1 && rlt[i-1]) | ||||||
|  |                         rlt[i] = rlt[i-1] + 1; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         for(int i = 0 ; i <length;i++) | ||||||
|  |             if(!rlt[i]) rlt[i] = 1; | ||||||
|  |         int sum = 0; | ||||||
|  |         for(int i = 1; i < length ; i++){ | ||||||
|  |             sum += rlt[i]; | ||||||
|  |             cout<<rlt[i]<<' '; | ||||||
|  |         } | ||||||
|  |         cout<<endl; | ||||||
|  |         sum+=1; | ||||||
|  |         return sum; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | int main(){ | ||||||
|  |     Solution sol; | ||||||
|  |     vector<int> ex1 = {1,0,2}; | ||||||
|  |     cout<<sol.candy(ex1)<<endl; | ||||||
|  |     vector<int> ex2 = {1,2,2}; | ||||||
|  |     cout<<sol.candy(ex2)<<endl; | ||||||
|  |     vector<int> ex3 = {4,1,4,3,2,1,5,8,7,5,2}; | ||||||
|  |     cout<<sol.candy(ex3)<<endl; | ||||||
|  |     vector<int> ex4 = {1,3,4,5,2}; | ||||||
|  |     cout<<sol.candy(ex4)<<endl; | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user