Skip to content

Commit d8321c1

Browse files
committed
Added racket
1 parent 5c2e020 commit d8321c1

File tree

45 files changed

+1259
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1259
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2-
; #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3-
; #AI_can_be_used_to_solve_the_task #2025_01_28_Time_0_(100.00%)_Space_102.07_(45.83%)
2+
; #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Top_Interview_150_Hashmap
3+
; #Big_O_Time_O(n)_Space_O(n) #AI_can_be_used_to_solve_the_task
4+
; #2025_02_05_Time_0_(100.00%)_Space_102.04_(40.82%)
45

56
(define (two-sum-iter nums target hash index)
67
(cond

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
22
; #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3-
; #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
4-
; #2025_01_28_Time_0_(100.00%)_Space_128.42_(12.50%)
3+
; #Top_Interview_150_Linked_List #Big_O_Time_O(max(N,M))_Space_O(max(N,M))
4+
; #AI_can_be_used_to_solve_the_task #2025_02_05_Time_0_(100.00%)_Space_128.08_(72.73%)
55

66
; Definition for singly-linked list:
77
#|

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
22
; #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3-
; #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
4-
; #2025_01_28_Time_134_(75.00%)_Space_132.36_(100.00%)
3+
; #Top_Interview_150_Sliding_Window #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
4+
; #2025_02_06_Time_119_(71.43%)_Space_131.37_(28.57%)
55

66
; Helper function to get the sublist up to 'v' excluded.
77
(define (take-till q v)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2-
; #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
3-
; #2025_01_28_Time_0_(100.00%)_Space_128.57_(100.00%)
2+
; #Top_Interview_150_Binary_Search #Big_O_Time_O(log(min(N,M)))_Space_O(1)
3+
; #AI_can_be_used_to_solve_the_task #2025_02_06_Time_0_(100.00%)_Space_128.80_(25.00%)
44

55
(define/contract (find-median-sorted-arrays nums1 nums2)
66
(-> (listof exact-integer?) (listof exact-integer?) flonum?)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
22
; #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
3-
; #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
4-
; #2025_01_28_Time_10_(50.00%)_Space_102.35_(50.00%)
3+
; #Dynamic_Programming_I_Day_17 #Udemy_Strings #Top_Interview_150_Multidimensional_DP
4+
; #Big_O_Time_O(n)_Space_O(n) #2025_02_06_Time_4_(100.00%)_Space_101.40_(85.71%)
55

66
(define (longest-palindrome s)
77
(define (expand-around-center s left right)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
2+
; #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP
3+
; #Big_O_Time_O(m*n)_Space_O(m*n) #2025_02_04_Time_92_(100.00%)_Space_133.91_(100.00%)
4+
5+
; dynamic programming helper function
6+
(define (mpsAux grid curpos dpTable ub)
7+
(local
8+
[(define (get grid pos) (list-ref (list-ref grid (car pos)) (cdr pos)))]
9+
(cond
10+
; return start value
11+
[(equal? curpos (cons 0 0)) (car (car grid))]
12+
13+
; handle out of bounds
14+
[(or (< (car curpos) 0) (< (cdr curpos) 0)) +inf.0]
15+
16+
; position appeared before
17+
[(hash-ref dpTable curpos #f) (hash-ref dpTable curpos)]
18+
19+
; inductive case
20+
[else
21+
(let*
22+
(
23+
; go up
24+
[u_val (mpsAux grid (cons (- (car curpos) 1) (cdr curpos)) dpTable ub)]
25+
26+
; go left
27+
[l_val (mpsAux grid (cons (car curpos) (- (cdr curpos) 1)) dpTable ub)]
28+
29+
; compute the current least cost
30+
[cur-least-cost (+ (get grid curpos) (min u_val l_val))]
31+
)
32+
(hash-set! dpTable curpos cur-least-cost)
33+
cur-least-cost
34+
)
35+
]
36+
)
37+
)
38+
)
39+
40+
; the (x . y) position of the grid is the x'th row and y'th column of the grid
41+
(define/contract (min-path-sum grid)
42+
(-> (listof (listof exact-integer?)) exact-integer?)
43+
(local
44+
[(define dpTable (make-hash))]
45+
(let*
46+
(
47+
[curpos (cons (- (length grid) 1) (- (length (car grid)) 1))]
48+
[least-val (mpsAux grid curpos dpTable 200)]
49+
)
50+
(inexact->exact least-val)
51+
)
52+
)
53+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
64\. Minimum Path Sum
2+
3+
Medium
4+
5+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
6+
7+
**Note:** You can only move either down or right at any point in time.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
12+
13+
**Input:** grid = [[1,3,1],[1,5,1],[4,2,1]]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
18+
19+
**Example 2:**
20+
21+
**Input:** grid = [[1,2,3],[4,5,6]]
22+
23+
**Output:** 12
24+
25+
**Constraints:**
26+
27+
* `m == grid.length`
28+
* `n == grid[i].length`
29+
* `1 <= m, n <= 200`
30+
* `0 <= grid[i][j] <= 100`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
2+
; #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
3+
; #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Top_Interview_150_1D_DP
4+
; #Big_O_Time_O(n)_Space_O(n) #2025_02_04_Time_0_(100.00%)_Space_101.51_(100.00%)
5+
6+
(define (clmHelp n hTable)
7+
(cond
8+
; base cases
9+
((= 1 n) 1)
10+
((= 2 n) 2)
11+
((hash-ref hTable n #f) (hash-ref hTable n))
12+
13+
; inductive case
14+
(else
15+
(let*
16+
; the local variables
17+
(
18+
(a (clmHelp (- n 1) hTable))
19+
(b (clmHelp (- n 2) hTable))
20+
(numPos (+ a b))
21+
)
22+
23+
; the body
24+
(hash-set! hTable n numPos)
25+
numPos
26+
)
27+
)
28+
)
29+
)
30+
31+
(define/contract (climb-stairs n)
32+
(-> exact-integer? exact-integer?)
33+
(local
34+
; local definitions
35+
((define hTable (make-hash)))
36+
37+
; function body
38+
(clmHelp n hTable)
39+
)
40+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
70\. Climbing Stairs
2+
3+
Easy
4+
5+
You are climbing a staircase. It takes `n` steps to reach the top.
6+
7+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** 2
14+
15+
**Explanation:** There are two ways to climb to the top.
16+
17+
1. 1 step + 1 step
18+
19+
2. 2 steps
20+
21+
**Example 2:**
22+
23+
**Input:** n = 3
24+
25+
**Output:** 3
26+
27+
**Explanation:** There are three ways to climb to the top.
28+
29+
1. 1 step + 1 step + 1 step
30+
31+
2. 1 step + 2 steps
32+
33+
3. 2 steps + 1 step
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 45`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
2+
; #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
3+
; #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP #Big_O_Time_O(n^2)_Space_O(n2)
4+
; #2025_02_04_Time_4_(100.00%)_Space_102.31_(100.00%)
5+
6+
(define/contract (min-distance word1 word2)
7+
(-> string? string? exact-integer?)
8+
(let* ((n1 (string-length word1))
9+
(n2 (string-length word2)))
10+
(if (> n2 n1)
11+
(min-distance word2 word1)
12+
(let ((dp (make-vector (+ n2 1) 0)))
13+
(for ([j (in-range (+ n2 1))])
14+
(vector-set! dp j j))
15+
(for ([i (in-range 1 (+ n1 1))])
16+
(let ((pre (vector-ref dp 0)))
17+
(vector-set! dp 0 i)
18+
(for ([j (in-range 1 (+ n2 1))])
19+
(let* ((tmp (vector-ref dp j))
20+
(cost (if (char=? (string-ref word1 (- i 1)) (string-ref word2 (- j 1)))
21+
pre
22+
(+ 1 (min pre (vector-ref dp j) (vector-ref dp (- j 1)))))))
23+
(vector-set! dp j cost)
24+
(set! pre tmp)))))
25+
(vector-ref dp n2)))))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
72\. Edit Distance
2+
3+
Hard
4+
5+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
6+
7+
You have the following three operations permitted on a word:
8+
9+
* Insert a character
10+
* Delete a character
11+
* Replace a character
12+
13+
**Example 1:**
14+
15+
**Input:** word1 = "horse", word2 = "ros"
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
horse -> rorse (replace 'h' with 'r')
22+
23+
rorse -> rose (remove 'r')
24+
25+
rose -> ros (remove 'e')
26+
27+
**Example 2:**
28+
29+
**Input:** word1 = "intention", word2 = "execution"
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
intention -> inention (remove 't')
36+
37+
inention -> enention (replace 'i' with 'e')
38+
39+
enention -> exention (replace 'n' with 'x')
40+
41+
exention -> exection (replace 'n' with 'c')
42+
43+
exection -> execution (insert 'u')
44+
45+
**Constraints:**
46+
47+
* `0 <= word1.length, word2.length <= 500`
48+
* `word1` and `word2` consist of lowercase English letters.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
2+
; #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
3+
; #Udemy_2D_Arrays/Matrix #Top_Interview_150_Binary_Search #Big_O_Time_O(endRow+endCol)_Space_O(1)
4+
; #2025_02_04_Time_0_(100.00%)_Space_101.20_(100.00%)
5+
6+
(define/contract (search-matrix matrix target)
7+
(-> (listof (listof exact-integer?)) exact-integer? boolean?)
8+
(not (equal? #f (member target (apply append matrix)))))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
74\. Search a 2D Matrix
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
6+
7+
* Integers in each row are sorted from left to right.
8+
* The first integer of each row is greater than the last integer of the previous row.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
13+
14+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
21+
22+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= m, n <= 100`
31+
* <code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
; #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
; #Level_2_Day_14_Sliding_Window/Two_Pointer #Top_Interview_150_Sliding_Window
3+
; #Big_O_Time_O(s.length())_Space_O(1) #2025_02_04_Time_151_(100.00%)_Space_130.75_(100.00%)
4+
5+
(define (zip lst1 lst2)
6+
(map cons lst1 lst2))
7+
8+
(define (make-zero-hash lst)
9+
(make-immutable-hash (zip lst (make-list (length lst) 0))))
10+
11+
(define (hash-inc k ht)
12+
(hash-update ht k add1 0))
13+
14+
(define (safe-hash-inc k ht)
15+
(if (hash-ref ht k #f) (hash-update ht k add1) ht))
16+
17+
(define (safe-hash-dec k ht)
18+
(if (hash-ref ht k #f) (hash-update ht k sub1) ht))
19+
20+
(define (frequencies lst)
21+
(foldl hash-inc (hash) lst))
22+
23+
(define (pair-min l1 r1 l2 r2)
24+
(if (< (- r1 l1) (- r2 l2)) (values l1 r1) (values l2 r2)))
25+
26+
(define (sref s i) (string-ref s (min (sub1 (string-length s)) i)))
27+
28+
(define (min-window s t)
29+
(define t-lst (string->list t))
30+
31+
(define target (frequencies t-lst))
32+
33+
(define (delta-dec delta seen c)
34+
(cond
35+
[(and (hash-ref target c #f)
36+
(< (hash-ref seen c) (hash-ref target c)))
37+
(sub1 delta)]
38+
[else delta]))
39+
40+
(define (delta-inc delta seen c)
41+
(cond
42+
[(and (hash-ref target c #f)
43+
(<= (hash-ref seen c) (hash-ref target c)))
44+
(add1 delta)]
45+
[else delta]))
46+
47+
(let loop ([l 0] [r 0] [seen (make-zero-hash t-lst)]
48+
[delta (length t-lst)] [sl 0] [sr 0])
49+
(define-values (left-char right-char) (values (sref s l) (sref s r)))
50+
(define-values (l* r*) (pair-min l r sl sr))
51+
(cond
52+
[(and (= 0 sr)
53+
(= delta 0))
54+
(loop (add1 l) r (safe-hash-dec left-char seen)
55+
(delta-inc delta seen left-char) l r)]
56+
[(and (= r (string-length s)) (= delta 0))
57+
(loop (add1 l) r (safe-hash-dec left-char seen)
58+
(delta-inc delta seen left-char) l* r*)]
59+
[(= r (string-length s))
60+
(substring s sl sr)]
61+
[(= delta 0)
62+
(loop (add1 l) r (safe-hash-dec left-char seen)
63+
(delta-inc delta seen left-char) l* r*)]
64+
[else
65+
(loop l (add1 r) (safe-hash-inc right-char seen)
66+
(delta-dec delta seen right-char) sl sr)])))

0 commit comments

Comments
 (0)