Skip to content

Commit

Permalink
Minimize the Maximum Adjacent Element Difference
Browse files Browse the repository at this point in the history
  • Loading branch information
Goutham Vidya Pradhan authored and Goutham Vidya Pradhan committed Dec 5, 2024
1 parent 2f69b2f commit c2e7d31
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ My accepted leetcode solutions to some of the common interview problems.
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Minimum Window Subsequence](src/main/java/binary_search/MinimumWindowSubsequence.java) (Hard)
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Koko Eating Bananas](src/main/java/binary_search/KokoEatingBananas.java) (Hard)
- [Single Element in a Sorted Array](src/main/java/binary_search/SingleElementInASortedArray.java) (Medium)
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Minimize the Maximum Adjacent Element Difference](src/main/java/binary_search/MinimizeTheMaximumAdjacentElementDifference.java) (Hard)

#### [Bit Manipulation](src/main/java/bit_manipulation)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
id 'java'
id 'com.github.sherter.google-java-format' version '0.8'
id 'com.github.sherter.google-java-format' version '0.9'
}

dependencies {
implementation 'com.google.googlejavaformat:google-java-format:1.12.0'
implementation 'com.google.googlejavaformat:google-java-format:1.8.0'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package binary_search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MinimizeTheMaximumAdjacentElementDifference {
record Interval(int s, int e, boolean hasMoreThanOne){
static boolean check(int a, int mid, int b, int range){
return Math.abs(mid - a) <= range && Math.abs(mid - b) <= range;
}
static boolean check(int a, int mid1, int mid2, int b, int range){
return Math.abs(mid1 - a) <= range && Math.abs(mid1 - mid2) <= range && Math.abs(mid2 - b) <= range;
}
}

public static void main(String[] args) {
int[] nums = new int[]{-1,10,-1,8};
int res = new MinimizeTheMaximumAdjacentElementDifference().minDifference(nums);
System.out.println(res);
}

public int minDifference(int[] nums) {
boolean noPositiveNum = Arrays.stream(nums).filter(i -> i != -1).findAny().isEmpty();
if(noPositiveNum){
return 0;
}
int currentMax = getCurrentMax(nums);
List<Interval> intervals = buildIntervals(nums);
int minStart = Integer.MAX_VALUE, maxEnd = Integer.MIN_VALUE;
for (Interval interval : intervals) {
minStart = Math.min(minStart, Math.min(interval.e, interval.s));
maxEnd = Math.max(maxEnd, Math.max(interval.e, interval.s));
}
int l = 0, h = maxEnd, m;
int ans = -1;
while(l <= h){
m = l + (h - l) / 2;
boolean result = checkIfThisNumberSatisfiesAllIntervals(intervals, minStart + m, maxEnd - m, m);
if(result){
ans = m;
h = m - 1;
} else {
l = m + 1;
}
}
return Math.max(ans, currentMax);
}

private int getCurrentMax(int[] nums){
int currMax = Integer.MIN_VALUE;
int previous = nums[0];
for(int i = 1; i < nums.length; i ++){
if(nums[i] != -1){
if(previous != -1){
currMax = Math.max(currMax, Math.abs(previous - nums[i]));
}
previous = nums[i];
} else {
previous = -1;
}
}
return currMax;
}

private List<Interval> buildIntervals(int[] nums) {
int previous = -1;
int minusOneCount = 0;
List<Interval> intervals = new ArrayList<>();
for (int num : nums) {
if (num == -1) {
minusOneCount ++;
} else {
if (minusOneCount > 0) {
intervals.add(new Interval(previous != -1 ? previous : num, num, minusOneCount > 1));
minusOneCount = 0;
}
previous = num;
}
}
if(nums[nums.length - 1] == -1){
intervals.add(new Interval(previous, previous, minusOneCount > 1));
}
return intervals;
}

boolean checkIfThisNumberSatisfiesAllIntervals(List<Interval> intervals, int minStart, int maxEnd, int maxDiff){
for (Interval interval : intervals) {
if (interval.hasMoreThanOne) {
boolean res1 = Interval.check(interval.s, minStart, minStart, interval.e, maxDiff);
boolean res2 = Interval.check(interval.s, minStart, maxEnd, interval.e, maxDiff);
boolean res3 = Interval.check(interval.s, maxEnd, minStart, interval.e, maxDiff);
boolean res4 = Interval.check(interval.s, maxEnd, maxEnd, interval.e, maxDiff);
if (!res1 && !res2 && !res3 && !res4) {
return false;
}
} else {
boolean res1 = Interval.check(interval.s, minStart, interval.e, maxDiff);
boolean res2 = Interval.check(interval.s, maxEnd, interval.e, maxDiff);
if (!res1 && !res2) {
return false;
}
}
}
return true;
}
}

0 comments on commit c2e7d31

Please sign in to comment.