Skip to content

feat: add solutions to lc problem: No.0365 #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions solution/0300-0399/0365.Water and Jug Problem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,82 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```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<int[]> stk = new ArrayDeque<>();
stk.add(new int[]{0, 0});
Set<Long> 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];
}
}
```

### **...**
Expand Down
71 changes: 69 additions & 2 deletions solution/0300-0399/0365.Water and Jug Problem/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int[]> stk = new ArrayDeque<>();
stk.add(new int[]{0, 0});
Set<Long> 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];
}
}
```

### **...**
Expand Down
39 changes: 39 additions & 0 deletions solution/0300-0399/0365.Water and Jug Problem/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
Deque<int[]> stk = new ArrayDeque<>();
stk.add(new int[]{0, 0});
Set<Long> 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];
}
}
30 changes: 30 additions & 0 deletions solution/0300-0399/0365.Water and Jug Problem/Solution.py
Original file line number Diff line number Diff line change
@@ -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