DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `24`

πŸ”Ή Problem: 2294. Partition Array Such That Maximum Difference Is K

Difficulty: #Medium
Tags: #Greedy, #Sorting


πŸ“ Problem Summary

You are given an array nums and an integer k. Your goal is to partition the array into the minimum number of subsequences, such that:

  • Each subsequence contains contiguous elements (not necessarily adjacent in the original array, but you pick them together after sorting),
  • The maximum difference between the smallest and largest element in a subsequence is at most k.

Return the minimum number of such subsequences required to partition the array.


🧠 My Thought Process

  • Brute Force Idea:
    I initially thought about trying all groupings using backtracking or recursion, keeping track of the max and min in each group. But it was obviously too slow for large inputs (n ≀ 10⁡), and clearly would TLE.

  • Optimized Strategy:
    Sorting the array simplifies the problem. Once sorted, we can just greedily group elements until the difference between the current element and the starting element exceeds k. When it does, start a new group.

βœ… This way, we guarantee we form groups with the minimum possible size and minimize total groups.

  • Algorithm Used: [[greedy]] + [[sorting]]

βš™οΈ Code Implementation (Python)

class Solution:
    def partitionArray(self, nums: List[int], k: int) -> int:
        nums.sort()
        res = 0
        left = 0
        right = 0

        while right < len(nums):
            if nums[right] - nums[left] <= k:
                right += 1
            else:
                res += 1
                left = right

        return res + 1
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n log n) β€” due to sorting
  • Space: O(1) β€” no extra space used (in-place logic after sorting)

🧩 Key Takeaways

  • βœ… Sorting + Greedy is a powerful combo when you care about differences between values.
  • πŸ’‘ Don’t overthink with complicated DP when a greedy grouping strategy can work.
  • πŸ’­ Problems that ask for minimizing the number of groups with a constraint often hint at greedy or sliding window.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help? β†’ Yes
  • [x] Did I write code from scratch? β†’ Yes
  • [x] Did I understand why it works? β†’ Yes
  • [x] Will I be able to recall this in a week? β†’ Absolutely. Classic greedy stuff.

πŸ“š Related Problems

  • [[1438 Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit]]
  • [[2779 Maximum Beauty of an Array After Applying Operation]]

πŸš€ Progress Tracker

Metric Value
Day 24
Total Problems Solved 356
Confidence Today πŸ˜ƒ

Top comments (0)