From 49209f50da59303c77a23cd9a42dea21e1c90a4c Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Tue, 21 Dec 2021 14:15:17 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0365 0365.Water and Jug Problem --- .../0365.Water and Jug Problem/README.md | 71 ++++++++++++++++++- .../0365.Water and Jug Problem/README_EN.md | 71 ++++++++++++++++++- .../0365.Water and Jug Problem/Solution.java | 39 ++++++++++ .../0365.Water and Jug Problem/Solution.py | 30 ++++++++ 4 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 solution/0300-0399/0365.Water and Jug Problem/Solution.java create mode 100644 solution/0300-0399/0365.Water and Jug Problem/Solution.py diff --git a/solution/0300-0399/0365.Water and Jug Problem/README.md b/solution/0300-0399/0365.Water and Jug Problem/README.md index 3fe1b837651c9..82b2fe0d0a722 100644 --- a/solution/0300-0399/0365.Water and Jug Problem/README.md +++ b/solution/0300-0399/0365.Water and Jug Problem/README.md @@ -42,7 +42,36 @@ ```python - +class Solution: + def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool: + stk, seen = [], set() + stk.append([0, 0]) + + def get_hash(nums): + return nums[0] * 10000006 + nums[1] + + while stk: + if get_hash(stk[-1]) in seen: + stk.pop() + continue + seen.add(get_hash(stk[-1])) + cur = stk.pop() + cur1, cur2 = cur[0], cur[1] + if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity: + return True + stk.append([jug1Capacity, cur2]) + stk.append([0, cur2]) + stk.append([cur1, jug2Capacity]) + stk.append([cur1, 0]) + if cur1 + cur2 > jug1Capacity: + stk.append([jug1Capacity, cur2 - jug1Capacity + cur1]) + else: + stk.append([cur1 + cur2, 0]) + if cur1 + cur2 > jug2Capacity: + stk.append([cur1 - jug2Capacity + cur2, jug2Capacity]) + else: + stk.append([0, cur1 + cur2]) + return False ``` ### **Java** @@ -50,7 +79,45 @@ ```java - +class Solution { + public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) { + Deque stk = new ArrayDeque<>(); + stk.add(new int[]{0, 0}); + Set seen = new HashSet<>(); + while (!stk.isEmpty()) { + if (seen.contains(hash(stk.peek()))) { + stk.pop(); + continue; + } + int[] cur = stk.pop(); + seen.add(hash(cur)); + int cur1 = cur[0], cur2 = cur[1]; + if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) { + return true; + } + stk.offer(new int[]{jug1Capacity, cur2}); + stk.offer(new int[]{0, cur2}); + stk.offer(new int[]{cur1, jug1Capacity}); + stk.offer(new int[]{cur2, 0}); + if (cur1 + cur2 > jug1Capacity) { + stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1}); + } else { + stk.offer(new int[]{cur1 + cur2, 0}); + } + if (cur1 + cur2 > jug2Capacity) { + stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity}); + } else { + stk.offer(new int[]{0, cur1 + cur2}); + } + + } + return false; + } + + public long hash(int[] nums) { + return nums[0] * 10000006L + nums[1]; + } +} ``` ### **...** diff --git a/solution/0300-0399/0365.Water and Jug Problem/README_EN.md b/solution/0300-0399/0365.Water and Jug Problem/README_EN.md index 628fa6027d8cd..4f0977397ce00 100644 --- a/solution/0300-0399/0365.Water and Jug Problem/README_EN.md +++ b/solution/0300-0399/0365.Water and Jug Problem/README_EN.md @@ -54,13 +54,80 @@ ### **Python3** ```python - +class Solution: + def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool: + stk, seen = [], set() + stk.append([0, 0]) + + def get_hash(nums): + return nums[0] * 10000006 + nums[1] + + while stk: + if get_hash(stk[-1]) in seen: + stk.pop() + continue + seen.add(get_hash(stk[-1])) + cur = stk.pop() + cur1, cur2 = cur[0], cur[1] + if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity: + return True + stk.append([jug1Capacity, cur2]) + stk.append([0, cur2]) + stk.append([cur1, jug2Capacity]) + stk.append([cur1, 0]) + if cur1 + cur2 > jug1Capacity: + stk.append([jug1Capacity, cur2 - jug1Capacity + cur1]) + else: + stk.append([cur1 + cur2, 0]) + if cur1 + cur2 > jug2Capacity: + stk.append([cur1 - jug2Capacity + cur2, jug2Capacity]) + else: + stk.append([0, cur1 + cur2]) + return False ``` ### **Java** ```java - +class Solution { + public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) { + Deque stk = new ArrayDeque<>(); + stk.add(new int[]{0, 0}); + Set seen = new HashSet<>(); + while (!stk.isEmpty()) { + if (seen.contains(hash(stk.peek()))) { + stk.pop(); + continue; + } + int[] cur = stk.pop(); + seen.add(hash(cur)); + int cur1 = cur[0], cur2 = cur[1]; + if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) { + return true; + } + stk.offer(new int[]{jug1Capacity, cur2}); + stk.offer(new int[]{0, cur2}); + stk.offer(new int[]{cur1, jug1Capacity}); + stk.offer(new int[]{cur2, 0}); + if (cur1 + cur2 > jug1Capacity) { + stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1}); + } else { + stk.offer(new int[]{cur1 + cur2, 0}); + } + if (cur1 + cur2 > jug2Capacity) { + stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity}); + } else { + stk.offer(new int[]{0, cur1 + cur2}); + } + + } + return false; + } + + public long hash(int[] nums) { + return nums[0] * 10000006L + nums[1]; + } +} ``` ### **...** diff --git a/solution/0300-0399/0365.Water and Jug Problem/Solution.java b/solution/0300-0399/0365.Water and Jug Problem/Solution.java new file mode 100644 index 0000000000000..c3f83603a842f --- /dev/null +++ b/solution/0300-0399/0365.Water and Jug Problem/Solution.java @@ -0,0 +1,39 @@ +class Solution { + public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) { + Deque stk = new ArrayDeque<>(); + stk.add(new int[]{0, 0}); + Set seen = new HashSet<>(); + while (!stk.isEmpty()) { + if (seen.contains(hash(stk.peek()))) { + stk.pop(); + continue; + } + int[] cur = stk.pop(); + seen.add(hash(cur)); + int cur1 = cur[0], cur2 = cur[1]; + if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) { + return true; + } + stk.offer(new int[]{jug1Capacity, cur2}); + stk.offer(new int[]{0, cur2}); + stk.offer(new int[]{cur1, jug1Capacity}); + stk.offer(new int[]{cur2, 0}); + if (cur1 + cur2 > jug1Capacity) { + stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1}); + } else { + stk.offer(new int[]{cur1 + cur2, 0}); + } + if (cur1 + cur2 > jug2Capacity) { + stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity}); + } else { + stk.offer(new int[]{0, cur1 + cur2}); + } + + } + return false; + } + + public long hash(int[] nums) { + return nums[0] * 10000006L + nums[1]; + } +} \ No newline at end of file diff --git a/solution/0300-0399/0365.Water and Jug Problem/Solution.py b/solution/0300-0399/0365.Water and Jug Problem/Solution.py new file mode 100644 index 0000000000000..5afc8c701367e --- /dev/null +++ b/solution/0300-0399/0365.Water and Jug Problem/Solution.py @@ -0,0 +1,30 @@ +class Solution: + def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool: + stk, seen = [], set() + stk.append([0, 0]) + + def get_hash(nums): + return nums[0] * 10000006 + nums[1] + + while stk: + if get_hash(stk[-1]) in seen: + stk.pop() + continue + seen.add(get_hash(stk[-1])) + cur = stk.pop() + cur1, cur2 = cur[0], cur[1] + if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity: + return True + stk.append([jug1Capacity, cur2]) + stk.append([0, cur2]) + stk.append([cur1, jug2Capacity]) + stk.append([cur1, 0]) + if cur1 + cur2 > jug1Capacity: + stk.append([jug1Capacity, cur2 - jug1Capacity + cur1]) + else: + stk.append([cur1 + cur2, 0]) + if cur1 + cur2 > jug2Capacity: + stk.append([cur1 - jug2Capacity + cur2, jug2Capacity]) + else: + stk.append([0, cur1 + cur2]) + return False \ No newline at end of file