-
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
9 changed files
with
395 additions
and
1 deletion.
There are no files selected for viewing
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,26 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/9/6 10:43 | ||
*/ | ||
package main | ||
|
||
func lcaDeepestLeaves(root *TreeNode) *TreeNode { | ||
var dfs func(node *TreeNode) (int, *TreeNode) | ||
dfs = func(node *TreeNode) (int, *TreeNode) { | ||
if node == nil { | ||
return 0, nil | ||
} | ||
lv, lt := dfs(node.Left) | ||
rv, rt := dfs(node.Right) | ||
if lv == rv { | ||
return lv + 1, node | ||
} | ||
if lv < rv { | ||
return rv + 1, rt | ||
} | ||
return lv + 1, lt | ||
} | ||
|
||
_, res := dfs(root) | ||
return res | ||
} |
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
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,95 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/8/30 10:11 | ||
*/ | ||
package main | ||
|
||
func minimumJumps(forbidden []int, a int, b int, x int) int { | ||
maxF := 0 | ||
forbiddenSet := map[int]bool{} | ||
max := func(x, y int) int { | ||
if x < y { | ||
x = y | ||
} | ||
return x | ||
} | ||
min := func(x, y int) int { | ||
if x > y { | ||
x = y | ||
} | ||
return x | ||
} | ||
for _, num := range forbidden { | ||
maxF = max(maxF, num) | ||
forbiddenSet[num] = true | ||
} | ||
|
||
var bound int | ||
if a > b { | ||
bound = x + b | ||
} else if a == b { | ||
bound = x | ||
} else { | ||
bound = max(maxF+a+b, x) | ||
} | ||
dp := make([][2]int, bound+1) // -1: unknown, not visited, 0+: known, -2: visited, -3: can't reach | ||
for i := range dp { | ||
dp[i] = [2]int{-1, -1} | ||
} | ||
dp[0][0], dp[0][1] = 0, 0 | ||
var bfs func(p int, direct int) int // return: 0+: min steps, -1: can't reach | ||
|
||
bfs = func(p int, direct int) int { | ||
// 0 right, 1 left | ||
if p > bound || p < 0 || forbiddenSet[p] { | ||
return -1 | ||
} | ||
if dp[p][direct] >= 0 { | ||
return dp[p][direct] | ||
} else if dp[p][direct] == -2 || dp[p][direct] == -3 { | ||
return -1 | ||
} else { | ||
dp[p][direct] = -2 | ||
} | ||
|
||
if direct == 0 { | ||
r := bfs(p-a, 0) | ||
l := bfs(p-a, 1) | ||
if r == -1 && l == -1 { | ||
dp[p][direct] = -3 | ||
return -1 | ||
} | ||
if r == -1 { | ||
dp[p][direct] = l + 1 | ||
return l + 1 | ||
} | ||
if l == -1 { | ||
dp[p][direct] = r + 1 | ||
return r + 1 | ||
} | ||
dp[p][direct] = min(l, r) + 1 | ||
return min(l, r) + 1 | ||
} else { | ||
r := bfs(p+b, 0) | ||
if r == -1 { | ||
dp[p][direct] = -3 | ||
return -1 | ||
} else { | ||
dp[p][direct] = r + 1 | ||
return r + 1 | ||
} | ||
} | ||
} | ||
r := bfs(x, 0) | ||
l := bfs(x, 1) | ||
if r == -1 && l == -1 { | ||
return -1 | ||
} | ||
if r == -1 { | ||
return l | ||
} | ||
if l == -1 { | ||
return r | ||
} | ||
return min(l, r) | ||
} |
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,9 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/8/31 10:27 | ||
*/ | ||
package main | ||
|
||
func sum(num1 int, num2 int) int { | ||
return num1 + num2 | ||
} |
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,9 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/8/31 9:58 | ||
*/ | ||
package main | ||
|
||
func checkTree(root *TreeNode) bool { | ||
return root.Val == root.Left.Val+root.Right.Val | ||
} |
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,38 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/9/5 10:34 | ||
*/ | ||
package main | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
) | ||
|
||
func minNumber(nums1 []int, nums2 []int) int { | ||
sort.Ints(nums1) | ||
sort.Ints(nums2) | ||
v1, v2 := nums1[0], nums2[0] | ||
i, j := 0, 0 | ||
res := math.MaxInt | ||
min := func(x, y int) int { | ||
if x > y { | ||
x = y | ||
} | ||
return x | ||
} | ||
for i < len(nums1) && j < len(nums2) { | ||
v1 = nums1[i] | ||
v2 = nums2[j] | ||
res = min(res, min(v1*10+v2, v2*10+v1)) | ||
if v1 == v2 { | ||
return v1 | ||
} | ||
if v1 < v2 { | ||
i++ | ||
} else { | ||
j++ | ||
} | ||
} | ||
return res | ||
} |
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,122 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/9/4 9:50 | ||
*/ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
|
||
type Codec struct { | ||
} | ||
|
||
func Constructor() Codec { | ||
return Codec{} | ||
} | ||
|
||
func (this *Codec) putInt32(x int32) []byte { | ||
buf := make([]byte, 4) | ||
i := 0 | ||
for x >= 0x80 { | ||
buf[i] = byte(x) | 0x80 | ||
x >>= 7 | ||
i++ | ||
} | ||
buf[i] = byte(x) | ||
return buf | ||
} | ||
|
||
func (this *Codec) getInt32(buf []byte) int { | ||
res := 0 | ||
res += int(buf[0]) | ||
res += int(buf[1]) << 7 | ||
res += int(buf[2]) << 14 | ||
res += int(buf[3]) << 21 | ||
return res | ||
} | ||
|
||
// Serializes a tree to a single string. | ||
func (this *Codec) serialize(root *TreeNode) string { | ||
start := 0 | ||
var mid func(root *TreeNode, builder *strings.Builder) | ||
var pre func(root *TreeNode, builder *strings.Builder) | ||
mid = func(root *TreeNode, builder *strings.Builder) { | ||
if root == nil { | ||
return | ||
} | ||
builder.Write(this.putInt32(int32(root.Val))) | ||
mid(root.Left, builder) | ||
mid(root.Right, builder) | ||
} | ||
pre = func(root *TreeNode, builder *strings.Builder) { | ||
if root == nil { | ||
return | ||
} | ||
pre(root.Left, builder) | ||
builder.Write(this.putInt32(int32(root.Val))) | ||
pre(root.Right, builder) | ||
} | ||
builder := &strings.Builder{} | ||
mid(root, builder) | ||
start = builder.Len() | ||
pre(root, builder) | ||
builder.Write(this.putInt32(int32(start))) | ||
return builder.String() | ||
} | ||
|
||
// Deserializes your encoded data to tree. | ||
func (this *Codec) deserialize(data string) *TreeNode { | ||
byteData := []byte(data) | ||
newData := make([]int, len(data)/4) | ||
for i := range newData { | ||
newData[i] = this.getInt32(byteData[i*4 : i*4+4]) | ||
} | ||
|
||
start := newData[len(newData)-1] / 4 | ||
|
||
mid := newData[:start] | ||
pre := newData[start : len(newData)-1] | ||
|
||
var decode func(mid []int, pre []int) *TreeNode | ||
decode = func(mid []int, pre []int) *TreeNode { | ||
if len(mid) == 0 { | ||
return nil | ||
} | ||
val := mid[0] | ||
m := 0 | ||
for i := range pre { | ||
if pre[i] == val { | ||
m = i | ||
break | ||
} | ||
} | ||
root := &TreeNode{ | ||
Val: int(val), | ||
Left: nil, | ||
Right: nil, | ||
} | ||
root.Left = decode(mid[1:m+1], pre[:m]) | ||
root.Right = decode(mid[m+1:], pre[m+1:]) | ||
return root | ||
} | ||
return decode(mid, pre) | ||
} | ||
|
||
/** | ||
* Your Codec object will be instantiated and called as such: | ||
* ser := Constructor() | ||
* deser := Constructor() | ||
* tree := ser.serialize(root) | ||
* ans := deser.deserialize(tree) | ||
* return ans | ||
*/ |
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,66 @@ | ||
/** | ||
* @author: baowj | ||
* @time: 2023/8/29 16:17 | ||
*/ | ||
package main | ||
|
||
import "sort" | ||
|
||
func numFactoredBinaryTrees(arr []int) int { | ||
const MaxValue = int64(1e9 + 7) | ||
existNum := map[int]bool{} | ||
sort.Ints(arr) | ||
for _, num := range arr { | ||
existNum[num] = true | ||
} | ||
numSubs := map[int][][2]int{} | ||
for _, num := range arr { | ||
for i := 0; i < len(arr); i++ { | ||
x := arr[i] | ||
y := num / x | ||
if x > y { | ||
break | ||
} | ||
if x*y == num && existNum[y] { | ||
if subs, ok := numSubs[num]; ok { | ||
numSubs[num] = append(subs, [2]int{x, y}) | ||
} else { | ||
numSubs[num] = [][2]int{{x, y}} | ||
} | ||
} | ||
} | ||
} | ||
fnTable := map[int]int64{} | ||
|
||
var fn func(value int) int64 | ||
fn = func(value int) int64 { | ||
if res, ok := fnTable[value]; ok { | ||
return res | ||
} | ||
res := int64(1) | ||
subNums := numSubs[value] | ||
if subNums == nil { | ||
return res | ||
} | ||
|
||
for _, subNum := range subNums { | ||
if subNum[0] == subNum[1] { | ||
v := fn(subNum[0]) | ||
res = (res + ((v * v) % MaxValue)) % MaxValue | ||
} else { | ||
vL := fn(subNum[0]) | ||
vR := fn(subNum[1]) | ||
res = (res + ((2 * ((vL * vR) % MaxValue)) % MaxValue)) % MaxValue | ||
} | ||
} | ||
res %= MaxValue | ||
fnTable[value] = res | ||
return res | ||
} | ||
|
||
res := int64(0) | ||
for _, num := range arr { | ||
res = (res + fn(num)) % MaxValue | ||
} | ||
return int(res) | ||
} |
Oops, something went wrong.