Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class LRUCache {
private capacity: number;
private cache: Map<number, number>;

constructor(capacity: number) {
this.capacity = capacity;
this.cache = new Map<number, number>();
}

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

22 changes: 22 additions & 0 deletions tests/test_150_questions_round_22/test_65_lru_cache_round_22.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)