๐กAlgorithm/๋ฌธ์ ํ๊ธฐ
ํ๋ก๊ทธ๋๋จธ์ค | ์์ฃผํ์ง ๋ชปํ ์ ์(ํด์)
์ง์์
2021. 9. 25. 12:14
1. ๋ฌธ์
๋จ ํ๋ช ์ ์ ์๋ฅผ ์ ์ธํ๊ณ ๋ ๋ชจ๋ ์ ์๊ฐ ๋ง๋ผํค์ ์์ฃผํ๋ค.
์ฐธ์ฌ ์ ์์ ์ด๋ฆ์ด ๋ด๊ธด ๋ฐฐ์ด participant
์์ฃผํ ์ ์์ ์ด๋ฆ์ด ๋ด๊ธด ๋ฐฐ์ด completion
์ฐธ๊ฐ์ ์ค์๋ ๋๋ช ์ด์ธ์ด ์์ ์๋ ์๋ค.
์ ๋ ฅ์์ | participant ["leo", "kiki", "eden"], completion ["eden", "kiki"]
์ถ๋ ฅ์์ | "leo"
2. ํด์ ์๊ณ ๋ฆฌ์ฆ ์๊ฐํ์ง ์๊ณ ํ์๋ ์ฝ๋
const solution = (participant, completion) => {
participant.sort();
completion.sort();
for (let i = 0; i < participant.length; i++) {
if (participant[i] !== completion[i])
return participant[i];
}
}
const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));
3. ํด์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํผ ์ฝ๋
const solution = (participant, completion) => {
let answer = '';
let map1 = new Map();
let map2 = new Map();
for (let x of participant) {
if (map1.has(x)) { map1.set(x, map1.get(x) + 1) }
else { map1.set(x, 1) }
};
for (let x of completion) {
if (map2.has(x)) { map2.set(x, map2.get(x) + 1) }
else { map2.set(x, 1) }
};
for (let [key, value] of map1) {
if (!map2.has(key) || map2.get(key) !== value) {
answer = key;
}
}
return answer;
}
const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));
ํด์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํธ๋ ๊ฒ ํจ์จ์ฑ์ด ๋ ์ข๋ค.