일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- kaggle
- 밴픽
- Generator
- data science
- 30. Substring with Concatenation of All Words
- Protocol
- Substring with Concatenation of All Words
- Python Implementation
- shiba
- t1
- concurrency
- iterator
- LeetCode
- 컴퓨터의 구조
- Python
- 43. Multiply Strings
- Regular Expression
- 315. Count of Smaller Numbers After Self
- Python Code
- 프로그래머스
- 운영체제
- DWG
- Convert Sorted List to Binary Search Tree
- 파이썬
- Decorator
- 시바견
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- attribute
- Class
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/
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 |