Skip to content

Instantly share code, notes, and snippets.

@geirtul
Last active February 5, 2025 08:21
Show Gist options
  • Save geirtul/31d995c1ba84b995c6f8ea5025ff6f27 to your computer and use it in GitHub Desktop.
Save geirtul/31d995c1ba84b995c6f8ea5025ff6f27 to your computer and use it in GitHub Desktop.
Ukens trial by fire løsninger

Uke 6

Lett

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii

Løsning
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return head
    
        curr = head
        vals = []
        while curr != None:
            vals.append(curr.val)
            curr = curr.next
    
        vals = sorted(vals)
        to_delete = []
        for i in range(0, len(vals) - 1):
            if vals[i + 1] == vals[i]:
                to_delete.append(vals[i])
        for el in to_delete:
            while el in vals:
                vals.remove(el)
        if not vals:
            return None
    
        prev = ListNode(vals[0])
        head = prev
        for i in range(1, len(vals)):
            prev.next = ListNode(vals[i])
            prev = prev.next
        return head

Vanskelig

https://leetcode.com/problems/candy/

Løsning

Gåte

The Bridge and Lantern Riddle

Four people are trying to cross a bridge. They all need to get across the bridge within 17 minutes, and they each travel across the bridge at different speeds:

  • Person A only needs 1 minute to cross the bridge.
  • Person B needs 2 minutes to cross the bridge.
  • Person C needs 5 minutes to cross the bridge.
  • Person D is pretty slow and requires a full 10 minutes to cross the bridge.

And there are a few additional challenges to consider.

  • The bridge can only hold two people at a time.
  • It’s dark and dangerous on the bridge. To safely cross, they must use a single shared lantern to light the way. The lantern must be used at all times while crossing the bridge in either direction.
Løsning
  • A og B krysser: 2min
  • A går tilbake: 3 min
  • C og D krysser: 13 min
  • B går tilbake: 15 min
  • A og B kryusser: 17 min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment