mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 0938、1642
This commit is contained in:
34
leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST.go
Normal file
34
leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST.go
Normal file
@ -0,0 +1,34 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func rangeSumBST(root *TreeNode, low int, high int) int {
|
||||
res := 0
|
||||
preOrder(root, low, high, &res)
|
||||
return res
|
||||
}
|
||||
|
||||
func preOrder(root *TreeNode, low, high int, res *int) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
if low <= root.Val && root.Val <= high {
|
||||
*res += root.Val
|
||||
}
|
||||
preOrder(root.Left, low, high, res)
|
||||
preOrder(root.Right, low, high, res)
|
||||
}
|
53
leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST_test.go
Normal file
53
leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question938 struct {
|
||||
para938
|
||||
ans938
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para938 struct {
|
||||
one []int
|
||||
low int
|
||||
high int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans938 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem938(t *testing.T) {
|
||||
|
||||
qs := []question938{
|
||||
|
||||
{
|
||||
para938{[]int{10, 5, 15, 3, 7, structures.NULL, 18}, 7, 15},
|
||||
ans938{32},
|
||||
},
|
||||
|
||||
{
|
||||
para938{[]int{10, 5, 15, 3, 7, 13, 18, 1, structures.NULL, 6}, 6, 10},
|
||||
ans938{23},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 938------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans938, q.para938
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", rangeSumBST(rootOne, p.low, p.high))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
80
leetcode/0938.Range-Sum-of-BST/README.md
Normal file
80
leetcode/0938.Range-Sum-of-BST/README.md
Normal file
@ -0,0 +1,80 @@
|
||||
# [938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given the `root` node of a binary search tree, return *the sum of values of all nodes with a value in the range `[low, high]`*.
|
||||
|
||||
**Example 1:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
|
||||
Output: 32
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
|
||||
Output: 23
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- The number of nodes in the tree is in the range `[1, 2 * 10^4]`.
|
||||
- `1 <= Node.val <= 10^5`
|
||||
- `1 <= low <= high <= 10^5`
|
||||
- All `Node.val` are **unique**.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。因为二叉搜索树的有序性,先序遍历即为有序。遍历过程中判断节点值是否位于区间范围内,在区间内就累加,不在区间内节点就不管。最终输出累加和。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func rangeSumBST(root *TreeNode, low int, high int) int {
|
||||
res := 0
|
||||
preOrder(root, low, high, &res)
|
||||
return res
|
||||
}
|
||||
|
||||
func preOrder(root *TreeNode, low, high int, res *int) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
if low <= root.Val && root.Val <= high {
|
||||
*res += root.Val
|
||||
}
|
||||
preOrder(root.Left, low, high, res)
|
||||
preOrder(root.Right, low, high, res)
|
||||
}
|
||||
```
|
@ -0,0 +1,40 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
func furthestBuilding(heights []int, bricks int, ladder int) int {
|
||||
usedLadder := &heightDiffPQ{}
|
||||
for i := 1; i < len(heights); i++ {
|
||||
needbricks := heights[i] - heights[i-1]
|
||||
if needbricks < 0 {
|
||||
continue
|
||||
}
|
||||
if ladder > 0 {
|
||||
heap.Push(usedLadder, needbricks)
|
||||
ladder--
|
||||
} else {
|
||||
if len(*usedLadder) > 0 && needbricks > (*usedLadder)[0] {
|
||||
needbricks, (*usedLadder)[0] = (*usedLadder)[0], needbricks
|
||||
heap.Fix(usedLadder, 0)
|
||||
}
|
||||
if bricks -= needbricks; bricks < 0 {
|
||||
return i - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(heights) - 1
|
||||
}
|
||||
|
||||
type heightDiffPQ []int
|
||||
|
||||
func (pq heightDiffPQ) Len() int { return len(pq) }
|
||||
func (pq heightDiffPQ) Less(i, j int) bool { return pq[i] < pq[j] }
|
||||
func (pq heightDiffPQ) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
|
||||
func (pq *heightDiffPQ) Push(x interface{}) { *pq = append(*pq, x.(int)) }
|
||||
func (pq *heightDiffPQ) Pop() interface{} {
|
||||
x := (*pq)[len(*pq)-1]
|
||||
*pq = (*pq)[:len(*pq)-1]
|
||||
return x
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1642 struct {
|
||||
para1642
|
||||
ans1642
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1642 struct {
|
||||
heights []int
|
||||
bricks int
|
||||
ladders int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1642 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1642(t *testing.T) {
|
||||
|
||||
qs := []question1642{
|
||||
|
||||
{
|
||||
para1642{[]int{1, 5, 1, 2, 3, 4, 10000}, 4, 1},
|
||||
ans1642{5},
|
||||
},
|
||||
|
||||
{
|
||||
para1642{[]int{4, 2, 7, 6, 9, 14, 12}, 5, 1},
|
||||
ans1642{4},
|
||||
},
|
||||
|
||||
{
|
||||
para1642{[]int{4, 12, 2, 7, 3, 18, 20, 3, 19}, 10, 2},
|
||||
ans1642{7},
|
||||
},
|
||||
|
||||
{
|
||||
para1642{[]int{14, 3, 19, 3}, 17, 0},
|
||||
ans1642{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1642------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1642, q.para1642
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, furthestBuilding(p.heights, p.bricks, p.ladders))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
113
leetcode/1642.Furthest-Building-You-Can-Reach/README.md
Normal file
113
leetcode/1642.Furthest-Building-You-Can-Reach/README.md
Normal file
@ -0,0 +1,113 @@
|
||||
# [1642. Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer array `heights` representing the heights of buildings, some `bricks`, and some `ladders`.
|
||||
|
||||
You start your journey from building `0` and move to the next building by possibly using bricks or ladders.
|
||||
|
||||
While moving from building `i` to building `i+1` (**0-indexed**),
|
||||
|
||||
- If the current building's height is **greater than or equal** to the next building's height, you do **not** need a ladder or bricks.
|
||||
- If the current building's height is **less than** the next building's height, you can either use **one ladder** or `(h[i+1] - h[i])` **bricks**.
|
||||
|
||||
*Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
|
||||
Output: 4
|
||||
Explanation: Starting at building 0, you can follow these steps:
|
||||
- Go to building 1 without using ladders nor bricks since 4 >= 2.
|
||||
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
|
||||
- Go to building 3 without using ladders nor bricks since 7 >= 6.
|
||||
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
|
||||
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
|
||||
Output: 7
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
|
||||
Output: 3
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= heights.length <= 10^5`
|
||||
- `1 <= heights[i] <= 10^6`
|
||||
- `0 <= bricks <= 10^9`
|
||||
- `0 <= ladders <= heights.length`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数数组 heights ,表示建筑物的高度。另有一些砖块 bricks 和梯子 ladders 。你从建筑物 0 开始旅程,不断向后面的建筑物移动,期间可能会用到砖块或梯子。当从建筑物 i 移动到建筑物 i+1(下标 从 0 开始 )时:
|
||||
|
||||
- 如果当前建筑物的高度 大于或等于 下一建筑物的高度,则不需要梯子或砖块。
|
||||
- 如果当前建筑的高度 小于 下一个建筑的高度,您可以使用 一架梯子 或 (h[i+1] - h[i]) 个砖块
|
||||
|
||||
如果以最佳方式使用给定的梯子和砖块,返回你可以到达的最远建筑物的下标(下标 从 0 开始 )。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题可能会想到贪心算法。梯子很厉害,可以无限长,所以梯子用来跨越最高的楼。遇到非最高的距离差,先用砖头。这样贪心的话不正确。例如,[1, 5, 1, 2, 3, 4, 10000] 这组数据,梯子有 1 个,4 块砖头。最大的差距在 10000 和 4 之间,贪心选择在此处用梯子。但是砖头不足以让我们走到最后两栋楼。贪心得到的结果是 3,正确的结果是 5,先用梯子,再用砖头走过 3,4,5 号楼。
|
||||
- 上面的贪心解法错误在于没有“动态”的贪心,使用梯子应该选择能爬过楼里面最高的 2 个。于是顺理成章的想到了优先队列。维护一个长度为梯子个数的最小堆,当队列中元素超过梯子个数,便将队首最小值出队,出队的这个楼与楼的差距用砖头填补。所有砖头用完了,即是可以到达的最远楼号。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
func furthestBuilding(heights []int, bricks int, ladder int) int {
|
||||
usedLadder := &heightDiffPQ{}
|
||||
for i := 1; i < len(heights); i++ {
|
||||
needbricks := heights[i] - heights[i-1]
|
||||
if needbricks < 0 {
|
||||
continue
|
||||
}
|
||||
if ladder > 0 {
|
||||
heap.Push(usedLadder, needbricks)
|
||||
ladder--
|
||||
} else {
|
||||
if len(*usedLadder) > 0 && needbricks > (*usedLadder)[0] {
|
||||
needbricks, (*usedLadder)[0] = (*usedLadder)[0], needbricks
|
||||
heap.Fix(usedLadder, 0)
|
||||
}
|
||||
if bricks -= needbricks; bricks < 0 {
|
||||
return i - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(heights) - 1
|
||||
}
|
||||
|
||||
type heightDiffPQ []int
|
||||
|
||||
func (pq heightDiffPQ) Len() int { return len(pq) }
|
||||
func (pq heightDiffPQ) Less(i, j int) bool { return pq[i] < pq[j] }
|
||||
func (pq heightDiffPQ) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
|
||||
func (pq *heightDiffPQ) Push(x interface{}) { *pq = append(*pq, x.(int)) }
|
||||
func (pq *heightDiffPQ) Pop() interface{} {
|
||||
x := (*pq)[len(*pq)-1]
|
||||
*pq = (*pq)[:len(*pq)-1]
|
||||
return x
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user