Skip to content

Commit 7e9dc3d

Browse files
committed
Added racket
1 parent d8321c1 commit 7e9dc3d

File tree

24 files changed

+720
-13
lines changed

24 files changed

+720
-13
lines changed

src/main/racket/g0001_0100/s0079_word_search/Solution.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation #Backtracking
2-
; #Algorithm_II_Day_9_Recursion_Backtracking #Udemy_Backtracking/Recursion
3-
; #Big_O_Time_O(2^n)_Space_O(n*2^n) #2025_02_04_Time_0_(100.00%)_Space_101.30_(100.00%)
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
2+
; #Algorithm_II_Day_11_Recursion_Backtracking #Top_Interview_150_Backtracking
3+
; #Big_O_Time_O(4^(m*n))_Space_O(m*n) #2025_02_07_Time_2517_(100.00%)_Space_130.64_(_%)
44

55
(define/contract (exist board word)
66
(-> (listof (listof char?)) string? boolean?)

src/main/racket/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Tree #Binary_Tree
2-
; #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Top_Interview_150_Binary_Tree_General
3-
; #Big_O_Time_O(N)_Space_O(N) #2025_02_05_Time_23_(100.00%)_Space_101.43_(100.00%)
1+
; #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Stack #Linked_List
2+
; #Udemy_Linked_List #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(N)
3+
; #2025_02_07_Time_0_(100.00%)_Space_101.49_(100.00%)
44

55
; Definition for a binary tree node.
66
#|

src/main/racket/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.rkt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
; #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2-
; #Data_Structure_I_Day_3_Array #Dynamic_Programming_I_Day_7 #Level_1_Day_5_Greedy #Udemy_Arrays
3-
; #Top_Interview_150_Array/String #Big_O_Time_O(N)_Space_O(1)
4-
; #2025_02_05_Time_7_(100.00%)_Space_131.60_(50.00%)
1+
; #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Depth_First_Search
2+
; #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_General
3+
; #Big_O_Time_O(N)_Space_O(N) #2025_02_07_Time_0_(100.00%)_Space_131.06_(100.00%)
54

65
; Definition for a binary tree node.
76
#|

src/main/racket/g0101_0200/s0136_single_number/Solution.rkt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2-
; #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N)
3-
; #2025_02_05_Time_43_(100.00%)_Space_142.17_(100.00%)
1+
; #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation
2+
; #Data_Structure_II_Day_1_Array #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Integers
3+
; #Top_Interview_150_Bit_Manipulation #Big_O_Time_O(N)_Space_O(1)
4+
; #2025_02_07_Time_0_(100.00%)_Space_129.05_(100.00%)
45

56
(define (single-number nums)
67
(apply bitwise-xor nums)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table
2+
; #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
3+
; #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP
4+
; #Big_O_Time_O(M+max*N)_Space_O(M+N+max) #2025_02_06_Time_4_(100.00%)_Space_102.55_(100.00%)
5+
6+
(define/contract (word-break s wordDict)
7+
(-> string? (listof string?) boolean?)
8+
(let ((memo (make-hash)))
9+
(define (dp i)
10+
(cond
11+
[(= i (string-length s)) #t]
12+
[(hash-has-key? memo i) (hash-ref memo i)]
13+
[else
14+
(let loop ((words wordDict))
15+
(if (null? words)
16+
(begin (hash-set! memo i #f) #f)
17+
(let* ((word (car words))
18+
(len (string-length word)))
19+
(if (and (<= (+ i len) (string-length s))
20+
(string=? (substring s i (+ i len)) word)
21+
(dp (+ i len)))
22+
(begin (hash-set! memo i #t) #t)
23+
(loop (cdr words))))))]))
24+
(dp 0)))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
139\. Word Break
2+
3+
Medium
4+
5+
Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words.
6+
7+
**Note** that the same word in the dictionary may be reused multiple times in the segmentation.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "leetcode", wordDict = ["leet","code"]
12+
13+
**Output:** true
14+
15+
**Explanation:** Return true because "leetcode" can be segmented as "leet code".
16+
17+
**Example 2:**
18+
19+
**Input:** s = "applepenapple", wordDict = ["apple","pen"]
20+
21+
**Output:** true
22+
23+
**Explanation:** Return true because "applepenapple" can be segmented as "apple pen apple".
24+
25+
Note that you are allowed to reuse a dictionary word.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 300`
36+
* `1 <= wordDict.length <= 1000`
37+
* `1 <= wordDict[i].length <= 20`
38+
* `s` and `wordDict[i]` consist of only lowercase English letters.
39+
* All the strings of `wordDict` are **unique**.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Design #Linked_List
2+
; #Doubly_Linked_List #Udemy_Linked_List #Top_Interview_150_Linked_List
3+
; #Big_O_Time_O(1)_Space_O(capacity) #2025_02_06_Time_471_(100.00%)_Space_163.64_(100.00%)
4+
5+
(define lru-cache%
6+
(class object%
7+
(super-new)
8+
9+
(init-field capacity)
10+
11+
(define head #f)
12+
(define tail #f)
13+
(define cache (make-hash))
14+
15+
(define/private (move-to-head node)
16+
(when (not (eq? node head))
17+
(when (eq? node tail)
18+
(set! tail (hash-ref node 'prev))
19+
(when tail (hash-set! tail 'next #f)))
20+
(let ((prev (hash-ref node 'prev #f))
21+
(next (hash-ref node 'next #f)))
22+
(when prev (hash-set! prev 'next next))
23+
(when next (hash-set! next 'prev prev))
24+
(hash-set! node 'prev #f)
25+
(hash-set! node 'next head)
26+
(when head (hash-set! head 'prev node))
27+
(set! head node))))
28+
29+
(define/public (get key)
30+
(let ((node (hash-ref cache key #f)))
31+
(if node
32+
(begin (move-to-head node)
33+
(hash-ref node 'value))
34+
-1)))
35+
36+
(define/public (put key value)
37+
(let ((node (hash-ref cache key #f)))
38+
(if node
39+
(begin
40+
(hash-set! node 'value value)
41+
(move-to-head node))
42+
(begin
43+
(when (>= (hash-count cache) capacity)
44+
(when tail
45+
(hash-remove! cache (hash-ref tail 'key))
46+
(set! tail (hash-ref tail 'prev #f))
47+
(when tail (hash-set! tail 'next #f))))
48+
(let ((new-node (make-hash)))
49+
(hash-set! new-node 'key key)
50+
(hash-set! new-node 'value value)
51+
(hash-set! new-node 'prev #f)
52+
(hash-set! new-node 'next head)
53+
(when head (hash-set! head 'prev new-node))
54+
(set! head new-node)
55+
(unless tail (set! tail new-node))
56+
(hash-set! cache key new-node))))))))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
146\. LRU Cache
2+
3+
Medium
4+
5+
Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**.
6+
7+
Implement the `LRUCache` class:
8+
9+
* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`.
10+
* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`.
11+
* `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key.
12+
13+
The functions `get` and `put` must each run in `O(1)` average time complexity.
14+
15+
**Example 1:**
16+
17+
**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]]
18+
19+
**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4]
20+
21+
**Explanation:**
22+
23+
LRUCache lRUCache = new LRUCache(2);
24+
25+
lRUCache.put(1, 1); // cache is {1=1}
26+
27+
lRUCache.put(2, 2); // cache is {1=1, 2=2}
28+
29+
lRUCache.get(1); // return 1
30+
31+
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
32+
33+
lRUCache.get(2); // returns -1 (not found)
34+
35+
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
36+
37+
lRUCache.get(1); // return -1 (not found)
38+
39+
lRUCache.get(3); // return 3
40+
41+
lRUCache.get(4); // return 4
42+
43+
**Constraints:**
44+
45+
* `1 <= capacity <= 3000`
46+
* <code>0 <= key <= 10<sup>4</sup></code>
47+
* <code>0 <= value <= 10<sup>5</sup></code>
48+
* At most <code>2 * 10<sup>5</sup></code> calls will be made to `get` and `put`.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
2+
; #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Top_Interview_150_Divide_and_Conquer
3+
; #Big_O_Time_O(log(N))_Space_O(log(N)) #2025_02_08_Time_31_(100.00%)_Space_132.15_(100.00%)
4+
5+
; Definition for singly-linked list:
6+
#|
7+
8+
; val : integer?
9+
; next : (or/c list-node? #f)
10+
(struct list-node
11+
(val next) #:mutable #:transparent)
12+
13+
; constructor
14+
(define (make-list-node [val 0])
15+
(list-node val #f))
16+
17+
|#
18+
19+
; Helper function to split the list into two halves
20+
(define (split-list head)
21+
(let loop ([slow head]
22+
[fast head]
23+
[pre #f])
24+
(cond
25+
[(or (not fast) (not (list-node-next fast)))
26+
(when pre
27+
(set-list-node-next! pre #f))
28+
slow]
29+
[else
30+
(loop (list-node-next slow)
31+
(list-node-next (list-node-next fast))
32+
slow)])))
33+
34+
; Helper function to merge two sorted lists
35+
(define (merge-lists l1 l2)
36+
(let ([dummy (list-node 1 #f)])
37+
(let loop ([curr dummy]
38+
[first l1]
39+
[second l2])
40+
(cond
41+
[(not first)
42+
(set-list-node-next! curr second)]
43+
[(not second)
44+
(set-list-node-next! curr first)]
45+
[else
46+
(if (<= (list-node-val first) (list-node-val second))
47+
(begin
48+
(set-list-node-next! curr first)
49+
(loop first (list-node-next first) second))
50+
(begin
51+
(set-list-node-next! curr second)
52+
(loop second first (list-node-next second))))])
53+
(list-node-next dummy))))
54+
55+
; Main sort-list function with contract
56+
(define/contract (sort-list head)
57+
(-> (or/c list-node? #f) (or/c list-node? #f))
58+
(cond
59+
[(or (not head) (not (list-node-next head)))
60+
head]
61+
[else
62+
(let* ([middle (split-list head)]
63+
[first (sort-list head)]
64+
[second (sort-list middle)])
65+
(merge-lists first second))]))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
148\. Sort List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
10+
11+
**Input:** head = [4,2,1,3]
12+
13+
**Output:** [1,2,3,4]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
18+
19+
**Input:** head = [-1,5,3,4,0]
20+
21+
**Output:** [-1,0,3,4,5]
22+
23+
**Example 3:**
24+
25+
**Input:** head = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
32+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
33+
34+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?

0 commit comments

Comments
 (0)