Leetcode projectionArea challenge
This article is written in English in the bottom. 今週のLeetcodeコーディングチャレンジも挑戦してきました。 スコアは自慢できるほどのものではないので、公開はしません。
一番難易度が低い問題に一番苦戦してしまいました。 その問題 projectionAreaの合格率70%!!
かなり難易度的には低い!それもそのはず、問題内容とインプットの内容が正しく理解できれば大したことのないシンプルな問題でした。 苦戦した点は問題文の理解。 問題文はLeetcodeのサイトから引用。 On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). 赤字でハイライトした部分の理解できませんでしたが、要するに それぞれの配列の中に特定の高さのタワーがv個、グリッドセル(i, j)に入ってますよと言う意味です。
例えば、
上図引用元: Projection Area of 3D Shapes - LeetCode 上図はインプットが[[1,2],[3,4]]の場合を示しています。 手前右側から左側へ高さが1, 2。奥の右側から左側へ高さが3, 4のタワーがグリッド内に入っている例です。
そして問題内容ですが、この4つのタワーの前面、側面、上面から見たときの面積を求めろとのことでした。 ここでは、それぞれの面をJavaScriptで回答した例を記載します。 インプットの引数名は、grid。 変数
let top = 0, front = 0, side = 0; const frontPair = [];
上面
grid.forEach((elem) => { elem.forEach((val) => { if (val > 0) { top++; } }); });
前面
// FRONT grid.forEach((elem) => { elem.forEach((val, key) => { if (frontPair[key] === undefined) { frontPair[key] = val; } else if (val > frontPair[key]) { frontPair[key] = val; } }); });
側面
// SIDE grid.forEach((elem) => { let current = 0; elem.forEach((val) => { if (current < val) { current = val; } }); // console.log(current); side += current; });
そして最後にreduce使って合計値の算出。
front = frontPair.reduce((total, crr) => total + crr, 0);
JavaScript使ったと言うこともあるし、コードももっと洗練されたものに改善できるかもしれませんが、現時点ではこれが精一杯できたことものです。 もっと良い計算方法があればコメントください。
Hello there! This week, I hav ejoined in the contest at Leetcode again! The toughest challenge in this week was the easiest challenge! I have no idea why the acceptance rate is over 70% during the contest. But after understanding the description, it is obvious! It was very straightforward challenge I found! The below is picked up from the description of the challenge.
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
At the moment, I have no idea the highlighted part. But it means that there are v of the towers that has the written height in the grid cell(i, j). So the number in a each array of elements represent the height of each towers!!
Cite: Projection Area of 3D Shapes - LeetCode
The diagram above stands for the case you have the array of elements [[1,2],[3,4]]. The foremost from right to left, the heights are 1, 2. Then the back towers have 3, 4 as their heights.
And the objective is to culculate the area of towers when looking at the front, side and top. I wrote in JavaScript like this... The given input name is 'grid'.
Variables:
let top = 0, front = 0, side = 0; const frontPair = [];
TOP
grid.forEach((elem) => { elem.forEach((val) => { if (val > 0) { top++; } }); });
Front
// FRONT grid.forEach((elem) => { elem.forEach((val, key) => { if (frontPair[key] === undefined) { frontPair[key] = val; } else if (val > frontPair[key]) { frontPair[key] = val; } }); });
Side
// SIDE grid.forEach((elem) => { let current = 0; elem.forEach((val) => { if (current < val) { current = val; } }); // console.log(current); side += current; });
And the total of front, side and top are culculated by reduce.
front = frontPair.reduce((total, crr) => total + crr, 0);
Although this is my current solution, if you have your better way to solve this challenge. Give me the comment below. Thank you.