力扣:365. 水壶问题

知道逻辑是啥,不会写,哈哈哈

只能CV一下官方代码了,哈哈哈



import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

/**
 * @author xnl
 * @Description:
 * @date: 2022/6/28   22:52
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        Deque<int[]> stack = new LinkedList<>();
        stack.push(new int[]{0 ,0});
        Set<Long> seen = new HashSet<>();
        while (!stack.isEmpty()){
            if (seen.contains(hash(stack.peek()))){
                stack.pop();
                continue;
            }
            seen.add(hash(stack.peek()));

            int[] state = stack.pop();
            // 代表第一个水壶和第二个水壶
            int remain_x = state[0], remain_y = state[1];
            // 第一个水壶达到了预期值,或者第二个水壶达到了预期值,或者第一个水壶加第二个水壶到达预期值
            if (remain_x == targetCapacity || remain_y == targetCapacity || remain_x + remain_y == targetCapacity){
                return true;
            }

            // 把第一个水壶灌满
            stack.push(new int[]{jug1Capacity, remain_y});
            // 把第二个水壶灌满
            stack.push(new int[]{remain_x, jug2Capacity});
            // 把第一个水壶倒空
            stack.push(new int[]{0, remain_y});
            // 把第二个个水壶倒空
            stack.push(new int[]{remain_x, 0});
            // 把第一个水壶灌进第二个水壶,直到满或者空
            stack.push(new int[]{remain_x - Math.min(remain_x, jug2Capacity - remain_y), remain_y + Math.min(remain_x, jug2Capacity - remain_y)});
            // 把第二个水壶灌进第一个水壶,直到满或者空
            stack.push(new int[]{remain_x + Math.min(remain_y, jug1Capacity - remain_x), remain_y - Math.min(remain_y, jug1Capacity - remain_x)});
        }
        return false;
    }

    private long hash(int[] state){
        return (long) state[0] * 1000001 + state[1];
    }

    /**
     * 贝祖定理
     * @param x
     * @param y
     * @param z
     * @return
     */
    private  boolean canMeasureWater2(int x, int y, int z){
        if (x + y < z){
            return false;
        }

        if (x == 0 || y == 0 ){
            return z == 0 || x + y == z;
        }
        return z % gcd(x, y) == 0;
    }

    private int gcd(int x, int y){
        int remainder  = x % y;
        while (remainder != 0){
            x = y;
            y = remainder;
            remainder = x % y;
        }
        return y;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值