Skip to content

Commit b33a20e

Browse files
committed
feat: add solutions to lc problem: No.0628, No.0665
1 parent 4c4be55 commit b33a20e

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed

solution/0600-0699/0628.Maximum Product of Three Numbers/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,48 @@ func max(a, b int) int {
221221
}
222222
```
223223

224+
### **TypeScript**
225+
226+
```ts
227+
function maximumProduct(nums: number[]): number {
228+
nums.sort((a, b) => a - b);
229+
const n = nums.length;
230+
const a = nums[n - 1] * nums[n - 2] * nums[n - 3];
231+
const b = nums[n - 1] * nums[0] * nums[1];
232+
return Math.max(a, b);
233+
}
234+
```
235+
236+
```ts
237+
function maximumProduct(nums: number[]): number {
238+
const inf = 1 << 30;
239+
let mi1 = inf,
240+
mi2 = inf;
241+
let mx1 = -inf,
242+
mx2 = -inf,
243+
mx3 = -inf;
244+
for (const x of nums) {
245+
if (x < mi1) {
246+
mi2 = mi1;
247+
mi1 = x;
248+
} else if (x < mi2) {
249+
mi2 = x;
250+
}
251+
if (x > mx1) {
252+
mx3 = mx2;
253+
mx2 = mx1;
254+
mx1 = x;
255+
} else if (x > mx2) {
256+
mx3 = mx2;
257+
mx2 = x;
258+
} else if (x > mx3) {
259+
mx3 = x;
260+
}
261+
}
262+
return Math.max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
263+
}
264+
```
265+
224266
### **...**
225267

226268
```

solution/0600-0699/0628.Maximum Product of Three Numbers/README_EN.md

+42
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,48 @@ func max(a, b int) int {
181181
}
182182
```
183183

184+
### **TypeScript**
185+
186+
```ts
187+
function maximumProduct(nums: number[]): number {
188+
nums.sort((a, b) => a - b);
189+
const n = nums.length;
190+
const a = nums[n - 1] * nums[n - 2] * nums[n - 3];
191+
const b = nums[n - 1] * nums[0] * nums[1];
192+
return Math.max(a, b);
193+
}
194+
```
195+
196+
```ts
197+
function maximumProduct(nums: number[]): number {
198+
const inf = 1 << 30;
199+
let mi1 = inf,
200+
mi2 = inf;
201+
let mx1 = -inf,
202+
mx2 = -inf,
203+
mx3 = -inf;
204+
for (const x of nums) {
205+
if (x < mi1) {
206+
mi2 = mi1;
207+
mi1 = x;
208+
} else if (x < mi2) {
209+
mi2 = x;
210+
}
211+
if (x > mx1) {
212+
mx3 = mx2;
213+
mx2 = mx1;
214+
mx1 = x;
215+
} else if (x > mx2) {
216+
mx3 = mx2;
217+
mx2 = x;
218+
} else if (x > mx3) {
219+
mx3 = x;
220+
}
221+
}
222+
return Math.max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
223+
}
224+
```
225+
184226
### **...**
185227

186228
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function maximumProduct(nums: number[]): number {
2+
const inf = 1 << 30;
3+
let mi1 = inf,
4+
mi2 = inf;
5+
let mx1 = -inf,
6+
mx2 = -inf,
7+
mx3 = -inf;
8+
for (const x of nums) {
9+
if (x < mi1) {
10+
mi2 = mi1;
11+
mi1 = x;
12+
} else if (x < mi2) {
13+
mi2 = x;
14+
}
15+
if (x > mx1) {
16+
mx3 = mx2;
17+
mx2 = mx1;
18+
mx1 = x;
19+
} else if (x > mx2) {
20+
mx3 = mx2;
21+
mx2 = x;
22+
} else if (x > mx3) {
23+
mx3 = x;
24+
}
25+
}
26+
return Math.max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
27+
}

solution/0600-0699/0665.Non-decreasing Array/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ func checkPossibility(nums []int) bool {
162162
}
163163
```
164164

165+
### **TypeScript**
166+
167+
```ts
168+
function checkPossibility(nums: number[]): boolean {
169+
const isSorted = (nums: number[]) => {
170+
for (let i = 0; i < nums.length - 1; ++i) {
171+
if (nums[i] > nums[i + 1]) {
172+
return false;
173+
}
174+
}
175+
return true;
176+
};
177+
for (let i = 0; i < nums.length - 1; ++i) {
178+
const a = nums[i],
179+
b = nums[i + 1];
180+
if (a > b) {
181+
nums[i] = b;
182+
if (isSorted(nums)) {
183+
return true;
184+
}
185+
nums[i] = a;
186+
nums[i + 1] = a;
187+
return isSorted(nums);
188+
}
189+
}
190+
return true;
191+
}
192+
```
193+
165194
### **...**
166195

167196
```

solution/0600-0699/0665.Non-decreasing Array/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,35 @@ func checkPossibility(nums []int) bool {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function checkPossibility(nums: number[]): boolean {
148+
const isSorted = (nums: number[]) => {
149+
for (let i = 0; i < nums.length - 1; ++i) {
150+
if (nums[i] > nums[i + 1]) {
151+
return false;
152+
}
153+
}
154+
return true;
155+
};
156+
for (let i = 0; i < nums.length - 1; ++i) {
157+
const a = nums[i],
158+
b = nums[i + 1];
159+
if (a > b) {
160+
nums[i] = b;
161+
if (isSorted(nums)) {
162+
return true;
163+
}
164+
nums[i] = a;
165+
nums[i + 1] = a;
166+
return isSorted(nums);
167+
}
168+
}
169+
return true;
170+
}
171+
```
172+
144173
### **...**
145174

146175
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function checkPossibility(nums: number[]): boolean {
2+
const isSorted = (nums: number[]) => {
3+
for (let i = 0; i < nums.length - 1; ++i) {
4+
if (nums[i] > nums[i + 1]) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
};
10+
for (let i = 0; i < nums.length - 1; ++i) {
11+
const a = nums[i],
12+
b = nums[i + 1];
13+
if (a > b) {
14+
nums[i] = b;
15+
if (isSorted(nums)) {
16+
return true;
17+
}
18+
nums[i] = a;
19+
nums[i + 1] = a;
20+
return isSorted(nums);
21+
}
22+
}
23+
return true;
24+
}

0 commit comments

Comments
 (0)