当前位置:网站首页>Brainstorm: Goals and
Brainstorm: Goals and
2022-08-10 15:25:00 【InfoQ】
title
Input: nums: [1, 1, 1, 1, 1], S: 3
Output: 5
- The array is not empty and the length will not exceed 20.
- The initial array sum will not exceed 1000.
- The final result returned is guaranteed to be stored as a 32-bit integer.
Solution ideas
Code Implementation
class Solution {
public int findTargetSumWays(int[] nums, int target) {
int sum = 0;
for(int i = 0; i < nums.length; i++) sum += nums[i];
if ((target + sum) % 2 != 0) return 0;
int size = (target + sum) / 2;
if(size < 0) size = -size;
int[] dp = new int[size + 1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j=size; j >= nums[i]; j--) {
dp[j] += dp[j - nums[i]];
}
}
return dp[size];
}
}
Last
- Time complexity: O(n × m), n is a positive number, m is the backpack capacity
- Space complexity: O(m), m is the backpack capacity
边栏推荐
猜你喜欢

JS entry to proficient full version

Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"

Understanding_Data_Types_in_Go

NFT digital collection development issue - digital collection platform

storage of data in memory

推荐几款最好用的MySQL开源客户端,建议收藏!

MySQL Principle and Optimization: Update Optimization

机器学习总结(一)

Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack

程序调试介绍及其使用
随机推荐
Lilac Garden
Rich Dad Poor Dad Reading Notes
fatal error C1083 无法打开包括文件'io.h' No such file
Programmer = overtime??- Master the time to master the life
推荐几款最好用的MySQL开源客户端,建议收藏!
MySQL 原理与优化:Update 优化
fastposter v2.9.1 程序员必备海报生成器
奢侈品鉴定机构小程序开发制作功能介绍
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
【吴恩达来信】强化学习的发展!
中学数学建模书籍及相关的视频等(2022.08.09)
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
基于 Azuki 系列:NFT估值分析框架“DRIC”
XML基本学习
Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack
NFT数字藏品——数字藏品发行平台开发
640. Solving Equations: Simple Simulation Problems
自定义picker滚动选择器样式
使用Uiautomator2进行APP自动化测试
机器学习总结(一)