File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 287
287
355|[ Design Twitter] ( ./0355-design-twitter.js ) |Medium|
288
288
357|[ Count Numbers with Unique Digits] ( ./0357-count-numbers-with-unique-digits.js ) |Medium|
289
289
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|
290
291
367|[ Valid Perfect Square] ( ./0367-valid-perfect-square.js ) |Easy|
291
292
371|[ Sum of Two Integers] ( ./0371-sum-of-two-integers.js ) |Medium|
292
293
372|[ Super Pow] ( ./0372-super-pow.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments