Skip to content

Instantly share code, notes, and snippets.

View atn19808's full-sized avatar

Anh Ngo atn19808

View GitHub Profile
@atn19808
atn19808 / gist:7d05133c327d17b6a6d70fe27ac9f907
Created October 29, 2025 04:51
Convert an Array Into a 2D Array With Conditions
class Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
ans = [set()]
# m {n:cur_index}
m = {}
for n in nums:
m[n] = m.get(n, -1) + 1
if m[n] + 1 > len(ans):
ans.append(set())
ans[-1].add(n)
@atn19808
atn19808 / gist:9f7e6f31fe3bcc0717d92bc8dc43f079
Created October 29, 2025 04:51
Smallest Value of the Rearranged Number
class Solution:
def smallestNumber(self, num: int) -> int:
sign = 1
if num < 0:
sign = -1
num = num * -1
ans = [int(c) for c in str(num)]
ans.sort()
if sign == -1:
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
hor = [set() for i in range(9)]
ver = [set() for j in range(9)]
boxes = [[set() for j in range(3)] for i in range(3)]
for i in range(9):
for j in range(9):
num = board[i][j]
if num in '123456789':
@atn19808
atn19808 / gist:450d09a9b0e3bfced6275e827518f29f
Created October 29, 2025 04:50
Increasing Triplet Subsequence
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
first = second = float('inf')
for n in nums:
if n <= first:
first = n
elif n <= second:
second = n
else:
@atn19808
atn19808 / gist:c258ccc78e4cdb0186bb457749b90d7b
Created October 29, 2025 04:50
Sort Even and Odd Indices Independently
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
even = []
odd = []
for i in range(len(nums)):
if i % 2 == 0:
even.append(nums[i])
else:
odd.append(nums[i])
@atn19808
atn19808 / gist:2949e70be5943eb9e87c5071de4e96a1
Created October 29, 2025 04:49
Remove Duplicates from Sorted Array
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
k = 0
for i in range(len(nums)):
if nums[i] > nums[k]:
k += 1
nums[k] = nums[i]
return k + 1
class Solution:
def validPalindrome(self, s: str) -> bool:
def isPalindrome(l, r) -> bool:
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True
@atn19808
atn19808 / gist:a0553b69c1ab43badc40061ce1498448
Created October 29, 2025 04:38
Find Numbers with Even Number of Digits
class Solution:
def findNumbers(self, nums: List[int]) -> int:
def countDigits(num:int) -> int:
ans = 1
while num > 9:
num = num // 10
ans += 1
return ans
res = 0
for n in nums:
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxL = 0
cur = 0
for n in nums:
if n == 1:
cur += 1
maxL = max(maxL, cur)
else: