47
47
48
48
<!-- 这里可写通用的实现逻辑 -->
49
49
50
- ** 方法一:排序 + 贪心 **
50
+ ** 方法一:排序 + 双指针 **
51
51
52
- 时间复杂度 $O(mlogm+nlogn)$,其中 $m$ 表示 $g$ 的长度,$n$ 表示 $s$ 的长度。
52
+ 根据题目描述,我们应该优先将饼干分配给胃口值小的孩子,这样可以尽可能满足更多的孩子。
53
+
54
+ 因此,我们首先对两个数组进行排序,然后用两个指针 $i$ 和 $j$ 分别指向数组 $g$ 和 $s$ 的头部,每次比较 $g[ i] $ 和 $s[ j] $ 的大小:
55
+
56
+ - 如果 $s[ j] \lt g[ i] $,说明当前饼干 $s[ j] $ 无法满足当前孩子 $g[ i] $,我们应该将尺寸更大的饼干分配给当前孩子,因此 $j$ 应该右移一位;如果 $j$ 越界,说明无法满足当前孩子,此时成功分配的孩子数量为 $i$,直接返回即可;
57
+ - 如果 $s[ j] \ge g[ i] $,说明当前饼干 $s[ j] $ 可以满足当前孩子 $g[ i] $,我们将当前饼干分配给当前孩子,因此 $i$ 和 $j$ 都应该右移一位。
58
+
59
+ 如果遍历完数组 $g$,则说明所有孩子都已经分配到饼干,则返回孩子总数即可。
60
+
61
+ 时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\log m + \log n)$。其中 $m$ 和 $n$ 分别为数组 $g$ 和 $s$ 的长度。
53
62
54
63
<!-- tabs:start -->
55
64
@@ -63,8 +72,8 @@ class Solution:
63
72
g.sort()
64
73
s.sort()
65
74
j = 0
66
- for i, v in enumerate (g):
67
- while j < len (s) and s[j] < v :
75
+ for i, x in enumerate (g):
76
+ while j < len (s) and s[j] < g[i] :
68
77
j += 1
69
78
if j >= len (s):
70
79
return i
@@ -81,17 +90,17 @@ class Solution {
81
90
public int findContentChildren (int [] g , int [] s ) {
82
91
Arrays . sort(g);
83
92
Arrays . sort(s);
84
- int i = 0 , j = 0 ;
85
- for (; i < g. length; ++ i) {
86
- while (j < s. length && s[j] < g[i]) {
93
+ int m = g. length;
94
+ int n = s. length;
95
+ for (int i = 0 , j = 0 ; i < m; ++ i) {
96
+ while (j < n && s[j] < g[i]) {
87
97
++ j;
88
98
}
89
- if (j >= s . length ) {
90
- break ;
99
+ if (j++ >= n ) {
100
+ return i ;
91
101
}
92
- ++ j;
93
102
}
94
- return i ;
103
+ return m ;
95
104
}
96
105
}
97
106
```
@@ -104,17 +113,16 @@ public:
104
113
int findContentChildren(vector<int >& g, vector<int >& s) {
105
114
sort(g.begin(), g.end());
106
115
sort(s.begin(), s.end());
107
- int i = 0, j = 0 ;
108
- for (; i < g.size() ; ++i) {
109
- while (j < s.size() && s[ j] < g[ i] ) {
116
+ int m = g.size(), n = s.size() ;
117
+ for (int i = 0, j = 0 ; i < m ; ++i) {
118
+ while (j < n && s[ j] < g[ i] ) {
110
119
++j;
111
120
}
112
- if (j >= s.size() ) {
113
- break ;
121
+ if (j++ >= n ) {
122
+ return i ;
114
123
}
115
- ++j;
116
124
}
117
- return i ;
125
+ return m ;
118
126
}
119
127
};
120
128
```
@@ -125,16 +133,17 @@ public:
125
133
func findContentChildren(g []int, s []int) int {
126
134
sort.Ints(g)
127
135
sort.Ints(s)
128
- i, j := 0, 0
129
- for ; i < len(g); i++ {
130
- for ; j < len(s) && s[j] < g[i]; j++ {
136
+ j := 0
137
+ for i, x := range g {
138
+ for j < len(s) && s[j] < x {
139
+ j++
131
140
}
132
141
if j >= len(s) {
133
- break
142
+ return i
134
143
}
135
144
j++
136
145
}
137
- return i
146
+ return len(g)
138
147
}
139
148
```
140
149
@@ -149,21 +158,40 @@ func findContentChildren(g []int, s []int) int {
149
158
var findContentChildren = function (g , s ) {
150
159
g .sort ((a , b ) => a - b);
151
160
s .sort ((a , b ) => a - b);
152
- let i = 0 ;
153
- let j = 0 ;
154
- for (; i < g . length ; ++ i) {
155
- while (j < s . length && s[j] < g[i]) {
161
+ const m = g . length ;
162
+ const n = s . length ;
163
+ for (let i = 0 , j = 0 ; i < m ; ++ i) {
164
+ while (j < n && s[j] < g[i]) {
156
165
++ j;
157
166
}
158
- if (j >= s . length ) {
159
- break ;
167
+ if (j++ >= n ) {
168
+ return i ;
160
169
}
161
- ++ j;
162
170
}
163
- return i ;
171
+ return m ;
164
172
};
165
173
```
166
174
175
+ ### ** TypeScript**
176
+
177
+ ``` ts
178
+ function findContentChildren(g : number [], s : number []): number {
179
+ g .sort ((a , b ) => a - b );
180
+ s .sort ((a , b ) => a - b );
181
+ const m = g .length ;
182
+ const n = s .length ;
183
+ for (let i = 0 , j = 0 ; i < m ; ++ i ) {
184
+ while (j < n && s [j ] < g [i ]) {
185
+ ++ j ;
186
+ }
187
+ if (j ++ >= n ) {
188
+ return i ;
189
+ }
190
+ }
191
+ return m ;
192
+ }
193
+ ```
194
+
167
195
### ** ...**
168
196
169
197
```
0 commit comments