+ 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.
3 RĂ©ponses
+ 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.
+ 1
You can pass vector to getBattery as const reference.
+ 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.