Skip to content

Conversation

@Moonjonghoo
Copy link
Collaborator

๐Ÿ“Œ ํ‘ผ ๋ฌธ์ œ

๋ฌธ์ œ์ด๋ฆ„ ๋ฌธ์ œ๋งํฌ
์ˆœ์„œ์Œ์˜ ๊ฐœ์ˆ˜ https://school.programmers.co.kr/learn/courses/30/lessons/120836
์ ์˜ ์œ„์น˜ ๊ตฌํ•˜๊ธฐ https://school.programmers.co.kr/learn/courses/30/lessons/120841
๋กœ๊ทธ์ธ ์„ฑ๊ณต? https://school.programmers.co.kr/learn/courses/30/lessons/120883
ํŠน์ดํ•œ ์ •๋ ฌ https://school.programmers.co.kr/learn/courses/30/lessons/120880
ํ”„๋กœ์„ธ์Šค https://school.programmers.co.kr/learn/courses/30/lessons/42587


๐Ÿ“ ๊ฐ„๋‹จํ•œ ํ’€์ด ๊ณผ์ •

์ˆœ์„œ์Œ์˜๊ฐœ์ˆ˜

์ฃผ์–ด์ง„ ์ •์ˆ˜ n์˜ ์•ฝ์ˆ˜ ์ค‘์—์„œ (a, b) ํ˜•ํƒœ๋กœ ๊ณฑํ–ˆ์„ ๋•Œ a * b = n์ด ๋˜๋Š” ๋ชจ๋“  ์ˆœ์„œ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

function solution(n) {
  let count = 0;

  for (let i = 1; i <= Math.sqrt(n); i++) {
    if (n % i === 0) {
      count++; // i๊ฐ€ ์•ฝ์ˆ˜
      if (i !== n / i) {
        count++; // i๊ฐ€ n์˜ ์ œ๊ณฑ๊ทผ์ด ์•„๋‹ˆ๋ฉด ๋‚˜๋จธ์ง€ ์ง๋„ ์ถ”๊ฐ€
      }
    }
  }

  return count;
}

ํ•ต์‹ฌ ์•„์ด๋””์–ด
1๋ถ€ํ„ฐ โˆšn๊นŒ์ง€์˜ ์ˆซ์ž๋งŒ ํƒ์ƒ‰ํ•˜์—ฌ ์•ฝ์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ณ , ๊ฐ ์•ฝ์ˆ˜์— ๋Œ€ํ•ด (a, b) ์Œ์„ ์ฐพ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐ


์ ์˜ ์œ„์น˜ ๊ตฌํ•˜๊ธฐ

function solution(dot) {
  const [x, y] = dot;

  if (x > 0 && y > 0) return 1; // 1์‚ฌ๋ถ„๋ฉด
  if (x < 0 && y > 0) return 2; // 2์‚ฌ๋ถ„๋ฉด
  if (x < 0 && y < 0) return 3; // 3์‚ฌ๋ถ„๋ฉด
  if (x > 0 && y < 0) return 4; // 4์‚ฌ๋ถ„๋ฉด
}

ํ•ต์‹ฌ ์•„์ด๋””์–ด
x, y์˜ ๋ถ€ํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ๋ถ„๋ฉด์„ ๊ฒฐ์ •
๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋ถ„๊ธฐํ•˜์—ฌ ์‚ฌ๋ถ„๋ฉด ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.


๋กœ๊ทธ์ธ ์„ฑ๊ณต?

function solution(id_pw, db) {
  const [id, pw] = id_pw;

  for (const [dbId, dbPw] of db) {
    if (id === dbId) {
      return pw === dbPw ? "login" : "wrong pw";
    }
  }

  return "fail";
}


ํŠน์ดํ•œ ์ •๋ ฌ

function solution(numlist, n) {
  return numlist.sort((a, b) => {
    const diffA = Math.abs(a - n);
    const diffB = Math.abs(b - n);

    if (diffA === diffB) {
      return b - a; // ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์œผ๋ฉด ํฐ ์ˆซ์ž๊ฐ€ ์šฐ์„ 
    }
    return diffA - diffB; // ๊ฑฐ๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
  });
}

ํ•ต์‹ฌ ์•„์ด๋””์–ด
์ ˆ๋Œ€๊ฐ’ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ: ๊ฐ ์ˆซ์ž์™€ ๊ธฐ์ค€๊ฐ’ n ๊ฐ„์˜ ๊ฑฐ๋ฆฌ(Math.abs(a - n))๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉ.
๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ˆซ์ž ํฌ๊ธฐ(b - a)๋กœ ์ •๋ ฌ.
๊ฑฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๊ฑฐ๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ(diffA - diffB) ์ •๋ ฌ.


ํ”„๋กœ์„ธ์Šค

function solution(priorities, location) {
  let order = 0;

  while (priorities.length > 0) {
    const current = priorities.shift();
    if (priorities.some((priority) => priority > current)) {
      priorities.push(current);
      location = location === 0 ? priorities.length - 1 : location - 1;
    } else {
      order++;
      if (location === 0) {
        return order; // ๋ชฉํ‘œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ ์ˆœ์„œ๋ฅผ ๋ฐ˜ํ™˜
      }
      location--;
    }
  }
}

** ํ•ต์‹ฌ ์•„์ด๋””์–ด **
priorities ๋ฐฐ์—ด์„ ํ์ฒ˜๋Ÿผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ๊ณ , some() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ตฌ๋ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „์ฒด์ ์œผ๋กœ ๋„ˆ๋ฌด ๊น”๋”ํ•œ ์ฝ”๋“œ์˜€์Šต๋‹ˆ๋‹ค!! ์ž˜๋ดค์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.sqrt(n) ์ƒ๊ฐ๋„ ๋ชปํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ„๊ฒฐํ•ด์„œ ์ข‹์€๊ฑฐ ๊ฐ™์•„์š”!

@JooKangsan JooKangsan merged commit 2b5ed8e into main Jan 23, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants