Skip to content

Commit 7d76e56

Browse files
committed
Added racket
1 parent b2000e1 commit 7d76e56

File tree

11 files changed

+633
-2
lines changed

11 files changed

+633
-2
lines changed

src/main/erlang/g0001_0100/s0001_two_sum/readme.md

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ You can return the answer in any order.
4343
## Solution
4444

4545
```erlang
46-
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
47-
4846
-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].
4947
two_sum(Nums, Target) ->
5048
two_sum(Nums, Target, #{}, 0).
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+
## 207\. Course Schedule
5+
6+
Medium
7+
8+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.
9+
10+
* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
11+
12+
Return `true` if you can finish all courses. Otherwise, return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** numCourses = 2, prerequisites = \[\[1,0]]
17+
18+
**Output:** true
19+
20+
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
21+
22+
**Example 2:**
23+
24+
**Input:** numCourses = 2, prerequisites = \[\[1,0],[0,1]]
25+
26+
**Output:** false
27+
28+
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
29+
30+
**Constraints:**
31+
32+
* `1 <= numCourses <= 2000`
33+
* `0 <= prerequisites.length <= 5000`
34+
* `prerequisites[i].length == 2`
35+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
36+
* All the pairs prerequisites[i] are **unique**.
37+
38+
## Solution
39+
40+
```racket
41+
(define (can-finish num-courses prereqs)
42+
(let ([adj (make-hash)]
43+
[seen (make-vector num-courses 0)])
44+
(for ([x prereqs])
45+
(hash-set! adj (first x)
46+
(cons (second x) (hash-ref adj (first x) '()))))
47+
48+
(define (dfs node)
49+
(cond ((= 1 (vector-ref seen node)) #f)
50+
((= 2 (vector-ref seen node)) #t)
51+
(else
52+
(vector-set! seen node 1)
53+
(if (andmap dfs (hash-ref adj node '()))
54+
(begin
55+
(vector-set! seen node 2)
56+
#t)
57+
#f))))
58+
59+
(andmap dfs (hash-keys adj))))
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
## 208\. Implement Trie (Prefix Tree)
5+
6+
Medium
7+
8+
A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
9+
10+
Implement the Trie class:
11+
12+
* `Trie()` Initializes the trie object.
13+
* `void insert(String word)` Inserts the string `word` into the trie.
14+
* `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise.
15+
* `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise.
16+
17+
**Example 1:**
18+
19+
**Input** ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
20+
21+
**Output:** [null, null, true, false, true, null, true]
22+
23+
**Explanation:**
24+
25+
Trie trie = new Trie();
26+
27+
trie.insert("apple");
28+
29+
trie.search("apple"); // return True
30+
31+
trie.search("app"); // return False
32+
33+
trie.startsWith("app"); // return True
34+
35+
trie.insert("app");
36+
37+
trie.search("app"); // return True
38+
39+
**Constraints:**
40+
41+
* `1 <= word.length, prefix.length <= 2000`
42+
* `word` and `prefix` consist only of lowercase English letters.
43+
* At most <code>3 * 10<sup>4</sup></code> calls **in total** will be made to `insert`, `search`, and `startsWith`.
44+
45+
## Solution
46+
47+
```racket
48+
(define trie%
49+
(class object%
50+
(super-new)
51+
52+
;; Define TrieNode struct
53+
(struct trie-node (children is-word?) #:mutable)
54+
55+
;; Root node of the Trie
56+
(init-field)
57+
(define root (trie-node (make-hash) #f))
58+
(define start-with? #f)
59+
60+
;; Inserts a word into the trie.
61+
(define/public (insert word)
62+
(define (insert-helper node word idx)
63+
(if (= idx (string-length word))
64+
(set-trie-node-is-word?! node #t)
65+
(let* ([ch (string-ref word idx)]
66+
[children (trie-node-children node)]
67+
[next-node (hash-ref children ch (lambda () (trie-node (make-hash) #f)))])
68+
(hash-set! children ch next-node)
69+
(insert-helper next-node word (+ idx 1)))))
70+
(insert-helper root word 0))
71+
72+
;; Searches for a word in the trie.
73+
(define/public (search word)
74+
(define (search-helper node word idx)
75+
(if (= idx (string-length word))
76+
(begin
77+
(set! start-with? #t)
78+
(trie-node-is-word? node))
79+
(let* ([ch (string-ref word idx)]
80+
[children (trie-node-children node)]
81+
[next-node (hash-ref children ch #f)])
82+
(if next-node
83+
(search-helper next-node word (+ idx 1))
84+
(begin
85+
(set! start-with? #f)
86+
#f)))))
87+
(search-helper root word 0))
88+
89+
;; Checks if any word in the trie starts with the given prefix.
90+
(define/public (starts-with prefix)
91+
(send this search prefix)
92+
start-with?)))
93+
94+
;; Your trie% object will be instantiated and called as such:
95+
;; (define obj (new trie%))
96+
;; (send obj insert word)
97+
;; (define param_2 (send obj search word))
98+
;; (define param_3 (send obj starts-with prefix))
99+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
## 215\. Kth Largest Element in an Array
5+
6+
Medium
7+
8+
Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _largest element in the array_.
9+
10+
Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.
11+
12+
You must solve it in `O(n)` time complexity.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [3,2,1,5,6,4], k = 2
17+
18+
**Output:** 5
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,2,3,1,2,4,5,5,6], k = 4
23+
24+
**Output:** 4
25+
26+
**Constraints:**
27+
28+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
30+
31+
## Solution
32+
33+
```racket
34+
(define/contract (find-kth-largest nums k)
35+
(-> (listof exact-integer?) exact-integer? exact-integer?)
36+
(let* ([sorted-nums (sort nums >)] ; Sort in descending order
37+
[index (- k 1)]) ; Convert 1-based to 0-based index
38+
(list-ref sorted-nums index))) ; Return the k-th largest element
39+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
## 221\. Maximal Square
5+
6+
Medium
7+
8+
Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg)
13+
14+
**Input:** matrix = \[\["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
15+
16+
**Output:** 4
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg)
21+
22+
**Input:** matrix = \[\["0","1"],["1","0"]]
23+
24+
**Output:** 1
25+
26+
**Example 3:**
27+
28+
**Input:** matrix = \[\["0"]]
29+
30+
**Output:** 0
31+
32+
**Constraints:**
33+
34+
* `m == matrix.length`
35+
* `n == matrix[i].length`
36+
* `1 <= m, n <= 300`
37+
* `matrix[i][j]` is `'0'` or `'1'`.
38+
39+
## Solution
40+
41+
```racket
42+
(define/contract (maximal-square matrix)
43+
(-> (listof (listof char?)) exact-integer?)
44+
(let* ([m (length matrix)]
45+
[n (if (zero? m) 0 (length (first matrix)))]
46+
[dp (make-hash)] ; Hash table to simulate a 2D array
47+
[max-size 0])
48+
49+
(define (get-dp i j)
50+
(hash-ref dp (cons i j) 0)) ; Default to 0 if not found
51+
52+
;; Iterate over the matrix
53+
(for ([i (in-range m)])
54+
(for ([j (in-range n)])
55+
(when (char=? (list-ref (list-ref matrix i) j) #\1)
56+
(let* ([min-neighbor (min (get-dp i j) (get-dp (+ i 1) j) (get-dp i (+ j 1)))]
57+
[size (+ 1 min-neighbor)])
58+
(hash-set! dp (cons (+ i 1) (+ j 1)) size)
59+
(set! max-size (max max-size size))))))
60+
61+
(* max-size max-size))) ; Return area of the largest square
62+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 226\. Invert Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, invert the tree, and return _its root_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg)
13+
14+
**Input:** root = [4,2,7,1,3,6,9]
15+
16+
**Output:** [4,7,2,9,6,3,1]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg)
21+
22+
**Input:** root = [2,1,3]
23+
24+
**Output:** [2,3,1]
25+
26+
**Example 3:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range `[0, 100]`.
35+
* `-100 <= Node.val <= 100`
36+
37+
## Solution
38+
39+
```racket
40+
; Definition for a binary tree node.
41+
#|
42+
; val : integer?
43+
; left : (or/c tree-node? #f)
44+
; right : (or/c tree-node? #f)
45+
(struct tree-node
46+
(val left right) #:mutable #:transparent)
47+
48+
; Helper function to create a tree node.
49+
|#
50+
(define (make-tree-node val [left #f] [right #f])
51+
(tree-node val left right))
52+
53+
; Function to invert a binary tree.
54+
(define/contract (invert-tree root)
55+
(-> (or/c tree-node? #f) (or/c tree-node? #f))
56+
(cond
57+
[(not root) #f] ; Base case: empty tree.
58+
[else
59+
(make-tree-node
60+
(tree-node-val root)
61+
(invert-tree (tree-node-right root))
62+
(invert-tree (tree-node-left root)))]))
63+
```

0 commit comments

Comments
 (0)