From b6980f1569dc447057aba1a4529368fc7c8530f4 Mon Sep 17 00:00:00 2001 From: GrishinAnton Date: Mon, 11 May 2020 18:48:19 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array/977.squares-of-a-sorted-array.js | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 array/977.squares-of-a-sorted-array.js diff --git a/array/977.squares-of-a-sorted-array.js b/array/977.squares-of-a-sorted-array.js new file mode 100644 index 0000000..971b889 --- /dev/null +++ b/array/977.squares-of-a-sorted-array.js @@ -0,0 +1,33 @@ +/* + * @lc app=leetcode id=977 lang=javascript + * + * [977] Squares of a Sorted Array + */ + +// @lc code=start +/** + * @param {number[]} A + * @return {number[]} + */ +var sortedSquares = function (A) { + let hashmap = [] + let result = [] + for (let i = 0; i < A.length; i++) { + hashmap[Math.abs(A[i])] = (hashmap[Math.abs(A[i])] || 0) + 1 + } + + for (let k = 0; k < hashmap.length; k++) { + if (hashmap[k]) { + + let element = hashmap[k] + while (element) { + result.push(k ** 2) + + element-- + } + } + } + return result +}; +// @lc code=end + From c754058de18ce5ed244b23a3d5e0e3992ec4736c Mon Sep 17 00:00:00 2001 From: GrishinAnton Date: Thu, 14 May 2020 08:25:35 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=91=D0=B5=D0=B7=20=D0=B4=D0=BE=D0=BF=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array/977.squares-of-a-sorted-array.js | 61 +++++++++++++++++++++----- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/array/977.squares-of-a-sorted-array.js b/array/977.squares-of-a-sorted-array.js index 971b889..9d4f7ce 100644 --- a/array/977.squares-of-a-sorted-array.js +++ b/array/977.squares-of-a-sorted-array.js @@ -1,4 +1,4 @@ -/* +/** * @lc app=leetcode id=977 lang=javascript * * [977] Squares of a Sorted Array @@ -10,24 +10,63 @@ * @return {number[]} */ var sortedSquares = function (A) { - let hashmap = [] + if (A.length === 1) return [Math.abs(A[0])] let result = [] + let minNumber = -10001 + let minIndexArr = null + + for (let i = 0; i < A.length; i++) { - hashmap[Math.abs(A[i])] = (hashmap[Math.abs(A[i])] || 0) + 1 + if (Math.min(Math.abs(A[i]), Math.abs(minNumber)) === A[i]) { + minNumber = A[i] + minIndexArr = i + } + + A[i] = Math.abs(A[i]) ** 2 } - for (let k = 0; k < hashmap.length; k++) { - if (hashmap[k]) { + console.log(minIndexArr); - let element = hashmap[k] - while (element) { - result.push(k ** 2) - element-- - } - } + let min = minIndexArr - 1 + let max = minIndexArr + 1 + result.push(A[minIndexArr]) + + while (true) { + + let dec = A[min] || -1 + let inc = A[max] || 10001 + + min >= 0 && result.push(Math.min(dec, inc)) + max <= A.length - 1 && result.push(Math.max(dec, inc)) + + if (min <= 0 && max >= A.length - 1) break + + min-- + max++ } + return result }; +// var sortedSquares = function (A) { +// let hashmap = [] +// let result = [] +// for (let i = 0; i < A.length; i++) { +// hashmap[Math.abs(A[i])] = (hashmap[Math.abs(A[i])] || 0) + 1 +// } + +// for (let k = 0; k < hashmap.length; k++) { +// if (hashmap[k]) { + +// let element = hashmap[k] +// while (element) { +// result.push(k ** 2) + +// element-- +// } +// } +// } +// return result +// }; // @lc code=end From 46f2bb2cbc6c8c58044187bce42f0a44262f32b3 Mon Sep 17 00:00:00 2001 From: GrishinAnton Date: Fri, 15 May 2020 08:36:14 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array/977.squares-of-a-sorted-array.js | 76 +++++++++++++------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/array/977.squares-of-a-sorted-array.js b/array/977.squares-of-a-sorted-array.js index 9d4f7ce..8b4ddbb 100644 --- a/array/977.squares-of-a-sorted-array.js +++ b/array/977.squares-of-a-sorted-array.js @@ -9,45 +9,6 @@ * @param {number[]} A * @return {number[]} */ -var sortedSquares = function (A) { - if (A.length === 1) return [Math.abs(A[0])] - let result = [] - let minNumber = -10001 - let minIndexArr = null - - - for (let i = 0; i < A.length; i++) { - if (Math.min(Math.abs(A[i]), Math.abs(minNumber)) === A[i]) { - minNumber = A[i] - minIndexArr = i - } - - A[i] = Math.abs(A[i]) ** 2 - } - - console.log(minIndexArr); - - - let min = minIndexArr - 1 - let max = minIndexArr + 1 - result.push(A[minIndexArr]) - - while (true) { - - let dec = A[min] || -1 - let inc = A[max] || 10001 - - min >= 0 && result.push(Math.min(dec, inc)) - max <= A.length - 1 && result.push(Math.max(dec, inc)) - - if (min <= 0 && max >= A.length - 1) break - - min-- - max++ - } - - return result -}; // var sortedSquares = function (A) { // let hashmap = [] // let result = [] @@ -68,5 +29,42 @@ var sortedSquares = function (A) { // } // return result // }; +function findIndexOfMin(arr) { + let minNumber = -10001 + let minIndex = null + + for (let i = 0; i < arr.length; i++) { + if (Math.min(Math.abs(arr[i]), Math.abs(minNumber)) === Math.abs(arr[i])) { + minNumber = arr[i] + minIndex = i + } + } + + return minIndex +} +var sortedSquares = function (A) { + if (A.length === 1) return [Math.abs(A[0])] + + let result = [] + const minIndex = findIndexOfMin(A) + + let min = minIndex + let max = minIndex + 1 > A.length - 1 ? A.length - 1 : minIndex + 1 + + while (min > 0 || max < A.length - 1) { + + const minSqr = A[min] ** 2 + const maxSqr = A[max] ** 2 + + min >= 0 && result.push(Math.min(minSqr, maxSqr)) + max <= A.length - 1 && result.push(Math.max(minSqr, maxSqr)) + + min > 0 && min-- + max < A.length - 1 && max++ + } + + return result +}; + // @lc code=end From 29c803855454fd46a4373c104cf809da75b1a61f Mon Sep 17 00:00:00 2001 From: GrishinAnton Date: Fri, 15 May 2020 21:00:58 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9E=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B8=D0=BD=D0=B8=D0=BC=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BF=D0=BE=D0=B7=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array/977.squares-of-a-sorted-array.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/array/977.squares-of-a-sorted-array.js b/array/977.squares-of-a-sorted-array.js index 8b4ddbb..f444d07 100644 --- a/array/977.squares-of-a-sorted-array.js +++ b/array/977.squares-of-a-sorted-array.js @@ -48,19 +48,13 @@ var sortedSquares = function (A) { let result = [] const minIndex = findIndexOfMin(A) - let min = minIndex - let max = minIndex + 1 > A.length - 1 ? A.length - 1 : minIndex + 1 + let min = minIndex - 1 + let max = minIndex + 1 - while (min > 0 || max < A.length - 1) { - const minSqr = A[min] ** 2 - const maxSqr = A[max] ** 2 + while (min > 0 || max < A.length - 1) { - min >= 0 && result.push(Math.min(minSqr, maxSqr)) - max <= A.length - 1 && result.push(Math.max(minSqr, maxSqr)) - min > 0 && min-- - max < A.length - 1 && max++ } return result