Skip to content

Commit c7862ae

Browse files
committed
Add solution 0665、0669、1423、1463、1579
1 parent 5e1c45c commit c7862ae

File tree

46 files changed

+1592
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1592
-310
lines changed

README.md

Lines changed: 174 additions & 173 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
func checkPossibility(nums []int) bool {
4+
count := 0
5+
for i := 0; i < len(nums)-1; i++ {
6+
if nums[i] > nums[i+1] {
7+
count++
8+
if count > 1 {
9+
return false
10+
}
11+
if i > 0 && nums[i+1] < nums[i-1] {
12+
nums[i+1] = nums[i]
13+
}
14+
}
15+
}
16+
return true
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question665 struct {
9+
para665
10+
ans665
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para665 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans665 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem665(t *testing.T) {
26+
27+
qs := []question665{
28+
29+
{
30+
para665{[]int{4, 2, 3}},
31+
ans665{true},
32+
},
33+
34+
{
35+
para665{[]int{4, 2, 1}},
36+
ans665{false},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 665------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans665, q.para665
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, checkPossibility(p.nums))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [665. Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)
2+
3+
## 题目
4+
5+
Given an array `nums` with `n` integers, your task is to check if it could become non-decreasing by modifying **at most one element**.
6+
7+
We define an array is non-decreasing if `nums[i] <= nums[i + 1]` holds for every `i` (**0-based**) such that (`0 <= i <= n - 2`).
8+
9+
**Example 1:**
10+
11+
```
12+
Input: nums = [4,2,3]
13+
Output: true
14+
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: nums = [4,2,1]
21+
Output: false
22+
Explanation: You can't get a non-decreasing array by modify at most one element.
23+
```
24+
25+
**Constraints:**
26+
27+
- `n == nums.length`
28+
- `1 <= n <= 104`
29+
- `-10^5 <= nums[i] <= 10^5`
30+
31+
## 题目大意
32+
33+
给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列。我们是这样定义一个非递减数列的: 对于数组中任意的 i (0 <= i <= n-2),总满足 nums[i] <= nums[i + 1]
34+
35+
## 解题思路
36+
37+
- 简单题。循环扫描数组,找到 `nums[i] > nums[i+1]` 这种递减组合。一旦这种组合超过 2 组,直接返回 false。找到第一组递减组合,需要手动调节一次。如果 `nums[i + 1] < nums[i - 1]`,就算交换 `nums[i+1]``nums[i]`,交换结束,`nums[i - 1]` 仍然可能大于 `nums[i + 1]`,不满足题意。正确的做法应该是让较小的那个数变大,即 `nums[i + 1] = nums[i]`。两个元素相等满足非递减的要求。
38+
39+
## 代码
40+
41+
```go
42+
package leetcode
43+
44+
func checkPossibility(nums []int) bool {
45+
count := 0
46+
for i := 0; i < len(nums)-1; i++ {
47+
if nums[i] > nums[i+1] {
48+
count++
49+
if count > 1 {
50+
return false
51+
}
52+
if i > 0 && nums[i+1] < nums[i-1] {
53+
nums[i+1] = nums[i]
54+
}
55+
}
56+
}
57+
return true
58+
}
59+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
20+
if root == nil {
21+
return root
22+
}
23+
if root.Val > high {
24+
return trimBST(root.Left, low, high)
25+
}
26+
if root.Val < low {
27+
return trimBST(root.Right, low, high)
28+
}
29+
root.Left = trimBST(root.Left, low, high)
30+
root.Right = trimBST(root.Right, low, high)
31+
return root
32+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question669 struct {
11+
para669
12+
ans669
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para669 struct {
18+
one []int
19+
low int
20+
high int
21+
}
22+
23+
// ans 是答案
24+
// one 代表第一个答案
25+
type ans669 struct {
26+
one []int
27+
}
28+
29+
func Test_Problem669(t *testing.T) {
30+
31+
qs := []question669{
32+
33+
{
34+
para669{[]int{1, 0, 2}, 1, 2},
35+
ans669{[]int{1, structures.NULL, 2}},
36+
},
37+
38+
{
39+
para669{[]int{3, 0, 4, structures.NULL, 2, structures.NULL, structures.NULL, 1}, 1, 3},
40+
ans669{[]int{3, 2, structures.NULL, 1}},
41+
},
42+
43+
{
44+
para669{[]int{1}, 1, 2},
45+
ans669{[]int{1}},
46+
},
47+
48+
{
49+
para669{[]int{1, structures.NULL, 2}, 1, 3},
50+
ans669{[]int{1, structures.NULL, 2}},
51+
},
52+
53+
{
54+
para669{[]int{1, structures.NULL, 2}, 2, 4},
55+
ans669{[]int{2}},
56+
},
57+
}
58+
59+
fmt.Printf("------------------------Leetcode Problem 669------------------------\n")
60+
61+
for _, q := range qs {
62+
_, p := q.ans669, q.para669
63+
fmt.Printf("【input】:%v ", p)
64+
root := structures.Ints2TreeNode(p.one)
65+
fmt.Printf("【output】:%v \n", structures.Tree2ints(trimBST(root, p.low, p.high)))
66+
}
67+
fmt.Printf("\n\n\n")
68+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# [669. Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/)
2+
3+
4+
## 题目
5+
6+
Given the `root` of a binary search tree and the lowest and highest boundaries as `low` and `high`, trim the tree so that all its elements lies in `[low, high]`. Trimming the tree should **not** change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a **unique answer**.
7+
8+
Return *the root of the trimmed binary search tree*. Note that the root may change depending on the given bounds.
9+
10+
**Example 1:**
11+
12+
![https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg](https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg)
13+
14+
```
15+
Input: root = [1,0,2], low = 1, high = 2
16+
Output: [1,null,2]
17+
```
18+
19+
**Example 2:**
20+
21+
![https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg](https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg)
22+
23+
```
24+
Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
25+
Output: [3,2,null,1]
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: root = [1], low = 1, high = 2
32+
Output: [1]
33+
```
34+
35+
**Example 4:**
36+
37+
```
38+
Input: root = [1,null,2], low = 1, high = 3
39+
Output: [1,null,2]
40+
```
41+
42+
**Example 5:**
43+
44+
```
45+
Input: root = [1,null,2], low = 2, high = 4
46+
Output: [2]
47+
```
48+
49+
**Constraints:**
50+
51+
- The number of nodes in the tree in the range `[1, 10^4]`.
52+
- `0 <= Node.val <= 10^4`
53+
- The value of each node in the tree is **unique**.
54+
- `root` is guaranteed to be a valid binary search tree.
55+
- `0 <= low <= high <= 10^4`
56+
57+
## 题目大意
58+
59+
给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树不应该改变保留在树中的元素的相对结构(即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在唯一的答案。所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
60+
61+
## 解题思路
62+
63+
- 这一题考察二叉搜索树中的递归遍历。递归遍历二叉搜索树每个结点,根据有序性,当前结点如果比 high 大,那么当前结点的右子树全部修剪掉,再递归修剪左子树;当前结点如果比 low 小,那么当前结点的左子树全部修剪掉,再递归修剪右子树。处理完越界的情况,剩下的情况都在区间内,分别递归修剪左子树和右子树即可。
64+
65+
## 代码
66+
67+
```go
68+
package leetcode
69+
70+
import (
71+
"github.com/halfrost/LeetCode-Go/structures"
72+
)
73+
74+
// TreeNode define
75+
type TreeNode = structures.TreeNode
76+
77+
/**
78+
* Definition for a binary tree node.
79+
* type TreeNode struct {
80+
* Val int
81+
* Left *TreeNode
82+
* Right *TreeNode
83+
* }
84+
*/
85+
86+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
87+
if root == nil {
88+
return root
89+
}
90+
if root.Val > high {
91+
return trimBST(root.Left, low, high)
92+
}
93+
if root.Val < low {
94+
return trimBST(root.Right, low, high)
95+
}
96+
root.Left = trimBST(root.Left, low, high)
97+
root.Right = trimBST(root.Right, low, high)
98+
return root
99+
}
100+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func maxScore(cardPoints []int, k int) int {
4+
windowSize, sum := len(cardPoints)-k, 0
5+
for _, val := range cardPoints[:windowSize] {
6+
sum += val
7+
}
8+
minSum := sum
9+
for i := windowSize; i < len(cardPoints); i++ {
10+
sum += cardPoints[i] - cardPoints[i-windowSize]
11+
if sum < minSum {
12+
minSum = sum
13+
}
14+
}
15+
total := 0
16+
for _, pt := range cardPoints {
17+
total += pt
18+
}
19+
return total - minSum
20+
}

0 commit comments

Comments
 (0)