Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6dd3ade

Browse files
Added tasks 365, 367, 368.
1 parent f014f4a commit 6dd3ade

File tree

9 files changed

+270
-0
lines changed

9 files changed

+270
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0301_0400.s0365_water_and_jug_problem;
2+
3+
// #Medium #Math #Depth_First_Search #Breadth_First_Search
4+
5+
public class Solution {
6+
private int gcd(int n1, int n2) {
7+
if (n2 == 0) {
8+
return n1;
9+
}
10+
return gcd(n2, n1 % n2);
11+
}
12+
13+
public boolean canMeasureWater(int jug1, int jug2, int target) {
14+
if (jug1 + jug2 < target) {
15+
return false;
16+
}
17+
int gcd = gcd(jug1, jug2);
18+
return target % gcd == 0;
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
365\. Water and Jug Problem
2+
3+
Medium
4+
5+
You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
6+
7+
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` liters of water contained **within one or both buckets** by the end.
8+
9+
Operations allowed:
10+
11+
* Fill any of the jugs with water.
12+
* Empty any of the jugs.
13+
* Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.
14+
15+
**Example 1:**
16+
17+
**Input:** jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
18+
19+
**Output:** true
20+
21+
**Explanation:** The famous [Die Hard](https://www.youtube.com/watch?v=BVtQNK_ZUJg&ab_channel=notnek01) example
22+
23+
**Example 2:**
24+
25+
**Input:** jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
26+
27+
**Output:** false
28+
29+
**Example 3:**
30+
31+
**Input:** jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
32+
33+
**Output:** true
34+
35+
**Constraints:**
36+
37+
* <code>1 <= jug1Capacity, jug2Capacity, targetCapacity <= 10<sup>6</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0301_0400.s0367_valid_perfect_square;
2+
3+
// #Easy #Math #Binary_Search
4+
5+
public class Solution {
6+
public boolean isPerfectSquare(int num) {
7+
if (num == 0) {
8+
// If num is 0 return false
9+
return false;
10+
}
11+
// long datatype can holds huge number.
12+
long start = 0;
13+
long end = num;
14+
long mid = 0;
15+
while (start <= end) {
16+
// untill start is lesser or equal to end do this
17+
// Finding middle value
18+
mid = start + (end - start) / 2;
19+
if (mid * mid == num) {
20+
// if mid*mid == num return true
21+
return true;
22+
} else if (mid * mid < num) {
23+
// if num is greater than mid*mid then make start = mid + 1
24+
start = mid + 1;
25+
} else if (mid * mid > num) {
26+
// if num is lesser than mid*mid then make end = mid - 1
27+
end = mid - 1;
28+
}
29+
}
30+
return false;
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
367\. Valid Perfect Square
2+
3+
Easy
4+
5+
Given a **positive** integer _num_, write a function which returns True if _num_ is a perfect square else False.
6+
7+
**Follow up:** **Do not** use any built-in library function such as `sqrt`.
8+
9+
**Example 1:**
10+
11+
**Input:** num = 16
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** num = 14
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `1 <= num <= 2^31 - 1`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g0301_0400.s0368_largest_divisible_subset;
2+
3+
// #Medium #Array #Dynamic_Programming #Math #Sorting
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
// Helper class containing value and an arraylist
11+
public class Helper {
12+
int val;
13+
List<Integer> al;
14+
15+
Helper(int val) {
16+
this.val = val;
17+
al = new ArrayList<>();
18+
}
19+
}
20+
21+
public List<Integer> largestDivisibleSubset(int[] nums) {
22+
// Initializing Helper type array
23+
Helper[] dp = new Helper[nums.length];
24+
// Sorting given array
25+
Arrays.sort(nums);
26+
// max variable will keep track size of Longest Divisible Subset
27+
int max = 0;
28+
// index variable will keep track the index at which dp contains Longest Divisible Subset
29+
int index = 0;
30+
dp[0] = new Helper(1);
31+
dp[0].al.add(nums[0]);
32+
// Operation similar to LIS
33+
for (int i = 1; i < nums.length; i++) {
34+
int max2 = 0;
35+
int pos = i;
36+
for (int j = 0; j < i; j++) {
37+
if (nums[i] % nums[j] == 0) {
38+
if (max2 < dp[j].val) {
39+
max2 = dp[j].val;
40+
pos = j;
41+
}
42+
}
43+
}
44+
// max2 = 0, means no element exists which can divide the present element
45+
if (max2 == 0) {
46+
dp[i] = new Helper(max2 + 1);
47+
dp[i].al.add(nums[i]);
48+
} else {
49+
dp[i] = new Helper(max2 + 1);
50+
for (int val : dp[pos].al) {
51+
dp[i].al.add(val);
52+
}
53+
dp[i].al.add(nums[i]);
54+
}
55+
if (max2 > max) {
56+
max = max2;
57+
index = i;
58+
}
59+
}
60+
// Returning Largest Divisible Subset
61+
return dp[index].al;
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
368\. Largest Divisible Subset
2+
3+
Medium
4+
5+
Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
6+
7+
* `answer[i] % answer[j] == 0`, or
8+
* `answer[j] % answer[i] == 0`
9+
10+
If there are multiple solutions, return any of them.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = \[1,2,3\]
15+
16+
**Output:** \[1,2\]
17+
18+
**Explanation:** \[1,3\] is also accepted.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = \[1,2,4,8\]
23+
24+
**Output:** \[1,2,4,8\]
25+
26+
**Constraints:**
27+
28+
* `1 <= nums.length <= 1000`
29+
* <code>1 <= nums[i] <= 2 * 10<sup>9</sup></code>
30+
* All the integers in `nums` are **unique**.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0301_0400.s0365_water_and_jug_problem;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void canMeasureWater() {
11+
assertThat(new Solution().canMeasureWater(3, 5, 4), equalTo(true));
12+
}
13+
14+
@Test
15+
void canMeasureWater2() {
16+
assertThat(new Solution().canMeasureWater(2, 6, 5), equalTo(false));
17+
}
18+
19+
@Test
20+
void canMeasureWater3() {
21+
assertThat(new Solution().canMeasureWater(1, 2, 3), equalTo(true));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0301_0400.s0367_valid_perfect_square;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void isPerfectSquare() {
11+
assertThat(new Solution().isPerfectSquare(16), equalTo(true));
12+
}
13+
14+
@Test
15+
void isPerfectSquare2() {
16+
assertThat(new Solution().isPerfectSquare(14), equalTo(false));
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0301_0400.s0368_largest_divisible_subset;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void largestDivisibleSubset() {
13+
assertThat(
14+
new Solution().largestDivisibleSubset(new int[] {1, 2, 3}),
15+
equalTo(new ArrayList<Integer>(Arrays.asList(1, 2))));
16+
}
17+
18+
@Test
19+
void largestDivisibleSubset2() {
20+
assertThat(
21+
new Solution().largestDivisibleSubset(new int[] {1, 2, 4, 8}),
22+
equalTo(new ArrayList<Integer>(Arrays.asList(1, 2, 4, 8))));
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /