Skip to content

Commit c167e72

Browse files
committed
Add solution #365
1 parent 495e466 commit c167e72

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
355|[Design Twitter](./0355-design-twitter.js)|Medium|
288288
357|[Count Numbers with Unique Digits](./0357-count-numbers-with-unique-digits.js)|Medium|
289289
363|[Max Sum of Rectangle No Larger Than K](./0363-max-sum-of-rectangle-no-larger-than-k.js)|Hard|
290+
365|[Water and Jug Problem](./0365-water-and-jug-problem.js)|Medium|
290291
367|[Valid Perfect Square](./0367-valid-perfect-square.js)|Easy|
291292
371|[Sum of Two Integers](./0371-sum-of-two-integers.js)|Medium|
292293
372|[Super Pow](./0372-super-pow.js)|Medium|

Diff for: solutions/0365-water-and-jug-problem.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 365. Water and Jug Problem
3+
* https://leetcode.com/problems/water-and-jug-problem/
4+
* Difficulty: Medium
5+
*
6+
* You are given two jugs with capacities x liters and y liters. You have an infinite water
7+
* supply. Return whether the total amount of water in both jugs may reach target using the
8+
* following operations:
9+
* - Fill either jug completely with water.
10+
* - Completely empty either jug.
11+
* - Pour water from one jug into another until the receiving jug is full, or the
12+
* transferring jug is empty.
13+
*/
14+
15+
/**
16+
* @param {number} x
17+
* @param {number} y
18+
* @param {number} target
19+
* @return {boolean}
20+
*/
21+
var canMeasureWater = function(x, y, target) {
22+
if (target > x + y) return false;
23+
if (target === 0) return true;
24+
if (x === 0) return target === y;
25+
if (y === 0) return target === x;
26+
return target % gcd(x, y) === 0;
27+
28+
function gcd(a, b) {
29+
return b === 0 ? a : gcd(b, a % b);
30+
}
31+
};

0 commit comments

Comments
 (0)