Skip to content

Commit b552f93

Browse files
committed
adding algo
1 parent 6ed9650 commit b552f93

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import OrderedDict
4+
5+
class LRUCache:
6+
7+
def __init__(self, capacity: int):
8+
self.capacity = capacity
9+
self.dic = OrderedDict()
10+
11+
12+
def get(self, key: int) -> int:
13+
14+
if key not in self.dic:
15+
return -1
16+
val = self.dic[key]
17+
self.dic.move_to_end(key)
18+
return val
19+
20+
def put(self, key: int, value: int) -> None:
21+
22+
self.dic[key] = value
23+
self.dic.move_to_end(key)
24+
25+
if len(self.dic) > self.capacity:
26+
self.dic.popitem(last=False)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
class Solution:
12+
def maxDepth(self, root: Optional[TreeNode]) -> int:
13+
14+
if not root:
15+
return 0
16+
else:
17+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
6+
constructor(val: number = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
7+
this.val = val;
8+
this.left = left;
9+
this.right = right;
10+
}
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class LRUCache {
2+
private capacity: number;
3+
private cache: Map<number, number>;
4+
5+
constructor(capacity: number) {
6+
this.capacity = capacity;
7+
this.cache = new Map<number, number>();
8+
}
9+
10+
get(key: number): number {
11+
if (!this.cache.has(key)) {
12+
return -1;
13+
}
14+
const value = this.cache.get(key)!;
15+
// Move to end (most recently used)
16+
this.cache.delete(key);
17+
this.cache.set(key, value);
18+
return value;
19+
}
20+
21+
put(key: number, value: number): void {
22+
// If key exists, delete it first to update position
23+
if (this.cache.has(key)) {
24+
this.cache.delete(key);
25+
}
26+
this.cache.set(key, value);
27+
28+
// If capacity exceeded, remove the least recently used (first item)
29+
if (this.cache.size > this.capacity) {
30+
const firstKey = this.cache.keys().next().value!;
31+
this.cache.delete(firstKey);
32+
}
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function maxDepth(root: TreeNode | null): number {
4+
if (!root) {
5+
return 0;
6+
} else {
7+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
8+
}
9+
}
10+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_65_lru_cache import LRUCache
4+
5+
class LRUCacheTestCase(unittest.TestCase):
6+
7+
def test_example_1(self):
8+
# Example 1:
9+
# Input: ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
10+
# [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
11+
# Output: [null, null, null, 1, null, -1, null, -1, 3, 4]
12+
13+
lRUCache = LRUCache(2)
14+
self.assertIsNone(lRUCache.put(1, 1)) # cache is {1=1}
15+
self.assertIsNone(lRUCache.put(2, 2)) # cache is {1=1, 2=2}
16+
self.assertEqual(lRUCache.get(1), 1) # return 1
17+
self.assertIsNone(lRUCache.put(3, 3)) # LRU key was 2, evicts key 2, cache is {1=1, 3=3}
18+
self.assertEqual(lRUCache.get(2), -1) # returns -1 (not found)
19+
self.assertIsNone(lRUCache.put(4, 4)) # LRU key was 1, evicts key 1, cache is {4=4, 3=3}
20+
self.assertEqual(lRUCache.get(1), -1) # return -1 (not found)
21+
self.assertEqual(lRUCache.get(3), 3) # return 3
22+
self.assertEqual(lRUCache.get(4), 4) # return 4
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_66_maximum_depth_binary_tree import Solution, TreeNode
4+
5+
class MaximumDepthBinaryTreeTestCase(unittest.TestCase):
6+
7+
def create_binary_tree(self, values):
8+
"""
9+
Helper function to create a binary tree from a list of values (level-order).
10+
11+
:param values: List of node values (None represents null nodes)
12+
:return: Root of the binary tree
13+
"""
14+
if not values:
15+
return None
16+
17+
root = TreeNode(values[0])
18+
queue = [root]
19+
i = 1
20+
21+
while queue and i < len(values):
22+
node = queue.pop(0)
23+
24+
if i < len(values) and values[i] is not None:
25+
node.left = TreeNode(values[i])
26+
queue.append(node.left)
27+
i += 1
28+
29+
if i < len(values) and values[i] is not None:
30+
node.right = TreeNode(values[i])
31+
queue.append(node.right)
32+
i += 1
33+
34+
return root
35+
36+
def test_example_1(self):
37+
# Example 1: Input: root = [3,9,20,null,null,15,7]
38+
# Output: 3
39+
solution = Solution()
40+
root = self.create_binary_tree([3, 9, 20, None, None, 15, 7])
41+
result = solution.maxDepth(root)
42+
self.assertEqual(result, 3)
43+
44+
def test_example_2(self):
45+
# Example 2: Input: root = [1,null,2]
46+
# Output: 2
47+
solution = Solution()
48+
root = self.create_binary_tree([1, None, 2])
49+
result = solution.maxDepth(root)
50+
self.assertEqual(result, 2)

0 commit comments

Comments
 (0)