Skip to content
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

Array - Hard - Java version #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions Array/Hard/Candy.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
26 changes: 26 additions & 0 deletions Array/Hard/MaxChunnkSortedTwo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
50 changes: 50 additions & 0 deletions Array/Hard/MaximumGap.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
43 changes: 43 additions & 0 deletions Array/Hard/MedianOfTwoSorted.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions Array/Hard/MinReplacementToSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.*;

public class MinReplacementToSort {
public long minimumReplacement(List<Integer> 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<Integer> nums = Arrays.asList(3,9,3);
long result = solution.minimumReplacement(nums);
System.out.println("Minimum Replacements: " + result);
}
}
38 changes: 38 additions & 0 deletions Array/Hard/TrappingRainWater.java
Original file line number Diff line number Diff line change
@@ -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);
}
}