PORTFOLIO / LEETCODE

Leetcode Problemen

Van uitdagende algoritmes tot slimme oplossingen. Hier vind je mijn LeetCode-uitwerkingen en de voortgang van mijn programmeervaardigheden.

DSA

LEETCODE / ALLE PROBLEMEN EN OPLOSSINGEN

LEETCODE PROBLEMEN

// voor elk probleem een oplossing

Programmeertaal

Rating

Moeilijkheid

Runtime

Memory

1. Two Sum

Easy

Programmeertaal:
Python
Rating:
2.0/10
Runtime ms:
999.99
Runtime Percentage:
5.00%
Memory MB:
19.56
Memory Percentage:
99.42%

Functions:

  • len()

Data Structures:

  • list

Control Structures:

  • while loop
Leetcode (opent in een nieuw tabblad)
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        slow, fast = 0, 1
       
        while True:
            if fast == len(nums):
                slow += 1
                fast = slow + 1

            if nums[slow] + nums[fast] == target:
                return [slow, fast]
            else:
                fast += 1

3. Longest Substring Without Repeating Characters

Medium

Programmeertaal:
Python
Rating:
2.0/10
Runtime ms:
27.00
Runtime Percentage:
100.00%
Memory MB:
13.27
Memory Percentage:
98.89%

Functions:

  • set()
  • max()
  • len()
  • range()

Methods:

  • .remove()
  • .add()

Control Structures:

  • for loop
  • while loop

Algorithms:

  • Sliding Window
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        left = 0
        longest = 0
        sett = set()

        for right in range(len(s)):
            while s[right] in sett:
                sett.remove(s[left])
                left += 1

            width = (right - left) + 1
            longest = max(longest, width)
            sett.add(s[right])

        return longest

9. Palindrome Number

Easy

Programmeertaal:
Python
Rating:
2.0/10
Runtime ms:
40.00
Runtime Percentage:
5.38%
Memory MB:
12.33
Memory Percentage:
56.27%

Functions:

  • int()
  • str()
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def isPalindrome(self, x):
        if str(x)[0] == "-":
            return False

        reversed_num = int(str(x)[::-1])

        if x == reversed_num:
            return True
        else:
            return False

11. Container With Most Water

Medium

Programmeertaal:
Python
Rating:
5.0/10
Runtime ms:
58.00
Runtime Percentage:
56.18%
Memory MB:
29.70
Memory Percentage:
15.18%

Functions:

  • float()
  • abs()
  • max()
  • len()

Data Structures:

  • list

Control Structures:

  • while loop

Algorithms:

  • Two Pointers
Leetcode (opent in een nieuw tabblad)
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left = 0
        right = len(height) - 1
        current_vol = float("-inf")
        max_vol = current_vol

        while left < right:
            if height[left] < height[right]:
                current_vol = height[left] * abs(right - left)
                left += 1
            else:
                current_vol = height[right] * abs(left - right)
                right -= 1
           
            max_vol = max(current_vol, max_vol)

        return max_vol

13. Roman to Integer

Easy

Programmeertaal:
Python
Rating:
4.0/10
Runtime ms:
18.00
Runtime Percentage:
12.40%
Memory MB:
12.31
Memory Percentage:
58.85%

Functions:

  • len()
  • enumerate()

Methods:

  • .append()
  • .get()

Control Structures:

  • for loop
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def romanToInt(self, s):
        roman = {
            "I": 1,
            "V": 5,
            "X": 10,
            "L": 50,
            "C": 100,
            "D": 500,
            "M": 1000
            }

        number_list = []
        total_number = 0

        for roman_num in s:
            roman_decimal = roman.get(roman_num)
            number_list.append(roman_decimal)
                
        for index, number in enumerate(number_list):
            if (index + 1) != len(number_list):
                if number_list[index] < number_list[index + 1]:
                    total_number -= number
                total_number += number
            else:
                total_number += number
            
        return total_number

15. 3Sum

Medium

Programmeertaal:
Python
Rating:
5.0/10
Runtime ms:
723.00
Runtime Percentage:
28.11%
Memory MB:
22.17
Memory Percentage:
80.93%

Functions:

  • len()

Methods:

  • .sort()
  • .append()

Data Structures:

  • list

Control Structures:

  • for loop
  • while loop
  • nested loop

Algorithms:

  • Two Pointers
Leetcode (opent in een nieuw tabblad)
class Solution:
    def threeSum(self, nums: list[int]) -> list[list[int]]:
        if len(nums) <= 2:
            return []

        left = 1
        right = len(nums) - 1
        valid_sums = []
        current_sum = 0
        nums.sort()

        for i in range(len(nums)):
            if i > 0 and nums[i] == nums[i - 1]:
                continue      

            left = i + 1
            right = len(nums) - 1

            while left < right:
                current_sum = nums[i] + nums[left] + nums[right]

                if current_sum < 0:
                    left += 1
                if current_sum > 0:
                    right -= 1
                if current_sum == 0:
                    new_sum = [nums[i], nums[left], nums[right]]
                    valid_sums.append(new_sum)
                    left += 1
                    right -= 1

                    while left < right and nums[left] == nums[left - 1]:
                        left += 1
                    while left < right and nums[right] == nums[right + 1]:
                        right -= 1
               
        return valid_sums

26. Remove Duplicates from Sorted Array

Easy

Programmeertaal:
Python
Rating:
3.0/10
Runtime ms:
0.00
Runtime Percentage:
100.00%
Memory MB:
20.53
Memory Percentage:
43.84%

Functions:

  • len()
  • range()

Data Structures:

  • list

Control Structures:

  • for loop

Algorithms:

  • Two Pointers
  • Fast Pointer / Runner Technique
Leetcode (opent in een nieuw tabblad)
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow = 0
        counter = 1

        if len(nums) == 0:
            return []


        for fast in range(1, len(nums)):
            if nums[fast] != nums[slow]:
                counter += 1
                slow += 1
                nums[slow] = nums[fast]

        return counter

27. Remove Element

Easy

Programmeertaal:
Python
Rating:
1.0/10
Runtime ms:
4.00
Runtime Percentage:
3.15%
Memory MB:
12.36
Memory Percentage:
55.00%

Functions:

  • len()

Data Structures:

  • list

Control Structures:

  • List Comprehension
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def removeElement(self, nums, val):
        nums[:] = [num for num in nums if num != val]

        return len(nums)

35. Search Insert Position

Easy

Programmeertaal:
Python
Rating:
1.0/10
Runtime ms:
0.00
Runtime Percentage:
100.00%
Memory MB:
13.08
Memory Percentage:
31.49%

Functions:

  • enumerate()

Data Structures:

  • list

Control Structures:

  • for loop

Algorithms:

  • Linear Search
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def searchInsert(self, nums, target):
        counter = 0

        for i, n in enumerate(nums):
            if target == n:
                return i
            else:
                if target < n:
                    new_index = counter
                else:
                    counter += 1

        return counter

58. Length of Last Word

Easy

Programmeertaal:
Python
Rating:
1.0/10
Runtime ms:
1.00
Runtime Percentage:
9.32%
Memory MB:
12.53
Memory Percentage:
17.16%

Functions:

  • len()

Methods:

  • .split()
Leetcode (opent in een nieuw tabblad)
class Solution(object):
    def lengthOfLastWord(self, s):
        words = s.split()
        return len(words[-1])
Laden…