Title Description
The sum of the array monotone and the F (i) values for all element I are now defined. The f (i) function here is defined as the sum of the numbers that are less than or equal to the left side of element I (excluding itself). Please design an efficient algorithm to calculate the monotony of the array.
Given an array of size nfor the given array, return the monotone of the array. Ensure that the array size is less than or equal to 500, while guaranteeing monotonic and not exceeding the int range.
Test examples:
[1,3,5,2,4,6],6
Returns: 27
Solution 1: (violence)
classMonosum { Public: intCalcmonosum (vector<int> A,intN) {//Write code here intsum =0; for(inti =0; I < n; ++i) {sum+=F (A, I); } returnsum; } intF (vector<int> A,intk) {intresult =0; for(inti =0; I < K; ++i) {if(A[i] <=A[k]) {Result+=A[i]; } } returnresult; }};
Solution 2:
Array single Harmonic