[solution] min max with Left Right
parent
56f7b520ac
commit
c6b5b0bf9e
@ -0,0 +1,39 @@
|
|||||||
|
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);
|
||||||
Loading…
Reference in New Issue