์•Œ๊ณ ๋ฆฌ์ฆ˜ & ๋ฌธ์ œํ’€์ด/Programmers-1

๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

YunCow 2022. 9. 5. 20:44

๋ฌธ์ œ ๋‚ด์šฉ

์ด๐Ÿ™Ž๐Ÿป‍โ™‚๏ธ

function solution(s) {
  const convertedArr = Array.from(s);
  const result = convertedArr.map((v) => Number(v));

  if (s.length === 4 || s.length === 6) {
    return result.includes(NaN) ? false : true;
  }
  return false;
}
/* 
(ํ’€์ด)
๋ฌธ์ž์—ด s๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ๊ณ  map๋ฉ”์†Œ๋“œ๋กœ ๊ฐ์ž Numberํƒ€์ž…์œผ๋กœ ๋ณ€ํ˜•.
๋ฌธ์ž๋‚˜ -, ์†Œ์ˆ˜ ๋“ฑ์€ NaN ์œผ๋กœ ๋‚˜์˜ค๋ฏ€๋กœ ์š”์†Œ๊ฐฏ์ˆ˜๊ฐ€ 4,6 ์ธ ๋ฐฐ์—ด์— NaN๊ฐ€ ์žˆ๋‹ค๋ฉด false ์—†์œผ๋ฉด true
*/
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 11๋ฒˆ์ด ๊ณ„์† ์‹คํŒจํ–ˆ๋‹ค. ๋ฌธ์ œ๋Š” 3e6 ์€ 3 * 10 ^ 6์„ ์˜๋ฏธํ•œ๋‹ค. Number๋กœ ํ˜• ๋ณ€ํ™˜ํ•  ๋•Œ ์ด ์ผ€์ด์Šค ๋•Œ๋ฌธ์— ๋„˜์–ด๊ฐ€์งˆ ๋ชปํ•˜๊ณ  ๊ณ ์ƒํ•˜๋‹ค๊ฐ€ ํ’€์—ˆ๋‹ค.

 

ํ”ผ๋“œ๋ฐฑ ํ’€์ด

function solution(s) {
  const convertedArr = Array.from(s);
  const result = convertedArr.filter((v) => isNaN(Number(v)));
  if (s.length === 4 || s.length === 6) {
    return result.length > 0 ? false : true   
  }
  return false;
}
  • ๋‚ด ํ’€์ด๋Š” map(0(N))์„ ํ•œ๋ฒˆ, ๊ทธ๋ฆฌ๊ณ  if๋ฌธ์—์„œ includes(์ตœ์•…์˜ ๊ฒฝ์šฐ0(N))์„ ๋Œ๋ฆฌ๋Š”๋ฐ (์ด (O(2N)) ํŒ€ ๋ฆฌ๋”๋‹˜์€ ๋‚ด ํ’€์ด๋ฅผ ๋ณด๊ณ  isNaN๊ณผ filter๋ฅผ ์ด์šฉํ•ด O(N)์œผ๋กœ ํ’€์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ์…จ๋‹ค. ๐Ÿ‘

 

Point .

  1. ์ดํ„ฐ๋Ÿฌ๋ธ”, ์ดํ„ฐ๋ ˆ์ดํ„ฐ ๊ฐœ๋… ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ
  2. isNaN ์ •๋ฆฌํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ (isNaN๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๊ฐ’์ด NaN์ธ์ง€ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฑฐ์˜€๋‹ค. ๐Ÿ‘€)