You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
835 B
TypeScript
40 lines
835 B
TypeScript
export const min = (list: number[]): number => {
|
|
const sortList = 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;
|
|
|
|
L++;
|
|
R--;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
export const max = (list: number[]): number => {
|
|
const sortList = 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;
|
|
|
|
L++;
|
|
R--;
|
|
}
|
|
|
|
return res;
|
|
};
|
|
|
|
console.log(min([-52, 56, 30, 29, -54, 0, -110]), -110);
|
|
console.log(min([42, 54, 65, 87, 0]), 0);
|
|
|
|
console.log(max([4, 6, 2, 1, 9, 63, -134, 566]), 566);
|
|
console.log(max([5]), 5);
|