From c6b5b0bf9e08c9522df4abe290abd34e7fcc5c2c Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Thu, 19 Sep 2024 23:27:50 +0300 Subject: [PATCH] [solution] min max with Left Right --- lib/minMaxNumber.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/minMaxNumber.ts diff --git a/lib/minMaxNumber.ts b/lib/minMaxNumber.ts new file mode 100644 index 0000000..de76c7c --- /dev/null +++ b/lib/minMaxNumber.ts @@ -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);