当前位置:网站首页>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]
}
边栏推荐
- Constant pointer, pointer constant
- Sentinel implements the persistence of pull pattern rules
- 《nlp入门+实战:第二章:pytorch的入门使用 》
- [about Modelsim simulation] design and Simulation of 4-bit counter
- Real time weather API
- redis 缓存设置,实现类似putIfAbsent功能
- QT application prevents multiple opening, that is, single instance operation
- 5个最佳WordPress广告插件
- Arduino + AD9833 waveform generator
- 零基础学习CANoe Panel(7)—— 开关/显示控件(Input/Output Box )
猜你喜欢

PC Museum (1) 1970 datapoint 2000

NiO knowledge points

ECCV 2022 | Tsinghua proposes the first transformer to embed spectral sparsity

很佩服的一个Google大佬,离职了。。

38. REM adaptive layout

MySQL - full text index

图像处理:RGB565转RGB888

Web Security Foundation - file upload (file upload bypass)

OSPF includes special area experiments, mGRE construction and re release

Record AP and map calculation examples
随机推荐
NiO knowledge points
MySQL - full text index
Review of new services and functions of Amazon cloud technology in June 2022
Partition data 2
[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,
测试左移和测试右移,我们为何要“上下求索”?
Image processing: floating point number to fixed point number
CMS vulnerability recurrence - ultra vires vulnerability
Daily three questions 7.22
Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
In depth analysis of common cross end technology stacks of app
《nlp入门+实战:第二章:pytorch的入门使用 》
[dish of learning notes dog learning C] advanced pointer
[about Modelsim simulation] design and Simulation of 4-bit counter
MySQL - 唯一索引
Hongmeng's first note
563页(30万字)智慧化工园区(一期)总体设计方案
MySQL - 普通索引
MySQL - 全文索引
[carving master learning programming] Arduino hands-on (59) - RS232 to TTL serial port module