๊ด€๋ฆฌ ๋ฉ”๋‰ด

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 1157 ๋‹จ์–ด๊ณต๋ถ€ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 1157 ๋‹จ์–ด๊ณต๋ถ€

minjuuu 2021. 10. 16. 16:59
728x90

๐Ÿฑ‍๐Ÿ‘“ 1. ๋ฌธ์ œ : 1157 

https://www.acmicpc.net/problem/1157


โšก 2. ์กฐ๊ฑด ๋ฐ ํ’€์ด ๋ฐฉ์‹?

// 1. ๋ฌธ์ž๋“ค์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ.
// 2. a-z๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑ.

// 3. ์•ŒํŒŒ๋ฒณ๋“ค์„ charCodeAt()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  a(65)๋งŒํผ์„ ๋บ€,
// ๊ทธ ์ˆซ์ž๋ฅผ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋กœ ์žก๊ณ , 1์„ ์ถ”๊ฐ€ ํ•ด์คŒ.
// ex) ์•ŒํŒŒ๋ฒณ์ด a๋ผ๋ฉด 65 - 65 = 0 ์ด๋ฏ€๋กœ, ์ธ๋ฑ์Šค 0์— 1์ถ”๊ฐ€.

// 4. indexOf()๋กœ ๋ฐฐ์—ด์˜ index max ๊ฐ’์„ ๊ตฌํ•˜๊ณ ,
// 5. for๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋˜ max ๊ฐ’์„ Math.max()๋กœ ๊ตฌํ•ด์„œ,
// 6. ๋‘˜์˜ max ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ, ์ค‘๋ณต์ด ์žˆ์„ ๊ฒฝ์šฐ "?"์„ ์ถœ๋ ฅ
// 7. ์ค‘๋ณต์ด ์—†๋‹ค๋ฉด, fromCharCode()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜.

๐Ÿ”ฅ 3-1. ์ฝ”๋“œ ๋ฐ ํ’€์ด

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().toUpperCase();

// a-z๊นŒ์ง€์˜ ๋ฐฐ์—ด.
const array = new Array(26).fill(0);

// ํ•ด๋‹น ๋‹จ์–ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋œ ์•ŒํŒŒ๋ฒณ๋“ค์„ ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋กœ ์ ์šฉ.
for (let i = 0; i < input.length; i++) {
  array[input.charCodeAt(i) - 65] ++;
}

// console.log(array)
// [
//   3, 1, 0, 0, 0, 0, 0, 0,
//   0, 0, 0, 0, 0, 0, 0, 0,
//   0, 0, 0, 0, 0, 0, 0, 0,
//   0, 0
// ]

// ํ•ด๋‹น ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’ : [3]
const max = Math.max(...array);

// ์ตœ๋Œ“๊ฐ’์˜ ์ธ๋ฑ์Šค : 0
const index = array.indexOf(max);

// max ์™€ index๊ฐ€ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์ผ ๊ฒฝ์šฐ, true๋กœ ๋ณ€ํ™˜์‹œ์ผœ์„œ "?" ์ถœ๋ ฅ
let isSame = false;

for (let j = 0; j < 26; j++) {
  if (array[j] === max && index != j) {
    isSame = true;
    break;
  }
}
console.log(isSame ? "?" : String.fromCharCode(index + 65));


๐Ÿ”ฅ 3-2. ์ฝ”๋“œ ๋ฐ ํ’€์ด

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().toUpperCase();

// a-z๊นŒ์ง€์˜ ๋ฐฐ์—ด.
const array = new Array(26).fill(0);

// ํ•ด๋‹น ๋‹จ์–ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋œ ์•ŒํŒŒ๋ฒณ๋“ค์„ ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋กœ ์ ์šฉ.
for (let i = 0; i < input.length; i++) {
  array[input.charCodeAt(i) - 65] ++;
}

// console.log(array)
// [
//   3, 1, 0, 0, 0, 0, 0, 0,
//   0, 0, 0, 0, 0, 0, 0, 0,
//   0, 0, 0, 0, 0, 0, 0, 0,
//   0, 0
// ]

// ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์„ filter() ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ.
// console.log(array.filter(val => val)) : [3, 1]
// console.log(array.filter(val => val === max)) : [3]
// console.log(array.filter(val => val === max).length) : 1

// ํ•ด๋‹น ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’ : [3] ์˜ ๊ธธ์ด๊ฐ€ 1์ผ ๊ฒฝ์šฐ, ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅ
// max์˜ ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ์—๋Š”, ์ตœ๋Œ“๊ฐ’์ด 2๊ฐœ๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ, "?" ์ถœ๋ ฅ

const max = Math.max(...array);
let result2 = array.filter(val => val === max).length === 1 ?
String.fromCharCode(array.findIndex(val => val === max) + 65) : '?';
console.log(result2);

728x90