Skip to content

Commit de3e80e

Browse files
committed
Added tasks 1-62
1 parent ed18ee3 commit de3e80e

File tree

36 files changed

+4011
-1
lines changed

36 files changed

+4011
-1
lines changed

README.md

+1,451-1
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 1\. Two Sum
5+
6+
Easy
7+
8+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9+
10+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
11+
12+
You can return the answer in any order.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,7,11,15], target = 9
17+
18+
**Output:** [0,1]
19+
20+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,2,4], target = 6
25+
26+
**Output:** [1,2]
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [3,3], target = 6
31+
32+
**Output:** [0,1]
33+
34+
**Constraints:**
35+
36+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
39+
* **Only one valid answer exists.**
40+
41+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
42+
43+
## Solution
44+
45+
```racket
46+
; #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
47+
; #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
48+
; #AI_can_be_used_to_solve_the_task #2025_01_28_Time_0_(100.00%)_Space_102.07_(45.83%)
49+
50+
(define (two-sum-iter nums target hash index)
51+
(cond
52+
((null? nums) '())
53+
((hash-has-key? hash (car nums) ) (list (hash-ref hash (car nums) ) index ) )
54+
(else (two-sum-iter (cdr nums) target (hash-set hash (- target (car nums ) ) index ) (+ index 1) ))
55+
)
56+
)
57+
58+
(define/contract (two-sum nums target)
59+
(-> (listof exact-integer?) exact-integer? (listof exact-integer?))
60+
(two-sum-iter nums target (make-immutable-hash) 0)
61+
)
62+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 2\. Add Two Numbers
5+
6+
Medium
7+
8+
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.
9+
10+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
15+
16+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
17+
18+
**Output:** [7,0,8]
19+
20+
**Explanation:** 342 + 465 = 807.
21+
22+
**Example 2:**
23+
24+
**Input:** l1 = [0], l2 = [0]
25+
26+
**Output:** [0]
27+
28+
**Example 3:**
29+
30+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
31+
32+
**Output:** [8,9,9,9,0,0,0,1]
33+
34+
**Constraints:**
35+
36+
* The number of nodes in each linked list is in the range `[1, 100]`.
37+
* `0 <= Node.val <= 9`
38+
* It is guaranteed that the list represents a number that does not have leading zeros.
39+
40+
## Solution
41+
42+
```racket
43+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
44+
; #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
45+
; #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
46+
; #2025_01_28_Time_0_(100.00%)_Space_128.42_(12.50%)
47+
48+
; Definition for singly-linked list:
49+
#|
50+
51+
; val : integer?
52+
; next : (or/c list-node? #f)
53+
(struct list-node
54+
(val next) #:mutable #:transparent)
55+
56+
; constructor
57+
(define (make-list-node [val 0])
58+
(list-node val #f))
59+
60+
|#
61+
62+
(define/contract (add-two-numbers l1 l2)
63+
(-> (or/c list-node? #f) (or/c list-node? #f) (or/c list-node? #f))
64+
(add-two-numbers-help l1 l2 0)
65+
)
66+
67+
(define (add-two-numbers-help l1 l2 carry-sum)
68+
(cond
69+
[(and (false? l1) (false? l2)) (if (zero? carry-sum) #f (list-node carry-sum #f))]
70+
[(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)))]
71+
[(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)))]
72+
[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)))]
73+
)
74+
)
75+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 3\. Longest Substring Without Repeating Characters
5+
6+
Medium
7+
8+
Given a string `s`, find the length of the **longest substring** without repeating characters.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "abcabcbb"
13+
14+
**Output:** 3
15+
16+
**Explanation:** The answer is "abc", with the length of 3.
17+
18+
**Example 2:**
19+
20+
**Input:** s = "bbbbb"
21+
22+
**Output:** 1
23+
24+
**Explanation:** The answer is "b", with the length of 1.
25+
26+
**Example 3:**
27+
28+
**Input:** s = "pwwkew"
29+
30+
**Output:** 3
31+
32+
**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.
33+
34+
**Example 4:**
35+
36+
**Input:** s = ""
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
43+
* `s` consists of English letters, digits, symbols and spaces.
44+
45+
## Solution
46+
47+
```racket
48+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
49+
; #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
50+
; #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
51+
; #2025_01_28_Time_134_(75.00%)_Space_132.36_(100.00%)
52+
53+
; Helper function to get the sublist up to 'v' excluded.
54+
(define (take-till q v)
55+
(define (take-till/helper q v acc)
56+
(if (or (null? q)
57+
(eq? (car q) v))
58+
acc
59+
(take-till/helper (cdr q) v (cons (car q) acc))))
60+
(foldl cons null (take-till/helper q v null)))
61+
62+
; (take-till '(1 2 3 4 5) 4) returns '(1 2 3)
63+
64+
(define (max-substr s queue len max-len)
65+
(cond
66+
[(null? s) max-len]
67+
[(pair? s)
68+
(let ((x (car s))
69+
(xs (cdr s)))
70+
(if (member x queue)
71+
(let* ((tqueue (take-till queue x)) ; queue from last repeated char
72+
(tqueue-len (+ 1 (length tqueue))))
73+
(max-substr xs (cons x tqueue) tqueue-len (max tqueue-len max-len)))
74+
; else
75+
(max-substr xs (cons x queue) (+ 1 len) (max (+ 1 len) max-len))))]))
76+
77+
(define/contract (length-of-longest-substring s)
78+
(-> string? exact-integer?)
79+
(max-substr (string->list s) null 0 0))
80+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 4\. Median of Two Sorted Arrays
5+
6+
Hard
7+
8+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
9+
10+
The overall run time complexity should be `O(log (m+n))`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums1 = [1,3], nums2 = [2]
15+
16+
**Output:** 2.00000
17+
18+
**Explanation:** merged array = [1,2,3] and median is 2.
19+
20+
**Example 2:**
21+
22+
**Input:** nums1 = [1,2], nums2 = [3,4]
23+
24+
**Output:** 2.50000
25+
26+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
27+
28+
**Example 3:**
29+
30+
**Input:** nums1 = [0,0], nums2 = [0,0]
31+
32+
**Output:** 0.00000
33+
34+
**Example 4:**
35+
36+
**Input:** nums1 = [], nums2 = [1]
37+
38+
**Output:** 1.00000
39+
40+
**Example 5:**
41+
42+
**Input:** nums1 = [2], nums2 = []
43+
44+
**Output:** 2.00000
45+
46+
**Constraints:**
47+
48+
* `nums1.length == m`
49+
* `nums2.length == n`
50+
* `0 <= m <= 1000`
51+
* `0 <= n <= 1000`
52+
* `1 <= m + n <= 2000`
53+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
54+
55+
## Solution
56+
57+
```racket
58+
; #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
59+
; #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
60+
; #2025_01_28_Time_0_(100.00%)_Space_128.57_(100.00%)
61+
62+
(define/contract (find-median-sorted-arrays nums1 nums2)
63+
(-> (listof exact-integer?) (listof exact-integer?) flonum?)
64+
65+
(define (find-kth-smallest k nums1 nums2)
66+
(cond
67+
[(empty? nums1) (list-ref nums2 k)]
68+
[(empty? nums2) (list-ref nums1 k)]
69+
[(= k 0) (min (car nums1) (car nums2))]
70+
[else
71+
(let* ([mid1 (min (length nums1) (quotient (+ k 1) 2))]
72+
[mid2 (min (length nums2) (quotient (+ k 1) 2))]
73+
[median1 (list-ref nums1 (sub1 mid1))]
74+
[median2 (list-ref nums2 (sub1 mid2))])
75+
(cond
76+
[(< median1 median2)
77+
(find-kth-smallest (- k mid1) (drop nums1 mid1) nums2)]
78+
[else
79+
(find-kth-smallest (- k mid2) nums1 (drop nums2 mid2))]))]))
80+
81+
(define (find-median)
82+
(let* ([m (length nums1)]
83+
[n (length nums2)]
84+
[total-length (+ m n)]
85+
[half-length (quotient total-length 2)])
86+
(if (even? total-length)
87+
(/ (+ (exact->inexact (find-kth-smallest half-length nums1 nums2))
88+
(exact->inexact (find-kth-smallest (sub1 half-length) nums1 nums2)))
89+
2.0)
90+
(exact->inexact (find-kth-smallest half-length nums1 nums2)))))
91+
92+
(find-median))
93+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 5\. Longest Palindromic Substring
5+
6+
Medium
7+
8+
Given a string `s`, return _the longest palindromic substring_ in `s`.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "babad"
13+
14+
**Output:** "bab" **Note:** "aba" is also a valid answer.
15+
16+
**Example 2:**
17+
18+
**Input:** s = "cbbd"
19+
20+
**Output:** "bb"
21+
22+
**Example 3:**
23+
24+
**Input:** s = "a"
25+
26+
**Output:** "a"
27+
28+
**Example 4:**
29+
30+
**Input:** s = "ac"
31+
32+
**Output:** "a"
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 1000`
37+
* `s` consist of only digits and English letters.
38+
39+
## Solution
40+
41+
```racket
42+
; #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
43+
; #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
44+
; #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
45+
; #2025_01_28_Time_10_(50.00%)_Space_102.35_(50.00%)
46+
47+
(define (longest-palindrome s)
48+
(define (expand-around-center s left right)
49+
(let loop ([l left] [r right])
50+
(if (and (>= l 0)
51+
(< r (string-length s))
52+
(char=? (string-ref s l) (string-ref s r)))
53+
(loop (sub1 l) (add1 r))
54+
(values (add1 l) (sub1 r))))) ;; Return correct boundaries
55+
56+
(define (find-longest-palindrome s)
57+
(define len (string-length s))
58+
(define start 0)
59+
(define end 0)
60+
(for ([i (in-range len)])
61+
;; Odd-length palindromes centered at i
62+
(define-values (l1 r1) (expand-around-center s i i))
63+
;; Even-length palindromes centered between i and i + 1
64+
(define-values (l2 r2) (expand-around-center s i (add1 i)))
65+
66+
;; Update the maximum length palindrome if necessary
67+
(when (> (- r1 l1) (- end start))
68+
(set! start l1)
69+
(set! end r1))
70+
(when (> (- r2 l2) (- end start))
71+
(set! start l2)
72+
(set! end r2)))
73+
(substring s start (add1 end)))
74+
75+
(find-longest-palindrome s))
76+
```

0 commit comments

Comments
 (0)