Skip to content

Commit f9ef4ce

Browse files
committed
Added racket
1 parent 7d76e56 commit f9ef4ce

File tree

15 files changed

+969
-0
lines changed

15 files changed

+969
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 84\. Largest Rectangle in Histogram
5+
6+
Hard
7+
8+
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_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)
13+
14+
**Input:** heights = [2,1,5,6,2,3]
15+
16+
**Output:** 10
17+
18+
**Explanation:** The above is a histogram where width of each bar is 1.
19+
20+
The largest rectangle is shown in the red area, which has an area = 10 units.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)
25+
26+
**Input:** heights = [2,4]
27+
28+
**Output:** 4
29+
30+
**Constraints:**
31+
32+
* <code>1 <= heights.length <= 10<sup>5</sup></code>
33+
* <code>0 <= heights[i] <= 10<sup>4</sup></code>
34+
35+
## Solution
36+
37+
```racket
38+
(define/contract (largest-rectangle-area heights)
39+
(-> (listof exact-integer?) exact-integer?)
40+
(let* ((n (length heights))
41+
(heights-vec (list->vector (append heights '(0))))
42+
(stack '())
43+
(max-area 0))
44+
45+
(for ([i (in-range (add1 n))])
46+
(let loop ()
47+
(when (and (not (null? stack))
48+
(<= (vector-ref heights-vec i) (vector-ref heights-vec (car stack))))
49+
(let* ((top (car stack))
50+
(new-stack (cdr stack))
51+
(height (vector-ref heights-vec top))
52+
(width (if (null? new-stack) i (- i (car new-stack) 1)))
53+
(area (* height width)))
54+
(set! max-area (max max-area area))
55+
(set! stack new-stack)
56+
(loop))))
57+
(set! stack (cons i stack)))
58+
59+
max-area))
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
## 322\. Coin Change
5+
6+
Medium
7+
8+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
9+
10+
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`.
11+
12+
You may assume that you have an infinite number of each kind of coin.
13+
14+
**Example 1:**
15+
16+
**Input:** coins = [1,2,5], amount = 11
17+
18+
**Output:** 3
19+
20+
**Explanation:** 11 = 5 + 5 + 1
21+
22+
**Example 2:**
23+
24+
**Input:** coins = [2], amount = 3
25+
26+
**Output:** -1
27+
28+
**Example 3:**
29+
30+
**Input:** coins = [1], amount = 0
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* `1 <= coins.length <= 12`
37+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
38+
* <code>0 <= amount <= 10<sup>4</sup></code>
39+
40+
## Solution
41+
42+
```racket
43+
(define/contract (coin-change coins amount)
44+
(-> (listof exact-integer?) exact-integer? exact-integer?)
45+
(let ([dp (make-vector (+ amount 1) 0)])
46+
(vector-set! dp 0 1)
47+
(for ([coin coins])
48+
(for ([i (in-range coin (+ amount 1))])
49+
(let ([prev (vector-ref dp (- i coin))])
50+
(when (> prev 0)
51+
(vector-set! dp i
52+
(if (= (vector-ref dp i) 0)
53+
(+ prev 1)
54+
(min (vector-ref dp i) (+ prev 1))))))))
55+
(- (vector-ref dp amount) 1)))
56+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
## 338\. Counting Bits
5+
6+
Easy
7+
8+
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`.
9+
10+
**Example 1:**
11+
12+
**Input:** n = 2
13+
14+
**Output:** [0,1,1]
15+
16+
**Explanation:**
17+
18+
0 --> 0
19+
1 --> 1
20+
2 --> 10
21+
22+
**Example 2:**
23+
24+
**Input:** n = 5
25+
26+
**Output:** [0,1,1,2,1,2]
27+
28+
**Explanation:**
29+
30+
0 --> 0
31+
1 --> 1
32+
2 --> 10
33+
3 --> 11
34+
4 --> 100
35+
5 --> 101
36+
37+
**Constraints:**
38+
39+
* <code>0 <= n <= 10<sup>5</sup></code>
40+
41+
**Follow up:**
42+
43+
* 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?
44+
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)?
45+
46+
## Solution
47+
48+
```racket
49+
(define (near-bit n i)
50+
(if (and (<= i n) (< n (* i 2)))
51+
i
52+
(near-bit n (* i 2))))
53+
54+
(define/contract (count-bits n)
55+
(-> exact-integer? (listof exact-integer?))
56+
(match n
57+
[0 '(0)]
58+
[1 '(0 1)]
59+
[else
60+
(let* ([b (near-bit n 1)]
61+
[prev (count-bits (- b 1))]
62+
[next (map (lambda (n) (+ n 1)) prev)])
63+
(take (append prev next) (+ n 1)))]
64+
))
65+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
## 347\. Top K Frequent Elements
5+
6+
Medium
7+
8+
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,1,1,2,2,3], k = 2
13+
14+
**Output:** [1,2]
15+
16+
**Example 2:**
17+
18+
**Input:** nums = [1], k = 1
19+
20+
**Output:** [1]
21+
22+
**Constraints:**
23+
24+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
25+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
26+
* `k` is in the range `[1, the number of unique elements in the array]`.
27+
* It is **guaranteed** that the answer is **unique**.
28+
29+
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size.
30+
31+
## Solution
32+
33+
```racket
34+
(define (hash-table-counter ls)
35+
(let ([counts (make-hash)])
36+
(for-each (λ (v) (hash-update! counts v add1 0)) ls)
37+
counts))
38+
39+
(define (top-k-frequent nums k)
40+
(let ([counter (hash-table-counter nums)])
41+
(map car (take (sort (hash->list counter) #:key cdr >) k))))
42+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
## 394\. Decode String
5+
6+
Medium
7+
8+
Given an encoded string, return its decoded string.
9+
10+
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.
11+
12+
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]`.
13+
14+
The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "3[a]2[bc]"
19+
20+
**Output:** "aaabcbc"
21+
22+
**Example 2:**
23+
24+
**Input:** s = "3[a2[c]]"
25+
26+
**Output:** "accaccacc"
27+
28+
**Example 3:**
29+
30+
**Input:** s = "2[abc]3[cd]ef"
31+
32+
**Output:** "abcabccdcdcdef"
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 30`
37+
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
38+
* `s` is guaranteed to be **a valid** input.
39+
* All the integers in `s` are in the range `[1, 300]`.
40+
41+
## Solution
42+
43+
```racket
44+
(define/contract (decode-string s)
45+
(-> string? string?)
46+
(let-values ([(result _) (decode-helper (string->list s) 0)])
47+
result))
48+
49+
(define (decode-helper chars pos)
50+
(let loop ([current-pos pos]
51+
[count 0]
52+
[result ""])
53+
(if (>= current-pos (length chars))
54+
(values result current-pos)
55+
(let ([char (list-ref chars current-pos)])
56+
(cond
57+
; If it's a letter, append it to result
58+
[(char-alphabetic? char)
59+
(loop (add1 current-pos)
60+
count
61+
(string-append result (string char)))]
62+
63+
; If it's a digit, update count
64+
[(char-numeric? char)
65+
(loop (add1 current-pos)
66+
(+ (* count 10) (- (char->integer char) (char->integer #\0)))
67+
result)]
68+
69+
; If it's '[', handle nested string
70+
[(char=? char #\[)
71+
(let-values ([(nested-str new-pos) (decode-helper chars (add1 current-pos))])
72+
(loop new-pos
73+
0
74+
(string-append result
75+
(string-join
76+
(make-list count nested-str)
77+
""))))]
78+
79+
; If it's ']', return current result and position
80+
[(char=? char #\])
81+
(values result (add1 current-pos))]
82+
83+
; Skip other characters
84+
[else (loop (add1 current-pos) count result)])))))
85+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
## 416\. Partition Equal Subset Sum
5+
6+
Medium
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,5,11,5]
13+
14+
**Output:** true
15+
16+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [1,2,3,5]
21+
22+
**Output:** false
23+
24+
**Explanation:** The array cannot be partitioned into equal sum subsets.
25+
26+
**Constraints:**
27+
28+
* `1 <= nums.length <= 200`
29+
* `1 <= nums[i] <= 100`
30+
31+
## Solution
32+
33+
```racket
34+
(define/contract (can-partition nums)
35+
(-> (listof exact-integer?) boolean?)
36+
(let* ([sum (apply + nums)])
37+
(if (odd? sum)
38+
#f
39+
(let* ([target (/ sum 2)]
40+
[dp (make-hash)])
41+
(hash-set! dp 0 #t)
42+
(for ([num nums])
43+
(for ([key (reverse (hash-keys dp))])
44+
(let ([new-sum (+ key num)])
45+
(when (<= new-sum target)
46+
(hash-set! dp new-sum #t)))))
47+
(hash-ref dp target #f)))))
48+
```

0 commit comments

Comments
 (0)