当前位置:网站首页>leetcode:1755. 最接近目标值的子序列和
leetcode:1755. 最接近目标值的子序列和
2022-07-05 20:43:00 【OceanStar的学习笔记】
题目来源
题目描述


class Solution {
public:
int minAbsDifference(vector<int>& nums, int goal) {
}
};
题目解析
分析数据量
在看题之前,我们先看下给出的数据量: 1 < = n u m s . l e n g t h < = 40 1 <= nums.length <= 40 1<=nums.length<=40,这意味着:
- 一方面,它几乎不太可能具有 O ( N 2 ) 、 O ( N 2 l o g N ) 、 O ( N 3 ) O(N^2)、O(N^2 logN)、O(N^3) O(N2)、O(N2logN)、O(N3),这样的时间复杂度,因为[1,40]的数据范围显得太小了
- 另一方面,它也不可能具有 O ( 2 N ) 、 O ( N ∗ 2 N ) 、 O ( 3 N ) O(2^N)、O(N * 2^N)、O(3^N) O(2N)、O(N∗2N)、O(3N)这样的时间复杂度,因为[1,40]的数据范围又太大了
一般来讲,数据量 n < = 20 n <= 20 n<=20,这暗示着我们应该用一个指数级别的算法来解决,比如分治、回溯、dfs等,像哪些动态规划之类的通通pass。那么对于 n < = 40 n <= 40 n<=40,唯一的可能性就是将40拆分成两个20,然后两边分别运行一个指数复杂度的解法
然后看下数据范围,好像有点大呀,动态规划pass
分析题意
然后再来看题。题目要求我们从数组中选出一些子集,经sum(子集) 最近接goal。
因为要求最接近,所以我们是肯定需要看尽所有的子集的。
枚举所有的子集可以用回溯的方法。
但是因为数据量 n < = 40 n <= 40 n<=40,如果枚举全部子集那么复杂度为 2 4 0 2^40 240,肯定会超时,所以需要做一定的处理
补充
ps:关于“最接近目标值的子序列和”这一类问题:
- 如果数组长度特别大,但是值小,我们可以使用背包问题的方式来解决
- 但是如果数组长度不大,但是数值特别大的话,使用DFS。
dfs

class Solution {
int point = 0; // 数组数据组合的指针(表示数组的下标)
/** * @param nums 题目给出的数组 * @param start 数组的开始位置 * @param end 数组的结束位置 * @param sum 保存左侧组合的和(sum) * @param arr 保存数组组合的和 */
void dfs(vector<int>& nums, int goal, int start, int sum, vector<int>& arr){
arr[point++] = sum;
for (int i = start; i < nums.size(); ++i) {
dfs(nums, goal, i + 1, sum + nums[i], arr);
}
}
public:
int minAbsDifference(vector<int>& nums, int goal) {
int N = nums.size();
int mid = N >> 1;
point = 0;
// 数据{ 5, -7, 3, 5 }==>左侧[5,-7] 右侧[5,-2]
// 保存所有左侧数据的组合==>[0, -7, 5, -2] 左侧数据为(5,-7)
std::vector<int> l(1 << 20);
// 保存右侧所有数据的组合==>[0, 5, 3, 8] 右侧数据为(5,-2)
std::vector<int> r(1 << 20);
dfs(nums, 0, mid - 1, 0, l); // 枚举左侧所有组合
point = 0;// 指针归0
dfs(nums, mid, N - 1, 0, r);// 枚举右侧所有组合
int ans = INT32_MAX;
std::sort(l.begin(), l.end());
std::sort(r.begin(), r.end());
//组合左侧数据与右侧数据
int left = 0, right = N - 1;
for(int x : l){
ans = std::min(ans, std::abs(goal - x));
}
for(int x : r){
ans = std::min(ans, std::abs(goal - x));
}
while (left < N && right >= 0){
int t = l[left] + r[right]; //临时保存左侧数据与右侧数据的组合
ans = std::min(ans, std::abs(t - goal));
if(t > goal){
right--;
}else{
left++;
}
}
return ans;
}
};
暴力(超时)
枚举所有子序列
class Solution {
int ans = INT32_MAX;
void dfs(vector<int>& nums, int goal, int start, int sum){
ans = std::min(ans, std::abs(sum - goal));
for (int i = start; i < nums.size(); ++i) {
dfs(nums, goal, i + 1, sum + nums[i]); //注意这里是i+1......易错点
}
}
public:
int minAbsDifference(vector<int>& nums, int goal) {
if(nums.empty()){
return goal;
}
dfs(nums, goal, 0, 0);
return ans;
}
};

小结:数据范围 n :
- n <= 20, 可以暴力,2^20
- n <= 40, 不能直接暴力,但是可以对半来,20 * 2^20
边栏推荐
- 解析创客教育的知识迁移和分享精神
- 珍爱网微服务底层框架演进从开源组件封装到自研
- Duchefa丨D5124 MD5A 培养基中英文说明书
- 清除app data以及获取图标
- Is it safe to open an account online? Where can I get a low commission?
- Mongodb/ document operation
- Abnova fluorescent dye 620-m streptavidin scheme
- Applet global configuration
- AI automatically generates annotation documents from code
- Mongodb basic exercises
猜你喜欢

14、Transformer--VIT TNT BETR

Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案

Abnova丨血液总核酸纯化试剂盒预装相关说明书

Duchefa p1001 plant agar Chinese and English instructions

How to form standard interface documents

Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral

王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动

ProSci LAG3抗体的化学性质和应用说明

Applet global configuration

Abnova丨荧光染料 620-M 链霉亲和素方案
随机推荐
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
3.3、项目评估
教你自己训练的pytorch模型转caffe(三)
Abnova cyclosporin a monoclonal antibody and its research tools
1. Strengthen learning basic knowledge points
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
NPDP如何续证?操作指南来了!
Introduction to dead letter queue (two consumers, one producer)
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
解析创客教育的知识迁移和分享精神
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
Nprogress plug-in progress bar
[record of question brushing] 1 Sum of two numbers
14、Transformer--VIT TNT BETR
Common view container class components
Fundamentals - configuration file analysis
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
Abnova fluorescent dye 620-m streptavidin scheme
Duchefa丨P1001植物琼脂中英文说明书