๋ฌธ์
5๋ช ์ ํ์์ด ์ฃผ์ด์ง ๋ ๊ฐ ํ์์ด ๋ฐ์ ์ ์์ ํ๊ท ์ ๊ตฌํ์ฌ ๊ธฐ์ค์ ๋ฐ๋ผ ํ์ ์ ๋ถ์ฌ
์กฐ๊ฑด : ์๊ธฐ์์ ์ ํ๊ฐํ ์ ์๊ฐ ์ ์ผํ ์ต๊ณ ์ ๋๋ ์ ์ผํ ์ต์ ์ ์ด๋ผ๋ฉด, ๊ทธ ์ ์๋ ์ ์ธํ๊ณ ํ๊ท ์ ๊ตฌํ๋ค.
ํ์ด
ํ๊ท ์ ๋จผ์ ๊ตฌํ๊ณ , ํ์ ์ ๊ตฌํ๋ค. ๊ฐ ํ์ ํ๊ท ์ ๊ตฌํด์ผ ํ ์ ์๊ฐ ๋ค์ด์๋ค.
ํต๊ณผํ์ง๋ชปํ ์ฝ๋
const solution = (scores) => {
let sum = 0;
let sumArr = [];
let scoreArr = [];
let studentScoreArr = [];
let len;
let answer = '';
// ๋ฐฐ์ด ๊ฐ๊ณต
for (let i = 0; i < scores.length; i++) {
studentScoreArr = [];
for (let j = 0; j < scores.length; j++) {
studentScoreArr.push(scores[j][i]);
}
scoreArr.push(studentScoreArr);
}
// ์ ์ ๊ฐ๊ณต
for (let i = 0; i < scores.length; i++) {
sum = 0;
len = scoreArr[i].length;
for (let j = 0; j < scores.length; j++) {
let findIdx = scoreArr.indexOf(scoreArr[i]);
let findCurrentIdx = scoreArr[i].indexOf(scoreArr[i][j]);
if (findIdx === findCurrentIdx) {
const myScore = scoreArr[i][j];
const dubCheck = isDuplicate(scoreArr[i]);
if (dubCheck) {
if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
let myIdx = scoreArr[i].indexOf(myScore);
console.log(myIdx);
scoreArr[i].splice(myIdx, 1, 0);
len--;
} else {
sum += scoreArr[i][j];
}
} else if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
let myIdx = scoreArr[i].indexOf(myScore);
scoreArr[i].splice(myIdx, 1, 0);
len--;
} else {
sum += scoreArr[i][j]
}
} else {
sum += scoreArr[i][j];
}
}
console.log(len);
sumArr.push(sum / len);
}
// ํ์ ๊ณ์ฐ
for (let i = 0; i < sumArr.length; i++) {
if (sumArr[i] >= 90) {
answer += 'A';
} else if (90 > sumArr[i] && sumArr[i] >= 80) {
answer += 'B';
} else if (80 > sumArr[i] && sumArr[i] >= 70) {
answer += 'C';
} else if (70 > sumArr[i] && sumArr[i] >= 50) {
answer += 'D';
} else {
answer += 'F';
}
}
return answer;
}
const isDuplicate = (arr) => {
const isDup = arr.some(function (x) {
return arr.indexOf(x) !== arr.lastIndexOf(x);
});
return isDup;
}
๊ธฐ๋ณธ ํ ์คํธ๋ ํต๊ณผํ๋๋ฐ ํ ์คํธ ์ผ์ด์ค 3๊ฐ๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ค.
ํต๊ณผํ ์ฝ๋
const solution = (scores) => {
let answer = '';
for (let i = 0; i < scores.length; i++) {
let myScore = scores[i][i];
let count = scores.length;
let min = 101;
let max = -1;
let sum = 0;
let flag = true;
for (let j = 0; j < scores.length; j++) {
let score = scores[j][i];
if (i !== j && myScore === score) { // ๋ด์ ์๊ฐ ์๋๊ณ ๋์ผํ ์ ์๊ฐ ์๋ ๋
flag = false;
}
min = Math.min(min, score);
max = Math.max(max, score);
sum += score;
}
if (flag && (min === myScore || max === myScore)) { // ๋ด ์ ์์ผ ๋
count--;
sum -= myScore;
}
answer += findGrade(sum / count);
}
return answer;
}
const findGrade = (score) => {
if (score >= 90) {
return 'A';
} else if (90 > score && score >= 80) {
return 'B';
} else if (80 > score && score >= 70) {
return 'C';
} else if (70 > score && score >= 50) {
return 'D';
} else {
return 'F';
}
}
'๐กAlgorithm > ๋ฌธ์ ํ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค | ์์ฃผํ์ง ๋ชปํ ์ ์(ํด์) (0) | 2021.09.25 |
---|---|
JS์๊ณ ๋ฆฌ์ฆ | ์๋๊ทธ๋จ์ด ๋๋ ๋ถ๋ถ๋ฌธ์์ด์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2021.09.24 |
ํ๋ก๊ทธ๋๋จธ์ค(Level 1) - ์ซ์๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2021.09.18 |
JS์๊ณ ๋ฆฌ์ฆ | ํ๋ก๊ทธ๋๋จธ์ค(Level 1) - ํฐ์ผ๋ชฌ (0) | 2021.09.17 |
JS์๊ณ ๋ฆฌ์ฆ | ํ๋ก๊ทธ๋๋จธ์ค(Level 1) - ์์๋ํ๊ธฐ (0) | 2021.09.17 |