Skip to content

Instantly share code, notes, and snippets.

View linminhtoo's full-sized avatar

Lin Min Htoo linminhtoo

View GitHub Profile
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
'''
@linminhtoo
linminhtoo / accounts_merge.py
Created March 12, 2025 15:19
https://leetcode.com/problems/accounts-merge/ this is slow, 1779 ms (5%), need to optimize
from typing import List
class UnionFind:
def __init__(self, total: int):
# each node's parent is itself
self.parents = list(range(total))
# for union by rank optimisation
self.ranks = [1 for _ in range(total)]
from collections import Counter
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
'''
anagram is just an unordered bag of letters
2 pointer sliding window approach on s?
@linminhtoo
linminhtoo / word_search_optimised_v2.py
Last active March 7, 2025 15:53
optimised v2: 99% runtime (3 ms), 65% memory (17.88 mb), optimised v1: 98% runtime (424 ms ), 40% memory (17.98 mb). v1 74% runtime (3537 ms), 87% memory (17.8 mb) https://leetcode.com/problems/word-search/
from collections import Counter
DIRS = [
(+1, 0),
(0, +1),
(-1, 0),
(0, -1)
]
DIGIT_TO_LETTERS = {
"2": ["a", "b", "c"],
"3": ["d", "e", "f"],
"4": ["g", "h", "i"],
"5": ["j", "k", "l"],
"6": ["m", "n", "o"],
"7": ["p", "q", "r", "s"],
"8": ["t", "u", "v"],
"9": ["w", "x", "y", "z"],
}
@linminhtoo
linminhtoo / construct_binary_tree_optimised.py
Last active March 5, 2025 17:38
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ the best way to solve this is to draw out the tree and both inorder & preorder arrays. 0 ms, beats 100% runtime. 19 mb, beats 67% memory.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
# build hashmap of indices
num_to_inorder_idx: dict[int, int] = {num: idx for idx, num in enumerate(inorder)}
@linminhtoo
linminhtoo / subsets.py
Created March 5, 2025 12:54
find all subsets
from typing import List
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
'''
DFS
naive solution: choose or don't choose at each index
mental notes:
@linminhtoo
linminhtoo / 3sum_2pointers.py
Created March 5, 2025 12:21
3 sum with 2 pointers without hashmap
class Solution:
# two-pointer solution
def threeSum(self, nums: list[int]) -> list[int]:
nums.sort()
seen = set()
len_nums = len(nums)
result_triplets = []
minhtoo@minhtoo:~/.nextflow/capsule/deps$ jstack 63923
2024-01-23 02:54:51
Full thread dump OpenJDK 64-Bit Server VM (17.0.6+10-LTS mixed mode, sharing):
Threads class SMR info:
_java_thread_list=0x00007f46f0001890, length=38, elements={
0x00007f4748028010, 0x00007f4748167420, 0x00007f4748168800, 0x00007f4748177b10,
0x00007f4748178ec0, 0x00007f474817a2d0, 0x00007f474817bd00, 0x00007f474817d230,
0x00007f474817e6a0, 0x00007f47481b30f0, 0x00007f47481b5050, 0x00007f4748fc0990,
0x00007f474892a1c0, 0x00007f4748de2880, 0x00007f4749203220, 0x00007f47494b6d80,
Jan-23 02:39:35.355 [main] DEBUG nextflow.cli.Launcher - $> nextflow run genepi/nf-gwas -r v1.0.4 -profile test,docker
Jan-23 02:39:35.404 [main] INFO nextflow.cli.CmdRun - N E X T F L O W ~ version 23.10.1
Jan-23 02:39:35.422 [main] DEBUG nextflow.plugin.PluginsFacade - Setting up plugin manager > mode=prod; embedded=false; plugins-dir=/home/minhtoo/.nextflow/plugins; core-plugins: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
Jan-23 02:39:35.429 [main] INFO o.pf4j.DefaultPluginStatusProvider - Enabled plugins: []
Jan-23 02:39:35.430 [main] INFO o.pf4j.DefaultPluginStatusProvider - Disabled plugins: []
Jan-23 02:39:35.432 [main] INFO org.pf4j.DefaultPluginManager - PF4J version 3.4.1 in 'deployment' mode
Jan-23 02:39:35.440 [main] INFO org.pf4j.AbstractPluginManager - No plugins
Jan-23 02:39:35.452 [main] DEBUG nextflow.scm.ProviderConfig - Using SCM config path: /home/minhtoo/.nextflow/scm
Jan-23 02: