Skip to content

Commit a7ad51e

Browse files
committed
Added racket
1 parent 0cf5001 commit a7ad51e

File tree

30 files changed

+903
-0
lines changed

30 files changed

+903
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Stack #Monotonic_Stack
2+
; #Big_O_Time_O(n_log_n)_Space_O(log_n) #2025_02_12_Time_18_ms_(100.00%)_Space_132.44_MB_(100.00%)
3+
4+
(define/contract (largest-rectangle-area heights)
5+
(-> (listof exact-integer?) exact-integer?)
6+
(let* ((n (length heights))
7+
(heights-vec (list->vector (append heights '(0))))
8+
(stack '())
9+
(max-area 0))
10+
11+
(for ([i (in-range (add1 n))])
12+
(let loop ()
13+
(when (and (not (null? stack))
14+
(<= (vector-ref heights-vec i) (vector-ref heights-vec (car stack))))
15+
(let* ((top (car stack))
16+
(new-stack (cdr stack))
17+
(height (vector-ref heights-vec top))
18+
(width (if (null? new-stack) i (- i (car new-stack) 1)))
19+
(area (* height width)))
20+
(set! max-area (max max-area area))
21+
(set! stack new-stack)
22+
(loop))))
23+
(set! stack (cons i stack)))
24+
25+
max-area))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
84\. Largest Rectangle in Histogram
2+
3+
Hard
4+
5+
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return _the area of the largest rectangle in the histogram_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)
10+
11+
**Input:** heights = [2,1,5,6,2,3]
12+
13+
**Output:** 10
14+
15+
**Explanation:** The above is a histogram where width of each bar is 1.
16+
17+
The largest rectangle is shown in the red area, which has an area = 10 units.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)
22+
23+
**Input:** heights = [2,4]
24+
25+
**Output:** 4
26+
27+
**Constraints:**
28+
29+
* <code>1 <= heights.length <= 10<sup>5</sup></code>
30+
* <code>0 <= heights[i] <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search
2+
; #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
3+
; #Level_2_Day_12_Dynamic_Programming #Top_Interview_150_1D_DP #Big_O_Time_O(m*n)_Space_O(amount)
4+
; #2025_02_11_Time_29_ms_(100.00%)_Space_101.98_MB_(100.00%)
5+
6+
(define/contract (coin-change coins amount)
7+
(-> (listof exact-integer?) exact-integer? exact-integer?)
8+
(let ([dp (make-vector (+ amount 1) 0)])
9+
(vector-set! dp 0 1)
10+
(for ([coin coins])
11+
(for ([i (in-range coin (+ amount 1))])
12+
(let ([prev (vector-ref dp (- i coin))])
13+
(when (> prev 0)
14+
(vector-set! dp i
15+
(if (= (vector-ref dp i) 0)
16+
(+ prev 1)
17+
(min (vector-ref dp i) (+ prev 1))))))))
18+
(- (vector-ref dp amount) 1)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
322\. Coin Change
2+
3+
Medium
4+
5+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
6+
7+
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
**Example 1:**
12+
13+
**Input:** coins = [1,2,5], amount = 11
14+
15+
**Output:** 3
16+
17+
**Explanation:** 11 = 5 + 5 + 1
18+
19+
**Example 2:**
20+
21+
**Input:** coins = [2], amount = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** coins = [1], amount = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
* `1 <= coins.length <= 12`
34+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
35+
* <code>0 <= amount <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
2+
; #Big_O_Time_O(num)_Space_O(num) #2025_02_11_Time_3_ms_(100.00%)_Space_135.21_MB_(100.00%)
3+
4+
(define (near-bit n i)
5+
(if (and (<= i n) (< n (* i 2)))
6+
i
7+
(near-bit n (* i 2))))
8+
9+
(define/contract (count-bits n)
10+
(-> exact-integer? (listof exact-integer?))
11+
(match n
12+
[0 '(0)]
13+
[1 '(0 1)]
14+
[else
15+
(let* ([b (near-bit n 1)]
16+
[prev (count-bits (- b 1))]
17+
[next (map (lambda (n) (+ n 1)) prev)])
18+
(take (append prev next) (+ n 1)))]
19+
))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
338\. Counting Bits
2+
3+
Easy
4+
5+
Given an integer `n`, return _an array_ `ans` _of length_ `n + 1` _such that for each_ `i` (`0 <= i <= n`)_,_ `ans[i]` _is the **number of**_ `1`_**'s** in the binary representation of_ `i`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 2
10+
11+
**Output:** [0,1,1]
12+
13+
**Explanation:**
14+
15+
0 --> 0
16+
1 --> 1
17+
2 --> 10
18+
19+
**Example 2:**
20+
21+
**Input:** n = 5
22+
23+
**Output:** [0,1,1,2,1,2]
24+
25+
**Explanation:**
26+
27+
0 --> 0
28+
1 --> 1
29+
2 --> 10
30+
3 --> 11
31+
4 --> 100
32+
5 --> 101
33+
34+
**Constraints:**
35+
36+
* <code>0 <= n <= 10<sup>5</sup></code>
37+
38+
**Follow up:**
39+
40+
* It is very easy to come up with a solution with a runtime of `O(n log n)`. Can you do it in linear time `O(n)` and possibly in a single pass?
41+
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting
2+
; #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue
3+
; #Big_O_Time_O(n*log(n))_Space_O(k) #2025_02_11_Time_3_ms_(100.00%)_Space_103.26_MB_(100.00%)
4+
5+
(define (hash-table-counter ls)
6+
(let ([counts (make-hash)])
7+
(for-each (λ (v) (hash-update! counts v add1 0)) ls)
8+
counts))
9+
10+
(define (top-k-frequent nums k)
11+
(let ([counter (hash-table-counter nums)])
12+
(map car (take (sort (hash->list counter) #:key cdr >) k))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
347\. Top K Frequent Elements
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,1,1,2,2,3], k = 2
10+
11+
**Output:** [1,2]
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1], k = 1
16+
17+
**Output:** [1]
18+
19+
**Constraints:**
20+
21+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
22+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
23+
* `k` is in the range `[1, the number of unique elements in the array]`.
24+
* It is **guaranteed** that the answer is **unique**.
25+
26+
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
2+
; #Big_O_Time_O(n)_Space_O(n) #2025_02_11_Time_0_ms_(100.00%)_Space_101.43_MB_(100.00%)
3+
4+
(define/contract (decode-string s)
5+
(-> string? string?)
6+
(let-values ([(result _) (decode-helper (string->list s) 0)])
7+
result))
8+
9+
(define (decode-helper chars pos)
10+
(let loop ([current-pos pos]
11+
[count 0]
12+
[result ""])
13+
(if (>= current-pos (length chars))
14+
(values result current-pos)
15+
(let ([char (list-ref chars current-pos)])
16+
(cond
17+
; If it's a letter, append it to result
18+
[(char-alphabetic? char)
19+
(loop (add1 current-pos)
20+
count
21+
(string-append result (string char)))]
22+
23+
; If it's a digit, update count
24+
[(char-numeric? char)
25+
(loop (add1 current-pos)
26+
(+ (* count 10) (- (char->integer char) (char->integer #\0)))
27+
result)]
28+
29+
; If it's '[', handle nested string
30+
[(char=? char #\[)
31+
(let-values ([(nested-str new-pos) (decode-helper chars (add1 current-pos))])
32+
(loop new-pos
33+
0
34+
(string-append result
35+
(string-join
36+
(make-list count nested-str)
37+
""))))]
38+
39+
; If it's ']', return current result and position
40+
[(char=? char #\])
41+
(values result (add1 current-pos))]
42+
43+
; Skip other characters
44+
[else (loop (add1 current-pos) count result)])))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
394\. Decode String
2+
3+
Medium
4+
5+
Given an encoded string, return its decoded string.
6+
7+
The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer.
8+
9+
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, `k`. For example, there will not be input like `3a` or `2[4]`.
10+
11+
The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "3[a]2[bc]"
16+
17+
**Output:** "aaabcbc"
18+
19+
**Example 2:**
20+
21+
**Input:** s = "3[a2[c]]"
22+
23+
**Output:** "accaccacc"
24+
25+
**Example 3:**
26+
27+
**Input:** s = "2[abc]3[cd]ef"
28+
29+
**Output:** "abcabccdcdcdef"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 30`
34+
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
35+
* `s` is guaranteed to be **a valid** input.
36+
* All the integers in `s` are in the range `[1, 300]`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
2+
; #Big_O_Time_O(n*sums)_Space_O(n*sums)
3+
; #2025_02_11_Time_1098_ms_(100.00%)_Space_132.26_MB_(100.00%)
4+
5+
(define/contract (can-partition nums)
6+
(-> (listof exact-integer?) boolean?)
7+
(let* ([sum (apply + nums)])
8+
(if (odd? sum)
9+
#f
10+
(let* ([target (/ sum 2)]
11+
[dp (make-hash)])
12+
(hash-set! dp 0 #t)
13+
(for ([num nums])
14+
(for ([key (reverse (hash-keys dp))])
15+
(let ([new-sum (+ key num)])
16+
(when (<= new-sum target)
17+
(hash-set! dp new-sum #t)))))
18+
(hash-ref dp target #f)))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
416\. Partition Equal Subset Sum
2+
3+
Medium
4+
5+
Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,5,11,5]
10+
11+
**Output:** true
12+
13+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3,5]
18+
19+
**Output:** false
20+
21+
**Explanation:** The array cannot be partitioned into equal sum subsets.
22+
23+
**Constraints:**
24+
25+
* `1 <= nums.length <= 200`
26+
* `1 <= nums[i] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; #Medium #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree #Big_O_Time_O(n)_Space_O(n)
2+
; #2025_02_11_Time_107_ms_(100.00%)_Space_136.84_MB_(100.00%)
3+
4+
; Definition for a binary tree node.
5+
#|
6+
7+
; val : integer?
8+
; left : (or/c tree-node? #f)
9+
; right : (or/c tree-node? #f)
10+
(struct tree-node
11+
(val left right) #:mutable #:transparent)
12+
13+
; constructor
14+
(define (make-tree-node [val 0])
15+
(tree-node val #f #f))
16+
17+
|#
18+
19+
(define/contract (path-sum root targetSum)
20+
(-> (or/c tree-node? #f) exact-integer? exact-integer?)
21+
(let recur ([root root][candidates empty])
22+
(if (boolean? root) 0
23+
(let ([new-candidates
24+
(cons (tree-node-val root)
25+
(map (lambda (i) (+ i (tree-node-val root))) candidates))])
26+
(+ (recur (tree-node-left root) new-candidates) (recur (tree-node-right root) new-candidates)
27+
(foldl (lambda (i res) (+ res (if (= i targetSum) 1 0))) 0 new-candidates))))))

0 commit comments

Comments
 (0)