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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Dynamic Programming / 416번 / Partition Equal Subset Sum / JS

2023. 5. 14. 19:18

< 문제 바로가기 >

 

Partition Equal Subset Sum - LeetCode

Can you solve this real interview question? Partition Equal Subset Sum - Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.   Example 1: I

leetcode.com

 

< 문제 간단설명 >

주어진 nums 배열을 둘의 부분집합으로 나누었을 때, 그 둘의 부분집합 각각의 합이 같을 경우 true, 아니라면 false를 반환하는 문제이다.

 

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canPartition = function (nums) {
  nums.sort((a, b) => a - b);
  let totalSum = nums.reduce((acc, cur) => acc + cur);
  let target = totalSum / 2;

  // 총합이 홀수라면 반으로 나눌 수 없으므로 false 반환
  if (totalSum % 2 === 1) {
    return false;
  }

  const backTracking = (current, index) => {
    // target 값이 가능하면 true 반환
    if (current === target) {
      return true;
    }

    // 작은수부터 더해갔는데 target값을 넘거나,
    // index가 범위를 넘었다면, 즉 다 돌아도 할 수 없다면 false 반환
    if (current > target || index >= nums.length) {
      return false;
    }

    // 현재값에 다음값을 더하고, 다음에 더할 수의 인덱스를 하나 더하던가,
    // 다음값을 더했을 때 false 일 수 있으므로 현재값을 유지한채 다음에 더할 인덱스만 하나 더해줌
    return (
      backTracking(current + nums[index], index + 1) ||
      backTracking(current, index + 1)
    );
  };
  return backTracking(0, 0);
};
저작자표시

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

LeetCode / Tree / 100번 / Same Tree / JS  (1) 2023.05.14
LeetCode / Two Pointer / 16번 / 3Sum Closese / JS  (0) 2023.05.14
LeetCode / Dynamic Programming / 322번 / Coin Change / JS  (0) 2023.05.14
LeetCode / Dynamic Programming / 198번 / House Robber / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Tree / 100번 / Same Tree / JS
    • LeetCode / Two Pointer / 16번 / 3Sum Closese / JS
    • LeetCode / Dynamic Programming / 322번 / Coin Change / JS
    • LeetCode / Dynamic Programming / 198번 / House Robber / JS
    KimMinJun
    KimMinJun

    티스토리툴바