当前位置:网站首页>Golang Li Kou leetcode 494. goals and
Golang Li Kou leetcode 494. goals and
2022-07-24 10:45:00 【cheems~】
494. Objectives and
494. Objectives and
Answer key
subject : Give an array , And a target, Array elements can be positive or negative , After the change sign , Sum of array elements =target Number of alternatives
Ideas :1 Dynamic programming
Set satisfaction target In the array , All positive sums are x, All negative sums are y
x+y==target
x+y+x-y=target+x-y
because x-y= All positive numbers minus all negative numbers = Array elements and , Set to sum
2x=target+sum
therefore x=(sum - target) / 2
Now the problem is to select several elements , Make the sum of the elements =x, Calculate the number of solutions
Then we can use dynamic programming to do
dp[i][j]: before i Select from the elements , Make the sum of the elements equal to j Number of alternatives
State shift :dp[i][j] = dp[i-1][j]// No election j < nums[i]
dp[i][j] = dp[i-1][j] + dp[i-1][j-curVal] Number of schemes = No election + choose j>=nums[i]
initial :dp[0][0] = 1 before 0 Select and from the elements as 0 Number of alternatives , You have to choose nothing ,1 Medium scheme
answer :dp[n][neg]
2 violence dfs, Enumerate all the situations
func findTargetSumWays(nums []int, target int) int {
ans := 0
var dfs func(idx int, cur int)
dfs = func(idx int, cur int) {
if idx == len(nums) {
if cur == target {
ans++
}
return
}
dfs(idx+1, cur+nums[idx])
dfs(idx+1, cur-nums[idx])
}
dfs(0, 0)
return ans
}
Code
func findTargetSumWays(nums []int, target int) int {
sum, n := 0, len(nums)
for _, v := range nums {
sum += v
}
if sum < target || (target+sum)%2 != 0 {
return 0
}
neg := (sum - target) / 2
dp := make([][]int, n+1)
for i := range dp {
dp[i] = make([]int, neg+1)
}
dp[0][0] = 1
for i := 1; i <= n; i++ {
for j := 0; j <= neg; j++ {
curVal := nums[i-1]
if j < curVal {
dp[i][j] = dp[i-1][j] // No election
} else {
dp[i][j] = dp[i-1][j] + dp[i-1][j-curVal] // Number of schemes = Unselected number + Number of options selected
}
}
}
return dp[n][neg]
}
边栏推荐
- Web Security Foundation - file upload (file upload bypass)
- CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
- App automation and simple environment construction
- Mina framework introduction "suggestions collection"
- JS function call download file link
- Sentinel 流量控制快速入门
- [recommendation system] the classic technical architecture of the data flow of the recommendation system + the most complete evolution map of the top 10 deep learning CTR models of Microsoft, Alibaba,
- Chapter V Modification implementation (impl) class
- Adobe Substance 3D Designer 2021软件安装包下载及安装教程
- MySQL - normal index
猜你喜欢

Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!

零基础学习CANoe Panel(3)—— 静态控件(Static Text , Group Box ,Picture Box)

MySQL——锁

Flink 运行架构详解

零基础学习CANoe Panel(5)——改变变量的值,控件图像也改变,这是怎么回事?

How to build a node development environment efficiently

UVM——双向通信

机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验

PC Museum (2) 1972 hp-9830a

Constant pointer, pointer constant
随机推荐
MySQL - 全文索引
图像处理:RGB565转RGB888
零基础学习CANoe Panel(5)——改变变量的值,控件图像也改变,这是怎么回事?
Cross platform audio playback Library
binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
MySQL - 多列索引
零基础学习CANoe Panel(6)—— 开关/显示控件(Switch/Indicator)
Daily three questions 7.21
js函数调用下载文件链接
What does resource pooling and resource pooling mean?
PostgreSQL rounding
ECCV 2022 | Tsinghua proposes the first transformer to embed spectral sparsity
划分数据1
[dish of learning notes dog learning C] initial level of pointer
Sentinel flow control quick start
谷歌联合高校研发通用模型ProteoGAN,可设计生成具有新功能的蛋白质
Tidb query SQL report runtime error: index out of range [-1] error
PC博物馆(1) 1970年 Datapoint 2000
Erlang learning 02
Pytorch common tricks summary