当前位置:网站首页>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 】
边栏推荐
- WPF 下的自定义控件以及 Grid 中控件的自适应
- Cannot edit in read-only editor if it appears in vscode
- How to write anti shake throttling for small programs?
- 双亲委派机制的理解学习
- "9 No" principle and "5 measurement dimensions" of extensible system
- TypeScript 联合类型
- 数据库系列之MySQL中的执行计划
- Introduction to kubernetes resource object and common commands
- MySQL错误
- 可扩展数据库(上)
猜你喜欢
随机推荐
Scalable storage system (I)
Li Kou daily question - day 29 -523 Count odd numbers in interval range
解析教育机器人的综合应用能力
可扩展数据库(上)
局域网内共享打印机的几种方式
"Five layer" architecture of cloud applications and services
JVM一:JVM入门以及Class文件认识
小程序image组件不显示图片?
学习---有用的资源
光的粒子说(光电效应/康普顿效应)
数据库系列之MySQL和TiDB中慢日志分析
Websocket (simple experience version)
加法器—笔记
A pit filling trip based on LNMP to build a personal website
Chapter IX app project test (3) test tools
数据库系列之MySQL中的分页查询优化
Self use tool unity video player that monkeys can use
ambari SSLError: Failed to connect. Please check openssl library versions.
多线程与高并发六:线程池源码解析
How does the open-ended Hall current sensor help the transformation of DC power distribution?









