当前位置:网站首页>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]
}
边栏推荐
- 5个最佳WordPress广告插件
- Adobe Substance 3D Designer 2021软件安装包下载及安装教程
- 零基础学习CANoe Panel(3)—— 静态控件(Static Text , Group Box ,Picture Box)
- Distributed transaction processing scheme big PK!
- Partition data 1
- Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
- 零基础学习CANoe Panel(6)—— 开关/显示控件(Switch/Indicator)
- [electronic device note 4] inductance parameters and type selection
- Distributed lock implementation scheme (glory collection version)
- 零基础学习CANoe Panel(10)—— 复选框(CheckBox)
猜你喜欢

二叉树基础知识概览

Rtklib source code, RTK difference calculation, rtkpos and replos function process sorting

Windows virtual machine security reinforcement process steps, detailed description of windows setting security policy, detailed description of win7 setting IP security policy, windows setting firewall

UVM——双向通信

火山引擎:开放字节跳动同款AI基建,一套系统解决多重训练任务

App automation and simple environment construction

Machine learning quiz (10) using QT and tensorflow to create cnn/fnn test environment

Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire

Hongmeng's first note

Real time weather API
随机推荐
Distributed lock implementation scheme (glory collection version)
MySQL - 多列索引
每日三题 7.22
Distributed transaction processing scheme big PK!
PC Museum (1) 1970 datapoint 2000
MySQL - lock
协议圣经-谈端口和四元组
563页(30万字)智慧化工园区(一期)总体设计方案
Flink 运行架构详解
[carving master learning programming] Arduino hands-on (59) - RS232 to TTL serial port module
图像处理:RGB565转RGB888
I admire a Google boss very much, and he left..
IEPE vibration sensor synchronous signal acquisition card /icp synchronous data network acquisition module
Record AP and map calculation examples
实时天气API
MySQL - 唯一索引
Array element removal problem
Five best WordPress advertising plug-ins
fatal: unable to commit credential store: Device or resource busy
Scaffold document directory description and document exposure