diff --git a/src/main/java/g0301_0400/s0365_water_and_jug_problem/Solution.java b/src/main/java/g0301_0400/s0365_water_and_jug_problem/Solution.java new file mode 100644 index 000000000..c0bf27327 --- /dev/null +++ b/src/main/java/g0301_0400/s0365_water_and_jug_problem/Solution.java @@ -0,0 +1,20 @@ +package g0301_0400.s0365_water_and_jug_problem; + +// #Medium #Math #Depth_First_Search #Breadth_First_Search + +public class Solution { + private int gcd(int n1, int n2) { + if (n2 == 0) { + return n1; + } + return gcd(n2, n1 % n2); + } + + public boolean canMeasureWater(int jug1, int jug2, int target) { + if (jug1 + jug2 < target) { + return false; + } + int gcd = gcd(jug1, jug2); + return target % gcd == 0; + } +} diff --git a/src/main/java/g0301_0400/s0365_water_and_jug_problem/readme.md b/src/main/java/g0301_0400/s0365_water_and_jug_problem/readme.md new file mode 100644 index 000000000..1564fad7b --- /dev/null +++ b/src/main/java/g0301_0400/s0365_water_and_jug_problem/readme.md @@ -0,0 +1,37 @@ +365\. Water and Jug Problem + +Medium + +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. + +If `targetCapacity` liters of water are measurable, you must have `targetCapacity` liters of water contained **within one or both buckets** by the end. + +Operations allowed: + +* Fill any of the jugs 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:** + +**Input:** jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4 + +**Output:** true + +**Explanation:** The famous [Die Hard](https://www.youtube.com/watch?v=BVtQNK_ZUJg&ab_channel=notnek01) example + +**Example 2:** + +**Input:** jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5 + +**Output:** false + +**Example 3:** + +**Input:** jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3 + +**Output:** true + +**Constraints:** + +* 1 <= jug1Capacity, jug2Capacity, targetCapacity <= 106 \ No newline at end of file diff --git a/src/main/java/g0301_0400/s0367_valid_perfect_square/Solution.java b/src/main/java/g0301_0400/s0367_valid_perfect_square/Solution.java new file mode 100644 index 000000000..abfb3b89e --- /dev/null +++ b/src/main/java/g0301_0400/s0367_valid_perfect_square/Solution.java @@ -0,0 +1,32 @@ +package g0301_0400.s0367_valid_perfect_square; + +// #Easy #Math #Binary_Search + +public class Solution { + public boolean isPerfectSquare(int num) { + if (num == 0) { + // If num is 0 return false + return false; + } + // long datatype can holds huge number. + long start = 0; + long end = num; + long mid = 0; + while (start <= end) { + // untill start is lesser or equal to end do this + // Finding middle value + mid = start + (end - start) / 2; + if (mid * mid == num) { + // if mid*mid == num return true + return true; + } else if (mid * mid < num) { + // if num is greater than mid*mid then make start = mid + 1 + start = mid + 1; + } else if (mid * mid> num) { + // if num is lesser than mid*mid then make end = mid - 1 + end = mid - 1; + } + } + return false; + } +} diff --git a/src/main/java/g0301_0400/s0367_valid_perfect_square/readme.md b/src/main/java/g0301_0400/s0367_valid_perfect_square/readme.md new file mode 100644 index 000000000..d56fdbebd --- /dev/null +++ b/src/main/java/g0301_0400/s0367_valid_perfect_square/readme.md @@ -0,0 +1,23 @@ +367\. Valid Perfect Square + +Easy + +Given a **positive** integer _num_, write a function which returns True if _num_ is a perfect square else False. + +**Follow up:** **Do not** use any built-in library function such as `sqrt`. + +**Example 1:** + +**Input:** num = 16 + +**Output:** true + +**Example 2:** + +**Input:** num = 14 + +**Output:** false + +**Constraints:** + +* `1 <= num <= 2^31 - 1` \ No newline at end of file diff --git a/src/main/java/g0301_0400/s0368_largest_divisible_subset/Solution.java b/src/main/java/g0301_0400/s0368_largest_divisible_subset/Solution.java new file mode 100644 index 000000000..c13ea57e6 --- /dev/null +++ b/src/main/java/g0301_0400/s0368_largest_divisible_subset/Solution.java @@ -0,0 +1,63 @@ +package g0301_0400.s0368_largest_divisible_subset; + +// #Medium #Array #Dynamic_Programming #Math #Sorting + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + // Helper class containing value and an arraylist + public class Helper { + int val; + List al; + + Helper(int val) { + this.val = val; + al = new ArrayList(); + } + } + + public List largestDivisibleSubset(int[] nums) { + // Initializing Helper type array + Helper[] dp = new Helper[nums.length]; + // Sorting given array + Arrays.sort(nums); + // max variable will keep track size of Longest Divisible Subset + int max = 0; + // index variable will keep track the index at which dp contains Longest Divisible Subset + int index = 0; + dp[0] = new Helper(1); + dp[0].al.add(nums[0]); + // Operation similar to LIS + for (int i = 1; i < nums.length; i++) { + int max2 = 0; + int pos = i; + for (int j = 0; j < i; j++) { + if (nums[i] % nums[j] == 0) { + if (max2 < dp[j].val) { + max2 = dp[j].val; + pos = j; + } + } + } + // max2 = 0, means no element exists which can divide the present element + if (max2 == 0) { + dp[i] = new Helper(max2 + 1); + dp[i].al.add(nums[i]); + } else { + dp[i] = new Helper(max2 + 1); + for (int val : dp[pos].al) { + dp[i].al.add(val); + } + dp[i].al.add(nums[i]); + } + if (max2> max) { + max = max2; + index = i; + } + } + // Returning Largest Divisible Subset + return dp[index].al; + } +} diff --git a/src/main/java/g0301_0400/s0368_largest_divisible_subset/readme.md b/src/main/java/g0301_0400/s0368_largest_divisible_subset/readme.md new file mode 100644 index 000000000..490bdc109 --- /dev/null +++ b/src/main/java/g0301_0400/s0368_largest_divisible_subset/readme.md @@ -0,0 +1,30 @@ +368\. Largest Divisible Subset + +Medium + +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: + +* `answer[i] % answer[j] == 0`, or +* `answer[j] % answer[i] == 0` + +If there are multiple solutions, return any of them. + +**Example 1:** + +**Input:** nums = \[1,2,3\] + +**Output:** \[1,2\] + +**Explanation:** \[1,3\] is also accepted. + +**Example 2:** + +**Input:** nums = \[1,2,4,8\] + +**Output:** \[1,2,4,8\] + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 2 * 109 +* All the integers in `nums` are **unique**. \ No newline at end of file diff --git a/src/test/java/g0301_0400/s0365_water_and_jug_problem/SolutionTest.java b/src/test/java/g0301_0400/s0365_water_and_jug_problem/SolutionTest.java new file mode 100644 index 000000000..2b63c9ecd --- /dev/null +++ b/src/test/java/g0301_0400/s0365_water_and_jug_problem/SolutionTest.java @@ -0,0 +1,23 @@ +package g0301_0400.s0365_water_and_jug_problem; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void canMeasureWater() { + assertThat(new Solution().canMeasureWater(3, 5, 4), equalTo(true)); + } + + @Test + void canMeasureWater2() { + assertThat(new Solution().canMeasureWater(2, 6, 5), equalTo(false)); + } + + @Test + void canMeasureWater3() { + assertThat(new Solution().canMeasureWater(1, 2, 3), equalTo(true)); + } +} diff --git a/src/test/java/g0301_0400/s0367_valid_perfect_square/SolutionTest.java b/src/test/java/g0301_0400/s0367_valid_perfect_square/SolutionTest.java new file mode 100644 index 000000000..2faa7d5a3 --- /dev/null +++ b/src/test/java/g0301_0400/s0367_valid_perfect_square/SolutionTest.java @@ -0,0 +1,18 @@ +package g0301_0400.s0367_valid_perfect_square; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isPerfectSquare() { + assertThat(new Solution().isPerfectSquare(16), equalTo(true)); + } + + @Test + void isPerfectSquare2() { + assertThat(new Solution().isPerfectSquare(14), equalTo(false)); + } +} diff --git a/src/test/java/g0301_0400/s0368_largest_divisible_subset/SolutionTest.java b/src/test/java/g0301_0400/s0368_largest_divisible_subset/SolutionTest.java new file mode 100644 index 000000000..6d7ac78fb --- /dev/null +++ b/src/test/java/g0301_0400/s0368_largest_divisible_subset/SolutionTest.java @@ -0,0 +1,24 @@ +package g0301_0400.s0368_largest_divisible_subset; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void largestDivisibleSubset() { + assertThat( + new Solution().largestDivisibleSubset(new int[] {1, 2, 3}), + equalTo(new ArrayList(Arrays.asList(1, 2)))); + } + + @Test + void largestDivisibleSubset2() { + assertThat( + new Solution().largestDivisibleSubset(new int[] {1, 2, 4, 8}), + equalTo(new ArrayList(Arrays.asList(1, 2, 4, 8)))); + } +}

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