From c2e7d31bd70b993b790204d5d237050578ae9f67 Mon Sep 17 00:00:00 2001 From: Goutham Vidya Pradhan Date: Thu, 5 Dec 2024 21:36:29 +0100 Subject: [PATCH] Minimize the Maximum Adjacent Element Difference --- README.md | 1 + build.gradle | 4 +- ...zeTheMaximumAdjacentElementDifference.java | 107 ++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/main/java/binary_search/MinimizeTheMaximumAdjacentElementDifference.java diff --git a/README.md b/README.md index 525f428b..5bd66863 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/build.gradle b/build.gradle index 22aa07b4..e807b14d 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/src/main/java/binary_search/MinimizeTheMaximumAdjacentElementDifference.java b/src/main/java/binary_search/MinimizeTheMaximumAdjacentElementDifference.java new file mode 100644 index 00000000..63e054a4 --- /dev/null +++ b/src/main/java/binary_search/MinimizeTheMaximumAdjacentElementDifference.java @@ -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 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 buildIntervals(int[] nums) { + int previous = -1; + int minusOneCount = 0; + List 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 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; + } +}