diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_65_lru_cache.py b/src/my_project/interviews/top_150_questions_round_22/ex_65_lru_cache.py new file mode 100644 index 00000000..22b4f7ed --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_65_lru_cache.py @@ -0,0 +1,26 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import OrderedDict + +class LRUCache: + + def __init__(self, capacity: int): + self.capacity = capacity + self.dic = OrderedDict() + + + def get(self, key: int) -> int: + + if key not in self.dic: + return -1 + val = self.dic[key] + self.dic.move_to_end(key) + return val + + def put(self, key: int, value: int) -> None: + + self.dic[key] = value + self.dic.move_to_end(key) + + if len(self.dic) > self.capacity: + self.dic.popitem(last=False) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_66_maximum_depth_binary_tree.py b/src/my_project/interviews/top_150_questions_round_22/ex_66_maximum_depth_binary_tree.py new file mode 100644 index 00000000..a41e6978 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_66_maximum_depth_binary_tree.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + + if not root: + return 0 + else: + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/TreeNode.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/TreeNode.ts new file mode 100644 index 00000000..df3eb8a6 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/TreeNode.ts @@ -0,0 +1,11 @@ +export class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + + constructor(val: number = 0, left: TreeNode | null = null, right: TreeNode | null = null) { + this.val = val; + this.left = left; + this.right = right; + } +} \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_65_lru_cache.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_65_lru_cache.ts new file mode 100644 index 00000000..417c2c91 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_65_lru_cache.ts @@ -0,0 +1,34 @@ +class LRUCache { + private capacity: number; + private cache: Map; + + constructor(capacity: number) { + this.capacity = capacity; + this.cache = new Map(); + } + + get(key: number): number { + if (!this.cache.has(key)) { + return -1; + } + const value = this.cache.get(key)!; + // Move to end (most recently used) + this.cache.delete(key); + this.cache.set(key, value); + return value; + } + + put(key: number, value: number): void { + // If key exists, delete it first to update position + if (this.cache.has(key)) { + this.cache.delete(key); + } + this.cache.set(key, value); + + // If capacity exceeded, remove the least recently used (first item) + if (this.cache.size > this.capacity) { + const firstKey = this.cache.keys().next().value!; + this.cache.delete(firstKey); + } + } +} \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_66_maximum_depth_binary_tree.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_66_maximum_depth_binary_tree.ts new file mode 100644 index 00000000..f9898575 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_66_maximum_depth_binary_tree.ts @@ -0,0 +1,10 @@ +import { TreeNode } from './TreeNode'; + +function maxDepth(root: TreeNode | null): number { + if (!root) { + return 0; + } else { + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} + diff --git a/tests/test_150_questions_round_22/test_65_lru_cache_round_22.py b/tests/test_150_questions_round_22/test_65_lru_cache_round_22.py new file mode 100644 index 00000000..56f15cba --- /dev/null +++ b/tests/test_150_questions_round_22/test_65_lru_cache_round_22.py @@ -0,0 +1,22 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_65_lru_cache import LRUCache + +class LRUCacheTestCase(unittest.TestCase): + + def test_example_1(self): + # Example 1: + # Input: ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] + # [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] + # Output: [null, null, null, 1, null, -1, null, -1, 3, 4] + + lRUCache = LRUCache(2) + self.assertIsNone(lRUCache.put(1, 1)) # cache is {1=1} + self.assertIsNone(lRUCache.put(2, 2)) # cache is {1=1, 2=2} + self.assertEqual(lRUCache.get(1), 1) # return 1 + self.assertIsNone(lRUCache.put(3, 3)) # LRU key was 2, evicts key 2, cache is {1=1, 3=3} + self.assertEqual(lRUCache.get(2), -1) # returns -1 (not found) + self.assertIsNone(lRUCache.put(4, 4)) # LRU key was 1, evicts key 1, cache is {4=4, 3=3} + self.assertEqual(lRUCache.get(1), -1) # return -1 (not found) + self.assertEqual(lRUCache.get(3), 3) # return 3 + self.assertEqual(lRUCache.get(4), 4) # return 4 \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_66_maximum_depth_binary_tree_round_22.py b/tests/test_150_questions_round_22/test_66_maximum_depth_binary_tree_round_22.py new file mode 100644 index 00000000..dd48c4a4 --- /dev/null +++ b/tests/test_150_questions_round_22/test_66_maximum_depth_binary_tree_round_22.py @@ -0,0 +1,50 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_66_maximum_depth_binary_tree import Solution, TreeNode + +class MaximumDepthBinaryTreeTestCase(unittest.TestCase): + + def create_binary_tree(self, values): + """ + Helper function to create a binary tree from a list of values (level-order). + + :param values: List of node values (None represents null nodes) + :return: Root of the binary tree + """ + if not values: + return None + + root = TreeNode(values[0]) + queue = [root] + i = 1 + + while queue and i < len(values): + node = queue.pop(0) + + if i < len(values) and values[i] is not None: + node.left = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root + + def test_example_1(self): + # Example 1: Input: root = [3,9,20,null,null,15,7] + # Output: 3 + solution = Solution() + root = self.create_binary_tree([3, 9, 20, None, None, 15, 7]) + result = solution.maxDepth(root) + self.assertEqual(result, 3) + + def test_example_2(self): + # Example 2: Input: root = [1,null,2] + # Output: 2 + solution = Solution() + root = self.create_binary_tree([1, None, 2]) + result = solution.maxDepth(root) + self.assertEqual(result, 2) \ No newline at end of file