Skip to content

2740. Find the Value of the Partition 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  int findValueOfPartition(vector<int>& nums) {
    int ans = INT_MAX;

    sort(begin(nums), end(nums));

    for (int i = 1; i < nums.size(); ++i)
      ans = min(ans, nums[i] - nums[i - 1]);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public int findValueOfPartition(int[] nums) {
    int ans = Integer.MAX_VALUE;

    Arrays.sort(nums);

    for (int i = 1; i < nums.length; ++i)
      ans = Math.min(ans, nums[i] - nums[i - 1]);

    return ans;
  }
}
1
2
3
class Solution:
  def findValueOfPartition(self, nums: list[int]) -> int:
    return min(b - a for a, b in itertools.pairwise(sorted(nums)))