Problem overview
The function clampAll gets a vector of integers nums and two limits, low and high, with low never greater than high. It returns every number moved inside the limits, in the same order. A number below low becomes low, a number above high becomes high, and any other number stays as it is. The lesson does not allow a built-in min, max or clamp function, so the comparing is written by hand.
For {12, -3, 7, 25} with limits 0 and 20 the answer is 12 0 7 20. For {-8, -1, 4} with limits -5 and 5 it is -5 -1 4. For {3, 9, 6} with both limits 6 it is 6 6 6. The tests print a vector as its values separated by spaces.
Approach
The work is split in two. The helper clamp deals with one number: given x and the limits, it returns x moved inside them. clampAll then only has to call the helper on every number and collect the answers. Each function has one job, which makes both easy to read and to test: the helper can be checked on single numbers before the loop exists.
Inside the helper, two if statements are enough. A number below low returns low; a number above high returns high. A number that survives both is between the limits, so it is returned unchanged, and no else is needed because return already leaves the function.
Following the first example
With limits 0 and 20, the helper treats each number of {12, -3, 7, 25} like this:
- 12 is not below 0 and not above 20, so it reaches the last line and stays 12;
- -3 is below 0, so the first
ifreturns 0; - 7 is inside, so it stays 7;
- 25 passes the first test, fails the second by being above 20, and the second
ifreturns 20.
The loop collects these answers in order, which gives 12 0 7 20.
Why a helper at all
The loop could hold both if statements itself, but then it would do two jobs at once: walking the vector and deciding about one number. With a helper, the loop body is a single line and the rule for one number lives in one place. If the rule changes, only the helper changes. The helper can also be tried on its own, with calls like clamp(-3, 0, 20), which returns 0, and clamp(25, 0, 20), which returns 20.
The C++ library has its own function called clamp that does the same job, which is why the lesson names the rule out loud and asks for the comparing to be written by hand. Inside the class, the plain name clamp means the class's own function.
Step by step
- In
clamp, check the lower limit: ifx < low, returnlow. - Check the upper limit: if
x > high, returnhigh. - Return
xfor a number already inside. - In
clampAll, start with an emptyvector<int>namedclamped. - Loop over
numswith anintnamednum. - Call the helper as
clamp(num, low, high)and add the answer to the end ofclampedwithpush_back. - Return
clampedafter the loop.
The solution in C++
class Solution {
public:
int clamp(int x, int low, int high) {
if (x < low) {
return low;
}
if (x > high) {
return high;
}
return x;
}
vector<int> clampAll(vector<int> &nums, int low, int high) {
vector<int> clamped;
for (int num : nums) {
clamped.push_back(clamp(num, low, high));
}
return clamped;
}
};
Notes on the C++:
- Both functions are members of
Solution, soclampAllcallsclampby its plain name. - The vector is passed by reference, so it isn't copied;
clampAllbuilds a new vector and leavesnumsunchanged. - The helper takes its numbers by value. Changing
xinside it would not affect the caller. - The first parameter of the helper is a plain
int, copied on each call. That is the usual choice for small values; references matter for big containers like the vector.
Complexity
Time is O(n) for n numbers: the helper does two comparisons, which take constant time, and it runs once per number. Space is O(n) for the new vector. The helper itself uses no extra memory.
Edge cases and common mistakes
- Equal limits. With
low == high == 6every number is below, above or equal to 6, so every result is 6. - Negative limits. The comparisons work the same way: with limits -5 and 5, the number -8 becomes -5.
- Returning inside the loop. Only the first number would come back.
- Mixing up the limits. Returning
highfor a number belowlowsends -3 to 20 instead of 0. - Testing the limits wrongly. A number equal to a limit is already inside, so
x <= lowwould work too but does extra work: it returnslow, which is the same value. Using<and>keeps the meaning plain. - Writing into
nums. The task asks for a new vector, and buildingclampedkeeps the input as it was.
Testing
{12, -3, 7, 25}, limits 0 and 20, gives12 0 7 20.{-8, -1, 4}, limits -5 and 5, gives-5 -1 4.{3, 9, 6}, limits 6 and 6, gives6 6 6.
