Skip to content

Commit afcb134

Browse files
Merge branch 'main' of https://github.com/contextlab/leetcode-solutions into main
2 parents f7ceb1d + f18c6fa commit afcb134

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

problems/1509/katie.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Every optimal move is going to consist of moving an extreme value to the center, so you can just take the range of 4 potential subsets starting with index 0, 1, 2, or 3 all of length `len(nums)-3`
2+
3+
```
4+
class Solution(object):
5+
def minDifference(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
if len(nums) <=4:
11+
return 0
12+
nums.sort()
13+
best = nums[-1] - nums[3]
14+
for x in range(3):
15+
best = min(best, nums[-4+x] - nums[x])
16+
return best
17+
```
18+
<img width="604" alt="Screen Shot 2024-07-04 at 4 17 17 PM" src="https://github.com/KatieONell/leetcode-solutions/assets/12962290/e691f187-1c77-4ce2-92f4-0056bce4762a">

problems/2181/katie.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [Problem 2181: Merge Nodes In Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/description/)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
Essentially I want to hang on to each zero node, and progressively check the next nodes. Every node that isn't zero, the sum should be added to the OG node and then should be dropped out of the list by making a new link between the OG node and the nextnext node.
5+
When we get to another 0, we want to repeat this process.
6+
7+
## Refining the problem
8+
The final 0 is annoying... I'll add a statement where if the next.next is None, then break out of the loop
9+
10+
## Attempted solution(s)
11+
```
12+
# Definition for singly-linked list.
13+
# class ListNode(object):
14+
# def __init__(self, val=0, next=None):
15+
# self.val = val
16+
# self.next = next
17+
class Solution(object):
18+
def mergeNodes(self, head):
19+
"""
20+
:type head: Optional[ListNode]
21+
:rtype: Optional[ListNode]
22+
"""
23+
node = head
24+
while node.next != None:
25+
if node.next.val != 0:
26+
node.val += node.next.val
27+
node.next = node.next.next
28+
else:
29+
if node.next.next == None:
30+
node.next = None
31+
break
32+
node = node.next
33+
return head
34+
```
35+
<img width="479" alt="Screen Shot 2024-07-04 at 4 44 32 PM" src="https://github.com/KatieONell/leetcode-solutions/assets/12962290/1864b9ab-f280-431e-88c8-f118c64f03b4">
36+

problems/350/katie.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# [Problem 350: Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
first order of business is figuring out which elements are present in both; second is getting the number of times
5+
6+
## Refining the problem
7+
8+
## Attempted solution(s)
9+
```
10+
class Solution(object):
11+
def intersect(self, nums1, nums2):
12+
"""
13+
:type nums1: List[int]
14+
:type nums2: List[int]
15+
:rtype: List[int]
16+
"""
17+
intersection = []
18+
for x in set(nums1).intersection(set(nums2)):
19+
intersection.extend([x]*min(nums1.count(x), nums2.count(x)))
20+
return intersection
21+
```
22+
<img width="611" alt="Screen Shot 2024-07-04 at 4 27 44 PM" src="https://github.com/KatieONell/leetcode-solutions/assets/12962290/eda945af-174a-4b98-b6b2-0332b9db4fd6">

0 commit comments

Comments
 (0)