I am trying to program a solution to a coding challenge on https://www.leetcode.com. The entry point to solutions from leetcode are a method of a class named Solution
. They control the method's name, it is different for each problem.
I'm working on a somewhat complicated approach to a problem, and to manage the complexity I write a lot of helper functions. Most of the helper functions will require some of the same variables, such as a vector<int>
I have to search through for something.
I'd like to avoid passing around those variables to every helper function I write. I am trying to do this by making them member variables of Solution
. I have usually done that with primitive variables like int
s, where that doesn't affect space complexity, but I have just tried to do so with a vector, where a copy does affect space complexity when I try to stick to constant-space solutions.
How would I store a vector as a member variable of a class without making a copy of it, and preferably without using pointers? I do not want to change my code to handle pointers.
Here is a minimum example of this for the pretend problem of finding the minimum and maximum of an array. If I give a solution to the problem, they will test my program by passing test cases to Solution().findMaximumAndMinimum(testCase);
.
class Solution {
vector<int> numbers;
pair<int, int> findMaximumAndMinimum(vector<int>& A) {
numbers = A;
return {findMinimum(), findMaximum()};
}
int findMaximum() {
auto maximum = numbers.front();
for (auto number : numbers) {
if (maximum < number) {
maximum = number;
}
}
return maximum;
}
int findMinimum() {
auto minimum = numbers.front();
for (auto number : numbers) {
if (minimum > number) {
minimum = number;
}
}
return minimum;
}
};
What I am trying to change is line 5 of the snippet: numbers = A
. This will cause A
to be copied into numbers
. I don't want a copy to happen: I am competing to make a solution that takes as little time and space as possible, and copying the vector I am searching through will definitely take up a lot of space.
Summary: I am trying to make a solution that looks kinda like above, but for a different and harder problem. The above snippet is deliberately contrived to be simple while displaying what I want. I want to avoid passing around A
explicitly to the other methods of Solution
.
Background:
- I initially tried to turn the vector into a reference member,
vector<int>& numbers
, but I ran into trouble with the constructor. I believe I could not use the default constructor in this case so it was reported as 'deleted', as this answer states. - Leetcode will call my class using a default constructor. I have no control over this.
- Perhaps I can place a class in
Solution
and use this class instead, where I will have control over how objects of the class are constructed, and thus reference member variables won't cause trouble, but I do not want to resort to this. I don't want my program twice indented to start with.
PS: Suggestions about how not to pass around A
by reference to many methods of a class are welcome. Keep in mind the constraints: the entry point to my solution is a method of a class named Solution
, and I cannot control this method's name. Suggestions about different ways to solve the above problem are not welcome because the above is a contrived MWE.