๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ (Linked List)

์ดํ›„์— ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ํŠธ๋ฆฌ, ๊ทธ๋ž˜ํ”„ ๊ฐ™์€ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๋ฐ‘๋ฐ”ํƒ•์ด ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. 

๋ฐฐ์—ด์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ์ด๋‹ค. 

๋ฐฐ์—ด์—์„œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์  ๊ณต๊ฐ„์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ˆœ์„œ๋Œ€๋กœ ์‘์ง‘๋˜์–ด ์ €์žฅ๋œ๋‹ค๋ฉด, ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ํฉ์–ด์ ธ์„œ ์ €์žฅ๋œ๋‹ค. 

 

์„ ํ˜• ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ์‚ฝ์ž…, ์‚ญ์ œ ์—ฐ์‚ฐ์ด ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์„ ๊ฒฝ์šฐ, ์›์†Œ๋“ค์˜ ์ด๋™์ž‘์—…๋„ ๊ทธ์— ๋”ฐ๋ผ ๋น„๋ก€ํ•œ๋‹ค. ์ด๋Š” ์„ฑ๋Šฅ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์ด ๊ฐ–๊ณ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์˜ ๋น„ํšจ์œจ์„ฑ ๋ฌธ์ œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ ์žˆ๋‹ค. 

 

ํŠน์ง•

data์˜ ์ž…๋ ฅ๊ณผ ์‚ญ์ œ๊ฐ€ ์ž์œ ๋กญ๋‹ค. ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ, ์„ธ๋กœ์šด ๋ฐ์ดํ„ฐ๋Š” ๋“ค์–ด๊ฐ€๊ณ ์žํ•˜๋Š”  ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ์•ž node์˜ tail์— ์ž์‹ ์˜ data๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ , ์ž์‹ ์˜ tail์„ ํ›„์œ„ node์™€ ์—ฐ๊ฒฐํ•œ๋‹ค. 

 

1. ์š”์†Œ์˜ ์‚ฝ์ž… ์‚ญ์ œ์‹œ์— ์šฉ์ดํ•˜๋‹ค.

2. ํŠน์ • ์š”์†Œ ๊ฒ€์ƒ‰์— ๋น„ํšจ์œจ์ ์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ Linked List์—์„œ๋Š” ์ค‘๊ฐ„์— ์‚ฝ์ž…ํ•˜๋”๋ผ๋„ ๋ฐ”๋กœ ์˜† ๊ณต๊ฐ„์— ๋„ฃ์„ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์•„๋ฌด๋ฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์€ ๋‹ค์Œ ์ด์ „ ์š”์†Œ๊ฐ€ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก, ๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋งํฌ์˜ ์—ฐ๊ฒฐ๋งŒ ์กฐ์ •ํ•ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ์š”์†Œ๋ฅผ ์ค‘๊ฐ„์— ๋ผ์›Œ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฐฐ์—ด์—์„œ ์š”์†Œ๋ฅผ ์ค‘๊ฐ„์— ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์€ O(n)์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๊ณ  Linked List์—์„œ๋Š” O(1)์ด ์†Œ์š”๋œ๋‹ค.

 

 

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์˜ˆ์‹œ๋กœ ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ next๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

 

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ์—ฐ์‚ฐ

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์—์„œ ์ถ”๊ฐ€, ์‚ญ์ œ, ์‚ฝ์ž… ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํƒ์ƒ‰ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. ์ถœ๋ฐœ ์ง€์ ๊ณผ ๋ ์ง€์ ์ด ์žˆ์–ด์•ผํ•˜๊ณ , head ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์ด ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰์„ ํ•œ๋‹ค. 

const linkedList =  () => {
	this.head = null; // ์ฒ˜์Œ์— ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ null๋กœ ์ดˆ๊ธฐํ™”
    this.length = 0; // ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด
}

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…, ์‚ญ์ œ๋Š” 2๊ฐ€์ง€์˜ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

 

1. ํƒ์ƒ‰

2. ์—ฐ์‚ฐ

 

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์— A → B → C ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๊ณ , ์šฐ๋ฆฌ๋Š” D๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ B์™€ C ์‚ฌ์ด์— ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 

B์™€ D ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. B๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๊ฐ’(link)๊ฐ€ D๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ƒˆ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌ์ผœ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋™์‹œ์— D๊ฐ€ ๋‹ค์Œ์œผ๋กœ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๊ฐ’(link)๋ฅผ C๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ๋” ํ•ด์•ผํ•œ๋‹ค. 

 

1) ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€

'head๊ฐ€ null ์ผ ๋•Œ / head๊ฐ€ null์ด ์•„๋‹ ๋•Œ'๋ฅผ ๋‚˜๋ˆ„์–ด์„œ ๋„ฃ์–ด์ค€๋‹ค. 

linkedList.add = (data) => {
	const newNode = new Node(data);
    if(this.head === null) {
    	this.head = newNode;
        this.length++;
    } else {
    	let currentNode = this.head;
        while(currentNode.link !== null) {
        	currentNode = currentNode.link;
        }
        currentNode.link = newNode;
        this.length++;
    }
}

1-1) ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (์ค‘๊ฐ„๋…ธ๋“œ์ž๋ฆฌ)

LinkedList.prototype.insertMiddleNode = (pre, data) => {
	const newNode = new Node(data);
    let currentNode = this.head;
    
    while(currentNode.data !== pre){
    	currentNode = currentNode.link;
    }
    
    let temp = currentNode.link;
    newNode.link = temp;
    currentNode.link = newNode;
    this.length++;
}

2-1) ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ (๋งˆ์ง€๋ง‰ ๋…ธ๋“œ)

LinkedList.prototype.remove = () => {
	let currentNode = this.head;
    while(currentNode.link.link !== null) {
    	currentNode = currentNode.link;
    }
    currentNode.link = null;
    thiks.length--;
}

2-2) ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ (์ค‘๊ฐ„ ๋…ธ๋“œ)

if(currentNode.data !== data) {
	while(currentNode.link.data !== data) {
    	currentNode =  currentNode.link;
    }
    temp = currentNode.link.link;
    currentNode.link.link = null;
    currentNode.link = temp;
} else {
	temp = currentNode.link;
    currentNode.link = null;
    this.head = temp;
}
this.length--;

 

 

 

 

์ฐธ๊ณ 

1. https://velog.io/@raram2/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-%EC%97%B0%EA%B2%B0-%EB%A6%AC%EC%8A%A4%ED%8A%B8%EC%9D%98-%EA%B0%9C%EB%85%90%EA%B3%BC-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC-%ED%86%B5%ED%95%9C-%EA%B5%AC%ED%98%84-Linked-List

2. https://taesung1993.tistory.com/10

 

์Šคํƒ (stack)

Last In Firtst Out ๋จผ์ € ๋“ค์–ด๊ฐ„ ์ž๋ฃŒ๊ฐ€ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ํ›„์ž…์„ ์ถœ ์ž๋ฃŒ๊ตฌ์กฐ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ stack์€ ๋ฐฐ์—ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” push() ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” pop() ์˜ ์‚ฌ์šฉ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์Šคํƒ์˜ ์‹œ๊ฐ„๋ณต์žก๋„

์‚ฝ์ž…  O(1)
์‚ญ์ œ O(1)
ํƒ์ƒ‰ O(n)

ํƒ์ƒ‰์„ ํ•  ๋•Œ n๊ฐœ ์š”์†Œ๋ฅผ ๋Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด๋‹ค. n๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œ pop()์„ n๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฉด n๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. pop()์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1) ์ด๋ฏ€๋กœ n๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด๋‹ค.

 

 


์˜ˆ์‹œ ๋ฌธ์ œ 1

๊ด„ํ˜ธ์˜ ์ง์ด ๋งž์œผ๋ฉด "YES"๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , ๋งž์ง€ ์•Š์œผ๋ฉด "NO"๋ฅผ ๋ฆฌํ„ดํ•ด๋ผ

 

const solution = (str) => {
  // ๋ฐฐ์—ด์„ for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ (์ด๋ฉด push )์ด๋ฉด popํ•ด์„œ ํ•˜๋‚˜๋„ ์•ˆ๋‚จ์•„์žˆ์œผ๋ฉด yes ๋ฆฌํ„ด, ํ•˜๋‚˜๋ผ๋„ ๋‚จ์•„์žˆ์œผ๋ฉด no ๋ฆฌํ„ด
  let answer = "YES";
  let newArr = [];

  for (let x of str) {
    if (x === '(') newArr.push(x);
    else {
      if (newArr.length === 0) {
        return "No";
      }
      newArr.pop(x);
    }
  }

  if (newArr.length === 0) {
    return answer;
  } else {
    return "NO";
  }
}

const str = "()()"
console.log(solution(str));

 

์˜ˆ์‹œ ๋ฌธ์ œ 2

์‡ ๋ง‰๋Œ€๊ธฐ๋Š” ์ž์‹ ๋ณด๋‹ค ๊ธด ์‡ ๋ง‰๋Œ€๊ธฐ ์œ„์—๋งŒ ๋†“์ผ ์ˆ˜ ์žˆ๋‹ค. ์‡ ๋ง‰๋Œ€๊ธฐ๋ฅผ ๋‹ค๋ฅธ ์‡ ๋ง‰๋Œ€๊ธฐ ์œ„ํ—ค ๋†“๋Š” ๊ฒฝ์šฐ ์™„์ „ํžˆ ํฌํ•จ๋˜๋„๋ก ๋†“๊ณ , ๋์ ์€ ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค.

 

๋ ˆ์ด์ €๋Š” ๊ด„ํ˜ธ์˜ ์Œ์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค =>  '( )'

์‡ ๋ง‰๋Œ€๊ธฐ์˜ ์™ผ์ชฝ ๋์€ ์—ฌ๋Š” ๊ด„ํ˜ธ '( ' ์ด๊ณ  ์˜ค๋ฅธ์ชฝ ๋์€ ๋‹ซํžŒ ๊ด„ํ˜ธ ')'๋กœ ํ‘œํ˜„๋œ๋‹ค.

 

์‡ ๋ง‰๋Œ€๊ธฐ๋Š” ๋ ˆ์ด์ €์— ์˜ํ•ด ๋ช‡ ๊ฐœ์˜ ์กฐ๊ฐ์œผ๋กœ ์ž˜๋ ค์ง„๋‹ค. 

์‡ ๋ง‰๋Œ€๊ธฐ์™€ ๋ ˆ์ด์ €์˜ ๋ฐฐ์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ด„ํ˜ธ ํ‘œํ˜„์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ž˜๋ ค์ง„ ์‡ ๋ง‰๋Œ€๊ธฐ ์กฐ๊ฐ์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ผ. 

 

์ž…๋ ฅ์˜ˆ์ œ

()(((()())(())()))(())

์ถœ๋ ฅ์˜ˆ์ œ

17

 

const solution = (str) => {
  let answer = 0;
  let stack = [];

  // ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ง‰๋Œ€๊ธฐ์˜ ์‹œ์ž‘์  ๋˜๋Š” ๋ ˆ์ด์ €์˜ ์‹œ์ž‘์ ์ด๋‹ค. -> stack์— ํ‘ธ์‹œ
  // ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ง‰๋Œ€๊ธฐ์˜ ๋‹ซ๋Š”์ ์ธ์ง€ ๋ ˆ์ด์ €์˜ ๋‹ซ๋Š”์ ์ธ์ง€ ํ™•์ธํ•ด์•ผํ•œ๋‹ค -> ๋ฐ”๋กœ ์•ž์„ ์‚ดํŽด๋ณธ๋‹ค

  for (let i = 0; i < str.length; i++) {
    if (str[i] === '(') { // str[i]๊ฐ€ ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ
      stack.push(str[i]);
    } else { // str[i]๊ฐ€ ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ
      stack.pop();
      if (str[i - 1] === '(') { // ๋ ˆ์ด์ €์ธ ๊ฒฝ์šฐ
        // ๋ง‰๋Œ€๊ธฐ๊ฐ€ ๋ช‡ ๊ฐœ๊ฐ€ ์ž˜๋ฆฌ๋Š” ์ง€ answer์— ์นด์šดํŒ… -> stack์˜ length ๋ˆ„์ 
        answer += stack.length;
      } else { // ๋ง‰๋Œ€๊ธฐ์˜ ๋์ธ ๊ฒฝ์šฐ
        // ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜๋Š” popํ•ด์•ผํ•œ๋‹ค.
        answer++;
      }
    }
  }
  return answer;
}

const str = "()(((()())(())()))(())";
console.log(solution(str));

 

 

 

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));

ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘ธ๋Š” ๊ฒŒ ํšจ์œจ์„ฑ์ด ๋” ์ข‹๋‹ค. 

ํ•ด์‹œ, ํˆฌํฌ์ธํ„ฐ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋Š” ๋ฌธ์ œ

 

1. ๋ฌธ์ œ

์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์—์„œ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ.

๋ถ€๋ถ„๋ฌธ์ž์—ด์€ ์—ฐ์†๋œ ๋ฌธ์ž์—ด์ด๋‹ค. 

์ž…๋ ฅ์˜ˆ์ œ | ๋ฌธ์ž์—ด bacaAacba, ๋ถ€๋ถ„ ๋ฌธ์ž์—ด abc

์ถœ๋ ฅ์˜ˆ์ œ | 3 

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

์ฃผ์–ด์ง„ ๋ถ€๋ถ„๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€(๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length-1)๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ”๊พผ๋‹ค.

ํฌ์ธํ„ฐ ์ง€์ ์„ ์ฐ์–ด๋†“๊ณ  for๋ฌธ์œผ๋กœ ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length๋งŒํผ์˜ ์ž๋ฆฌ๋ฅผ ๋น„๊ตํ•œ๋‹ค.

์•„๋‚˜๊ทธ๋žจ์ด ์•„๋‹ ๊ฒฝ์šฐ ํฌ์ธํ„ฐ๋ฅผ ๋‹ค์Œ ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚ค๊ณ ,

์•„๋‚˜๊ทธ๋žจ์ผ ๊ฒฝ์šฐ answer๋ฅผ ์นด์šดํŒ…ํ•˜๊ณ  ๋‹ค์Œ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚จ๋‹ค. 

 

 

3. ํ’€์ด๋ฐฉ๋ฒ•

1) answer์— ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ… ํ•ด์ฃผ๊ธฐ์œ„ํ•ด์„œ 0์œผ๋กœ ์„ธํŒ…ํ•œ๋‹ค. 

let answer = 0;

 

2) ์ฐพ์•„์•ผํ•˜๋Š” ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์–ด์ง„ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ์•ŒํŒŒ๋ฒณ๊ณผ ๊ฐ’์„ ๊ฐ์ฒดํ˜•ํƒœ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค. map ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

let map1 = new Map();
let map2 = new Map();

for (let x of t) {
	if (map2.has(x)) {
    	map2.set(x, map2.get(x) + 1);
    } else {
    	map2.set(x, 1);
    }
}

 

3) ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ธฐ๋ณธ ์„ธํŒ…์„ ํ•ด์ค€๋‹ค. 

let len = t.length -1;

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ์•ž์— 2๊ฐœ์˜ ๋ฌธ์ž๋งŒ ๊ฐ์ฒด๋กœ ์„ธํŒ…ํ•œ๋‹ค.

length๋ฅผ ๋ถ€๋ฌธ๋ถ„์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ํ•˜์ง€์•Š๊ณ  -1์„ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š”

๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๊ฐ€ 3์ผ๋•Œ, 2๋Š” ๊ณ ์ •์œผ๋กœ ๊ณ„์† ๋‘๊ณ  ํ•œ์ž๋ฆฌ๋งŒ ๋ฐ”๊พธ๋ฉด์„œ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

for (let i = 0; i < len; i++) {
	if (map1.has(s[i])) {
    	map1.set(s[i], map1.get(s[i] + 1);
    } else {
    	map1.set(s[i], 1);
    }
}

 

4) for๋ฌธ์œผ๋กœ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ

1๊ฐœ ์ถ”๊ฐ€ํ•ด์„œ 3๊ฐœ์”ฉ ๋น„๊ตํ•˜๊ณ (๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length) ์•„๋‚˜๊ทธ๋žจ์ด ์•„๋‹ ๊ฒฝ์šฐ ๋นผ์ฃผ๊ณ  ๋ฐ˜๋ณตํ•œ๋‹ค. 

๋น„๊ต ์ดํ›„์— ๋นผ์ฃผ๋Š” ์ž‘์—…์„ ํ•˜๊ธฐ์œ„ํ•ด์„œ ํˆฌ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ์ž‘์„ฑํ•ด์ค€๋‹ค.

let lt = 0;
rt๋Š” for ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ์„ ์–ธ
for (let rt = len; rt < s.length; rt++) {
    if (map1.has(s[rt])) {
      map1.set(s[rt], map1.get(s[rt]) + 1);
    } else {
      map1.set(s[rt], 1);
    }

	// ๋น„๊ต
    if (compareMaps(map1, map2)) {
      answer++;
    }
    
    // ๋น„๊ต ์ดํ›„์— ์™ผ์ชฝ ํฌ์ธํ„ฐ ๋นผ์ฃผ๋Š” ์ž‘์—… 
    map1.set(s[lt], map1.get(s[lt]) - 1); // ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค 1์ž‘๊ฒŒ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ์„ธํŒ…
    if (map1.get(s[lt]) === 0) {
      map1.delete(s[lt]);
    }
    lt++;
}

 

5) ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜ ์ž‘์„ฑ

๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ

key๊ฐ’์ด ๊ฐ™์€์ง€, value ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ

const compareMaps = (map1, map2) => {
	if (map1.size !== map2.size) {
    	return false;
    }
    for (let [key, value] of map1) {
    	if (!map2.has(key) || map2.get(key) !== value) {
        	return false;
        }
    }
    retrun true; // ๋ชจ๋‘ ์•„๋‹ˆ๋ฉด ๊ฐ™์€ ๊ฒƒ์œผ๋ฏ€๋กœ true ๋ฐ˜ํ™˜
}

 

 

4. ์ฝ”๋“œ

const solution = (s, t) => {
  // ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด ๊ฐœ์ˆ˜
  let answer = 0;

  let map1 = new Map(); // ๋ฌธ์ž์—ด์„ map ๊ฐ์ฒด๋กœ
  let map2 = new Map(); // ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ map ๊ฐ์ฒด๋กœ

  // ์ฐพ์•„์•ผํ•˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ map๊ฐ์ฒด๋กœ ๋งŒ๋“ฌ
  for (let x of t) {
    if (map2.has(x)) {
      map2.set(x, map2.get(x) + 1);
    } else {
      map2.set(x, 1);
    }
  }

  // ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํ•  ๋•Œ ๊ธธ์ด๊ฐ€ 3์ด๋ฉด 2๊ฐœ๋ฅผ ๋จผ์ € ์„ธํŒ…ํ•ด ๋†“๋Š” ๊ฒƒ
  let len = t.length - 1;

  for (let i = 0; i < len; i++) {
    if (map1.has(s[i])) {
      map1.set(s[i], map1.get(s[i]) + 1);
    } else {
      map1.set(s[i], 1);
    }
  }

  let lt = 0;

  // ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— rt๋Š” len๊ธธ์ด๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์„ธํŒ…
  // ์ถ”๊ฐ€ํ•ด์„œ 3๊ฐœ๋กœ ๋งž์ถฐ์ฃผ๊ณ 
  for (let rt = len; rt < s.length; rt++) {
    if (map1.has(s[rt])) {
      map1.set(s[rt], map1.get(s[rt]) + 1);
    } else {
      map1.set(s[rt], 1);
    }

    // 3๊ฐœ์ด๋‹ˆ๊นŒ ์ด์ œ ๋น„๊ต๊ฐ€๋Šฅ
    if (compareMaps(map1, map2)) {
      answer++;
    }

    // ๋น„๊ต ์ดํ›„์— ์™ผ์ชฝ ํฌ์ธํ„ฐ ๋นผ์ฃผ๋Š” ์ž‘์—… 
    map1.set(s[lt], map1.get(s[lt]) - 1); // ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค 1์ž‘๊ฒŒ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ์„ธํŒ…
    if (map1.get(s[lt]) === 0) {
      map1.delete(s[lt]);
    }
    lt++;
  }
  return answer;
}

const compareMaps = (map1, map2) => {
  if (map1.size !== map2.size) { // 1. ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ
    return false;
  }
  for (let [key, value] of map1) { // 2. key ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ
    if (!map2.has(key) || map2.get(key) !== value) { // 3. value ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ
      return false;
    }
  }
  return true;
}

const str1 = "bacaAacba";
const str2 = "abc";
console.log(solution(str1, str2));

 

ํ•ด์‹œ(Hash)

์ž„์˜์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์ •๋œ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๊ฒƒ

 

ํŠน์ •ํ•œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‚˜ ์œ„์น˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ ์žํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ์ด์š”ํ•ด ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. 

ํ•ด์‰ฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ฆ‰์‹œ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ฐพ๊ณ ์žํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋น ๋ฅธ ์†๋„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์— ๋ฐ”๋กœ ์ ‘๊ทผํ•ด์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ฐพ๊ณ ์žํ•˜๋Š” ๊ฐ’๊ณผ ํ…Œ์ด๋ธ”์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋™์ผํ•˜๋ฏ€๋กœ ๊ฐ’์ด ์ €์žฅ๋œ ๊ณต๊ฐ„์— ๋ฐ”๋กœ ์ ‘๊ทผํ•ด์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1)์ด๋‹ค. 

 

ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๊ฐ’์„ ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ ํ•˜๋Š” ํ–‰์œ„๋„ ๊ฐ’์ด ์–ด๋””์žˆ๋Š”์ง€๋งŒ ์•Œ๊ณ ์žˆ์œผ๋ฉด ๋ชจ๋‘ ํ•œ ๋ฐฉ์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

ํ•ด์‹œํ…Œ์ด๋ธ”

ํ•ด์‹œํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์— ํ•œ ๋ฒˆ ํ†ต๊ณผ์‹œ์ผœ์„œ ์‚ฌ์šฉํ•œ๋‹ค. 

ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋ฐ์ดํ„ฐ๋กœ ๋งคํ•‘ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

 

์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด์˜ค๋“  ๊ฐ„์— ๋‹ค ๋ญ‰๊ฐœ์„œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ธธ์ด์˜ ๊ฐ’์œผ๋กœ ๋งŒ๋“ ๋‹ค. 

 

key์™€ value๋กœ ์ด๋ฃจ์–ด์ง„ Map์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด์‹œํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ Map์€ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ด๋Š”๋ฐ ๊ฒฐ์ •์ ์ธ ์—ญํ• ์„ ํ•œ๋‹ค.

 

Map ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๊ธฐ

Map ๊ฐ์ฒด ์ƒ์„ฑ

const map = new Map();

 Map์˜ ๋ฉ”์†Œ๋“œ์ธ setํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด์— ์š”์†Œ ์ถ”๊ฐ€

map.set('B', 1)

๊ธฐ์กด ๊ฐ’์—๋‹ค๊ฐ€ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ

map.get('B', 1)

 

 

์˜ˆ์ œ๋ฌธ์ œ | ํ•™๊ธ‰ํšŒ์žฅ

๋ฌธ์ œ

ํ•™๊ธ‰ํšŒ์žฅ์„ ๋ฝ‘๋Š”๋ฐ ํ›„๋ณด๋กœ ๊ธฐํ˜ธ A,B,C,D,E ํ›„๋ณด๊ฐ€ ๋“ฑ๋ก์„ ํ–ˆ๋‹ค. ํˆฌํ‘œ ์šฉ์ง€์—๋Š” ๋ฐ˜ ํ•™์ƒ๋“ค์ด ์“ด ํ›„๋ณด์˜ ๊ธฐํ˜ธ๊ฐ€ ์“ฐ์—ฌ์žˆ์œผ๋ฉฐ, ์„ ์ƒ๋‹˜์€ ๊ทธ ๊ธฐํ˜ธ๋ฅผ ๋ฐœํ‘œํ•˜๊ณ  ์žˆ๋‹ค. ๋ฐœํ‘œ๊ฐ€ ๋๋‚œ ํ›„ ์–ด๋–ค ๊ธฐํ˜ธ์˜ ํ›„๋ณด๊ฐ€ ํ•™๊ธ‰ ํšŒ์žฅ์ด ๋˜์—ˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด๋ผ. 

 

์ž…๋ ฅ์˜ˆ์ œ | BACBACCACCBDEDE

์ถœ๋ ฅ์˜ˆ์ œ | C

 

์ฝ”๋“œ

const solution = (str) => {
  let answer;
  let student = new Map();

  for (let x of str) {
    if (student.has(x)) {
      student.set(x, student.get(x) + 1);
    } else {
      student.set(x, 1);
    }
  }

  // ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜๋ฅผ ๋น„๊ต๊ตฐ์œผ๋กœ ์„ค์ •
  let max = Number.MIN_SAFE_INTEGER;

  for (let [key, value] of student) {
    if (value > max) {
      max = value;
      answer = key;
    }
  }
  return answer;
}

const str = "BACBACCACCBDEDE";
console.log(solution(str));

 

 

์˜ˆ์ œ๋ฌธ์ œ | ์•„๋‚˜๊ทธ๋žจ

๋ฌธ์ œ

์•„๋‚˜๊ทธ๋žจ์ด๋ž€ ๋‘ ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์˜ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์ด ์ผ์น˜ํ•˜๋ฉด ๋‘ ๋‹จ์–ด๋Š” ์•„๋‚˜๊ทธ๋žจ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ด๋ฉด YES๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•ด๋ผ.

 

์ž…๋ ฅ์˜ˆ์ œ | AbaAeCe, baeeACA

์ถœ๋ ฅ์˜ˆ์ œ | YES

 

์ฝ”๋“œ

const solution = (word1, word2) => {
  // ์•„๋‚˜๊ทธ๋žจ : ์•ŒํŒŒ๋ฒณ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ(์•ŒํŒŒ๋ฒณ๊ณผ ๊ทธ ๊ฐœ์ˆ˜)์ด ์ผ์น˜
  // ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๋‘ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๊ทธ ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„
  // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„

  let answer = "Yes";

  // map1 ์„ ๊ธฐ์ค€ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ๋†“๊ณ  
  // map1 ๊ณผ word2๋ฅผ ๋น„๊ตํ•ด์„œ key๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ๋ณด๊ณ , ์žˆ์œผ๋ฉด value ๋น„๊ต
  let map1 = new Map();

  for (let x of word1) {
    if (map1.has(x)) {
      map1.set(x, map1.get(x) + 1);
    } else {
      map1.set(x, 1);
    }
  }

  for (let x of word2) {
    if (!map1.has(x) || map1.get(x) === 0) {
      return "No";
    }
    map1.set(x, map1.get(x) - 1);
  }

  return answer;
}

const word1 = "abaCC";
const word2 = "Caaab";
console.log(solution(word1, word2));

 

 

๋ฌธ์ œ

5๋ช…์˜ ํ•™์ƒ์ด ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ํ•™์ƒ์ด ๋ฐ›์€ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๊ธฐ์ค€์— ๋”ฐ๋ผ ํ•™์ ์„ ๋ถ€์—ฌ

์กฐ๊ฑด : ์ž๊ธฐ์ž์‹ ์„ ํ‰๊ฐ€ํ•œ ์ ์ˆ˜๊ฐ€ ์œ ์ผํ•œ ์ตœ๊ณ ์  ๋˜๋Š” ์œ ์ผํ•œ ์ตœ์ €์ ์ด๋ผ๋ฉด, ๊ทธ ์ ์ˆ˜๋Š” ์ œ์™ธํ•˜๊ณ  ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. 

 

 

ํ’€์ด

ํ‰๊ท ์„ ๋จผ์ € ๊ตฌํ•˜๊ณ , ํ•™์ ์„ ๊ตฌํ•œ๋‹ค. ๊ฐ ํ–‰์— ํ‰๊ท ์„ ๊ตฌํ•ด์•ผ ํ•  ์ ์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. 

 

 

ํ†ต๊ณผํ•˜์ง€๋ชปํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let sum = 0;

  let sumArr = [];
  let scoreArr = [];
  let studentScoreArr = [];
  let len;

  let answer = '';

  // ๋ฐฐ์—ด ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    studentScoreArr = [];
    for (let j = 0; j < scores.length; j++) {
      studentScoreArr.push(scores[j][i]);
    }
    scoreArr.push(studentScoreArr);
  }

  // ์ ์ˆ˜ ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    sum = 0;
    len = scoreArr[i].length;
    for (let j = 0; j < scores.length; j++) {
      let findIdx = scoreArr.indexOf(scoreArr[i]);
      let findCurrentIdx = scoreArr[i].indexOf(scoreArr[i][j]);

      if (findIdx === findCurrentIdx) {

        const myScore = scoreArr[i][j];
        const dubCheck = isDuplicate(scoreArr[i]);

        if (dubCheck) {
          if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
            let myIdx = scoreArr[i].indexOf(myScore);
            console.log(myIdx);
            scoreArr[i].splice(myIdx, 1, 0);
            len--;
          } else {
            sum += scoreArr[i][j];
          }
        } else if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
          let myIdx = scoreArr[i].indexOf(myScore);
          scoreArr[i].splice(myIdx, 1, 0);
          len--;
        } else {
          sum += scoreArr[i][j]
        }
      } else {
        sum += scoreArr[i][j];
      }
    }
    console.log(len);
    sumArr.push(sum / len);
  }

  // ํ•™์  ๊ณ„์‚ฐ
  for (let i = 0; i < sumArr.length; i++) {
    if (sumArr[i] >= 90) {
      answer += 'A';
    } else if (90 > sumArr[i] && sumArr[i] >= 80) {
      answer += 'B';
    } else if (80 > sumArr[i] && sumArr[i] >= 70) {
      answer += 'C';
    } else if (70 > sumArr[i] && sumArr[i] >= 50) {
      answer += 'D';
    } else {
      answer += 'F';
    }
  }
  return answer;
}

const isDuplicate = (arr) => {
  const isDup = arr.some(function (x) {
    return arr.indexOf(x) !== arr.lastIndexOf(x);
  });
  return isDup;
}

๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ–ˆ๋Š”๋ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 3๊ฐœ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

 

 

 

 

ํ†ต๊ณผํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let answer = '';

  for (let i = 0; i < scores.length; i++) {
    let myScore = scores[i][i];
    let count = scores.length;

    let min = 101;
    let max = -1;
    let sum = 0;

    let flag = true;

    for (let j = 0; j < scores.length; j++) {
      let score = scores[j][i];

      if (i !== j && myScore === score) { // ๋‚ด์ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ  ๋™์ผํ•œ ์ ์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ 
        flag = false;
      }

      min = Math.min(min, score);
      max = Math.max(max, score);
      sum += score;
    }

    if (flag && (min === myScore || max === myScore)) { // ๋‚ด ์ ์ˆ˜์ผ ๋•Œ
      count--;
      sum -= myScore;
    }

    answer += findGrade(sum / count);
  }
  return answer;
}

const findGrade = (score) => {
  if (score >= 90) {
    return 'A';
  } else if (90 > score && score >= 80) {
    return 'B';
  } else if (80 > score && score >= 70) {
    return 'C';
  } else if (70 > score && score >= 50) {
    return 'D';
  } else {
    return 'F';
  }
}

๋ฌธ์ œ

์ˆซ์ž์˜ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๊ฐ€ ์˜๋‹จ์–ด๋กœ ๋ฐ”๋€Œ์–ด์กŒ๊ฑฐ๋‚˜, ํ˜น์€ ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ์ธ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. s๊ฐ€ ์˜๋ฏธํ•˜๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ.

์ž…๋ ฅ์˜ˆ์ œ | "one4seveneight"

์ถœ๋ ฅ์˜ˆ์ œ | 1478

 

์ฒ˜์Œ ์ฝ”๋“œ(์‹คํŒจ)

const solution = (s) => {
  const numStr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

  let answer = s;

  for (let i = 0; i < numStr.length; i++) {
    let arr = answer.split(numStr[i]);

    if (arr.length > 1) {
      let idx = numStr.indexOf(numStr[i]);
      arr.splice(1, 0, num[idx]);
      answer = arr.join(i);
    }
  }
  let answer2 = answer.split('');

  let result = answer2.reduce((acc, cur, idx, arr) => {
    if (arr.indexOf(cur) === idx) acc.push(cur);
    return acc;
  }, []);

  return Number(result.join(''));
}

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒ ๋ณด๊ณ  ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ํ•˜๋ฉด๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ํ•˜๊ณ ๋ณด๋‹ˆ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์— ์ค‘๋ณต์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ๋งˆ์ € ์—†์• ๋Š” ์ฝ”๋“œ๊ฐ€ ๋˜์—ˆ๋‹ค. ๋‹ค์‹œ ์ƒˆ๋กœ ์งœ์•ผํ–ˆ๋‹ค. 

 

 

๋‹ค๋ฅธํ’€์ด

const solution = (s) => {
  let answer = 0;

  s = s.replace(/one/g, '1');
  s = s.replace(/two/g, '2');
  s = s.replace(/three/g, '3');
  s = s.replace(/four/g, '4');
  s = s.replace(/five/g, '5');
  s = s.replace(/six/g, '6');
  s = s.replace(/seven/g, '7');
  s = s.replace(/eight/g, '8');
  s = s.replace(/nine/g, '9');
  s = s.replace(/zero/g, '0');

  console.log(s);
  return Number(s);
}

 

 

๋‹ค๋ฅธํ’€์ด

const solution = (s) => {
  let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

  for (let i = 0; i < numbers.length; i++) {
    let arr = s.split(numbers[i]);
    s = arr.join(i);
  }

  return Number(s);
}

๋ฌธ์ œ

๊ฐ™์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์€ ๊ฐ™์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค. 

์ตœ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ฐ€์ง€๊ธธ ์›ํ•œ๋‹ค. 

์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ํฌํ•จํ•ด์„œ N/2๋งˆ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 

 

N๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,

N/2๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘

๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฌ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„

ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๋ฅผ return

 

์ž์—ฐ์ˆ˜

์–‘์˜ ์ •์ˆ˜, ์‚ฌ๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ๋•Œ ์“ฐ์ด๋Š” ์ˆ˜์—ฌ์„œ ๊ฐ€์žฅ '์ž์—ฐ์Šค๋Ÿฌ์šด ์ˆ˜' 

 

 

์ฝ”๋“œ

const solution = (nums) => {
  const selectNum = nums.length / 2;

  const possibleSelect = nums.reduce((acc, cur, idx, origin) => {
    if (origin.indexOf(cur) === idx) acc.push(cur);
    return acc;
  }, []);

  return possibleSelect.length >= selectNum ? selectNum : possibleSelect.length;
}

์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ๋จผ์ €ํ•ด์ฃผ๊ณ , ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

reduce๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” Set ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

 

๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด

function solution(nums) {
  const max = nums.length / 2;
  const arr = [...new Set(nums)];

  return arr.length > max ? max : arr.length
}

+ Recent posts