-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproblem2_group_integers.cpp
More file actions
68 lines (58 loc) · 2.46 KB
/
problem2_group_integers.cpp
File metadata and controls
68 lines (58 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
UCLA ACM ICPC: Interview Track Problem Solutions
Disclaimer: This is not the only valid solution and we do not claim our solutions
to be optimal in terms of runtime or memory usage, we are merely giving example
solutions to questions for learning purposes only
Group Integers
*/
// A vector is an STL data structure
// that is basically like an array,
// but you have access to the size()
// function!
// Given a vector of integers, return whether is it possible to
// choose a group of some of the integers such that the group sums
// to the given target
class Solution {
public:
// To simplify the problem, we will create a helper function that
// returns whether it is possible to choose a group of some of the
// integers at or after a given index such that the group sums to
// the given target.
bool groupSumHelper(vector<int> nums, int target, int index) {
// If target == 0, we can choose no elements, which by default
// sums up to 0.
if(target == 0) {
return true;
}
// when the given index is out of bounds, we can't choose any
// elements that add up to target (since target != 0), so we
// return false. We are assuming that the index will go out
// of bounds in the positive direction.
else if(index >= nums.size()) {
return false;
}
// If the nums[index] (the first element starting at index) equals
// the target, then we can choose just nums[index] to sum up to the
// target. Therefore, we return true.
else if(nums[index] == target) {
return true;
}
// In all other cases, we have to check the rest of the vector.
// We have two choices.
else {
return
// If nums[index] is in the group that sums
// up to the target, we have to check if there is a group in the
// rest of the vector that sums up to target - nums[index].
groupSumHelper(nums, target - nums[index], index + 1) ||
// If nums[index] is not in the group, then we just check if there
// if a group in the rest of the vector that sums up to target.
groupSumHelper(nums, target, index + 1);
}
}
// Now, all we need to do is to call groupSumHelper with index 0
// to get our answer.
bool groupSum(vector<int> nums, int target) {
return groupSumHelper(nums, target, 0);
}
};