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

Added C++, Java, and Python Solutions for Problem 3357 #3804

Open
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
public:
int minimizeMaxDifference(vector<int>& nums) {
int n = nums.size();
int minVal = INT_MAX, maxVal = INT_MIN;

for (int i = 0; i < n; ++i) {
if (nums[i] != -1) {
minVal = min(minVal, nums[i]);
maxVal = max(maxVal, nums[i]);
}
}

if (minVal == INT_MAX) {
return 0;
}

auto isPossible = [&](int maxDiff) -> bool {
int prev = -1;

for (int i = 0; i < n; ++i) {
if (nums[i] != -1) {
prev = nums[i];
} else {
if (prev != -1) {
if (abs(prev - minVal) > maxDiff && abs(prev - maxVal) > maxDiff) {
return false;
}
prev = (abs(prev - minVal) <= abs(prev - maxVal)) ? minVal : maxVal;
} else {
prev = minVal;
}
}
}

return true;
};

int left = 0, right = maxVal - minVal, result = right;

while (left <= right) {
int mid = left + (right - left) / 2;
if (isPossible(mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}

return result;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class Solution {

public int minimizeMaxDiff(int[] nums) {
int left = 0, right = (int) 1e9, result = (int) 1e9;

while (left <= right) {
int mid = left + (right - left) / 2;
if (isValid(nums, mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}

return result;
}

private boolean isValid(int[] nums, int maxDiff) {
int prev = nums[0] != -1 ? nums[0] : -1;

for (int i = 1; i < nums.length; i++) {
int current = nums[i];
if (current == -1) {
if (prev != -1) {
current = Math.max(prev - maxDiff, 1);
} else {
current = 1;
}
}
if (prev != -1 && Math.abs(current - prev) > maxDiff) {
return false;
}
prev = current;
}
return true;
}

public static void main(String[] args) {
Solution solver = new Solution();

int[] nums1 = {1, 2, -1, 10, 8};
System.out.println(solver.minimizeMaxDiff(nums1));

int[] nums2 = {-1, -1, -1};
System.out.println(solver.minimizeMaxDiff(nums2));

int[] nums3 = {-1, 10, -1, 8};
System.out.println(solver.minimizeMaxDiff(nums3));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def minimizeMaxDifference(self, nums: List[int], k: int) -> int:
nums.sort()
l, r = 0, nums[-1] - nums[0]

def can_minimize(target):
ops = 0
for i in range(1, len(nums)):
if nums[i] - nums[i - 1] > target:
ops += (nums[i] - nums[i - 1] - 1) // target
if ops > k:
return False
return True

while l < r:
mid = (l + r) // 2
if can_minimize(mid):
r = mid
else:
l = mid + 1
return l
Loading