File tree 8 files changed +79
-25
lines changed
0000-0099/0035.Search Insert Position
1688.Count of Matches in Tournament
1691.Maximum Height by Stacking Cuboids
8 files changed +79
-25
lines changed Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
- 二分查找。
51
+ ** 方法一:二分查找**
52
+
53
+ 由于 ` nums ` 数组已经有序,因此我们可以使用二分查找的方法找到目标值 ` target ` 的插入位置。
54
+
55
+ 时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 ` nums ` 的长度。
52
56
53
57
<!-- tabs:start -->
54
58
@@ -69,6 +73,12 @@ class Solution:
69
73
return left
70
74
```
71
75
76
+ ``` python
77
+ class Solution :
78
+ def searchInsert (self , nums : List[int ], target : int ) -> int :
79
+ return bisect_left(nums, target)
80
+ ```
81
+
72
82
### ** Java**
73
83
74
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -109,6 +119,15 @@ public:
109
119
};
110
120
```
111
121
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
+
112
131
### ** Go**
113
132
114
133
``` go
@@ -126,6 +145,12 @@ func searchInsert(nums []int, target int) int {
126
145
}
127
146
```
128
147
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
+
129
154
### ** JavaScript**
130
155
131
156
``` js
Original file line number Diff line number Diff line change @@ -61,6 +61,12 @@ class Solution:
61
61
return left
62
62
```
63
63
64
+ ``` python
65
+ class Solution :
66
+ def searchInsert (self , nums : List[int ], target : int ) -> int :
67
+ return bisect_left(nums, target)
68
+ ```
69
+
64
70
### ** Java**
65
71
66
72
``` java
@@ -99,6 +105,15 @@ public:
99
105
};
100
106
```
101
107
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
+
102
117
### ** Go**
103
118
104
119
``` go
@@ -116,6 +131,12 @@ func searchInsert(nums []int, target int) int {
116
131
}
117
132
```
118
133
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
+
119
140
### ** JavaScript**
120
141
121
142
``` js
Original file line number Diff line number Diff line change 52
52
53
53
<!-- 这里可写通用的实现逻辑 -->
54
54
55
- n 个人比赛,最终淘汰 n - 1 个人,所以配对次数是 n - 1。
55
+ ** 方法一:脑筋急转弯**
56
+
57
+ 根据题目描述我们知道,一共有 $n$ 支队伍,每一次的配对,都会淘汰一支队伍,所以配对次数就是淘汰的队伍数,即 $n - 1$。
58
+
59
+ 时间复杂度 $O(1)$,空间复杂度 $O(1)$。
56
60
57
61
<!-- tabs:start -->
58
62
@@ -109,6 +113,14 @@ var numberOfMatches = function (n) {
109
113
};
110
114
```
111
115
116
+ ### ** TypeScript**
117
+
118
+ ``` ts
119
+ function numberOfMatches(n : number ): number {
120
+ return n - 1 ;
121
+ }
122
+ ```
123
+
112
124
### ** ...**
113
125
114
126
```
Original file line number Diff line number Diff line change @@ -99,6 +99,14 @@ var numberOfMatches = function (n) {
99
99
};
100
100
```
101
101
102
+ ### ** TypeScript**
103
+
104
+ ``` ts
105
+ function numberOfMatches(n : number ): number {
106
+ return n - 1 ;
107
+ }
108
+ ```
109
+
102
110
### ** ...**
103
111
104
112
```
Original file line number Diff line number Diff line change
1
+ function numberOfMatches ( n : number ) : number {
2
+ return n - 1 ;
3
+ }
Original file line number Diff line number Diff line change 73
73
74
74
接下来,我们可以使用动态规划的方法求解本题。
75
75
76
- 我们定义 $f[ i] $ 表示以长方体 $i$ 为最底部长方体时的最大高度。我们可以枚举每个长方体 $i$ 的上方的长方体 $j$,如果 $j$ 可以放在 $i$ 的上方,那么我们可以得到状态转移方程:
76
+ 我们定义 $f[ i] $ 表示以长方体 $i$ 为最底部长方体时的最大高度。我们可以枚举每个长方体 $i$ 的上方的长方体 $j$,其中 $0 \leq j < i$。 如果 $j$ 可以放在 $i$ 的上方,那么我们可以得到状态转移方程:
77
77
78
78
$$
79
- f[i] = \max(f[i], f[j] + h[i])
79
+ f[i] = \max_{0 \leq j < i} \{ f[j] + h[i]\}
80
80
$$
81
81
82
82
其中 $h[ i] $ 表示长方体 $i$ 的高度。
@@ -164,13 +164,8 @@ func maxHeight(cuboids [][]int) (ans int) {
164
164
sort.Ints(c)
165
165
}
166
166
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])
174
169
})
175
170
n := len(cuboids)
176
171
f := make([]int, n)
Original file line number Diff line number Diff line change @@ -132,13 +132,8 @@ func maxHeight(cuboids [][]int) (ans int) {
132
132
sort.Ints(c)
133
133
}
134
134
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])
142
137
})
143
138
n := len(cuboids)
144
139
f := make([]int, n)
Original file line number Diff line number Diff line change @@ -3,13 +3,8 @@ func maxHeight(cuboids [][]int) (ans int) {
3
3
sort .Ints (c )
4
4
}
5
5
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 ])
13
8
})
14
9
n := len (cuboids )
15
10
f := make ([]int , n )
You can’t perform that action at this time.
0 commit comments