365. Water and Jug Problem (GCD or BFS) TBC

https://leetcode.com/problems/water-and-jug-problem/description/ -- 365

There are two methods to solve this problem : GCD(+ elementary number theory) --> how to get GCF, HCD,  BFS

Currently, I sove this by first method

1. how to compute GCD recursively

//get the GCD of two number s
    int GCD(int a, int b){
        if(a == 0) return b;
        if(b == 0) return a;
        return GCD(b,a%b);
    }

12, 8  -> 8,4 -> 4, 4 -> 4, 0

math solution

Bézout‘s identity (also called Bézout‘s lemma) is a theorem in the elementary theory of numbers:

let a and b be nonzero integers and let d be their greatest common divisor. Then there exist integers x
and y such that ax+by=d

In addition, the greatest common divisor d is the smallest positive integer that can be written as ax + by

every integer of the form ax + by is a multiple of the greatest common divisor d.

If a or b is negative this means we are emptying a jug of x or y gallons respectively.

Similarly if a or b is positive this means we are filling a jug of x or y gallons respectively.

x = 4, y = 6, z = 8.

GCD(4, 6) = 2

8 is multiple of 2

so this input is valid and we have:

-1 * 4 + 6 * 2 = 8

In this case, there is a solution obtained by filling the 6 gallon jug twice and emptying the 4 gallon jug once. (Solution. Fill the 6 gallon jug and empty 4 gallons to the 4 gallon jug. Empty the 4 gallon jug. Now empty the remaining two gallons from the 6 gallon jug to the 4 gallon jug. Next refill the 6 gallon jug. This gives 8 gallons in the end)

code:

class Solution {
   public boolean canMeasureWater(int x, int y, int z) {
       //check the limitiation which x + y < z such as 3,4 , 8: notmeeting the requirement
       if(x+ y < z) return false;
       //check all 0
      System.out.println(GCD(x,y));
        //there is a theory about that
       //ax + by = gcd   z%gcd == 0 Bézout‘s identity
       if(GCD(x,y) == 0) return z==0;
       else return (z%GCD(x,y)==0);
   }

    //get the GCD of two number s
    int GCD(int a, int b){
        if(a == 0) return b;
        if(b == 0) return a;
        return GCD(b,a%b);
    }

}

--------------------------------------------------------------------------------------------------------------------------------

BFS method

原文地址:https://www.cnblogs.com/stiles/p/leetcode365.html

时间: 2024-10-03 23:00:49

365. Water and Jug Problem (GCD or BFS) TBC的相关文章

365. Water and Jug Problem

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must

【leetcode】365. Water and Jug Problem

题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly zlitres using these two jugs. Operations allowed: Fill any of the jugs

Leetcode: Water and Jug Problem &amp;&amp; Summary: GCD求法(辗转相除法 or Euclidean algorithm)

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must

Longge&#39;s problem ( gcd的积性)

Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. "Oh, I

LeetCode-Water and Jug Problem

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must

http://poj.org/problem?id=3278(bfs)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea