[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 => {
|
||||
// const sortList = list.sort();
|
||||
// let L = 0;
|
||||
// let R = list.length - 1;
|
||||
// let res = list[L];
|
||||
export const min = (list: number[]): number => {
|
||||
list.sort();
|
||||
let L = 0;
|
||||
let R = list.length - 1;
|
||||
let res = list[L];
|
||||
|
||||
// while (L <= R) {
|
||||
// let currentMin = sortList[L] < sortList[R] ? sortList[L] : sortList[R];
|
||||
// res = currentMin < res ? currentMin : res;
|
||||
while (L <= R) {
|
||||
res = Math.min(list[L], list[R], res);
|
||||
|
||||
// L++;
|
||||
// R--;
|
||||
// }
|
||||
L++;
|
||||
R--;
|
||||
}
|
||||
|
||||
// return res;
|
||||
// };
|
||||
return res;
|
||||
};
|
||||
|
||||
// export const max = (list: number[]): number => {
|
||||
// const sortList = list.sort();
|
||||
// let L = 0;
|
||||
// let R = list.length - 1;
|
||||
// let res = list[L];
|
||||
export const max = (list: number[]): number => {
|
||||
list.sort();
|
||||
let L = 0;
|
||||
let R = list.length - 1;
|
||||
let res = list[L];
|
||||
|
||||
// while (L <= R) {
|
||||
// let currentMax = sortList[L] > sortList[R] ? sortList[L] : sortList[R];
|
||||
// res = currentMax > res ? currentMax : res;
|
||||
while (L <= R) {
|
||||
res = Math.max(list[L], list[R], res);
|
||||
|
||||
// L++;
|
||||
// R--;
|
||||
// }
|
||||
L++;
|
||||
R--;
|
||||
}
|
||||
|
||||
// return res;
|
||||
// };
|
||||
return res;
|
||||
};
|
||||
|
||||
// const min = (list) => Math.min(...list);
|
||||
// const max = (list) => Math.max(...list);
|
||||
|
||||
export const min = (list: number[]): number => {
|
||||
return list.sort((leftvalue, rightvalue): number => {
|
||||
return leftvalue - rightvalue;
|
||||
})[0];
|
||||
};
|
||||
// export const min = (list: number[]): number => {
|
||||
// return list.sort((leftvalue, rightvalue): number => {
|
||||
// return leftvalue - rightvalue;
|
||||
// })[0];
|
||||
// };
|
||||
|
||||
export const max = (list: number[]): number => {
|
||||
return list.sort((leftvalue, rightvalue): number => {
|
||||
return leftvalue - rightvalue;
|
||||
})[list.length - 1];
|
||||
};
|
||||
// export const max = (list: number[]): number => {
|
||||
// return list.sort((leftvalue, rightvalue): number => {
|
||||
// return leftvalue - rightvalue;
|
||||
// })[list.length - 1];
|
||||
// };
|
||||
|
||||
console.log(min([-52, 56, 30, 29, -54, 0, -110]), -110);
|
||||
console.log(min([42, 54, 65, 87, 0]), 0);
|
||||
|
||||
Reference in New Issue
Block a user