Skip to content

Commit a89db11

Browse files
committedFeb 6, 2025·
Added racket
1 parent f0a734d commit a89db11

File tree

20 files changed

+1318
-0
lines changed

20 files changed

+1318
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
## 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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
## 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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
## 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+
```
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+
## 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+
```

0 commit comments

Comments
 (0)
Please sign in to comment.