Skip to content

Instantly share code, notes, and snippets.

View markbrutx's full-sized avatar

Magzhan markbrutx

View GitHub Profile
@markbrutx
markbrutx / longest-palindromic-substring.py
Created May 11, 2023 16:30
5.Longest Palindromic Substring
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s) < 2:
return s
start = 0
end = 0
for i in range(len(s)):
len1 = self.expandAroundCen ter(s, i, i) # Odd-length palindromes
@markbrutx
markbrutx / median-of-two-sorted-arrays.py
Last active May 11, 2023 16:55
4. Median of Two Sorted Arrays
from typing import List
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
m, n = len(nums1), len(nums2)
left, right = 0, m
half_len = (m + n + 1) // 2
@markbrutx
markbrutx / zigzag-conversion.py
Created May 12, 2023 17:28
6. Zigzag Conversion
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
row_arr = [""] * numRows
row_idx = 1
going_up = True
for ch in s:
@markbrutx
markbrutx / reverse-integer.py
Created May 13, 2023 10:30
7. Reverse Integer
class Solution:
def reverse(self, x: int) -> int:
# Integer.MAX_VALUE = 2147483647 (end with 7)
# Integer.MIN_VALUE = -2147483648 (end with -8 )
MIN = -2147483648 # -2^31,
MAX = 2147483647 # 2^31 - 1
res = 0
while x:
@markbrutx
markbrutx / string-to-integer-atoi.py
Created May 13, 2023 11:14
8. String to Integer (atoi)
class Solution:
def myAtoi(self, str: str) -> int:
value, state, pos, sign = 0, 0, 0, 1
if len(str) == 0:
return 0
while pos < len(str):
current_char = str[pos]
if state == 0:
@markbrutx
markbrutx / product-of-array-except-self.py
Created May 16, 2023 16:02
238. Product of Array Except Self
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1] * (len(nums))
prefix = 1
for i in range(len(nums)):
res[i] = prefix
prefix *= nums[i]
@markbrutx
markbrutx / top-k-frequent-elements.py
Created May 16, 2023 16:05
347. Top K Frequent Elements
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count = {}
freq = [[] for i in range(len(nums) + 1)]
for n in nums:
count[n] = 1 + count.get(n, 0)
for n, c in count.items():
freq[c].append(n)
@markbrutx
markbrutx / valid-sudoku.py
Created May 17, 2023 18:58
36. Valid Sudoku
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
cols = collections.defaultdict(set)
rows = collections.defaultdict(set)
squares = collections.defaultdict(set) # key = (r /3, c /3)
for r in range(9):
for c in range(9):
if board[r][c] == ".":
continue
@markbrutx
markbrutx / longest-consecutive-sequence.py
Created May 18, 2023 19:28
128. Longest Consecutive Sequence
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
numSet = set(nums)
longest = 0
for n in nums:
if (n-1) not in numSet:
length = 1
while (length + n) in numSet:
length +=1
@markbrutx
markbrutx / valid-palindrome.py
Created May 19, 2023 18:12
125. Valid Palindrome
class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
while l < r and not self.alphaNum(s[l]):
l += 1
while r > l and not self.alphaNum(s[r]):
r -= 1
if s[l].lower() != s[r].lower():