Find the Value of the Partition — LeetCode 2740 Python Solution

MediumArraySorting
Problem
#2740
Pattern
Sorting
Reading time
2 min

The problem

You are given a positive integer array nums. Partition nums into two arrays, nums1 and nums2, such that: Each element of the array nums belongs to either the array nums1 or the array nums2.

Example

Input
nums = [1,3,2,4]
Output
1
Explanation
We can partition the array nums into nums1 = [1,2] and nums2 = [3,4].

Python solution

Python
class Solution:
    def findValueOfPartition(self, nums: List[int]) -> int:
        nums.sort()
        return min(b - a for a, b in pairwise(nums))

Complexity

MeasureComplexity
TimeO(n \times \log n)
SpaceO(\log n) auxiliary

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2740. Find the Value of the Partition is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2740. Find the Value of the Partition?
LeetCode 2740. Find the Value of the Partition is rated Medium on LeetCode.
What is the time complexity of LeetCode 2740. Find the Value of the Partition?
The Python solution on this page runs in O(n \times \log n).
What is the space complexity of LeetCode 2740. Find the Value of the Partition?
The Python solution on this page uses O(\log n) auxiliary space.
What topics does LeetCode 2740. Find the Value of the Partition cover?
LeetCode 2740. Find the Value of the Partition is tagged Array and Sorting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview