Skip to content

Commit b2000e1

Browse files
committed
Added racket
1 parent a89db11 commit b2000e1

File tree

10 files changed

+739
-0
lines changed

10 files changed

+739
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 139\. Word Break
5+
6+
Medium
7+
8+
Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words.
9+
10+
**Note** that the same word in the dictionary may be reused multiple times in the segmentation.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "leetcode", wordDict = ["leet","code"]
15+
16+
**Output:** true
17+
18+
**Explanation:** Return true because "leetcode" can be segmented as "leet code".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "applepenapple", wordDict = ["apple","pen"]
23+
24+
**Output:** true
25+
26+
**Explanation:** Return true because "applepenapple" can be segmented as "apple pen apple".
27+
28+
Note that you are allowed to reuse a dictionary word.
29+
30+
**Example 3:**
31+
32+
**Input:** s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `1 <= s.length <= 300`
39+
* `1 <= wordDict.length <= 1000`
40+
* `1 <= wordDict[i].length <= 20`
41+
* `s` and `wordDict[i]` consist of only lowercase English letters.
42+
* All the strings of `wordDict` are **unique**.
43+
44+
## Solution
45+
46+
```racket
47+
(define/contract (word-break s wordDict)
48+
(-> string? (listof string?) boolean?)
49+
(let ((memo (make-hash)))
50+
(define (dp i)
51+
(cond
52+
[(= i (string-length s)) #t]
53+
[(hash-has-key? memo i) (hash-ref memo i)]
54+
[else
55+
(let loop ((words wordDict))
56+
(if (null? words)
57+
(begin (hash-set! memo i #f) #f)
58+
(let* ((word (car words))
59+
(len (string-length word)))
60+
(if (and (<= (+ i len) (string-length s))
61+
(string=? (substring s i (+ i len)) word)
62+
(dp (+ i len)))
63+
(begin (hash-set! memo i #t) #t)
64+
(loop (cdr words))))))]))
65+
(dp 0)))
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 146\. LRU Cache
5+
6+
Medium
7+
8+
Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**.
9+
10+
Implement the `LRUCache` class:
11+
12+
* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`.
13+
* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`.
14+
* `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key.
15+
16+
The functions `get` and `put` must each run in `O(1)` average time complexity.
17+
18+
**Example 1:**
19+
20+
**Input** ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
21+
22+
**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4]
23+
24+
**Explanation:**
25+
26+
LRUCache lRUCache = new LRUCache(2);
27+
28+
lRUCache.put(1, 1); // cache is {1=1}
29+
30+
lRUCache.put(2, 2); // cache is {1=1, 2=2}
31+
32+
lRUCache.get(1); // return 1
33+
34+
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
35+
36+
lRUCache.get(2); // returns -1 (not found)
37+
38+
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
39+
40+
lRUCache.get(1); // return -1 (not found)
41+
42+
lRUCache.get(3); // return 3
43+
44+
lRUCache.get(4); // return 4
45+
46+
**Constraints:**
47+
48+
* `1 <= capacity <= 3000`
49+
* <code>0 <= key <= 10<sup>4</sup></code>
50+
* <code>0 <= value <= 10<sup>5</sup></code>
51+
* At most <code>2 * 10<sup>5</sup></code> calls will be made to `get` and `put`.
52+
53+
## Solution
54+
55+
```racket
56+
(define lru-cache%
57+
(class object%
58+
(super-new)
59+
60+
(init-field capacity)
61+
62+
(define head #f)
63+
(define tail #f)
64+
(define cache (make-hash))
65+
66+
(define/private (move-to-head node)
67+
(when (not (eq? node head))
68+
(when (eq? node tail)
69+
(set! tail (hash-ref node 'prev))
70+
(when tail (hash-set! tail 'next #f)))
71+
(let ((prev (hash-ref node 'prev #f))
72+
(next (hash-ref node 'next #f)))
73+
(when prev (hash-set! prev 'next next))
74+
(when next (hash-set! next 'prev prev))
75+
(hash-set! node 'prev #f)
76+
(hash-set! node 'next head)
77+
(when head (hash-set! head 'prev node))
78+
(set! head node))))
79+
80+
(define/public (get key)
81+
(let ((node (hash-ref cache key #f)))
82+
(if node
83+
(begin (move-to-head node)
84+
(hash-ref node 'value))
85+
-1)))
86+
87+
(define/public (put key value)
88+
(let ((node (hash-ref cache key #f)))
89+
(if node
90+
(begin
91+
(hash-set! node 'value value)
92+
(move-to-head node))
93+
(begin
94+
(when (>= (hash-count cache) capacity)
95+
(when tail
96+
(hash-remove! cache (hash-ref tail 'key))
97+
(set! tail (hash-ref tail 'prev #f))
98+
(when tail (hash-set! tail 'next #f))))
99+
(let ((new-node (make-hash)))
100+
(hash-set! new-node 'key key)
101+
(hash-set! new-node 'value value)
102+
(hash-set! new-node 'prev #f)
103+
(hash-set! new-node 'next head)
104+
(when head (hash-set! head 'prev new-node))
105+
(set! head new-node)
106+
(unless tail (set! tail new-node))
107+
(hash-set! cache key new-node))))))))
108+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 148\. Sort List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
13+
14+
**Input:** head = [4,2,1,3]
15+
16+
**Output:** [1,2,3,4]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
21+
22+
**Input:** head = [-1,5,3,4,0]
23+
24+
**Output:** [-1,0,3,4,5]
25+
26+
**Example 3:**
27+
28+
**Input:** head = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
35+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
36+
37+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
38+
39+
## Solution
40+
41+
```racket
42+
; Definition for singly-linked list:
43+
#|
44+
45+
; val : integer?
46+
; next : (or/c list-node? #f)
47+
(struct list-node
48+
(val next) #:mutable #:transparent)
49+
50+
; constructor
51+
(define (make-list-node [val 0])
52+
(list-node val #f))
53+
54+
|#
55+
56+
; Helper function to split the list into two halves
57+
(define (split-list head)
58+
(let loop ([slow head]
59+
[fast head]
60+
[pre #f])
61+
(cond
62+
[(or (not fast) (not (list-node-next fast)))
63+
(when pre
64+
(set-list-node-next! pre #f))
65+
slow]
66+
[else
67+
(loop (list-node-next slow)
68+
(list-node-next (list-node-next fast))
69+
slow)])))
70+
71+
; Helper function to merge two sorted lists
72+
(define (merge-lists l1 l2)
73+
(let ([dummy (list-node 1 #f)])
74+
(let loop ([curr dummy]
75+
[first l1]
76+
[second l2])
77+
(cond
78+
[(not first)
79+
(set-list-node-next! curr second)]
80+
[(not second)
81+
(set-list-node-next! curr first)]
82+
[else
83+
(if (<= (list-node-val first) (list-node-val second))
84+
(begin
85+
(set-list-node-next! curr first)
86+
(loop first (list-node-next first) second))
87+
(begin
88+
(set-list-node-next! curr second)
89+
(loop second first (list-node-next second))))])
90+
(list-node-next dummy))))
91+
92+
; Main sort-list function with contract
93+
(define/contract (sort-list head)
94+
(-> (or/c list-node? #f) (or/c list-node? #f))
95+
(cond
96+
[(or (not head) (not (list-node-next head)))
97+
head]
98+
[else
99+
(let* ([middle (split-list head)]
100+
[first (sort-list head)]
101+
[second (sort-list middle)])
102+
(merge-lists first second))]))
103+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 152\. Maximum Product Subarray
5+
6+
Medium
7+
8+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
9+
10+
The test cases are generated so that the answer will fit in a **32-bit** integer.
11+
12+
A **subarray** is a contiguous subsequence of the array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,3,-2,4]
17+
18+
**Output:** 6
19+
20+
**Explanation:** [2,3] has the largest product 6.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-2,0,-1]
25+
26+
**Output:** 0
27+
28+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
33+
* `-10 <= nums[i] <= 10`
34+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
35+
36+
## Solution
37+
38+
```racket
39+
(define/contract (max-product nums)
40+
(-> (listof exact-integer?) exact-integer?)
41+
(let* ([n (length nums)]
42+
[min-int (- (expt 2 31))])
43+
(let loop ([i 0]
44+
[start 1]
45+
[end 1]
46+
[overall-max-prod min-int])
47+
(if (= i n)
48+
overall-max-prod
49+
(let* ([new-start (if (zero? start) 1 start)]
50+
[new-end (if (zero? end) 1 end)]
51+
[start-prod (* new-start (list-ref nums i))]
52+
[end-prod (* new-end (list-ref nums (- n i 1)))])
53+
(loop (+ i 1)
54+
start-prod
55+
end-prod
56+
(max overall-max-prod start-prod end-prod)))))))
57+
```

0 commit comments

Comments
 (0)