[ํ๋ก๊ทธ๋๋จธ์ค] ์ํด๋ฆฌ ์ฑ๋ฆฐ์ง 10์ฃผ์ฐจ ๊ต์ ์ ๋ณ ๋ง๋ค๊ธฐ (js)
https://programmers.co.kr/learn/courses/30/lessons/87377?language=javascript
function isInteger(num) {
return num % 1 === 0;
}
function intersectionPoint(arr1,arr2){
const denominator = (arr1[0]*arr2[1])-(arr1[1]*arr2[0]);
const x = ((arr1[1]*arr2[2])-(arr1[2]*arr2[1])) / denominator;
const y = ((arr1[2]*arr2[0])-(arr1[0]*arr2[2])) / denominator;
return denominator === 0 ? 0 : [y,x]
}
function solution(line) {
let meetPointArr = [];
for(let i = 0; i < line.length; i++){
for(let j = i+1; j < line.length; j++){
const value = intersectionPoint(line[i], line[j]);
if(isInteger(value[0]) && isInteger(value[1])){
meetPointArr.push(value);
}
}
}
meetPointArr.sort((a,b) => a[0]-b[0]);
const [columnMin, columnMax] = [meetPointArr[0][0],meetPointArr[meetPointArr.length-1][0]];
meetPointArr.sort((a,b) => a[1]-b[1]);
const [rowMin,rowMax] = [meetPointArr[0][1],meetPointArr[meetPointArr.length-1][1]];
meetPointArr = meetPointArr.map(v=>v.join(""));
const answer = [];
for(let i = columnMin; i <= columnMax; i++){
let str = '';
for(let j = rowMin; j <= rowMax; j++){
if(meetPointArr.includes(i.toString()+j.toString())) str+="*";
else str+=".";
}
answer.push(str);
}
return answer.reverse();
}
์ ์ฝ๋๋ก ํ๋ฉด ๊ณ์ ํ ์คํธ์ผ์ด์ค 2๋ฒ๋ง ์๋๋ค. ์ด์ ๋ ์์ง๋ ๋ชป ์ฐพ์์ง๋ง
3์ค ํฌ๋ฌธ์ ์ฐ๊ธฐ๊ฐ ์ซ์ด์ includes ๋น๊ต๋ฅผ ์ํด ๋ชจ๋ ์ ์ ์ ์ ๊ต์ ๋ค์ ๋ชจ์๋์
30 Line์์ meetPointArr ๋ฐฐ์ด์ ๊ต์ ์ ๋ฌธ์์ด๋ก ๋ณํ ํ ํ
ex) meetPointArr => [[-4, -4], [-1, -2]] => ["-4-4", "-1-2"]
(i์ j๋ ๊ต์ ๋ค ์ค ๊ฐ์ฅ min๊ฐ~๊ฐ์ฅ max๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ ธ์์ ex)-4~4)
(37 Line) ๋ฐ๋ณต๋ฌธ์ i, j๋ ๋ฌธ์๋ก ๋ณํํ ํ ๋ํด meetPointArr์ includes๋์ด์๋ค๋ฉด str+="*"์ผ๋ก,
์์ผ๋ฉด str+="." ์ ๋ฆฌํ๋๋ฐ
(์ค๋ณต๋๋ ๊ต์ ์ ์์ด๋ ์ฒ๋ฆฌ๊ฐ ๋๋ค๊ณ ์๊ฐํจ)
(์ค์ ๋ก ์ค๋ณต๋๋ ๊ต์ ์ ์ฒซ๋ฒ์งธ meetPointArr pushํ๋ for๋ฌธ์์ ์ฒ๋ฆฌํด๋ ์๋๋ค.)
2๋ฒ๋ง ๊ณ์ ํต๊ณผ๊ฐ ์๋๋ค.
ํน์๋ ํ๊ณ meetPointArr๋ ๋ฌธ์๋ณํ์ด ์๋ ๊ทธ๋๋ก [[-4, -4], [-1, -2]] ์ ์งํ๊ณ
์๋ ์ฝ๋์ฒ๋ผ includes๊ฐ ์๋ forEach๋ฅผ ํตํด ๋น๊ตํ๋ ํต๊ณผ๊ฐ ๋๋ค.
function isInteger(num) {
return num % 1 === 0;
}
function intersectionPoint(arr1,arr2){
const denominator = (arr1[0]*arr2[1])-(arr1[1]*arr2[0]);
const x = ((arr1[1]*arr2[2])-(arr1[2]*arr2[1])) / denominator;
const y = ((arr1[2]*arr2[0])-(arr1[0]*arr2[2])) / denominator;
return denominator === 0 ? 0 : [y,x]
}
function solution(line) {
let meetPointArr = [];
for(let i = 0; i < line.length; i++){
for(let j = i+1; j < line.length; j++){
const value = intersectionPoint(line[i], line[j]);
if(isInteger(value[0]) && isInteger(value[1])){
meetPointArr.push(value);
}
}
}
meetPointArr.sort((a,b) => a[0]-b[0]);
const [columnMin, columnMax] = [meetPointArr[0][0],meetPointArr[meetPointArr.length-1][0]];
meetPointArr.sort((a,b) => a[1]-b[1]);
const [rowMin,rowMax] = [meetPointArr[0][1],meetPointArr[meetPointArr.length-1][1]];
const answer = [];
for(let i = columnMin; i <= columnMax; i++){
let str = '';
for(let j = rowMin; j <= rowMax; j++){
str += "."
meetPointArr.forEach(v => {
if(v[0] === i && v[1] === j){
str = str.slice(0, -1);
str += "*";
}
})
}
answer.push(str);
}
return answer.reverse();
}
2๋ฒ ์ผ์ด์ค๊ฐ ๋๋ฌด๋๋ฌด ๊ถ๊ธํ๋ค. ์ค๋ณต๋๋ ๊ต์ ์ด ์๋ ์๋ includes๋ ์๊ด์ด ์์ํ ๋ฐ..
๋ง์ฝ ์ค์ ์ฝ๋ฉํ ์คํธ ์ํ์ ์ด๊ฒ ๋์๋ค๋ฉด ์๊ฐ์ ๋ง์ด ์ก์๋จน์์ ๊ฒ ๊ฐ๋ค..
์ด์ ์์ธ๋ ๋ฌด์กฐ๊ฑด ์์ ์ ์๋ค๊ณ ์๊ฐํ๊ณ ํ์คํ๊ฒ ํ์ด์ผ๊ฒ ๋ค!