当前位置:网站首页>Leetcode 494. Objectives and
Leetcode 494. Objectives and
2022-06-12 18:41:00 【Changersh】
494. Objectives and
Give you an array of integers nums And an integer target .
Add... Before each integer in the array ‘+’ or ‘-’ , And then concatenate all the integers , You can construct a expression :
for example ,nums = [2, 1] , Can be in 2 Before adding ‘+’ , stay 1 Before adding ‘-’ , And then concatenate them to get the expression “+2-1” .
Returns the 、 The result is equal to target Different expression Number of .
Example 1:
Input :nums = [1,1,1,1,1], target = 3
Output :5
explain : Altogether 5 Ways to make the ultimate goal and 3 .
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Example 2:
Input :nums = [1], target = 1
Output :1
Ideas
This problem feels like dividing the elements in the array into two parts , Then take their difference , Find the difference equal to target The combination number of .
That is, find the number of methods to fill the backpack , It's usually dp[j] += dp[j - nums[i]];
First analyze the special situation , If sum Less than target The absolute value of ,namo You can't make it up , Go straight back to 0 that will do
We divided the set into two parts , Add subtract , Let the sum on the left be equal to L that The sum on the right is equal to R ,L + R = sum
L - (sum - L) = target namely L = (sum + target) / 2;
sum and target It's all known , So we can find out L This is the capacity of the backpack
Again because L It must be an integer , therefore (sum + target) It must be an even number , If it's odd , direct return 0 that will do
dp[i] meaning : manage to put together i Altogether dp[i] Methods
The recursive formula :dp[j] += dp[j - nums[i]];
initialization : dp[0] = 1, Make up 0 There is only one way
Code
class Solution {
public:
// There are several ways to fill a backpack dp[j] += dp[j - nums[i]];
// Join in L Is the sum of positive numbers Then there is R Is the sum of negative numbers , Not looking at the symbols adds up to sum
// therefore L - (sum - L) = target,sum and target Is constant , therefore
// L = (target + sum) / 2;
// L Is the maximum capacity of the backpack
int findTargetSumWays(vector<int>& nums, int target) {
int sum = 0;
for (int i = 0; i < nums.size(); i++) sum += nums[i];
// If target Greater than sum There is no plan
if (abs(target) > sum) return 0;
int L = (sum + target) / 2;
// because L It must be an integer , therefore (sum + target) It must be an even number
if ((sum + target) & 1) return 0;
int dp[L + 1]; // dp[i] Make a sum i common dp[i] Methods
for (int i = 0; i <= L; i++) dp[i] = 0;
dp[0] = 1; // Array initialization , And for 0 share 1 Methods
for (int i = 0; i < nums.size(); i++) {
for (int j = L; j >= nums[i]; j--) {
dp[j] += dp[j - nums[i]];
}
}
return dp[L];
}
};
边栏推荐
- 论大型政策性银行贷后,如何数字化转型 ?-亿信华辰
- The Bean Validation API is on the classpath but no implementation could be found
- leetcode:5270. 网格中的最小路径代价【简单层次dp】
- GD32F4xx控制DGUS 变量显示
- Order allocation strategy for instant delivery: from modeling and Optimization - Notes
- Extracting strings with grep awk
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- Leetcode 494. 目标和
- Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
- 国内如何下载Vega
猜你喜欢

Pytest automated testing framework (II)

VirtualLab基础实验教程-5.泊松亮斑

Title 37: sorting 10 numbers

In 2021, the global spice and seasoning revenue is about 18720million US dollars, and it is expected to reach 25960million US dollars in 2028

国内如何下载ProxyStrike

C language practice (4) -- multiplication and division of large numbers

从应无所住说起

间隔两个月,我的第二次上榜纪念日【2022.6.2】

【矩阵论 & 图论】期末考试复习思维导图

Basic SQL statement - select (single table query)
随机推荐
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update
liunx部署Seata(Nacos版)
Leetcode 474. 一和零
Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
OpenGL shadow implementation (hard shadow)
Review of MySQL (4): sorting operation
The practice of machine learning in meituan distribution system: restoring the real world with technology - Notes
观察网站的页面
Go init initialization function
Judging the quality of American advanced anti DDoS servers depends on three points!
六石认知学:大脑的显速与潜速
Review of MySQL (IX): index
Leetcode topic [string] -151- flip words in string
Leetcode 494. 目标和
kali通过iptables实现端口转发功能
Getting started with the go language is simple: read / write lock
看不懂Kotlin源码?从Contracts 函数说起~
间隔两个月,我的第二次上榜纪念日【2022.6.2】
yoloe 目标检测使用笔记
【sql语句基础】——查(select)(单表查询)