forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
887 additions
and
241 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
leetcode/0119.Pascals-Triangle-II/119. Pascal's Triangle II.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package leetcode | ||
|
||
func getRow(rowIndex int) []int { | ||
row := make([]int, rowIndex+1) | ||
row[0] = 1 | ||
for i := 1; i <= rowIndex; i++ { | ||
row[i] = row[i-1] * (rowIndex - i + 1) / i | ||
} | ||
return row | ||
} |
47 changes: 47 additions & 0 deletions
47
leetcode/0119.Pascals-Triangle-II/119. Pascal's Triangle II_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type question119 struct { | ||
para119 | ||
ans119 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para119 struct { | ||
rowIndex int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans119 struct { | ||
one []int | ||
} | ||
|
||
func Test_Problem119(t *testing.T) { | ||
|
||
qs := []question119{ | ||
|
||
{ | ||
para119{3}, | ||
ans119{[]int{1, 3, 3, 1}}, | ||
}, | ||
|
||
{ | ||
para119{0}, | ||
ans119{[]int{1}}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 119------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans119, q.para119 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, getRow(p.rowIndex)) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# [119. Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) | ||
|
||
|
||
## 题目 | ||
|
||
Given an integer `rowIndex`, return the `rowIndexth` row of the Pascal's triangle. | ||
|
||
Notice that the row index starts from **0**. | ||
|
||
![https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) | ||
|
||
In Pascal's triangle, each number is the sum of the two numbers directly above it. | ||
|
||
**Follow up:** | ||
|
||
Could you optimize your algorithm to use only *O*(*k*) extra space? | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: rowIndex = 3 | ||
Output: [1,3,3,1] | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: rowIndex = 0 | ||
Output: [1] | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: rowIndex = 1 | ||
Output: [1,1] | ||
``` | ||
|
||
**Constraints:** | ||
|
||
- `0 <= rowIndex <= 33` | ||
|
||
## 题目大意 | ||
|
||
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 | ||
|
||
## 解题思路 | ||
|
||
- 题目中的三角是杨辉三角,每个数字是 `(a+b)^n` 二项式展开的系数。题目要求我们只能使用 O(k) 的空间。那么需要找到两两项直接的递推关系。由组合知识得知: | ||
|
||
$$\begin{aligned}C_{n}^{m} &= \frac{n!}{m!(n-m)!} \\C_{n}^{m-1} &= \frac{n!}{(m-1)!(n-m+1)!}\end{aligned}$$ | ||
|
||
于是得到递推公式: | ||
|
||
$$C_{n}^{m} = C_{n}^{m-1} \times \frac{n-m+1}{m}$$ | ||
|
||
利用这个递推公式即可以把空间复杂度优化到 O(k) | ||
|
||
## 代码 | ||
|
||
```go | ||
package leetcode | ||
|
||
func getRow(rowIndex int) []int { | ||
row := make([]int, rowIndex+1) | ||
row[0] = 1 | ||
for i := 1; i <= rowIndex; i++ { | ||
row[i] = row[i-1] * (rowIndex - i + 1) / i | ||
} | ||
return row | ||
} | ||
``` |
82 changes: 82 additions & 0 deletions
82
...a-Matrix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package leetcode | ||
|
||
import "container/heap" | ||
|
||
func kthSmallest(mat [][]int, k int) int { | ||
if len(mat) == 0 || len(mat[0]) == 0 || k == 0 { | ||
return 0 | ||
} | ||
prev := mat[0] | ||
for i := 1; i < len(mat); i++ { | ||
prev = kSmallestPairs(prev, mat[i], k) | ||
} | ||
if k < len(prev) { | ||
return -1 | ||
} | ||
return prev[k-1] | ||
} | ||
|
||
func kSmallestPairs(nums1 []int, nums2 []int, k int) []int { | ||
res := []int{} | ||
if len(nums2) == 0 { | ||
return res | ||
} | ||
pq := newPriorityQueue() | ||
for i := 0; i < len(nums1) && i < k; i++ { | ||
heap.Push(pq, &pddata{ | ||
n1: nums1[i], | ||
n2: nums2[0], | ||
n2Idx: 0, | ||
}) | ||
} | ||
for pq.Len() > 0 { | ||
i := heap.Pop(pq) | ||
data := i.(*pddata) | ||
res = append(res, data.n1+data.n2) | ||
k-- | ||
if k <= 0 { | ||
break | ||
} | ||
idx := data.n2Idx | ||
idx++ | ||
if idx >= len(nums2) { | ||
continue | ||
} | ||
heap.Push(pq, &pddata{ | ||
n1: data.n1, | ||
n2: nums2[idx], | ||
n2Idx: idx, | ||
}) | ||
} | ||
return res | ||
} | ||
|
||
type pddata struct { | ||
n1 int | ||
n2 int | ||
n2Idx int | ||
} | ||
|
||
type priorityQueue []*pddata | ||
|
||
func newPriorityQueue() *priorityQueue { | ||
pq := priorityQueue([]*pddata{}) | ||
heap.Init(&pq) | ||
return &pq | ||
} | ||
|
||
func (pq priorityQueue) Len() int { return len(pq) } | ||
func (pq priorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] } | ||
func (pq priorityQueue) Less(i, j int) bool { return pq[i].n1+pq[i].n2 < pq[j].n1+pq[j].n2 } | ||
func (pq *priorityQueue) Pop() interface{} { | ||
old := *pq | ||
val := old[len(old)-1] | ||
old[len(old)-1] = nil | ||
*pq = old[0 : len(old)-1] | ||
return val | ||
} | ||
|
||
func (pq *priorityQueue) Push(i interface{}) { | ||
val := i.(*pddata) | ||
*pq = append(*pq, val) | ||
} |
56 changes: 56 additions & 0 deletions
56
...rix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type question1439 struct { | ||
para1439 | ||
ans1439 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para1439 struct { | ||
mat [][]int | ||
k int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans1439 struct { | ||
one int | ||
} | ||
|
||
func Test_Problem1439(t *testing.T) { | ||
|
||
qs := []question1439{ | ||
|
||
{ | ||
para1439{[][]int{{1, 3, 11}, {2, 4, 6}}, 5}, | ||
ans1439{7}, | ||
}, | ||
|
||
{ | ||
para1439{[][]int{{1, 3, 11}, {2, 4, 6}}, 9}, | ||
ans1439{17}, | ||
}, | ||
{ | ||
para1439{[][]int{{1, 10, 10}, {1, 4, 5}, {2, 3, 6}}, 7}, | ||
ans1439{9}, | ||
}, | ||
{ | ||
para1439{[][]int{{1, 1, 10}, {2, 2, 9}}, 7}, | ||
ans1439{12}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 1439------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans1439, q.para1439 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, kthSmallest(p.mat, p.k)) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
Oops, something went wrong.