From 0afdeeb17a599bba5887e27a3f25905c20116e95 Mon Sep 17 00:00:00 2001 From: boaglio Date: Sun, 8 Oct 2023 17:43:43 -0300 Subject: [PATCH] Array - Hard - Java version --- Array/Hard/Candy.java | 41 +++++++++++++++++++++++ Array/Hard/MaxChunnkSortedTwo.java | 26 +++++++++++++++ Array/Hard/MaximumGap.java | 50 ++++++++++++++++++++++++++++ Array/Hard/MedianOfTwoSorted.java | 43 ++++++++++++++++++++++++ Array/Hard/MinReplacementToSort.java | 28 ++++++++++++++++ Array/Hard/TrappingRainWater.java | 38 +++++++++++++++++++++ 6 files changed, 226 insertions(+) create mode 100644 Array/Hard/Candy.java create mode 100644 Array/Hard/MaxChunnkSortedTwo.java create mode 100644 Array/Hard/MaximumGap.java create mode 100644 Array/Hard/MedianOfTwoSorted.java create mode 100644 Array/Hard/MinReplacementToSort.java create mode 100644 Array/Hard/TrappingRainWater.java diff --git a/Array/Hard/Candy.java b/Array/Hard/Candy.java new file mode 100644 index 0000000..ae4031a --- /dev/null +++ b/Array/Hard/Candy.java @@ -0,0 +1,41 @@ +import java.util.Arrays; + +public class Candy { + + public int candy(int[] ratings) { + int n = ratings.length; + int[] candies = new int[n]; + Arrays.fill(candies, 1); + + if (n == 1) { + return 1; + } + + for (int i = 1; i < n; i++) { + if (ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) { + candies[i] = candies[i - 1] + 1; + } + } + + for (int i = n - 2; i >= 0; i--) { + if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) { + candies[i] = candies[i + 1] + 1; + } + } + + int totalCandies = 0; + for (int i = 0; i < n; i++) { + totalCandies += candies[i]; + } + + return totalCandies; + } + + public static void main(String[] args) { + Candy solution = new Candy(); + int[] ratings = {1, 0, 2}; + int result = solution.candy(ratings); + System.out.println("Minimum number of candies: " + result); + } + +} diff --git a/Array/Hard/MaxChunnkSortedTwo.java b/Array/Hard/MaxChunnkSortedTwo.java new file mode 100644 index 0000000..68b9f09 --- /dev/null +++ b/Array/Hard/MaxChunnkSortedTwo.java @@ -0,0 +1,26 @@ +public class MAxChunnkSortedTwo { + + public int maxChunksToSorted(int[] arr) { + int n = arr.length; + int[] rmin = new int[n + 1]; + rmin[n] = Integer.MAX_VALUE; + for (int i = n - 1; i >= 0; i--) { + rmin[i] = Math.min(arr[i], rmin[i + 1]); + } + int ans = 0, lmax = 0; + for (int i = 0; i < n; i++) { + lmax = Math.max(lmax, arr[i]); + if (lmax <= rmin[i + 1]) { + ans++; + } + } + return ans; + } + + public static void main(String[] args) { + MAxChunnkSortedTwo solution = new MAxChunnkSortedTwo(); + int[] arr = {2, 1, 3, 4, 4}; + int result = solution.maxChunksToSorted(arr); + System.out.println("Maximum number of chunks: " + result); + } +} diff --git a/Array/Hard/MaximumGap.java b/Array/Hard/MaximumGap.java new file mode 100644 index 0000000..60fead6 --- /dev/null +++ b/Array/Hard/MaximumGap.java @@ -0,0 +1,50 @@ +import java.util.Arrays; + +public class MaximumGap { + + public int maximumGap(int[] nums) { + if (nums.length < 2) { + return 0; + } + + int mini = nums[0]; + int maxi = 0; + for (int num : nums) { + mini = Math.min(mini, num); + maxi = Math.max(maxi, num); + } + + int interval = (int) Math.ceil((maxi - mini + 0.0) / (nums.length - 1)); + int[] minbuck = new int[nums.length - 1]; + int[] maxbuck = new int[nums.length - 1]; + Arrays.fill(minbuck, Integer.MAX_VALUE); + + for (int i = 0; i < nums.length; i++) { + if (nums[i] == mini || nums[i] == maxi) { + continue; + } + int index = (nums[i] - mini) / interval; + minbuck[index] = Math.min(minbuck[index], nums[i]); + maxbuck[index] = Math.max(maxbuck[index], nums[i]); + } + + int prev = mini; + int ans = 0; + for (int i = 0; i < minbuck.length; i++) { + if (maxbuck[i] == 0) { + continue; + } + ans = Math.max(ans, minbuck[i] - prev); + prev = maxbuck[i]; + } + ans = Math.max(ans, maxi - prev); + return ans; + } + + public static void main(String[] args) { + MaximumGap solution = new MaximumGap(); + int[] nums = {3, 6, 9, 1}; + int result = solution.maximumGap(nums); + System.out.println("Maximum Gap: " + result); + } +} diff --git a/Array/Hard/MedianOfTwoSorted.java b/Array/Hard/MedianOfTwoSorted.java new file mode 100644 index 0000000..475ea6b --- /dev/null +++ b/Array/Hard/MedianOfTwoSorted.java @@ -0,0 +1,43 @@ + + +public class MedianOfTwoSorted { + public double findMedianSortedArrays(int[] a, int[] b) { + int n1 = a.length, n2 = b.length; + // Se n1 for maior, troque os arrays: + if (n1 > n2) return findMedianSortedArrays(b, a); + + int n = n1 + n2; // comprimento total + int left = (n1 + n2 + 1) / 2; // comprimento da metade esquerda + // Aplicar busca binária: + int low = 0, high = n1; + while (low <= high) { + int mid1 = (low + high) >>> 1; + int mid2 = left - mid1; + // Calcular l1, l2, r1 e r2; + int l1 = Integer.MIN_VALUE, l2 = Integer.MIN_VALUE; + int r1 = Integer.MAX_VALUE, r2 = Integer.MAX_VALUE; + if (mid1 < n1) r1 = a[mid1]; + if (mid2 < n2) r2 = b[mid2]; + if (mid1 - 1 >= 0) l1 = a[mid1 - 1]; + if (mid2 - 1 >= 0) l2 = b[mid2 - 1]; + + if (l1 <= r2 && l2 <= r1) { + if (n % 2 == 1) return Math.max(l1, l2); + else return ((double)(Math.max(l1, l2) + Math.min(r1, r2))) / 2.0; + } + + // Elimine as metades: + else if (l1 > r2) high = mid1 - 1; + else low = mid1 + 1; + } + return 0; // declaração fictícia + } + + public static void main(String[] args) { + MedianOfTwoSorted solution = new MedianOfTwoSorted(); + int[] a = {1, 3}; + int[] b = {2}; + double result = solution.findMedianSortedArrays(a, b); + System.out.println("Median: " + result); + } +} diff --git a/Array/Hard/MinReplacementToSort.java b/Array/Hard/MinReplacementToSort.java new file mode 100644 index 0000000..7e4b645 --- /dev/null +++ b/Array/Hard/MinReplacementToSort.java @@ -0,0 +1,28 @@ +import java.util.*; + +public class MinReplacementToSort { + public long minimumReplacement(List nums) { + long ans = 0; + int n = nums.size(); + int nxt = (int) 1e9 + 7; + + for (int j = n - 1; j >= 0; j--) { + if (nums.get(j) <= nxt) { + nxt = nums.get(j); + continue; + } + + long parts = (long) Math.ceil(nums.get(j) / (double) nxt); + ans += parts - 1; + nxt = (int) (nums.get(j) / parts); + } + return ans; + } + + public static void main(String[] args) { + MinReplacementToSort solution = new MinReplacementToSort(); + List nums = Arrays.asList(3,9,3); + long result = solution.minimumReplacement(nums); + System.out.println("Minimum Replacements: " + result); + } +} diff --git a/Array/Hard/TrappingRainWater.java b/Array/Hard/TrappingRainWater.java new file mode 100644 index 0000000..08acb24 --- /dev/null +++ b/Array/Hard/TrappingRainWater.java @@ -0,0 +1,38 @@ +import java.util.Arrays; + +public class TrappingRainWater { + + public int trap(int[] height) { + int ans = 0; + int n = height.length; + int[] lmax = new int[n]; + int[] rmax = new int[n]; + int maxi = 0; + + for (int i = 0; i < n; i++) { + lmax[i] = maxi; + maxi = Math.max(maxi, height[i]); + } + + maxi = 0; + for (int i = n - 1; i >= 0; i--) { + rmax[i] = maxi; + maxi = Math.max(maxi, height[i]); + } + + for (int i = 0; i < n; i++) { + if (Math.min(lmax[i], rmax[i]) - height[i] > 0) { + ans += Math.min(lmax[i], rmax[i]) - height[i]; + } + } + + return ans; + } + + public static void main(String[] args) { + TrappingRainWater solution = new TrappingRainWater(); + int[] height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; + int result = solution.trap(height); + System.out.println("Trapped Rainwater: " + result); + } +}