[solution] fix sort mutate list, not return result
This commit is contained in:
+34
-36
@@ -1,51 +1,49 @@
|
|||||||
// export const min = (list: number[]): number => {
|
export const min = (list: number[]): number => {
|
||||||
// const sortList = list.sort();
|
list.sort();
|
||||||
// let L = 0;
|
let L = 0;
|
||||||
// let R = list.length - 1;
|
let R = list.length - 1;
|
||||||
// let res = list[L];
|
let res = list[L];
|
||||||
|
|
||||||
// while (L <= R) {
|
while (L <= R) {
|
||||||
// let currentMin = sortList[L] < sortList[R] ? sortList[L] : sortList[R];
|
res = Math.min(list[L], list[R], res);
|
||||||
// res = currentMin < res ? currentMin : res;
|
|
||||||
|
|
||||||
// L++;
|
L++;
|
||||||
// R--;
|
R--;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return res;
|
return res;
|
||||||
// };
|
};
|
||||||
|
|
||||||
// export const max = (list: number[]): number => {
|
export const max = (list: number[]): number => {
|
||||||
// const sortList = list.sort();
|
list.sort();
|
||||||
// let L = 0;
|
let L = 0;
|
||||||
// let R = list.length - 1;
|
let R = list.length - 1;
|
||||||
// let res = list[L];
|
let res = list[L];
|
||||||
|
|
||||||
// while (L <= R) {
|
while (L <= R) {
|
||||||
// let currentMax = sortList[L] > sortList[R] ? sortList[L] : sortList[R];
|
res = Math.max(list[L], list[R], res);
|
||||||
// res = currentMax > res ? currentMax : res;
|
|
||||||
|
|
||||||
// L++;
|
L++;
|
||||||
// R--;
|
R--;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return res;
|
return res;
|
||||||
// };
|
};
|
||||||
|
|
||||||
// const min = (list) => Math.min(...list);
|
// const min = (list) => Math.min(...list);
|
||||||
// const max = (list) => Math.max(...list);
|
// const max = (list) => Math.max(...list);
|
||||||
|
|
||||||
export const min = (list: number[]): number => {
|
// export const min = (list: number[]): number => {
|
||||||
return list.sort((leftvalue, rightvalue): number => {
|
// return list.sort((leftvalue, rightvalue): number => {
|
||||||
return leftvalue - rightvalue;
|
// return leftvalue - rightvalue;
|
||||||
})[0];
|
// })[0];
|
||||||
};
|
// };
|
||||||
|
|
||||||
export const max = (list: number[]): number => {
|
// export const max = (list: number[]): number => {
|
||||||
return list.sort((leftvalue, rightvalue): number => {
|
// return list.sort((leftvalue, rightvalue): number => {
|
||||||
return leftvalue - rightvalue;
|
// return leftvalue - rightvalue;
|
||||||
})[list.length - 1];
|
// })[list.length - 1];
|
||||||
};
|
// };
|
||||||
|
|
||||||
console.log(min([-52, 56, 30, 29, -54, 0, -110]), -110);
|
console.log(min([-52, 56, 30, 29, -54, 0, -110]), -110);
|
||||||
console.log(min([42, 54, 65, 87, 0]), 0);
|
console.log(min([42, 54, 65, 87, 0]), 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user