KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • programmers
  • 다이나믹 프로그래밍
  • string
  • codeup
  • 문자열
  • recursion
  • C
  • Level 0
  • tree
  • 제코베 JS 100제
  • C++
  • 백준
  • js
  • 정렬
  • 그래프
  • Level 1
  • Level1
  • Level 2
  • 수학
  • LeetCode

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Heap / 1046번 / Last Stone Weight / JS

2023. 4. 18. 01:37

< 문제 바로가기 >

 

Last Stone Weight - LeetCode

Can you solve this real interview question? Last Stone Weight - You are given an array of integers stones where stones[i] is the weight of the ith stone. We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them

leetcode.com

 

< 문제 간단설명 >

돌의 무게들이 배열로 들어온다. 그 중에서 가장 큰 두개의 돌의 무게를 x와 y라고 한다. 이때 x는 y이하이다.

  • 만약 x와 y가 같다면 둘다 없앤다.
  • 만약 x와 y가 같지 않다면 x는 없애고 y는 y-x로 대체한다.
  • 마지막 하나가 남을 때 까지 반복하여 마지막으로 남은 돌의 무게를 반환한다. 만약 남지 않았다면 0을 반환한다.

 

/**
 * @param {number[]} stones
 * @return {number}
 */
var lastStoneWeight = function(stones) {
    let [first, second] = [0, 0];
    while(stones.length > 1) {
        stones.sort((a, b) => a - b);
        first = stones.pop();
        second = stones.pop();

        if(first === second) {
            continue;
        }
        stones.push(Math.abs(first - second));
    }

    return stones;
}

 

저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Implementation / 202번 / Happy Number / JS  (0) 2023.04.18
LeetCode / Heap / 692번 / Top K Frequent Words / JS  (0) 2023.04.18
LeetCode / Stack / 394번 / Decode String / JS  (0) 2023.04.18
LeetCode / Stack / 844번 / Backspace String Compare / JS  (0) 2023.04.18
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Implementation / 202번 / Happy Number / JS
    • LeetCode / Heap / 692번 / Top K Frequent Words / JS
    • LeetCode / Stack / 394번 / Decode String / JS
    • LeetCode / Stack / 844번 / Backspace String Compare / JS
    KimMinJun
    KimMinJun

    티스토리툴바