Skip to content

Commit ed031af

Browse files
committed
Added racket
1 parent a7ad51e commit ed031af

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

src/main/racket/g0101_0200/s0146_lru_cache/LRUCache.rkt

+5
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@
5454
(set! head new-node)
5555
(unless tail (set! tail new-node))
5656
(hash-set! cache key new-node))))))))
57+
58+
;; Your lru-cache% object will be instantiated and called as such:
59+
;; (define obj (new lru-cache% [capacity capacity]))
60+
;; (define param_1 (send obj get key))
61+
;; (send obj put key value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
2+
; #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
3+
; #2025_02_15_Time_62_ms_(100.00%)_Space_137.01_MB_(100.00%)
4+
5+
(require racket/contract)
6+
7+
(define/contract (max-sliding-window nums k)
8+
(-> (listof exact-integer?) exact-integer? (listof exact-integer?))
9+
(if (= k 1)
10+
nums ; Special case where each window is just a single number
11+
(let* ([n (length nums)]
12+
[nums-vec (list->vector nums)]
13+
[capacity n]
14+
[dq (make-vector capacity 0)]
15+
[head (box 0)]
16+
[tail (box 0)]
17+
[result '()])
18+
19+
;; Helper functions for deque operations
20+
(define (deque-empty?) (= (unbox head) (unbox tail)))
21+
(define (deque-push-back! idx)
22+
(vector-set! dq (unbox tail) idx)
23+
(set-box! tail (modulo (+ (unbox tail) 1) capacity)))
24+
(define (deque-pop-back!)
25+
(set-box! tail (modulo (- (unbox tail) 1) capacity)))
26+
(define (deque-front) (vector-ref dq (unbox head)))
27+
(define (deque-pop-front!)
28+
(set-box! head (modulo (+ (unbox head) 1) capacity)))
29+
(define (deque-back)
30+
(vector-ref dq (if (= (unbox tail) 0) (- capacity 1) (- (unbox tail) 1))))
31+
(define (pop-back-while cur)
32+
(when (not (deque-empty?))
33+
(when (< (vector-ref nums-vec (deque-back)) cur)
34+
(deque-pop-back!)
35+
(pop-back-while cur))))
36+
37+
;; Iterate through nums
38+
(for ([idx (in-range n)])
39+
(let ([cur (vector-ref nums-vec idx)])
40+
(pop-back-while cur)
41+
(deque-push-back! idx)
42+
(when (and (>= idx k)
43+
(not (deque-empty?))
44+
(equal? (deque-front) (- idx k)))
45+
(deque-pop-front!))
46+
(when (>= idx (sub1 k))
47+
(set! result (cons (vector-ref nums-vec (deque-front)) result)))))
48+
49+
(reverse result))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
239\. Sliding Window Maximum
2+
3+
Hard
4+
5+
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
6+
7+
Return _the max sliding window_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
12+
13+
**Output:** [3,3,5,5,6,7]
14+
15+
**Explanation:**
16+
17+
Window position Max
18+
19+
--------------- -----
20+
21+
[1 3 -1] -3 5 3 6 7 **3**
22+
23+
1 [3 -1 -3] 5 3 6 7 **3**
24+
25+
1 3 [-1 -3 5] 3 6 7 **5**
26+
27+
1 3 -1 [-3 5 3] 6 7 **5**
28+
29+
1 3 -1 -3 [5 3 6] 7 **6**
30+
31+
1 3 -1 -3 5 [3 6 7] **7**
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [1], k = 1
36+
37+
**Output:** [1]
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
43+
* `1 <= k <= nums.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; #Hard #Top_100_Liked_Questions #Sorting #Two_Pointers #Design #Heap_Priority_Queue #Data_Stream
2+
; #Top_Interview_150_Heap #Big_O_Time_O(n*log_n)_Space_O(n)
3+
; #2025_02_15_Time_437_ms_(100.00%)_Space_132.32_MB_(100.00%)
4+
5+
(require data/heap racket/class)
6+
7+
(define median-finder%
8+
(class object%
9+
(super-new)
10+
11+
;; Max heap (stores smaller half, max at the top)
12+
(define max-heap (make-heap >))
13+
;; Min heap (stores larger half, min at the top)
14+
(define min-heap (make-heap <))
15+
16+
;; add-num : exact-integer? -> void?
17+
(define/public (add-num num)
18+
(heap-add! max-heap num) ; Add to max-heap
19+
(heap-add! min-heap (heap-min max-heap)) ; Move max-heap root to min-heap
20+
(heap-remove-min! max-heap) ; Remove moved element
21+
22+
;; Balance: Ensure max-heap has at least as many elements as min-heap
23+
(when (> (heap-count min-heap) (heap-count max-heap))
24+
(heap-add! max-heap (heap-min min-heap))
25+
(heap-remove-min! min-heap)))
26+
27+
;; find-median : -> flonum?
28+
(define/public (find-median)
29+
(if (> (heap-count max-heap) (heap-count min-heap))
30+
(exact->inexact (heap-min max-heap))
31+
(/ (+ (heap-min max-heap) (heap-min min-heap)) 2.0)))))
32+
33+
;; Your median-finder% object will be instantiated and called as such:
34+
;; (define obj (new median-finder%))
35+
;; (send obj add-num num)
36+
;; (define param_2 (send obj find-median))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
295\. Find Median from Data Stream
2+
3+
Hard
4+
5+
The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
6+
7+
* For example, for `arr = [2,3,4]`, the median is `3`.
8+
* For example, for `arr = [2,3]`, the median is `(2 + 3) / 2 = 2.5`.
9+
10+
Implement the MedianFinder class:
11+
12+
* `MedianFinder()` initializes the `MedianFinder` object.
13+
* `void addNum(int num)` adds the integer `num` from the data stream to the data structure.
14+
* `double findMedian()` returns the median of all elements so far. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
21+
[[], [1], [2], [], [3], []]
22+
23+
**Output:** [null, null, null, 1.5, null, 2.0]
24+
25+
**Explanation:**
26+
27+
MedianFinder medianFinder = new MedianFinder();
28+
medianFinder.addNum(1); // arr = [1]
29+
medianFinder.addNum(2); // arr = [1, 2]
30+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
31+
medianFinder.addNum(3); // arr[1, 2, 3]
32+
medianFinder.findMedian(); // return 2.0
33+
34+
**Constraints:**
35+
36+
* <code>-10<sup>5</sup> <= num <= 10<sup>5</sup></code>
37+
* There will be at least one element in the data structure before calling `findMedian`.
38+
* At most <code>5 * 10<sup>4</sup></code> calls will be made to `addNum` and `findMedian`.
39+
40+
**Follow up:**
41+
42+
* If all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?
43+
* If `99%` of all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?

0 commit comments

Comments
 (0)