Array Wrapper Problem
#2695
Description
LeetCode Problem 2695.
Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:
- When two instances of this class are added together with the
+operator, the resulting value is the sum of all the elements in both arrays. - When the
String()function is called on the instance, it will return a comma separated string surrounded by brackets. For example,[1,2,3].
Example 1:
Input: nums = [[1,2],[3,4]], operation = "Add"
Output: 10
Explanation:
const obj1 = new ArrayWrapper([1,2]);
const obj2 = new ArrayWrapper([3,4]);
obj1 + obj2; // 10
Example 2:
Input: nums = [[23,98,42,70]], operation = "String"
Output: "[23,98,42,70]"
Explanation:
const obj = new ArrayWrapper([23,98,42,70]);
String(obj); // "[23,98,42,70]"
Example 3:
Input: nums = [[],[]], operation = "Add"
Output: 0
Explanation:
const obj1 = new ArrayWrapper([]);
const obj2 = new ArrayWrapper([]);
obj1 + obj2; // 0
Constraints:
0 <= nums.length <= 10000 <= nums[i] <= 1000Note: nums is the array passed to the constructor
Solution
Approach: valueOf and toString
JavaScript decides how to turn an object into a primitive by calling its valueOf (for arithmetic such as + between two objects) or toString (for String(obj)). So define both on the class:
valueOfreturns the sum of the array, soa + bbecomes a number plus a number.toStringreturns the elements joined by commas inside brackets.
The sum is computed on demand, and an empty array sums to 0.
Complexity: Time O(n) per valueOf or toString, space O(1) besides the stored array.
var ArrayWrapper = function (nums) {
this.nums = nums;
};
// Used by `+`: obj1 + obj2 adds the two sums.
ArrayWrapper.prototype.valueOf = function () {
return this.nums.reduce((sum, x) => sum + x, 0);
};
// Used by String(obj).
ArrayWrapper.prototype.toString = function () {
return '[' + this.nums.join(',') + ']';
};