当前位置:网站首页>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
边栏推荐
- 从全球价值链视角看,京东云数智供应链对未来经济有何影响?
- Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
- 1004 (tree array + offline operation + discretization)
- 高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
- Custom picker scroll selector style
- How to code like a pro in 2022 and avoid If-Else
- E. Cross Swapping (and check out deformation/good questions)
- Zijin Example
- redhat替换yum源时redhat.repo无法删除或无法禁用的问题解决方法
- APP automation testing with Uiautomator2
猜你喜欢
Oracle数据库备份dmp文件太大,有什么办法可以在备份的时候拆分成多个dmp吗?
软件测试用例篇
PyTorch multi-machine multi-card training: DDP combat and skills
富爸爸穷爸爸之读书笔记
物资采购小程序开发制作功能介绍
使用Uiautomator2进行APP自动化测试
Redis -- Nosql
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
SWIG教程《一》
【MindSpore易点通机器人-02】设计与技术选型
随机推荐
BCG库简介
基于 Azuki 系列:NFT估值分析框架“DRIC”
社区动态——恭喜海豚调度中国区用户组新晋 9 枚“社群管理员”
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
10 advanced functions of scala
scala 基础篇
网络初识(二)
JS 从零手写实现一个bind方法
关于async\await 的理解和思考
How to code like a pro in 2022 and avoid If-Else
CSP-J1 CSP-S1 初赛 第1轮(2022.08.09)
"Thesis Reading" PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
Introduction to the Internet (2)
【教程】HuggingFace的Optimum组件已支持加速Graphcore和英特尔Habana芯片
QOS功能介绍
MySQL advanced (thirty-three) MySQL data table adding fields
蓝帽杯半决赛火炬木wp
[Semantic Segmentation] DeepLab Series
Go Context基本使用
High-paid programmers & interview questions series 135 How do you understand distributed?Do you know CAP theory?