Skip to content

Commit 7739403

Browse files
Merge pull request #2 from paxtonfitzpatrick/main
adding my solution & notes for problem 1509
2 parents 92197d8 + aed66f8 commit 7739403

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

problems/1509/paxton.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [Problem 1509: Minimum difference between largest and smallest value in three moves](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/description/)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
- reducing the difference between the largest and smallest values requires changing one of those values to something else
5+
- sounds like we'll need to sort `nums`, which takes $O(n \log n)$ time
6+
- could get away without sorting by finding `min(nums)` and `max(nums)`, `pop`ping one from the list, and repeating
7+
- probably not worth it... I think modifying the list in place multiple times would be slower than just doing it once with `.sort()`, and the solution doesn't actually ask for the list containing the min difference after 3 moves, just what that min difference *would* be.
8+
- How do we decide what to change a selected value to?
9+
- shouldn't specifically matter as long as the new value doesn't become the min or max for a subsequent step, or the final list -- so something towards the "center" of the values
10+
- best choice might theoretically be the median? I think the mean would be a bad choice (e.g., we oculd have `[1, 2, 3, 1000]`). But again, we don't actually have to return the final list, so we can be hand-wavey about that.
11+
- How do we decide whether it's better to change the min or the max?
12+
- Maybe whichever is further from the median? Once the list is sorted, median is easy to find (index with `len(nums) // 2`)
13+
- actually, if we can only make 3 moves, and each move entails changing the either the min or max, there aren't that many combinations of moves we could make... rather than coming up with a way to figure out the "optimal" move 3 times, can we just test all possibilities? Possible combinations (order of moves shouldn't matter):
14+
- change min 3 times
15+
- change max 3 times
16+
- change min 2 times, change max 1 time
17+
- change min 1 time, change max 2 times
18+
- So it's definitely worth sorting the list, because that will make the values of interest easy to access as the 1st 3 and last 3 items.
19+
- Also, if the list contains <= 4 items, the answer is 0 because we can change all the values to the same thing. So we can short circuit that case.
20+
- hmm... actually, this is also true if it contains > 4 items, and all but 3 are the same value (e.g., `[1, 2, 5, 5, 5, 5, 5, 9]`)... do we need to check for this?
21+
- I think not, because if all but 3 items are the same value, then any possible case of that shoudl be covered by checking the possible combinations listed above. E.g.:
22+
- `[1, 1, 1, 1, 1, 2, 3, 4]`: optimal moves are changing max 3x
23+
- `[1, 2, 2, 2, 2, 2, 3, 4]`: optimal moves are changin min once and max twice
24+
- `[1, 2, 3, 4, 4, 4, 4, 4]`: optimal moves are changing min 3x
25+
- To test all possible combinations of moves, we just need to use the indices in the sorted list of the resulting min and max values after making all 3 moves from each possibility:
26+
- change min 3 times
27+
- new min would be `nums[3]`; new max would be `nums[-1]`
28+
- change max 3 times
29+
- new min would be `nums[0]`; new max would be `nums[-4]`
30+
- change min 2 times, change max 1 time
31+
- new min would be `nums[2]`; new max would be `nums[-2]`
32+
- change min 1 time, change max 2 times
33+
- new min would be `nums[1]`; new max would be `nums[-3]`
34+
35+
## Refining the problem
36+
37+
## Attempted solution(s)
38+
```python
39+
class Solution:
40+
def minDifference(self, nums: List[int]) -> int:
41+
if len(nums) <= 4:
42+
return 0
43+
44+
# note: in reality, should really copy the list and sort the copy so we
45+
# don't modify the input... but since that'd take a tiny bit extra time
46+
# and memory, and best practices aren't part of the criteria, I skipped
47+
# it
48+
nums.sort()
49+
return min(
50+
nums[-1] - nums[3], # change min 3x
51+
nums[-4] - nums[0], # change max 3x
52+
nums[-2] - nums[2], # change min 2x, max 1x
53+
nums[-3] - nums[1] # change min 1x, max 2x
54+
)
55+
```
56+
57+
![](https://github.com/paxtonfitzpatrick/leetcode-solutions/assets/26118297/9b7a1f40-c018-43dd-aa42-65888463af51)

0 commit comments

Comments
 (0)