205-Isomorphic-Strings
https://leetcode.com/problems/isomorphic-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
return [*map(s.index, s)] == [*map(t.index, t)]
new Solution().isIsomorphic()
Score: 5
Read More
206-Reverse-Linked-List
https://leetcode.com/problems/reverse-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
newHead = self.reverseList(head.next)
head.next.next = head
head.next = None …
Read More
207-Course-Schedule
https://leetcode.com/problems/course-schedule
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from enum import Enum
class State(Enum):
kInit = 0
kVisiting = 1
kVisited = 2
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = [[] for _ in range(numCourses …
Read More
209-Minimum-Size-Subarray-Sum
https://leetcode.com/problems/minimum-size-subarray-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minSubArrayLen(self, s: int, nums: List[int]) -> int:
ans = math.inf
summ = 0
j = 0
for i, num in enumerate(nums):
summ += num
while summ >= s:
ans = min …
Read More
21-Merge-Two-Sorted-Lists
https://leetcode.com/problems/merge-two-sorted-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1 or not list2:
return list1 if list1 else list2
if list1.val > list2.val:
list1 …
Read More
210-Course-Schedule-Ii
https://leetcode.com/problems/course-schedule-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from enum import Enum
class State(Enum):
kInit = 0
kVisiting = 1
kVisited = 2
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
ans = []
graph = [[] for _ in …
Read More
212-Word-Search-Ii
https://leetcode.com/problems/word-search-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class TrieNode:
def __init__(self):
self.children: Dict[str, TrieNode] = defaultdict(TrieNode)
self.word: Optional[str] = None
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str …
Read More
213-House-Robber-Ii
https://leetcode.com/problems/house-robber-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) < 2:
return nums[0]
def rob(l: int, r: int) -> int:
dp1 = 0
dp2 …
Read More
214-Shortest-Palindrome
https://leetcode.com/problems/shortest-palindrome
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shortestPalindrome(self, s: str) -> str:
t = s[::-1]
for i in range(len(t)):
if s.startswith(t[i:]):
return t[:i] + s
return t + s
Read More
215-Kth-Largest-Element-In-An-Array
https://leetcode.com/problems/kth-largest-element-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
minHeap = []
for num in nums:
heapq.heappush(minHeap, num)
if len(minHeap) > k:
heapq.heappop(minHeap)
return minHeap[0]
Read More