Skip to content

Commit 6b25aca

Browse files
committed
Added racket
1 parent 419afae commit 6b25aca

File tree

71 files changed

+2762
-482
lines changed

Some content is hidden

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

71 files changed

+2762
-482
lines changed

README.md

Lines changed: 482 additions & 482 deletions
Large diffs are not rendered by default.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; #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%)
4+
5+
(define (two-sum-iter nums target hash index)
6+
(cond
7+
((null? nums) '())
8+
((hash-has-key? hash (car nums) ) (list (hash-ref hash (car nums) ) index ) )
9+
(else (two-sum-iter (cdr nums) target (hash-set hash (- target (car nums ) ) index ) (+ index 1) ))
10+
)
11+
)
12+
13+
(define/contract (two-sum nums target)
14+
(-> (listof exact-integer?) exact-integer? (listof exact-integer?))
15+
(two-sum-iter nums target (make-immutable-hash) 0)
16+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
; #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%)
5+
6+
; Definition for singly-linked list:
7+
#|
8+
9+
; val : integer?
10+
; next : (or/c list-node? #f)
11+
(struct list-node
12+
(val next) #:mutable #:transparent)
13+
14+
; constructor
15+
(define (make-list-node [val 0])
16+
(list-node val #f))
17+
18+
|#
19+
20+
(define/contract (add-two-numbers l1 l2)
21+
(-> (or/c list-node? #f) (or/c list-node? #f) (or/c list-node? #f))
22+
(add-two-numbers-help l1 l2 0)
23+
)
24+
25+
(define (add-two-numbers-help l1 l2 carry-sum)
26+
(cond
27+
[(and (false? l1) (false? l2)) (if (zero? carry-sum) #f (list-node carry-sum #f))]
28+
[(false? l1) (list-node (modulo (+ (list-node-val l2) carry-sum) 10) (add-two-numbers-help l1 (list-node-next l2) (/ (- (+ (list-node-val l2) carry-sum) (modulo (+ (list-node-val l2) carry-sum) 10)) 10)))]
29+
[(false? l2) (list-node (modulo (+ (list-node-val l1) carry-sum) 10) (add-two-numbers-help (list-node-next l1) l2 (/ (- (+ (list-node-val l1) carry-sum) (modulo (+ (list-node-val l1) carry-sum) 10)) 10)))]
30+
[else (list-node (modulo (+ (list-node-val l1) (list-node-val l2) carry-sum) 10) (add-two-numbers-help (list-node-next l1) (list-node-next l2) (/ (- (+ (list-node-val l1) (list-node-val l2) carry-sum) (modulo (+ (list-node-val l1) (list-node-val l2) carry-sum) 10)) 10)))]
31+
)
32+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
; #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%)
5+
6+
; Helper function to get the sublist up to 'v' excluded.
7+
(define (take-till q v)
8+
(define (take-till/helper q v acc)
9+
(if (or (null? q)
10+
(eq? (car q) v))
11+
acc
12+
(take-till/helper (cdr q) v (cons (car q) acc))))
13+
(foldl cons null (take-till/helper q v null)))
14+
15+
; (take-till '(1 2 3 4 5) 4) returns '(1 2 3)
16+
17+
(define (max-substr s queue len max-len)
18+
(cond
19+
[(null? s) max-len]
20+
[(pair? s)
21+
(let ((x (car s))
22+
(xs (cdr s)))
23+
(if (member x queue)
24+
(let* ((tqueue (take-till queue x)) ; queue from last repeated char
25+
(tqueue-len (+ 1 (length tqueue))))
26+
(max-substr xs (cons x tqueue) tqueue-len (max tqueue-len max-len)))
27+
; else
28+
(max-substr xs (cons x queue) (+ 1 len) (max (+ 1 len) max-len))))]))
29+
30+
(define/contract (length-of-longest-substring s)
31+
(-> string? exact-integer?)
32+
(max-substr (string->list s) null 0 0))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; #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%)
4+
5+
(define/contract (find-median-sorted-arrays nums1 nums2)
6+
(-> (listof exact-integer?) (listof exact-integer?) flonum?)
7+
8+
(define (find-kth-smallest k nums1 nums2)
9+
(cond
10+
[(empty? nums1) (list-ref nums2 k)]
11+
[(empty? nums2) (list-ref nums1 k)]
12+
[(= k 0) (min (car nums1) (car nums2))]
13+
[else
14+
(let* ([mid1 (min (length nums1) (quotient (+ k 1) 2))]
15+
[mid2 (min (length nums2) (quotient (+ k 1) 2))]
16+
[median1 (list-ref nums1 (sub1 mid1))]
17+
[median2 (list-ref nums2 (sub1 mid2))])
18+
(cond
19+
[(< median1 median2)
20+
(find-kth-smallest (- k mid1) (drop nums1 mid1) nums2)]
21+
[else
22+
(find-kth-smallest (- k mid2) nums1 (drop nums2 mid2))]))]))
23+
24+
(define (find-median)
25+
(let* ([m (length nums1)]
26+
[n (length nums2)]
27+
[total-length (+ m n)]
28+
[half-length (quotient total-length 2)])
29+
(if (even? total-length)
30+
(/ (+ (exact->inexact (find-kth-smallest half-length nums1 nums2))
31+
(exact->inexact (find-kth-smallest (sub1 half-length) nums1 nums2)))
32+
2.0)
33+
(exact->inexact (find-kth-smallest half-length nums1 nums2)))))
34+
35+
(find-median))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2+
; #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%)
5+
6+
(define (longest-palindrome s)
7+
(define (expand-around-center s left right)
8+
(let loop ([l left] [r right])
9+
(if (and (>= l 0)
10+
(< r (string-length s))
11+
(char=? (string-ref s l) (string-ref s r)))
12+
(loop (sub1 l) (add1 r))
13+
(values (add1 l) (sub1 r))))) ;; Return correct boundaries
14+
15+
(define (find-longest-palindrome s)
16+
(define len (string-length s))
17+
(define start 0)
18+
(define end 0)
19+
(for ([i (in-range len)])
20+
;; Odd-length palindromes centered at i
21+
(define-values (l1 r1) (expand-around-center s i i))
22+
;; Even-length palindromes centered between i and i + 1
23+
(define-values (l2 r2) (expand-around-center s i (add1 i)))
24+
25+
;; Update the maximum length palindrome if necessary
26+
(when (> (- r1 l1) (- end start))
27+
(set! start l1)
28+
(set! end r1))
29+
(when (> (- r2 l2) (- end start))
30+
(set! start l2)
31+
(set! end r2)))
32+
(substring s start (add1 end)))
33+
34+
(find-longest-palindrome s))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest palindromic substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab" **Note:** "aba" is also a valid answer.
12+
13+
**Example 2:**
14+
15+
**Input:** s = "cbbd"
16+
17+
**Output:** "bb"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "a"
22+
23+
**Output:** "a"
24+
25+
**Example 4:**
26+
27+
**Input:** s = "ac"
28+
29+
**Output:** "a"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consist of only digits and English letters.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; #Medium #String #Top_Interview_150_Array/String
2+
; #2025_02_03_Time_57_(100.00%)_Space_130.82_(60.00%)
3+
4+
(define/contract (convert s numRows)
5+
(-> string? exact-integer? string?)
6+
(let* ((sLen (string-length s))
7+
(maxDist (- (* numRows 2) 2)))
8+
(if (= numRows 1)
9+
s
10+
(let ((buf ""))
11+
(for ([i (in-range numRows)])
12+
(let loop ((index i))
13+
(when (< index sLen)
14+
(set! buf (string-append buf (string (string-ref s index))))
15+
(let ((next-step (- maxDist (* i 2))))
16+
(when (and (> i 0) (< i (- numRows 1)) (< (+ index next-step) sLen))
17+
(set! buf (string-append buf (string (string-ref s (+ index next-step))))))
18+
(loop (+ index maxDist))))))
19+
buf))))

0 commit comments

Comments
 (0)