+ 1

help to optimize code : Laptop battery

Hello All, Below is my code: https://code.sololearn.com/cCP8a4AEK27v input and output is explained in code. Request your view on the same in terms of optimization. Thanks a lot for your input in advance...! This is too simple code and I am surprised to observe that this code is also not optimized one and have scope of improvement.

9th Jun 2020, 5:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 1
You can use `std::min(value, 100)` to have an upper bound on the input. Or `std::clamp(value, 0, 100)` to bound the input between 0 and 100. Also, the whole thing is basically a fold, which means turning a whole list into a single value one element at a time, so you can do this: std::accumulate(vec.cbegin(), vec.cend(), 50, [] (int a, int b) { return std::clamp(a + b, 0, 100); }); But this shouldn't be a performance improvement, your code looks fine in that regard.
9th Jun 2020, 5:32 PM
Schindlabua
Schindlabua - avatar
+ 1
You can pass vector to getBattery as const reference.
9th Jun 2020, 5:35 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
Optimization is the action of making the best or most effective use of a situation or resource. I think from memory resourse point of view you can use normal array instead of vector.
10th Jun 2020, 4:10 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar