-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater-and-jug-problem.cc
38 lines (33 loc) · 999 Bytes
/
water-and-jug-problem.cc
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
#include "./leetcode.h"
using namespace std;
class Solution {
public:
bool canMeasureWater(const uint32_t size_1, const uint32_t size_2, const uint32_t target) {
if (target == 0) return true;
if (size_1 + size_2 < target) return false;
if (size_1 == 0) return target == size_2;
if (size_2 == 0) return target == size_1;
return target % gcd(size_1, size_2) == 0;
}
int gcd(uint32_t x, uint32_t y) {
if (x == y) return x;
uint32_t val = x > y ? (x - y) : (y - x);
while (val != y) {
if (y > val) {
x = y;
y = val;
} else {
x = val;
}
val = x - y;
}
return val;
}
};
int main(int argc, char const *argv[]) {
Solution solution;
assert(solution.canMeasureWater(3, 5, 4));
assert(!solution.canMeasureWater(2, 6, 5));
assert(!solution.canMeasureWater(1, 1, 12));
cout << "OK" << endl;
}