diff options
| author | Nuno Vieira <nunovieira220@gmail.com> | 2020-11-20 23:08:36 +0000 |
|---|---|---|
| committer | Nuno Vieira <nunovieira220@gmail.com> | 2020-11-20 23:11:09 +0000 |
| commit | 0b2547cacbf7b3c10361196b32133e88b9bfd90a (patch) | |
| tree | ec6c835f5ad12bd12b0498fc2902d3616fa359f8 | |
| parent | 25e97e2e5b5b23e4f948db430f636241e2ab56b0 (diff) | |
| download | perlweeklychallenge-club-0b2547cacbf7b3c10361196b32133e88b9bfd90a.tar.gz perlweeklychallenge-club-0b2547cacbf7b3c10361196b32133e88b9bfd90a.tar.bz2 perlweeklychallenge-club-0b2547cacbf7b3c10361196b32133e88b9bfd90a.zip | |
Add nunovieira220 js solution to challenge 087
| -rw-r--r-- | challenge-087/nunovieira220/js/ch-1.js | 26 | ||||
| -rw-r--r-- | challenge-087/nunovieira220/js/ch-2.js | 88 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-087/nunovieira220/js/ch-1.js b/challenge-087/nunovieira220/js/ch-1.js new file mode 100644 index 0000000000..ee330470eb --- /dev/null +++ b/challenge-087/nunovieira220/js/ch-1.js @@ -0,0 +1,26 @@ +// Input +const N = [20, 19, 9, 11, 10]; + +// Longest Consecutive Sequence +const nums = {}; +let end = -1; +let counter = 1; + +for (const num of N.sort((a, b) => a - b)) { + if(nums[num - 1]) { + const val = nums[num - 1] + 1; + nums[num] = val; + + if (val >= counter) { + counter = val; + end = num; + } + } else { + nums[num] = 1; + if(counter === 1) end = num; + } +} + +// Output +const start = end - counter; +console.log(Array.from({ length: counter }, (v, k) => start + k + 1));
\ No newline at end of file diff --git a/challenge-087/nunovieira220/js/ch-2.js b/challenge-087/nunovieira220/js/ch-2.js new file mode 100644 index 0000000000..79a95c013c --- /dev/null +++ b/challenge-087/nunovieira220/js/ch-2.js @@ -0,0 +1,88 @@ +// Input +const N = [ + [0, 0, 0, 1, 1, 1], + [1, 1, 1, 1, 1, 1], + [0, 0, 1, 0, 0, 1], + [0, 0, 1, 1, 1, 1], + [0, 0, 1, 1, 1, 1], +]; + +// Largest Rectangle +const R = []; +let points = []; +let totalArea = 0; + +for (let x = 0; x < N.length; x++) { + if(R[x] === undefined) R[x] = []; + + for (let y = 0; y < N[0].length; y++) { + if(N[x][y] === 1) { + let left = 1, up = 1; + let leftArea = 1, upArea = 1, maxArea = 1; + let leftPoint = [], upPoint = [], maxPoint = []; + + // Count left and up values + if(y > 0) left = R[x][y-1][0] + 1; + if(x > 0) up = R[x-1][y][1] + 1; + + // Get maximum area on the left side + if(y > 0) { + let limitUp = up; + let leftArea = up; + leftPoint = [x - limitUp + 1, y]; + + for(let d = 1; d < left; d++) { + limitUp = Math.min(limitUp, R[x][y-d][1]); + const area = (d + 1) * limitUp; + + if(area > leftArea) { + leftArea = area; + leftPoint = [x - limitUp + 1, y - d]; + } + } + } + + // Get maximum area on the upper side + if(x > 0) { + let limitLeft = left; + upArea = left; + upPoint = [x, y - limitLeft + 1]; + + for(let d = 1; d < up; d++) { + limitLeft = Math.min(limitLeft, R[x-d][y][0]); + const area = (d + 1) * limitLeft; + + if(area > upArea) { + upArea = area; + upPoint = [x - d, y - limitLeft + 1]; + } + } + } + + // Get maximum area until now + if(leftArea > upArea) { + maxPoint = leftPoint; + maxArea = leftArea; + } else { + maxPoint = upPoint; + maxArea = upArea; + } + + if(maxArea > totalArea) { + points = [maxPoint[0], maxPoint[1], x, y]; + totalArea = maxArea; + } + + R[x][y] = [left, up]; + } else { + R[x][y] = [0, 0]; + } + } +} + +// Output +if(totalArea <= 1) { + console.log('No rectangule was found on the matrix.'); +} else { + console.log(`Result rectangule is located between (${points[0]}, ${points[1]}) and (${points[2]}, ${points[3]}) with total area of ${totalArea}.`); +}
\ No newline at end of file |
