
g0401_0500.s0416_partition_equal_subset_sum.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_05_22_Time_199_ms_(41.03%)_Space_12.3_MB_(92.82%)
#include
using namespace std;
class Solution {
public:
bool canPartition(vector& nums) {
int sums = 0;
for (int num : nums) {
sums += num;
}
if (sums % 2 == 1) {
return false;
}
sums /= 2;
vector dp(sums + 1, false);
dp[0] = true;
for (int num : nums) {
for (int sum = sums; sum >= num; --sum) {
dp[sum] = dp[sum] || dp[sum - num];
}
}
return dp[sums];
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy