🥝 상하좌우

입력

5
R R R U D D

◼◼◼ Solution ◼◼◼

  1. L, R, U, D 에 따른 이동 방향을 미리 배열에 만들어 놓는다.
  const dx = [0, 0, -1, 1];
  const dy = [-1, 1, 0, 0];
  const moveTypes = ["L", "R", "U", "D"];
  1. 이동 계획을 하나씩 확인한다.
  2. 이동한다면 그 때의 x와 y 좌표를 구한다.
  3. 공간을 벗어나는 경우 무시한다.
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
  1. 벗어나지 않는 경우 이동한 값으로 x, y 좌표를 바꾼다.
    ...

문제 풀이

// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
  input.push(line);
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  const dx = [0, 0, -1, 1];
  const dy = [-1, 1, 0, 0];
  const moveTypes = ["L", "R", "U", "D"];
  let n = parseInt(input[0]);
  let arr = input[1].split(" ");
  let x = (y = 1);
  let nx = (ny = 0);
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < moveTypes.length; j++) {
      if (arr[i] == moveTypes[j]) {
        nx = x + dx[j];
        ny = y + dy[j];
      }
      if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
      [x, y] = [nx, ny];
    }
  }
  console.log(x, y);
};

구현할 때 개선점

  1. number를 더하고자 할 때는 선언할 때 number형으로 해놓자.
let nx;
nx + 3;
// nx == NaN
let nx = 0;
nx + 3;
// nx == 3;
  1. 위 솔루션처럼 이동 방향을 배열에 넣어놓자.
    내 풀이
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
  input.push(line);
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  let n = parseInt(input[0]);
  let arr = input[1].split(" ");
  let [x, y] = [1, 1];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == "R") {
      let nx = x + 1;
      if (nx <= n) [x, y] = [nx, y];
    } else if (arr[i] == "L") {
      let nx = x - 1;
      if (nx > 0) [x, y] = [nx, y];
    } else if (arr[i] == "U") {
      let ny = y - 1;
      if (ny > 0) [x, y] = [x, ny];
    } else if (arr[i] == "D") {
      let ny = y + 1;
      if (ny <= n) [x, y] = [x, ny];
    }
  }
  console.log(x, y);
};
  • 한 줄 평
    More Info: 벨로그 링크

🥝 시각

◼◼◼ Solution ◼◼◼

확인해야 할 전체 데이터의 개수가 100만 개 이하일 때 완전 탐색을 사용하면 적절하다.
1. 숫자가 아니라 문자열로 받기
2.
3.
4.
...

문제 풀이

// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = 0;
rl.on("line", function (line) {
  input += line;
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  let count = 0;
  for (let i = 0; i <= input; i++) {
    for (let j = 0; j < 60; j++) {
      for (let k = 0; k < 60; k++) {
        //빈 문자열""에 더하지 않고 toString()을 사용한다.
        //let time = "";
        //time += i + j + k;
        let time = i.toString() + j.toString() + k.toString();
        if (time.includes("3")) count++;
      }
    }
  }
  console.log(count);
};

구현할 때 개선점

string형태로 수를 더하고 싶다면 toString()을 쓰자.

let time = i.toString() + j.toString() + k.toString();

""에 더한다면, 1, 2, 3을 다 합한 값인 6을 string형태로 바꿔준다.

let time = "";
time += 1 + 2 + 3;
// 6
// typeof time은 string
  • 한 줄 평
    More Info: 벨로그 링크

🥝 왕실의 나이트

◼◼◼ Solution ◼◼◼

  1. ...

문제 풀이

// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = "";
rl.on("line", function (line) {
  input += line;
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  const steps = [
    [-2, -1],
    [-1, -2],
    [1, -2],
    [2, -1],
    [2, 1],
    [1, 2],
    [-1, 2],
    [-2, 1],
  ];
  const small = Array.from({ length: 8 }, (v, i) =>
    String.fromCharCode(i + 97)
  );
  let answer = 0;
  let pointArr = input.split("");
  let [x, y] = [parseInt(pointArr[1]), small.indexOf(pointArr[0]) + 1];
  for (let stepType of steps) {
    let nx = x + stepType[0];
    let ny = y + stepType[1];
    if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
      answer++;
    }
  }
  console.log(answer);
};

구현할 때 개선점

  • 한 줄 평
    More Info: 벨로그 링크

🥝 게임 개발

입력

4 4
1 1 0
1 1 1 1
1 0 0 1
1 1 0 1
1 1 1 1

◼◼◼ Solution ◼◼◼

  1. ...

문제 풀이

// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
  input.push(line);
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  let arr = [];
  console.log(input);
  for (let x of input) {
    arr.push(x.split(" "));
  }
  for (let x of arr) {
    for (let i = 0; i < x.length; i++) {
      x[i] = parseInt(x[i]);
    }
  }
  const firstLine = arr.shift();
  const secondLine = arr.shift();
  let [n, m] = [firstLine[0], firstLine[1]];
  let [x, y] = [secondLine[0], secondLine[1]];
  let direction = secondLine[2];
  let ch = Array.from(Array(n), () => Array(m).fill(0));
  let dx = [-1, 0, 1, 0];
  let dy = [0, 1, 0, -1];
  const turnLeft = () => {
      
  }
};

구현할 때 개선점

  • 한 줄 평
    More Info: 벨로그 링크

🥝 문자열 재정렬

입력

K1KA5CB7
AJKDLSI412K4JSJ9D

◼◼◼ Solution ◼◼◼

문제 풀이

// Run by Node.js
const { count } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let inputStr = "";
rl.on("line", function (line) {
  inputStr += line;
}).on("close", function () {
  solution();
  process.exit();
});

const solution = () => {
  let arr = inputStr.split("");
  let num = 0;
  let str = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].charCodeAt() >= 65) {
      str.push(arr[i]);
    } else num = num + parseInt(arr[i]);
  }
  str.sort();
  let answer = str.join("") + num;
  console.log(answer);
};

구현할 때 개선점

  • 한 줄 평
    More Info: 벨로그 링크

🥝 문자열 압축

◼◼◼ Solution ◼◼◼

  1. ...

문제 풀이

// Run by Node.js
const { count } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let s = "";
rl.on("line", function (line) {
  s += line;
}).on("close", function () {
  solution();
  process.exit();
});

function solution() {
  let n = s.length;
  let answer = s.length;
  for (let step = 1; step <= parseInt(n / 2); step++) {
    let compressed = "";
    let prev = s.substr(0, step);
    let cnt = 1;
    for (let j = step; j < n; j += step) {
      if (prev == s.substr(j, step)) cnt++;
      else {
        compressed += cnt >= 2 ? String(cnt) + prev : prev;
        prev = s.substr(j, step);
        cnt = 1;
      }
    }
    compressed += cnt >= 2 ? String(cnt) + prev : prev;
    answer = Math.min(answer, compressed.length);
  }
  console.log(answer);
  //return answer;
}

구현할 때 개선점

substr(잘라낼startIndex, 잘라낼길이);
substring(잘라낼startIndex, 잘라낼lastIndex);

  • 한 줄 평
    More Info: 벨로그 링크

🥝 번호, 문제 이름

◼◼◼ Solution ◼◼◼

  1. ...

문제 풀이

전체 코드

구현할 때 개선점

  • 한 줄 평
    More Info: 벨로그 링크

0개의 댓글