Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb21f1d

Browse files
committedFeb 5, 2025·
Added tasks 64-136
1 parent cefb524 commit bb21f1d

File tree

21 files changed

+1398
-2
lines changed

21 files changed

+1398
-2
lines changed
 

‎README.md

Lines changed: 80 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
## Solution
36+
37+
```racket
38+
; dynamic programming helper function
39+
(define (mpsAux grid curpos dpTable ub)
40+
(local
41+
[(define (get grid pos) (list-ref (list-ref grid (car pos)) (cdr pos)))]
42+
(cond
43+
; return start value
44+
[(equal? curpos (cons 0 0)) (car (car grid))]
45+
46+
; handle out of bounds
47+
[(or (< (car curpos) 0) (< (cdr curpos) 0)) +inf.0]
48+
49+
; position appeared before
50+
[(hash-ref dpTable curpos #f) (hash-ref dpTable curpos)]
51+
52+
; inductive case
53+
[else
54+
(let*
55+
(
56+
; go up
57+
[u_val (mpsAux grid (cons (- (car curpos) 1) (cdr curpos)) dpTable ub)]
58+
59+
; go left
60+
[l_val (mpsAux grid (cons (car curpos) (- (cdr curpos) 1)) dpTable ub)]
61+
62+
; compute the current least cost
63+
[cur-least-cost (+ (get grid curpos) (min u_val l_val))]
64+
)
65+
(hash-set! dpTable curpos cur-least-cost)
66+
cur-least-cost
67+
)
68+
]
69+
)
70+
)
71+
)
72+
73+
; the (x . y) position of the grid is the x'th row and y'th column of the grid
74+
(define/contract (min-path-sum grid)
75+
(-> (listof (listof exact-integer?)) exact-integer?)
76+
(local
77+
[(define dpTable (make-hash))]
78+
(let*
79+
(
80+
[curpos (cons (- (length grid) 1) (- (length (car grid)) 1))]
81+
[least-val (mpsAux grid curpos dpTable 200)]
82+
)
83+
(inexact->exact least-val)
84+
)
85+
)
86+
)
87+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 70\. Climbing Stairs
5+
6+
Easy
7+
8+
You are climbing a staircase. It takes `n` steps to reach the top.
9+
10+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are two ways to climb to the top.
19+
20+
1. 1 step + 1 step
21+
22+
2. 2 steps
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3
27+
28+
**Output:** 3
29+
30+
**Explanation:** There are three ways to climb to the top.
31+
32+
1. 1 step + 1 step + 1 step
33+
34+
2. 1 step + 2 steps
35+
36+
3. 2 steps + 1 step
37+
38+
**Constraints:**
39+
40+
* `1 <= n <= 45`
41+
42+
## Solution
43+
44+
```racket
45+
(define (clmHelp n hTable)
46+
(cond
47+
; base cases
48+
((= 1 n) 1)
49+
((= 2 n) 2)
50+
((hash-ref hTable n #f) (hash-ref hTable n))
51+
52+
; inductive case
53+
(else
54+
(let*
55+
; the local variables
56+
(
57+
(a (clmHelp (- n 1) hTable))
58+
(b (clmHelp (- n 2) hTable))
59+
(numPos (+ a b))
60+
)
61+
62+
; the body
63+
(hash-set! hTable n numPos)
64+
numPos
65+
)
66+
)
67+
)
68+
)
69+
70+
(define/contract (climb-stairs n)
71+
(-> exact-integer? exact-integer?)
72+
(local
73+
; local definitions
74+
((define hTable (make-hash)))
75+
76+
; function body
77+
(clmHelp n hTable)
78+
)
79+
)
80+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 72\. Edit Distance
5+
6+
Hard
7+
8+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
9+
10+
You have the following three operations permitted on a word:
11+
12+
* Insert a character
13+
* Delete a character
14+
* Replace a character
15+
16+
**Example 1:**
17+
18+
**Input:** word1 = "horse", word2 = "ros"
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
horse -> rorse (replace 'h' with 'r')
25+
26+
rorse -> rose (remove 'r')
27+
28+
rose -> ros (remove 'e')
29+
30+
**Example 2:**
31+
32+
**Input:** word1 = "intention", word2 = "execution"
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
intention -> inention (remove 't')
39+
40+
inention -> enention (replace 'i' with 'e')
41+
42+
enention -> exention (replace 'n' with 'x')
43+
44+
exention -> exection (replace 'n' with 'c')
45+
46+
exection -> execution (insert 'u')
47+
48+
**Constraints:**
49+
50+
* `0 <= word1.length, word2.length <= 500`
51+
* `word1` and `word2` consist of lowercase English letters.
52+
53+
## Solution
54+
55+
```racket
56+
(define/contract (min-distance word1 word2)
57+
(-> string? string? exact-integer?)
58+
(let* ((n1 (string-length word1))
59+
(n2 (string-length word2)))
60+
(if (> n2 n1)
61+
(min-distance word2 word1)
62+
(let ((dp (make-vector (+ n2 1) 0)))
63+
(for ([j (in-range (+ n2 1))])
64+
(vector-set! dp j j))
65+
(for ([i (in-range 1 (+ n1 1))])
66+
(let ((pre (vector-ref dp 0)))
67+
(vector-set! dp 0 i)
68+
(for ([j (in-range 1 (+ n2 1))])
69+
(let* ((tmp (vector-ref dp j))
70+
(cost (if (char=? (string-ref word1 (- i 1)) (string-ref word2 (- j 1)))
71+
pre
72+
(+ 1 (min pre (vector-ref dp j) (vector-ref dp (- j 1)))))))
73+
(vector-set! dp j cost)
74+
(set! pre tmp)))))
75+
(vector-ref dp n2)))))
76+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 74\. Search a 2D Matrix
5+
6+
Medium
7+
8+
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
9+
10+
* Integers in each row are sorted from left to right.
11+
* The first integer of each row is greater than the last integer of the previous row.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
16+
17+
**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
18+
19+
**Output:** true
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
24+
25+
**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* `m == matrix.length`
32+
* `n == matrix[i].length`
33+
* `1 <= m, n <= 100`
34+
* <code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code>
35+
36+
## Solution
37+
38+
```racket
39+
(define/contract (search-matrix matrix target)
40+
(-> (listof (listof exact-integer?)) exact-integer? boolean?)
41+
(not (equal? #f (member target (apply append matrix)))))
42+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 76\. Minimum Window Substring
5+
6+
Hard
7+
8+
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window substring** of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window. If there is no such substring__, return the empty string_ `""`_._
9+
10+
The testcases will be generated such that the answer is **unique**.
11+
12+
A **substring** is a contiguous sequence of characters within the string.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "ADOBECODEBANC", t = "ABC"
17+
18+
**Output:** "BANC"
19+
20+
**Explanation:** The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "a", t = "a"
25+
26+
**Output:** "a"
27+
28+
**Explanation:** The entire string s is the minimum window.
29+
30+
**Example 3:**
31+
32+
**Input:** s = "a", t = "aa"
33+
34+
**Output:** ""
35+
36+
**Explanation:** Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
37+
38+
**Constraints:**
39+
40+
* `m == s.length`
41+
* `n == t.length`
42+
* <code>1 <= m, n <= 10<sup>5</sup></code>
43+
* `s` and `t` consist of uppercase and lowercase English letters.
44+
45+
**Follow up:** Could you find an algorithm that runs in `O(m + n)` time?
46+
47+
## Solution
48+
49+
```racket
50+
(define (zip lst1 lst2)
51+
(map cons lst1 lst2))
52+
53+
(define (make-zero-hash lst)
54+
(make-immutable-hash (zip lst (make-list (length lst) 0))))
55+
56+
(define (hash-inc k ht)
57+
(hash-update ht k add1 0))
58+
59+
(define (safe-hash-inc k ht)
60+
(if (hash-ref ht k #f) (hash-update ht k add1) ht))
61+
62+
(define (safe-hash-dec k ht)
63+
(if (hash-ref ht k #f) (hash-update ht k sub1) ht))
64+
65+
(define (frequencies lst)
66+
(foldl hash-inc (hash) lst))
67+
68+
(define (pair-min l1 r1 l2 r2)
69+
(if (< (- r1 l1) (- r2 l2)) (values l1 r1) (values l2 r2)))
70+
71+
(define (sref s i) (string-ref s (min (sub1 (string-length s)) i)))
72+
73+
(define (min-window s t)
74+
(define t-lst (string->list t))
75+
76+
(define target (frequencies t-lst))
77+
78+
(define (delta-dec delta seen c)
79+
(cond
80+
[(and (hash-ref target c #f)
81+
(< (hash-ref seen c) (hash-ref target c)))
82+
(sub1 delta)]
83+
[else delta]))
84+
85+
(define (delta-inc delta seen c)
86+
(cond
87+
[(and (hash-ref target c #f)
88+
(<= (hash-ref seen c) (hash-ref target c)))
89+
(add1 delta)]
90+
[else delta]))
91+
92+
(let loop ([l 0] [r 0] [seen (make-zero-hash t-lst)]
93+
[delta (length t-lst)] [sl 0] [sr 0])
94+
(define-values (left-char right-char) (values (sref s l) (sref s r)))
95+
(define-values (l* r*) (pair-min l r sl sr))
96+
(cond
97+
[(and (= 0 sr)
98+
(= delta 0))
99+
(loop (add1 l) r (safe-hash-dec left-char seen)
100+
(delta-inc delta seen left-char) l r)]
101+
[(and (= r (string-length s)) (= delta 0))
102+
(loop (add1 l) r (safe-hash-dec left-char seen)
103+
(delta-inc delta seen left-char) l* r*)]
104+
[(= r (string-length s))
105+
(substring s sl sr)]
106+
[(= delta 0)
107+
(loop (add1 l) r (safe-hash-dec left-char seen)
108+
(delta-inc delta seen left-char) l* r*)]
109+
[else
110+
(loop l (add1 r) (safe-hash-inc right-char seen)
111+
(delta-dec delta seen right-char) sl sr)])))
112+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 78\. Subsets
5+
6+
Medium
7+
8+
Given an integer array `nums` of **unique** elements, return _all possible subsets (the power set)_.
9+
10+
The solution set **must not** contain duplicate subsets. Return the solution in **any order**.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,3]
15+
16+
**Output:** [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [0]
21+
22+
**Output:** [[],[0]]
23+
24+
**Constraints:**
25+
26+
* `1 <= nums.length <= 10`
27+
* `-10 <= nums[i] <= 10`
28+
* All the numbers of `nums` are **unique**.
29+
30+
## Solution
31+
32+
```racket
33+
(define/contract (subset-recur nums ans)
34+
(-> (listof exact-integer?) (listof (listof exact-integer?)) (listof (listof exact-integer?)))
35+
(cond [(empty? nums) ans]
36+
[else (let ([curr (map (lambda (x) (cons (first nums) x)) ans)])
37+
(subset-recur (rest nums) (append ans curr)))]))
38+
39+
(define/contract (subsets nums)
40+
(-> (listof exact-integer?) (listof (listof exact-integer?)))
41+
(subset-recur nums (list empty)))
42+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 79\. Word Search
5+
6+
Medium
7+
8+
Given an `m x n` grid of characters `board` and a string `word`, return `true` _if_ `word` _exists in the grid_.
9+
10+
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
15+
16+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
23+
24+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
25+
26+
**Output:** true
27+
28+
**Example 3:**
29+
30+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
31+
32+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `m == board.length`
39+
* `n = board[i].length`
40+
* `1 <= m, n <= 6`
41+
* `1 <= word.length <= 15`
42+
* `board` and `word` consists of only lowercase and uppercase English letters.
43+
44+
**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
45+
46+
## Solution
47+
48+
```racket
49+
(define/contract (exist board word)
50+
(-> (listof (listof char?)) string? boolean?)
51+
(let* ([rows (length board)]
52+
[cols (length (first board))]
53+
[word-len (string-length word)])
54+
55+
(define (dfs board x y index)
56+
(if (= index word-len)
57+
#t
58+
(let ([current (list-ref (list-ref board x) y)]
59+
[next-char (string-ref word index)])
60+
(define directions '((-1 0) (1 0) (0 -1) (0 1))) ; up down left right
61+
(define new-board
62+
(list-set board x
63+
(list-set (list-ref board x) y #\-)))
64+
65+
(ormap
66+
(λ (dir)
67+
(let ([new-x (+ x (first dir))]
68+
[new-y (+ y (second dir))])
69+
(and (>= new-x 0) (< new-x rows)
70+
(>= new-y 0) (< new-y cols)
71+
(char=? (list-ref (list-ref board new-x) new-y) next-char)
72+
(dfs new-board new-x new-y (add1 index)))))
73+
directions))))
74+
75+
(for*/or ([i (range rows)]
76+
[j (range cols)])
77+
(and (char=? (list-ref (list-ref board i) j)
78+
(string-ref word 0))
79+
(dfs board i j 1)))))
80+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 94\. Binary Tree Inorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
13+
14+
**Input:** root = [1,null,2,3]
15+
16+
**Output:** [1,3,2]
17+
18+
**Example 2:**
19+
20+
**Input:** root = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** root = [1]
27+
28+
**Output:** [1]
29+
30+
**Example 4:**
31+
32+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)
33+
34+
**Input:** root = [1,2]
35+
36+
**Output:** [2,1]
37+
38+
**Example 5:**
39+
40+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)
41+
42+
**Input:** root = [1,null,2]
43+
44+
**Output:** [1,2]
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is in the range `[0, 100]`.
49+
* `-100 <= Node.val <= 100`
50+
51+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
52+
53+
## Solution
54+
55+
```racket
56+
; Definition for a binary tree node.
57+
#|
58+
59+
; val : integer?
60+
; left : (or/c tree-node? #f)
61+
; right : (or/c tree-node? #f)
62+
(struct tree-node
63+
(val left right) #:mutable #:transparent)
64+
65+
; constructor
66+
(define (make-tree-node [val 0])
67+
(tree-node val #f #f))
68+
69+
|#
70+
71+
(define (inorder-traversal tree)
72+
(if (not tree)
73+
'()
74+
(append (inorder-traversal (tree-node-left tree))
75+
(list (tree-node-val tree))
76+
(inorder-traversal (tree-node-right tree)))))
77+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 96\. Unique Binary Search Trees
5+
6+
Medium
7+
8+
Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)
13+
14+
**Input:** n = 3
15+
16+
**Output:** 5
17+
18+
**Example 2:**
19+
20+
**Input:** n = 1
21+
22+
**Output:** 1
23+
24+
**Constraints:**
25+
26+
* `1 <= n <= 19`
27+
28+
## Solution
29+
30+
```racket
31+
(define/contract (num-trees n)
32+
(-> exact-integer? exact-integer?)
33+
(let loop ((i 0) (result 1))
34+
(if (= i n)
35+
(quotient result (+ n 1))
36+
(loop (+ i 1)
37+
(quotient (* result (- (* 2 n) i)) (+ i 1))))))
38+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 98\. Validate Binary Search Tree
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
9+
10+
A **valid BST** is defined as follows:
11+
12+
* The left subtree of a node contains only nodes with keys **less than** the node's key.
13+
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
14+
* Both the left and right subtrees must also be binary search trees.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg)
19+
20+
**Input:** root = [2,1,3]
21+
22+
**Output:** true
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg)
27+
28+
**Input:** root = [5,1,4,null,null,3,6]
29+
30+
**Output:** false
31+
32+
**Explanation:** The root node's value is 5 but its right child's value is 4.
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
37+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>
38+
39+
## Solution
40+
41+
```racket
42+
; Definition for a binary tree node.
43+
#|
44+
45+
; val : integer?
46+
; left : (or/c tree-node? #f)
47+
; right : (or/c tree-node? #f)
48+
(struct tree-node
49+
(val left right) #:mutable #:transparent)
50+
51+
; constructor
52+
(define (make-tree-node [val 0])
53+
(tree-node val #f #f))
54+
55+
|#
56+
57+
(define/contract (is-valid-bst-recur root low high)
58+
(-> (or/c tree-node? #f) exact-integer? exact-integer? boolean?)
59+
(if (tree-node? root)
60+
(let ([v (tree-node-val root)])
61+
(and (< low v) (> high v) (is-valid-bst-recur (tree-node-left root) low v) (is-valid-bst-recur (tree-node-right root) v high)))
62+
true))
63+
64+
(define/contract (is-valid-bst root)
65+
(-> (or/c tree-node? #f) boolean?)
66+
(is-valid-bst-recur root (- (-(expt 2 31)) 5) (expt 2 31)))
67+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 101\. Symmetric Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
13+
14+
**Input:** root = [1,2,2,3,4,4,3]
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg)
21+
22+
**Input:** root = [1,2,2,null,3,null,3]
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is in the range `[1, 1000]`.
29+
* `-100 <= Node.val <= 100`
30+
31+
**Follow up:** Could you solve it both recursively and iteratively?
32+
33+
## Solution
34+
35+
```racket
36+
; Definition for a binary tree node.
37+
#|
38+
39+
; val : integer?
40+
; left : (or/c tree-node? #f)
41+
; right : (or/c tree-node? #f)
42+
(struct tree-node
43+
(val left right) #:mutable #:transparent)
44+
45+
; constructor
46+
(define (make-tree-node [val 0])
47+
(tree-node val #f #f))
48+
49+
|#
50+
51+
(define/contract (is-symmetric root)
52+
(-> (or/c tree-node? #f) boolean?)
53+
(define (two-symmetric? left right)
54+
(cond
55+
[(and (not left) (not right )) #t]
56+
[(and left (not right )) #f]
57+
[(and (not left) right) #f]
58+
[(not (equal? (tree-node-val left) (tree-node-val right))) #f]
59+
[else
60+
(and (two-symmetric? (tree-node-left left) (tree-node-right right))
61+
(two-symmetric? (tree-node-right left) (tree-node-left right)))]))
62+
(two-symmetric? (tree-node-left root) (tree-node-right root)))
63+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 102\. Binary Tree Level Order Traversal
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
13+
14+
**Input:** root = [3,9,20,null,null,15,7]
15+
16+
**Output:** [[3],[9,20],[15,7]]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1]
21+
22+
**Output:** [[1]]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 2000]`.
33+
* `-1000 <= Node.val <= 1000`
34+
35+
## Solution
36+
37+
```racket
38+
; Definition for a binary tree node.
39+
#|
40+
41+
; val : integer?
42+
; left : (or/c tree-node? #f)
43+
; right : (or/c tree-node? #f)
44+
(struct tree-node
45+
(val left right) #:mutable #:transparent)
46+
47+
; constructor
48+
(define (make-tree-node [val 0])
49+
(tree-node val #f #f))
50+
51+
|#
52+
53+
(define (make-level-table table level node)
54+
(if node
55+
(let* ([table (hash-update table level
56+
(lambda (lst) (cons (tree-node-val node) lst))
57+
'())]
58+
[table (make-level-table table (add1 level) (tree-node-right node))])
59+
(make-level-table table (add1 level) (tree-node-left node)))
60+
table))
61+
62+
(define/contract (level-order root)
63+
(-> (or/c tree-node? #f) (listof (listof exact-integer?)))
64+
(let ([table (make-level-table (hasheqv) 0 root)])
65+
(for/list ([level (in-range (hash-count table))])
66+
(hash-ref table level))))
67+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 104\. Maximum Depth of Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _its maximum depth_.
9+
10+
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
15+
16+
**Input:** root = [3,9,20,null,null,15,7]
17+
18+
**Output:** 3
19+
20+
**Example 2:**
21+
22+
**Input:** root = [1,null,2]
23+
24+
**Output:** 2
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
29+
* `-100 <= Node.val <= 100`
30+
31+
## Solution
32+
33+
```racket
34+
; Definition for a binary tree node.
35+
#|
36+
37+
; val : integer?
38+
; left : (or/c tree-node? #f)
39+
; right : (or/c tree-node? #f)
40+
(struct tree-node
41+
(val left right) #:mutable #:transparent)
42+
43+
; constructor
44+
(define (make-tree-node [val 0])
45+
(tree-node val #f #f))
46+
47+
|#
48+
49+
(define/contract (max-depth root)
50+
(-> (or/c tree-node? #f) exact-integer?)
51+
(if root
52+
(+ 1
53+
(max
54+
(max-depth (tree-node-left root))
55+
(max-depth (tree-node-right root))))
56+
0))
57+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 105\. Construct Binary Tree from Preorder and Inorder Traversal
5+
6+
Medium
7+
8+
Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)
13+
14+
**Input:** preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
15+
16+
**Output:** [3,9,20,null,null,15,7]
17+
18+
**Example 2:**
19+
20+
**Input:** preorder = [-1], inorder = [-1]
21+
22+
**Output:** [-1]
23+
24+
**Constraints:**
25+
26+
* `1 <= preorder.length <= 3000`
27+
* `inorder.length == preorder.length`
28+
* `-3000 <= preorder[i], inorder[i] <= 3000`
29+
* `preorder` and `inorder` consist of **unique** values.
30+
* Each value of `inorder` also appears in `preorder`.
31+
* `preorder` is **guaranteed** to be the preorder traversal of the tree.
32+
* `inorder` is **guaranteed** to be the inorder traversal of the tree.
33+
34+
## Solution
35+
36+
```racket
37+
; Definition for a binary tree node.
38+
#|
39+
40+
; val : integer?
41+
; left : (or/c tree-node? #f)
42+
; right : (or/c tree-node? #f)
43+
(struct tree-node
44+
(val left right) #:mutable #:transparent)
45+
46+
; constructor
47+
(define (make-tree-node [val 0])
48+
(tree-node val #f #f))
49+
50+
|#
51+
52+
(define/contract (build-tree preorder inorder)
53+
(-> (listof exact-integer?) (listof exact-integer?) (or/c tree-node? #f))
54+
55+
; Create a hash table mapping values to their indices in inorder traversal
56+
(define inorder-map
57+
(for/hash ([val inorder]
58+
[idx (range (length inorder))])
59+
(values val idx)))
60+
61+
; Helper function that builds the tree recursively
62+
(define (build j start end)
63+
(if (or (> start end)
64+
(>= j (length preorder)))
65+
#f
66+
(let* ([value (list-ref preorder j)]
67+
[index (hash-ref inorder-map value)]
68+
[node (tree-node value #f #f)]
69+
[left-subtree (build (add1 j) start (sub1 index))]
70+
[right-subtree (build (+ j 1 (- index start)) (add1 index) end)])
71+
(struct-copy tree-node node
72+
[left left-subtree]
73+
[right right-subtree]))))
74+
75+
; Start the tree building process
76+
(if (empty? preorder)
77+
#f
78+
(build 0 0 (sub1 (length preorder)))))
79+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 114\. Flatten Binary Tree to Linked List
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, flatten the tree into a "linked list":
9+
10+
* The "linked list" should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
11+
* The "linked list" should be in the same order as a [**pre-order** **traversal**](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) of the binary tree.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg)
16+
17+
**Input:** root = [1,2,5,3,4,null,6]
18+
19+
**Output:** [1,null,2,null,3,null,4,null,5,null,6]
20+
21+
**Example 2:**
22+
23+
**Input:** root = []
24+
25+
**Output:** []
26+
27+
**Example 3:**
28+
29+
**Input:** root = [0]
30+
31+
**Output:** [0]
32+
33+
**Constraints:**
34+
35+
* The number of nodes in the tree is in the range `[0, 2000]`.
36+
* `-100 <= Node.val <= 100`
37+
38+
**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)?
39+
40+
## Solution
41+
42+
```racket
43+
; Definition for a binary tree node.
44+
#|
45+
46+
; val : integer?
47+
; left : (or/c tree-node? #f)
48+
; right : (or/c tree-node? #f)
49+
(struct tree-node
50+
(val left right) #:mutable #:transparent)
51+
52+
; constructor
53+
(define (make-tree-node [val 0])
54+
(tree-node val #f #f))
55+
56+
|#
57+
58+
(define/contract (flatten root)
59+
(-> (or/c tree-node? #f) void?)
60+
61+
(define (find-tail root)
62+
(cond
63+
[(not root) root]
64+
[else
65+
(let ([left (tree-node-left root)]
66+
[right (tree-node-right root)])
67+
(if left
68+
(let ([tail (find-tail left)])
69+
; Stitch right subtree below the tail
70+
(set-tree-node-left! root #f)
71+
(set-tree-node-right! root left)
72+
(when tail
73+
(set-tree-node-right! tail right))
74+
; Return final tail
75+
(if (and tail (tree-node-right tail))
76+
(find-tail (tree-node-right tail))
77+
tail))
78+
; If no left subtree, current node is tail
79+
(if (tree-node-right root)
80+
(find-tail (tree-node-right root))
81+
root)))]))
82+
83+
(when root
84+
(find-tail root))
85+
(void))
86+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 121\. Best Time to Buy and Sell Stock
5+
6+
Easy
7+
8+
You are given an array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day.
9+
10+
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
11+
12+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
13+
14+
**Example 1:**
15+
16+
**Input:** prices = [7,1,5,3,6,4]
17+
18+
**Output:** 5
19+
20+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
21+
22+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
23+
24+
**Example 2:**
25+
26+
**Input:** prices = [7,6,4,3,1]
27+
28+
**Output:** 0
29+
30+
**Explanation:** In this case, no transactions are done and the max profit = 0.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
35+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
36+
37+
## Solution
38+
39+
```racket
40+
(define/contract (max-profit prices)
41+
(-> (listof exact-integer?) exact-integer?)
42+
(if (empty? prices)
43+
0
44+
(let-values ([(final-profit _)
45+
(for/fold ([max-profit 0]
46+
[min-price (first prices)])
47+
([price (in-list (rest prices))])
48+
(if (> price min-price)
49+
(values (max max-profit (- price min-price))
50+
min-price)
51+
(values max-profit price)))])
52+
final-profit)))
53+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 124\. Binary Tree Maximum Path Sum
5+
6+
Hard
7+
8+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
9+
10+
The **path sum** of a path is the sum of the node's values in the path.
11+
12+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
17+
18+
**Input:** root = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
27+
28+
**Input:** root = [-10,9,20,null,null,15,7]
29+
30+
**Output:** 42
31+
32+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
37+
* `-1000 <= Node.val <= 1000`
38+
39+
## Solution
40+
41+
```racket
42+
; Definition for a binary tree node.
43+
#|
44+
45+
; val : integer?
46+
; left : (or/c tree-node? #f)
47+
; right : (or/c tree-node? #f)
48+
(struct tree-node
49+
(val left right) #:mutable #:transparent)
50+
51+
; constructor
52+
(define (make-tree-node [val 0])
53+
(tree-node val #f #f))
54+
55+
|#
56+
57+
(define max-box (box -inf.0)) ; Rename max to avoid conflict
58+
59+
(define/contract (max-path-sum root)
60+
(-> (or/c tree-node? #f) exact-integer?)
61+
(set-box! max-box -inf.0) ; Reset max-box before calculation
62+
(helper root)
63+
(unbox max-box)) ; Return the stored max value
64+
65+
(define (helper node)
66+
(if (not node)
67+
0
68+
(let* ((left (max 0 (helper (tree-node-left node))))
69+
(right (max 0 (helper (tree-node-right node))))
70+
(current (+ (tree-node-val node) left right)))
71+
(when (> current (unbox max-box)) ; Correctly update max-box
72+
(set-box! max-box current))
73+
(+ (tree-node-val node) (max left right))))) ; Choose the max path
74+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 128\. Longest Consecutive Sequence
5+
6+
Medium
7+
8+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
9+
10+
You must write an algorithm that runs in `O(n)` time.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [100,4,200,1,3,2]
15+
16+
**Output:** 4
17+
18+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [0,3,7,2,5,8,4,6,0,1]
23+
24+
**Output:** 9
25+
26+
**Constraints:**
27+
28+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
30+
31+
## Solution
32+
33+
```racket
34+
(define (longest-consecutive nums)
35+
(define uniq (list->set nums))
36+
37+
(define (seq x a)
38+
(cond
39+
[(set-member? uniq x) (seq (add1 x) (add1 a))]
40+
[else a]))
41+
42+
(let loop ([l (set->list uniq)] [m 0])
43+
(match l
44+
['() m]
45+
[(cons x xs) #:when (not (set-member? uniq (sub1 x))) (loop xs (max m (seq x 0)))]
46+
[(cons x xs) (loop xs m)])))
47+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 131\. Palindrome Partitioning
5+
6+
Medium
7+
8+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return all possible palindrome partitioning of `s`.
9+
10+
A **palindrome** string is a string that reads the same backward as forward.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aab"
15+
16+
**Output:** [["a","a","b"],["aa","b"]]
17+
18+
**Example 2:**
19+
20+
**Input:** s = "a"
21+
22+
**Output:** [["a"]]
23+
24+
**Constraints:**
25+
26+
* `1 <= s.length <= 16`
27+
* `s` contains only lowercase English letters.
28+
29+
## Solution
30+
31+
```racket
32+
(define/contract (partition s)
33+
(-> string? (listof (listof string?)))
34+
(define res '())
35+
(define (backtracking currArr start)
36+
(if (= start (string-length s))
37+
(set! res (cons (reverse currArr) res))
38+
(for ([end (in-range start (string-length s))])
39+
(when (is-palindrome? s start end)
40+
(backtracking (cons (substring s start (add1 end)) currArr) (add1 end))))))
41+
(backtracking '() 0)
42+
res)
43+
44+
(define (is-palindrome? s start end)
45+
(let loop ((i start) (j end))
46+
(or (>= i j)
47+
(and (char=? (string-ref s i) (string-ref s j))
48+
(loop (add1 i) (sub1 j))))))
49+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Racket/LeetCode-in-Racket?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Racket/LeetCode-in-Racket?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Racket/LeetCode-in-Racket/fork)
3+
4+
## 136\. Single Number
5+
6+
Easy
7+
8+
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
9+
10+
You must implement a solution with a linear runtime complexity and use only constant extra space.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [2,2,1]
15+
16+
**Output:** 1
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [4,1,2,1,2]
21+
22+
**Output:** 4
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [1]
27+
28+
**Output:** 1
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
33+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
34+
* Each element in the array appears twice except for one element which appears only once.
35+
36+
## Solution
37+
38+
```racket
39+
(define (single-number nums)
40+
(apply bitwise-xor nums)
41+
)
42+
```

0 commit comments

Comments
 (0)
Please sign in to comment.