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 have z liters of water contained within one or both buckets by the end.

Operations allowed:

  • Fill any of the jugs completely with water.
  • Empty any of the jugs.
  • Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.

Example 1: (From the famous "Die Hard" example)

Input: x = 3, y = 5, z = 4
Output: True

Example 2:

Input: x = 2, y = 6, z = 5
Output: False

Credits:
Special thanks to @vinod23 for adding this problem and creating all test cases.

Analysis:

It is a number theory problem.

https://discuss.leetcode.com/topic/49238/math-solution-java-solution/2

Solution:

public class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        if (x+y < z) return false;

        if (x==z || y==z || x+y==z) return true;

        return z%GCD(x,y)==0;

    }

    public int GCD(int x, int y){
        while (x%y!=0){
            int temp = x;
            x = y;
            y = temp%y;
        }
        return y;
    }
}
时间: 2024-10-06 17:31:42

LeetCode-Water and Jug Problem的相关文章

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

【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

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

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 recursive

[LeetCode#218] The Skyline Problem

Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), w

Java for LeetCode 218 The Skyline Problem【Comming Soon】

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a pr

[LeetCode] A Distance Maximizing Problem

Question: Given an array A of integers, find the maximum of j-i subjected to the constraint of A[i] < A[j]. // O(n) public int maxDistance(int[] A) {     // Assumptions...          int local = Integer.MIN_VALUE;     int global = Integer.MIN_VALUE;   

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

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482