当前位置:网站首页>leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
2022-06-28 03:53:00 【Oceanstar's learning notes】
Title source
Title Description

title
Recurrence of violence
Definition : For arrays arr[idx…] All the numbers in , Come up with rest The number of , What is the method .
int process(std::vector<int>&arr, int idx, int rest);
base case:
- When there is no number , There is no choice , At this time rest Is it 0, If 0, A method has been found , There is no other way
In general :
- Namely base case Other than that , That is, there are still numbers to choose from
- This is the time , We have to decide on arr[idx] Make a choice , Making a choice will be right rest Impact . Finally, the rest of the recursion is given to arr[idx+1…]
Memorandum
about :
int process(std::vector<int>&arr, int idx, int rest);
As long as the variable parameters idx and rest Fixed , that process The return value of is fixed
therefore , We just use one map Refers to caching , And then if there is , Just check it out , If not, calculate
Violent recursion to dynamic programming
Optimize
The original problem is equivalent to : find nums A positive subset P And a negative subset N, Make the sum equal to target. namely
s u m ( P ) − s u m ( N ) = = t a r g e t sum(P) - sum(N) == target sum(P)−sum(N)==target
At the same time there is :
s u m ( P ) + s u m ( N ) = = s u m sum(P) + sum(N) == sum sum(P)+sum(N)==sum
Add the above two expressions , obtain
2 ∗ s u m ( P ) = = t a r g e t + s u m ( n u m s ) 2 * sum(P) == target + sum(nums) 2∗sum(P)==target+sum(nums)
among target + sum(nums) must >=0 And it is even , Otherwise, the equation cannot hold .
Then the question turns to : There are some items , The first i The weight of each item is nums[i], The capacity of the backpack is sum§, ask : How many ways to pack a backpack 【 Just full 】
边栏推荐
- Li Kou daily question - day 29 -523 Count odd numbers in interval range
- iptables防火墙规则和firewalld防火墙规则详解
- What is the core problem to be solved in the East and West?
- A preliminary study of blackbody radiation
- How to automatically add author, time, etc. to eclipse
- 如何修改SE38编辑器主题
- 图片的懒加载和预加载
- Typescript union type
- 数字电路学习笔记(一)
- Execution plan in MySQL of database Series
猜你喜欢

工业物联网将取代人工发展吗?

Self use tool unity video player that monkeys can use

Summary of the use of composition API in the project

Extensible database (I)

错排兼排列组合公式

数据库系列之MySQL中的执行计划
![[graduation season] graduate summary](/img/f6/59134c1dbf70fc809652d925fd528f.jpg)
[graduation season] graduate summary

applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结

Anaconda command usage

Custom controls under WPF and adaption of controls in Grid
随机推荐
django. core. exceptions. ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
PyCharm设置仿sublime配色方案
Automatic backup of MySQL database
gcd最大公约数
从遇见大咖到成为大咖,昇腾AI开发者创享日给开发者带来无限可能
ambari SSLError: Failed to connect. Please check openssl library versions.
多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal
开口式霍尔电流传感器如何助力直流配电改造?
TypeScript 联合类型
Floating point and complex type of go data type (4)
Online DDL implementation mechanism in InnoDB of database Series
No&nbsp;result&nbsp;defined&amp;nbsp…
"9 No" principle and "5 measurement dimensions" of extensible system
INFO:&nbsp; HHH000397:&nbsp; Using…
数据库系列之MySQL中的分页查询优化
A solution to the inefficiency of setting debug mode in developing flask framework with pychar
applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结
小程序的防抖节流怎么写?
【毕业季】研究生の毕业总结
电学基础知识整理(一)