commit b027200d789669f827c66acbb1531882a457b492
parent ebf224386f9c7b7ee6a7773dc9ecc6d3d8cd67ef
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 18 Aug 2026 14:06:40 -0500
Did a few leetcode problems in python
Diffstat:
5 files changed, 122 insertions(+), 0 deletions(-)
diff --git a/longest-subarray-of-1s-after-deleting-one-element/longest-subarray-of-1s-after-deleting-one-element.py b/longest-subarray-of-1s-after-deleting-one-element/longest-subarray-of-1s-after-deleting-one-element.py
@@ -0,0 +1,22 @@
+def longest(nums, start):
+ running = 0
+ used = False
+ skip = 0
+
+ for idx in range(start, len(nums)):
+ if nums[idx] == 1:
+ running += 1
+ if nums[idx] == 0:
+ if used:
+ break
+ else:
+ skip = longest(nums, idx + 1)
+ used = True
+ if not used:
+ running -= 1
+
+ return max(running, skip)
+
+class Solution:
+ def longestSubarray(self, nums: List[int]) -> int:
+ return longest(nums, 0)
diff --git a/top-k-frequent-words/top-k-frequent-words-v2.py b/top-k-frequent-words/top-k-frequent-words-v2.py
@@ -0,0 +1,35 @@
+import heapq
+
+class OrderedWord:
+ def __init__(self, word):
+ self.word = word
+ def __lt__(self, other):
+ return self.word > other.word
+
+class Solution:
+ def topKFrequent(self, words: List[str], k: int) -> List[str]:
+
+ counts = {}
+ for word in words:
+ if word in counts:
+ counts[word] += 1
+ else:
+ counts[word] = 1
+
+ heap = []
+
+ for word in counts:
+ heappush (heap, (counts[word], OrderedWord(word)))
+ if len(heap) > k:
+ heappop (heap)
+
+ heap_ordered = []
+
+ for item in heap:
+ heappush (heap_ordered, (-item[0], item[1].word))
+
+ res = []
+
+ for i in range(k):
+ res.append(heappop(heap_ordered)[1])
+ return res
diff --git a/top-k-frequent-words/top-k-frequent-words.py b/top-k-frequent-words/top-k-frequent-words.py
@@ -0,0 +1,22 @@
+import heapq
+
+class Solution:
+ def topKFrequent(self, words: List[str], k: int) -> List[str]:
+
+ counts = {}
+ for word in words:
+ if word in counts:
+ counts[word] += 1
+ else:
+ counts[word] = 1
+
+ heap = []
+
+ for word in counts:
+ heappush (heap, (-counts[word], word))
+
+ res = []
+
+ for i in range(k):
+ res.append(heappop(heap)[1])
+ return res
diff --git a/transpose/transpose.py b/transpose/transpose.py
@@ -0,0 +1,8 @@
+class Solution:
+ def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
+ result = []
+ for x in range(0, len(matrix[0])):
+ result.append([])
+ for y in range(0, len(matrix)):
+ result[x].append(matrix[y][x])
+ return result
diff --git a/valid-square/valid-square.py b/valid-square/valid-square.py
@@ -0,0 +1,35 @@
+def dist(p1,p2):
+ return sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))
+
+class Solution:
+ def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
+ dists = [dist(p1, p2), dist(p2, p3), dist(p3, p4), dist(p4, p1), dist(p1, p3), dist(p2, p4)]
+
+ d1 = None
+ d1_count = 0
+ d2 = None
+ d2_count = 0
+
+ for distance in dists:
+ if distance == 0:
+ return False
+
+ if d1 is None:
+ d1 = distance
+ d1_count += 1
+ continue
+
+ if distance != d1 and d2 is None:
+ d2 = distance
+ d2_count += 1
+ continue
+
+ if distance == d1:
+ d1_count += 1
+ if distance == d2:
+ d2_count += 1
+
+
+ if (d1_count == 4 and d2_count == 2) or (d2_count == 4 and d1_count == 2):
+ return True
+ return False