Skip to content

Commit 1e91f18

Browse files
committed
feat: update solutions to lc problems
* No.0035.Search Insert Position * No.1688.Count of Matches in Tournament * No.1691.Maximum Height by Stacking Cuboids
1 parent 96cfb3f commit 1e91f18

File tree

8 files changed

+79
-25
lines changed

8 files changed

+79
-25
lines changed

solution/0000-0099/0035.Search Insert Position/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51-
二分查找。
51+
**方法一:二分查找**
52+
53+
由于 `nums` 数组已经有序,因此我们可以使用二分查找的方法找到目标值 `target` 的插入位置。
54+
55+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
5256

5357
<!-- tabs:start -->
5458

@@ -69,6 +73,12 @@ class Solution:
6973
return left
7074
```
7175

76+
```python
77+
class Solution:
78+
def searchInsert(self, nums: List[int], target: int) -> int:
79+
return bisect_left(nums, target)
80+
```
81+
7282
### **Java**
7383

7484
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -109,6 +119,15 @@ public:
109119
};
110120
```
111121
122+
```cpp
123+
class Solution {
124+
public:
125+
int searchInsert(vector<int>& nums, int target) {
126+
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
127+
}
128+
};
129+
```
130+
112131
### **Go**
113132

114133
```go
@@ -126,6 +145,12 @@ func searchInsert(nums []int, target int) int {
126145
}
127146
```
128147

148+
```go
149+
func searchInsert(nums []int, target int) int {
150+
return sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
151+
}
152+
```
153+
129154
### **JavaScript**
130155

131156
```js

solution/0000-0099/0035.Search Insert Position/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class Solution:
6161
return left
6262
```
6363

64+
```python
65+
class Solution:
66+
def searchInsert(self, nums: List[int], target: int) -> int:
67+
return bisect_left(nums, target)
68+
```
69+
6470
### **Java**
6571

6672
```java
@@ -99,6 +105,15 @@ public:
99105
};
100106
```
101107
108+
```cpp
109+
class Solution {
110+
public:
111+
int searchInsert(vector<int>& nums, int target) {
112+
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
113+
}
114+
};
115+
```
116+
102117
### **Go**
103118

104119
```go
@@ -116,6 +131,12 @@ func searchInsert(nums []int, target int) int {
116131
}
117132
```
118133

134+
```go
135+
func searchInsert(nums []int, target int) int {
136+
return sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
137+
}
138+
```
139+
119140
### **JavaScript**
120141

121142
```js

solution/1600-1699/1688.Count of Matches in Tournament/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
n 个人比赛,最终淘汰 n - 1 个人,所以配对次数是 n - 1。
55+
**方法一:脑筋急转弯**
56+
57+
根据题目描述我们知道,一共有 $n$ 支队伍,每一次的配对,都会淘汰一支队伍,所以配对次数就是淘汰的队伍数,即 $n - 1$。
58+
59+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5660

5761
<!-- tabs:start -->
5862

@@ -109,6 +113,14 @@ var numberOfMatches = function (n) {
109113
};
110114
```
111115

116+
### **TypeScript**
117+
118+
```ts
119+
function numberOfMatches(n: number): number {
120+
return n - 1;
121+
}
122+
```
123+
112124
### **...**
113125

114126
```

solution/1600-1699/1688.Count of Matches in Tournament/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ var numberOfMatches = function (n) {
9999
};
100100
```
101101

102+
### **TypeScript**
103+
104+
```ts
105+
function numberOfMatches(n: number): number {
106+
return n - 1;
107+
}
108+
```
109+
102110
### **...**
103111

104112
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function numberOfMatches(n: number): number {
2+
return n - 1;
3+
}

solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@
7373

7474
接下来,我们可以使用动态规划的方法求解本题。
7575

76-
我们定义 $f[i]$ 表示以长方体 $i$ 为最底部长方体时的最大高度。我们可以枚举每个长方体 $i$ 的上方的长方体 $j$,如果 $j$ 可以放在 $i$ 的上方,那么我们可以得到状态转移方程:
76+
我们定义 $f[i]$ 表示以长方体 $i$ 为最底部长方体时的最大高度。我们可以枚举每个长方体 $i$ 的上方的长方体 $j$,其中 $0 \leq j < i$。如果 $j$ 可以放在 $i$ 的上方,那么我们可以得到状态转移方程:
7777

7878
$$
79-
f[i] = \max(f[i], f[j] + h[i])
79+
f[i] = \max_{0 \leq j < i} \{f[j] + h[i]\}
8080
$$
8181

8282
其中 $h[i]$ 表示长方体 $i$ 的高度。
@@ -164,13 +164,8 @@ func maxHeight(cuboids [][]int) (ans int) {
164164
sort.Ints(c)
165165
}
166166
sort.Slice(cuboids, func(i, j int) bool {
167-
if cuboids[i][0] != cuboids[j][0] {
168-
return cuboids[i][0] < cuboids[j][0]
169-
}
170-
if cuboids[i][1] != cuboids[j][1] {
171-
return cuboids[i][1] < cuboids[j][1]
172-
}
173-
return cuboids[i][2] < cuboids[j][2]
167+
a, b := cuboids[i], cuboids[j]
168+
return a[0] < b[0] || a[0] == b[0] && (a[1] < b[1] || a[1] == b[1] && a[2] < b[2])
174169
})
175170
n := len(cuboids)
176171
f := make([]int, n)

solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README_EN.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,8 @@ func maxHeight(cuboids [][]int) (ans int) {
132132
sort.Ints(c)
133133
}
134134
sort.Slice(cuboids, func(i, j int) bool {
135-
if cuboids[i][0] != cuboids[j][0] {
136-
return cuboids[i][0] < cuboids[j][0]
137-
}
138-
if cuboids[i][1] != cuboids[j][1] {
139-
return cuboids[i][1] < cuboids[j][1]
140-
}
141-
return cuboids[i][2] < cuboids[j][2]
135+
a, b := cuboids[i], cuboids[j]
136+
return a[0] < b[0] || a[0] == b[0] && (a[1] < b[1] || a[1] == b[1] && a[2] < b[2])
142137
})
143138
n := len(cuboids)
144139
f := make([]int, n)

solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ func maxHeight(cuboids [][]int) (ans int) {
33
sort.Ints(c)
44
}
55
sort.Slice(cuboids, func(i, j int) bool {
6-
if cuboids[i][0] != cuboids[j][0] {
7-
return cuboids[i][0] < cuboids[j][0]
8-
}
9-
if cuboids[i][1] != cuboids[j][1] {
10-
return cuboids[i][1] < cuboids[j][1]
11-
}
12-
return cuboids[i][2] < cuboids[j][2]
6+
a, b := cuboids[i], cuboids[j]
7+
return a[0] < b[0] || a[0] == b[0] && (a[1] < b[1] || a[1] == b[1] && a[2] < b[2])
138
})
149
n := len(cuboids)
1510
f := make([]int, n)

0 commit comments

Comments
 (0)