Skip to content

Commit f6d7879

Browse files
committed
每日一题
1 parent 285466c commit f6d7879

File tree

9 files changed

+300
-11
lines changed

9 files changed

+300
-11
lines changed

algs/leetcode/1402_maxSatisfaction.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
// 动态规划, dp[i][j]表示前i个物体中,选中j个菜,可以达到的最大值 FIXME 该题解法有问题
6+
// dp[0][1] = arr[0]
7+
// dp[i][j] = max(dp[i-1][j-1], dp[i-1][j] + arr[i]*j)
8+
func maxSatisfaction(satisfaction []int) int {
9+
var (
10+
dp = make([][]int, len(satisfaction))
11+
maxFunc func(x, y int) int
12+
)
13+
14+
sort.Ints(satisfaction)
15+
maxFunc = func(x, y int) int {
16+
if x > y {
17+
return x
18+
}
19+
return y
20+
}
21+
22+
for i := 0; i < len(satisfaction); i++ {
23+
dp[i] = make([]int, len(satisfaction))
24+
}
25+
26+
dp[0][0] = 0
27+
dp[0][1] = satisfaction[0]
28+
for i := 1; i < len(satisfaction); i++ {
29+
for j := 0; j < i; j++ {
30+
if j == 0 {
31+
dp[i][j] = 0
32+
} else {
33+
if i == j {
34+
dp[i][j] = dp[i-1][j-1] + satisfaction[i-1]*j
35+
} else {
36+
dp[i][j] = maxFunc(dp[i-1][j], dp[i-1][j-1]+satisfaction[i-1]*j)
37+
}
38+
}
39+
}
40+
}
41+
42+
var ans = 0
43+
for i := 0; i <= len(satisfaction); i++ {
44+
ans = maxFunc(ans, dp[len(satisfaction)][i])
45+
}
46+
return ans
47+
}

algs/leetcode/1436_destCity.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
// 哈希表
4+
func destCity(paths [][]string) string {
5+
startMap := make(map[string]struct{}, len(paths))
6+
for _, path := range paths {
7+
startMap[path[0]] = struct{}{}
8+
}
9+
10+
for _, path := range paths {
11+
if _, ok := startMap[path[1]]; !ok {
12+
return path[1]
13+
}
14+
}
15+
16+
return ""
17+
}

algs/leetcode/1460_canBeEqual.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
func canBeEqual(target []int, arr []int) bool {
4+
if len(target) != len(arr) {
5+
return false
6+
}
7+
8+
if len(target) == 0 {
9+
return true
10+
}
11+
12+
targetM := make(map[int]int, len(target))
13+
for i := 0; i < len(target); i++ {
14+
targetM[target[i]]++
15+
}
16+
17+
for i := 0; i < len(arr); i++ {
18+
if val, ok := targetM[arr[i]]; !ok || val <= 0 {
19+
return false
20+
} else {
21+
targetM[arr[i]] = val - 1
22+
}
23+
}
24+
25+
return true
26+
}

algs/leetcode/1464_maxProduct.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import "math"
4+
5+
// 找到最大的两个值
6+
func maxProduct1(nums []int) int {
7+
var (
8+
max int
9+
max2nd int
10+
)
11+
12+
if nums[0] > nums[1] {
13+
max = nums[0]
14+
max2nd = nums[1]
15+
} else {
16+
max = nums[1]
17+
max2nd = nums[0]
18+
}
19+
for i := 2; i < len(nums); i++ {
20+
if nums[i] > max {
21+
max2nd, max = max, nums[i]
22+
} else if nums[i] > max2nd {
23+
max2nd = nums[i]
24+
}
25+
}
26+
27+
return (max - 1) * (max2nd - 1)
28+
}
29+
30+
// 暴力法
31+
func maxProduct2(nums []int) int {
32+
var (
33+
max = math.MinInt32
34+
calFunc func(x, y int) int
35+
maxFunc func(x, y int) int
36+
)
37+
38+
calFunc = func(x, y int) int {
39+
return (x - 1) * (y - 1)
40+
}
41+
maxFunc = func(x, y int) int {
42+
if x > y {
43+
return x
44+
}
45+
return y
46+
}
47+
for i := 0; i < len(nums); i++ {
48+
for j := i + 1; j < len(nums); j++ {
49+
max = maxFunc(max, calFunc(nums[i], nums[j]))
50+
}
51+
}
52+
53+
return max
54+
}

algs/leetcode/146_LRUCache_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package leetcode
22

33
import (
4-
"fmt"
54
"testing"
65
)
76

87
func TestLRUCache_Get(t *testing.T) {
9-
lruCache := Constructor(2)
10-
fmt.Println(lruCache.Get(2))
11-
lruCache.Put(2, 6)
12-
fmt.Println(lruCache.Get(1))
13-
lruCache.Put(1, 5)
14-
lruCache.Put(1, 2)
15-
fmt.Println(lruCache.Get(1))
16-
fmt.Println(lruCache.Get(2))
17-
18-
fmt.Println(10 / 4)
8+
//lruCache := Constructor(2)
9+
//fmt.Println(lruCache.Get(2))
10+
//lruCache.Put(2, 6)
11+
//fmt.Println(lruCache.Get(1))
12+
//lruCache.Put(1, 5)
13+
//lruCache.Put(1, 2)
14+
//fmt.Println(lruCache.Get(1))
15+
//fmt.Println(lruCache.Get(2))
16+
//
17+
//fmt.Println(10 / 4)
1918
}

algs/leetcode/17_calculate.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
func calculate(s string) int {
4+
var (
5+
x, y = 1, 0
6+
aFunc func(x, y int) int
7+
bFunc func(x, y int) int
8+
)
9+
aFunc = func(x, y int) int {
10+
return 2*x + y
11+
}
12+
13+
bFunc = func(x, y int) int {
14+
return x + 2*y
15+
}
16+
17+
for _, b := range s {
18+
if b == 'A' {
19+
x = aFunc(x, y)
20+
} else if b == 'B' {
21+
y = bFunc(x, y)
22+
}
23+
}
24+
25+
return x + y
26+
}
27+
28+
func calculate1(s string) int {
29+
return len(s) * 2
30+
}

algs/leetcode/284_iterator.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
//Below is the interface for Iterator, which is already defined for you.
4+
5+
type Iterator struct {
6+
}
7+
8+
func (this *Iterator) hasNext() bool {
9+
// Returns true if the iteration has more elements.
10+
return false
11+
}
12+
13+
func (this *Iterator) next() int {
14+
// Returns the next element in the iteration.
15+
return 0
16+
}
17+
18+
type PeekingIterator struct {
19+
head int
20+
iter *Iterator
21+
}
22+
23+
func Constructor(iter *Iterator) *PeekingIterator {
24+
return &PeekingIterator{
25+
head: 0,
26+
iter: iter,
27+
}
28+
}
29+
30+
func (this *PeekingIterator) hasNext() bool {
31+
if this.head > 0 {
32+
return true
33+
} else {
34+
return this.iter.hasNext()
35+
}
36+
}
37+
38+
func (this *PeekingIterator) next() int {
39+
if this.head > 0 {
40+
next := this.head
41+
this.head = 0
42+
return next
43+
} else {
44+
return this.iter.next()
45+
}
46+
}
47+
48+
func (this *PeekingIterator) peek() int {
49+
if this.head > 0 {
50+
return this.head
51+
} else {
52+
peek := this.iter.next()
53+
this.head = peek
54+
return peek
55+
}
56+
}

algs/leetcode/590_postorder.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import "zitrone.github.com/go-snippet/algs"
4+
5+
/**
6+
* Definition for a Node.
7+
* type Node struct {
8+
* Val int
9+
* Children []*Node
10+
* }
11+
*/
12+
13+
// 给定一个 N 叉树,返回其节点值的后序遍历。
14+
// 递归
15+
func postorder(root *Node) []int {
16+
if root == nil {
17+
return nil
18+
}
19+
var ret = make([]int, 0, 10)
20+
for i := 0; i < len(root.Children); i++ {
21+
ret = append(ret, postorder(root.Children[i])...)
22+
}
23+
24+
ret = append(ret, root.Val)
25+
return ret
26+
}
27+
28+
// 非递归 stack
29+
func postorder1(root *Node) []int {
30+
if root == nil {
31+
return nil
32+
}
33+
var (
34+
stack = &algs.Stack{}
35+
ret = make([]int, 0, 10)
36+
)
37+
stack.Push(root.Val)
38+
for !stack.IsEmpty() {
39+
rootI, _ := stack.Pop()
40+
root := rootI.(*Node)
41+
if root != nil {
42+
ret = append(ret, root.Val)
43+
for i := 0; i < len(root.Children); i++ {
44+
stack.Push(root.Children[i])
45+
}
46+
}
47+
}
48+
49+
for i, j := 0, len(ret)-1; i < len(ret)/2; i++ {
50+
ret[i], ret[j] = ret[j], ret[i]
51+
j--
52+
}
53+
return ret
54+
}

algs/leetcode/tree_node.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ type TreeNode struct {
66
Left *TreeNode
77
Right *TreeNode
88
}
9+
10+
//Definition for a Node.
11+
type Node struct {
12+
Val int
13+
Children []*Node
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy