I'm on a mission to master programming and tackle any coding challenge that comes my way. In this series, you'll find my daily journal entries, presented as simple bullet points. Nothing fancy—just my honest thoughts and experiences from each day. So, please, no judgment! 😂
Join me as I document my tech journey in Bangalore. Let's dive in!
Day 4 (03/07/24)
As usual started the tech activities by looking at today's LeetCode question 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
So, looking at this question, the first thing that came to mind was, how do we decide what elements to move? 🤔
After scribbling around in my notebook for a bit, I noticed that only changing the extreme elements (smallest or largest) would have any impact on the result. Since we can only make 3 moves, there are only three distinct extreme elements that we can change.
At first, I thought all I needed to do was iterate over the possible combinations of the extreme elements and change them with different elements so I could calculate the minimum difference for each combination.
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 3: return 0 nums.sort() curr_diff = nums[-1] - nums[0] for x in range(-3, 3): for y in range(-3, 3): for z in range(-3, 3): if x==y or z==x or y==z: continue for i in range(-4, 4): for j in range(-4, 4): for k in range(-4, 4): temp = list(nums) temp[x] = temp[i] temp[y] = temp[j] temp[z] = temp[k] curr_diff = min(curr_diff, max(temp) - min(temp)) return curr_diff
This gave me a Time Limit Exceeded error, but I'm always so happy to see that error, lol. It just means that I'm on the right track with my solution—I just need to optimize it now. In the second submission, I removed the different combinations in the inner loop as I noticed we can set all three elements to the same value.
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 3: return 0 nums.sort() curr_diff = nums[-1] - nums[0] for x in range(-3, 3): for y in range(-3, 3): for z in range(-3, 3): if x==y or z==x or y==z: continue for num in {nums[i] for i in range(-4, 4)}: temp = list(nums) temp[x] = temp[y] = temp[z] = num curr_diff = min(curr_diff, max(temp) - min(temp)) return curr_diff
With this one I was able to barely cross the submission threshold lol
Literally on the edge, xD. After this, I realized I must be making a big mistake somewhere. That's when it hit me—I don't even need to actually change the array elements; I can calculate the minimum difference without doing that.
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 3: return 0 nums.sort() curr_diff = nums[-1] - nums[0] for i in range(4): curr_diff = min(curr_diff,nums[-(4-i)] - nums[i]) return curr_diff
So, basically the bottleneck for this algorithm is
nums.sort()
. Since we only need the first 3 and last 3 elements, not the whole array, we can optimize this from O(nlogn) to O(n) by using a partial sort to just get the greatest 3 and smallest 3 numbers. Here's a one-liner I found on the solutions page:class Solution: def minDifference(self, a: List[int]) -> int: return min(map(sub,nlargest(4,a)[::-1],nsmallest(4,a)))
Revised sorting algorithms and basic data structures like stack, queue and heaps.
Was really tired after coming from the hospital. So I took a well deserved 4 hour nap
Gonna sleep early today as I'm still tired. See ya!