| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Python Implementation
- 운영체제
- Substring with Concatenation of All Words
- Decorator
- 파이썬
- 30. Substring with Concatenation of All Words
- 시바견
- 43. Multiply Strings
- 컴퓨터의 구조
- Generator
- 315. Count of Smaller Numbers After Self
- 밴픽
- Protocol
- Convert Sorted List to Binary Search Tree
- Class
- Python
- 109. Convert Sorted List to Binary Search Tree
- data science
- LeetCode
- 프로그래머스
- DWG
- attribute
- Regular Expression
- iterator
- concurrency
- 715. Range Module
- Python Code
- t1
- shiba
- kaggle
Archives
- Today
- Total
Scribbling
[C++] vector sum, dp table with vector 본문
LeetCode: 416. Partition Equal Subset Sum
https://leetcode.com/problems/partition-equal-subset-sum/description/
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
class Solution{
public:
vector<int> nums;
vector<vector<optional<bool>>> dp;
bool canPartition(vector<int>& nums) {
int total = accumulate(nums.begin(), nums.end(), 0);
if (total % 2 == 1) return false;
int n = nums.size();
int goal = total/2;
this->nums = nums;
this->dp = vector<vector<optional<bool>>>(n+1, vector<optional<bool>>(goal+1, nullopt));
return dfs(0, goal);
}
bool dfs(int idx, int goal) {
if (goal < 0) {
return false;
}
if (idx == nums.size()) {
return goal == 0;
}
if (dp[idx][goal] != nullopt) return dp[idx][goal] == true;
if (dfs(idx+1, goal-nums[idx]) == true) {
dp[idx][goal] = true;
return true;
}
if (dfs(idx+1, goal) == true) {
dp[idx][goal] = true;
return true;
}
dp[idx][goal] = false;
return false;
}
};'Computer Science > C++' 카테고리의 다른 글
| [C++] vector<vector<int>> sorting with comparator (0) | 2023.09.07 |
|---|---|
| [C++] priority queue (pair<int, pair<int, int>>), minheap (0) | 2023.09.02 |
| [C++] unordered_map, unordered_set, substr (0) | 2023.08.31 |
| [C++] vector sum, vector max (0) | 2023.08.31 |
| [C++] ostringstream, isdigit (0) | 2023.08.29 |