Skip to content

Commit dbe98e5

Browse files
committed
feat: update solutions to lc problem: No.0654
No.0654.Maximum Binary Tree
1 parent b699629 commit dbe98e5

File tree

12 files changed

+163
-163
lines changed

12 files changed

+163
-163
lines changed

lcof2/剑指 Offer II 064. 神奇的字典/tmp.py

-27
This file was deleted.

solution/0200-0299/0271.Encode and Decode Strings/Solution.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
class Codec:
22
def encode(self, strs: List[str]) -> str:
3-
"""Encodes a list of strings to a single string.
4-
"""
3+
"""Encodes a list of strings to a single string."""
54
ans = []
65
for s in strs:
76
ans.append('{:4}'.format(len(s)) + s)
87
return ''.join(ans)
98

109
def decode(self, s: str) -> List[str]:
11-
"""Decodes a single string to a list of strings.
12-
"""
10+
"""Decodes a single string to a list of strings."""
1311
ans = []
1412
i, n = 0, len(s)
1513
while i < n:
16-
size = int(s[i: i + 4])
14+
size = int(s[i : i + 4])
1715
i += 4
18-
ans.append(s[i: i + size])
16+
ans.append(s[i : i + size])
1917
i += size
2018
return ans
2119

solution/0200-0299/0277.Find the Celebrity/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ ans not knows 6
9898
ans = 6
9999
```
100100
101-
这里 $ans$ 认识 $3$,说明 $ans$ 不是名人($0$ 不是名人),那么名人会是 $1$ 或者 $2$ 吗?不会!因为若 $1$ 或者 $2$ 是名人,那么 $0$ 应该认识 $1$ 或者 $2$ 才对,与前面的例子冲突。因此,我们可以直接将 $ans$ 更新为 $i$。
101+
这里 $ans$ 认识 $3$,说明 $ans$ 不是名人($0$ 不是名人),那么名人会是 $1$ 或者 $2$ 吗?不会!因为若 $1$ 或者 $2$ 是名人,那么 $0$ 应该认识 $1$ 或者 $2$ 才对,与前面的例子冲突。因此,我们可以直接将 $ans$ 更新为 $i$。
102102
103103
我们找出 $ans$ 之后,接下来再遍历一遍,判断 $ans$ 是否满足名人的条件。若不满足,返回 $-1$。
104104

solution/0200-0299/0277.Find the Celebrity/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# return a bool, whether a knows b
33
# def knows(a: int, b: int) -> bool:
44

5+
56
class Solution:
67
def findCelebrity(self, n: int) -> int:
78
ans = 0

solution/0400-0499/0408.Valid Word Abbreviation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
同时遍历 $word$ 和 $abbr$,若 $abbr$ 遇到数字,则 $word$ 跳过对应数字长度的字符数。若数字为空,或者有前导零,则提前返回 false。
7070

71-
时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。其中 $m$ 是 $word$ 的长度,$n$ 是 $abbr$ 的长度。
71+
时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。其中 $m$ 是 $word$ 的长度,$n$ 是 $abbr$ 的长度。
7272

7373
<!-- tabs:start -->
7474

solution/0400-0499/0408.Valid Word Abbreviation/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def validWordAbbreviation(self, word: str, abbr: str) -> bool:
1111
k = j
1212
while k < n and abbr[k].isdigit():
1313
k += 1
14-
t = abbr[j: k]
14+
t = abbr[j:k]
1515
if not t.isdigit() or t[0] == '0' or int(t) == 0:
1616
return False
1717
i += int(t)

solution/0600-0699/0654.Maximum Binary Tree/README.md

+56-44
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
先找到数组的最大元素所在的位置,作为根节点,然后递归左右两侧的子数组,构建左右子树。
59+
**方法一:递归**
60+
61+
先找到数组 $nums$ 的最大元素所在的位置 $i$,将 $nums[i]$ 作为根节点,然后递归左右两侧的子数组,构建左右子树。
62+
63+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 是数组的长度。
6064

6165
<!-- tabs:start -->
6266

@@ -72,17 +76,18 @@
7276
# self.left = left
7377
# self.right = right
7478
class Solution:
75-
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
76-
def inner(nums, l, r):
77-
if l > r:
79+
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
80+
def dfs(nums):
81+
if not nums:
7882
return None
79-
mx = l
80-
for i in range(l + 1, r + 1):
81-
if nums[mx] < nums[i]:
82-
mx = i
83-
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
84-
85-
return inner(nums, 0, len(nums) - 1)
83+
val = max(nums)
84+
i = nums.index(val)
85+
root = TreeNode(val)
86+
root.left = dfs(nums[:i])
87+
root.right = dfs(nums[i + 1:])
88+
return root
89+
90+
return dfs(nums)
8691
```
8792

8893
### **Java**
@@ -106,21 +111,27 @@ class Solution:
106111
* }
107112
*/
108113
class Solution {
114+
private int[] nums;
115+
109116
public TreeNode constructMaximumBinaryTree(int[] nums) {
110-
return construct(nums, 0, nums.length - 1);
117+
this.nums = nums;
118+
return dfs(0, nums.length - 1);
111119
}
112120

113-
private TreeNode construct(int[] nums, int l, int r) {
121+
private TreeNode dfs(int l, int r) {
114122
if (l > r) {
115123
return null;
116124
}
117-
int mx = l;
118-
for (int i = l + 1; i <= r; ++i) {
119-
if (nums[mx] < nums[i]) {
120-
mx = i;
125+
int i = l;
126+
for (int j = l; j <= r; ++j) {
127+
if (nums[i] < nums[j]) {
128+
i = j;
121129
}
122130
}
123-
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
131+
TreeNode root = new TreeNode(nums[i]);
132+
root.left = dfs(l, i - 1);
133+
root.right = dfs(i + 1, r);
134+
return root;
124135
}
125136
}
126137
```
@@ -142,18 +153,20 @@ class Solution {
142153
class Solution {
143154
public:
144155
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
145-
return construct(nums, 0, nums.size() - 1);
156+
return dfs(nums, 0, nums.size() - 1);
146157
}
147158

148-
TreeNode* construct(vector<int>& nums, int l, int r) {
159+
TreeNode* dfs(vector<int>& nums, int l, int r) {
149160
if (l > r) return nullptr;
150-
int mx = l;
151-
for (int i = l + 1; i <= r; ++i) {
152-
if (nums[mx] < nums[i]) mx = i;
161+
int i = l;
162+
for (int j = l; j <= r; ++j) {
163+
if (nums[i] < nums[j]) {
164+
i = j;
165+
}
153166
}
154-
TreeNode* root = new TreeNode(nums[mx]);
155-
root->left = construct(nums, l, mx - 1);
156-
root->right = construct(nums, mx + 1, r);
167+
TreeNode* root = new TreeNode(nums[i]);
168+
root->left = dfs(nums, l, i - 1);
169+
root->right = dfs(nums, i + 1, r);
157170
return root;
158171
}
159172
};
@@ -171,24 +184,23 @@ public:
171184
* }
172185
*/
173186
func constructMaximumBinaryTree(nums []int) *TreeNode {
174-
return construct(nums, 0, len(nums)-1)
175-
}
176-
177-
func construct(nums []int, l, r int) *TreeNode {
178-
if l > r {
179-
return nil
180-
}
181-
mx := l
182-
for i := l + 1; i <= r; i++ {
183-
if nums[mx] < nums[i] {
184-
mx = i
185-
}
186-
}
187-
return &TreeNode{
188-
Val: nums[mx],
189-
Left: construct(nums, l, mx-1),
190-
Right: construct(nums, mx+1, r),
191-
}
187+
var dfs func(l, r int) *TreeNode
188+
dfs = func(l, r int) *TreeNode {
189+
if l > r {
190+
return nil
191+
}
192+
i := l
193+
for j := l; j <= r; j++ {
194+
if nums[i] < nums[j] {
195+
i = j
196+
}
197+
}
198+
root := &TreeNode{Val: nums[i]}
199+
root.Left = dfs(l, i - 1)
200+
root.Right = dfs(i + 1, r)
201+
return root
202+
}
203+
return dfs(0, len(nums)-1)
192204
}
193205
```
194206

solution/0600-0699/0654.Maximum Binary Tree/README_EN.md

+51-43
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@
6262
# self.left = left
6363
# self.right = right
6464
class Solution:
65-
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
66-
def inner(nums, l, r):
67-
if l > r:
65+
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
66+
def dfs(nums):
67+
if not nums:
6868
return None
69-
mx = l
70-
for i in range(l + 1, r + 1):
71-
if nums[mx] < nums[i]:
72-
mx = i
73-
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
74-
75-
return inner(nums, 0, len(nums) - 1)
69+
val = max(nums)
70+
i = nums.index(val)
71+
root = TreeNode(val)
72+
root.left = dfs(nums[:i])
73+
root.right = dfs(nums[i + 1:])
74+
return root
75+
76+
return dfs(nums)
7677
```
7778

7879
### **Java**
@@ -94,21 +95,27 @@ class Solution:
9495
* }
9596
*/
9697
class Solution {
98+
private int[] nums;
99+
97100
public TreeNode constructMaximumBinaryTree(int[] nums) {
98-
return construct(nums, 0, nums.length - 1);
101+
this.nums = nums;
102+
return dfs(0, nums.length - 1);
99103
}
100104

101-
private TreeNode construct(int[] nums, int l, int r) {
105+
private TreeNode dfs(int l, int r) {
102106
if (l > r) {
103107
return null;
104108
}
105-
int mx = l;
106-
for (int i = l + 1; i <= r; ++i) {
107-
if (nums[mx] < nums[i]) {
108-
mx = i;
109+
int i = l;
110+
for (int j = l; j <= r; ++j) {
111+
if (nums[i] < nums[j]) {
112+
i = j;
109113
}
110114
}
111-
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
115+
TreeNode root = new TreeNode(nums[i]);
116+
root.left = dfs(l, i - 1);
117+
root.right = dfs(i + 1, r);
118+
return root;
112119
}
113120
}
114121
```
@@ -130,18 +137,20 @@ class Solution {
130137
class Solution {
131138
public:
132139
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
133-
return construct(nums, 0, nums.size() - 1);
140+
return dfs(nums, 0, nums.size() - 1);
134141
}
135142

136-
TreeNode* construct(vector<int>& nums, int l, int r) {
143+
TreeNode* dfs(vector<int>& nums, int l, int r) {
137144
if (l > r) return nullptr;
138-
int mx = l;
139-
for (int i = l + 1; i <= r; ++i) {
140-
if (nums[mx] < nums[i]) mx = i;
145+
int i = l;
146+
for (int j = l; j <= r; ++j) {
147+
if (nums[i] < nums[j]) {
148+
i = j;
149+
}
141150
}
142-
TreeNode* root = new TreeNode(nums[mx]);
143-
root->left = construct(nums, l, mx - 1);
144-
root->right = construct(nums, mx + 1, r);
151+
TreeNode* root = new TreeNode(nums[i]);
152+
root->left = dfs(nums, l, i - 1);
153+
root->right = dfs(nums, i + 1, r);
145154
return root;
146155
}
147156
};
@@ -159,24 +168,23 @@ public:
159168
* }
160169
*/
161170
func constructMaximumBinaryTree(nums []int) *TreeNode {
162-
return construct(nums, 0, len(nums)-1)
163-
}
164-
165-
func construct(nums []int, l, r int) *TreeNode {
166-
if l > r {
167-
return nil
168-
}
169-
mx := l
170-
for i := l + 1; i <= r; i++ {
171-
if nums[mx] < nums[i] {
172-
mx = i
173-
}
174-
}
175-
return &TreeNode{
176-
Val: nums[mx],
177-
Left: construct(nums, l, mx-1),
178-
Right: construct(nums, mx+1, r),
179-
}
171+
var dfs func(l, r int) *TreeNode
172+
dfs = func(l, r int) *TreeNode {
173+
if l > r {
174+
return nil
175+
}
176+
i := l
177+
for j := l; j <= r; j++ {
178+
if nums[i] < nums[j] {
179+
i = j
180+
}
181+
}
182+
root := &TreeNode{Val: nums[i]}
183+
root.Left = dfs(l, i - 1)
184+
root.Right = dfs(i + 1, r)
185+
return root
186+
}
187+
return dfs(0, len(nums)-1)
180188
}
181189
```
182190

0 commit comments

Comments
 (0)