类似于背包问题,前提条件是数组全是正整数和0,先求和Sum,再从子数组中找出接近Sum/2的子数组

@interface TempState : NSObject

@property (nonatomic,assign) int iIdx;
@property (nonatomic,assign) int jIdx; @end @implementation TempState @end
- (void)getSubArryMinABS{

    // 输入
NSMutableArray *inputArry = [NSMutableArray arrayWithObjects:@"",@"",@"",@"",@"" ,nil];
NSLog(@"inputArry:%@",inputArry); // 输出
NSMutableArray *outputArry = [NSMutableArray arrayWithCapacity:]; // 保存对应i,j数组元素的istrue状态
NSMutableArray<TempState *> *tempStateArry = [NSMutableArray<TempState *> arrayWithCapacity:]; // 可根据实际情况扩容,如果做成通用,建议取count*1000,防止越界错误
NSMutableArray *dpArry = [NSMutableArray arrayWithCapacity:];
for (int i = ; i< ; i++) {
[dpArry addObject:@""];
} __block NSInteger orginSum = ;
[inputArry enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
orginSum += [obj integerValue];
}]; int halfSum = (int)orginSum/; for (int i = ; i< inputArry.count; i++) { for (int j = halfSum; j >= [inputArry[i] intValue]; j--) { if ([dpArry[j] intValue] < [dpArry[j-[inputArry[i] intValue]] intValue] + [inputArry[i] intValue]) { dpArry[j] = [NSString stringWithFormat:@"%d",[dpArry[j-[inputArry[i] intValue]] intValue] + [inputArry[i] intValue]]; TempState *temp = [[TempState alloc] init];
temp.iIdx = i;
temp.jIdx = j;
[tempStateArry addObject:temp];
}
}
} NSLog(@"dpArry:%@",dpArry); NSLog(@"tempStateArry:%@",tempStateArry); __block int jdx = halfSum; for (int index = (int)inputArry.count - ; index >= ; index --) {
// [4 12]
// [3 12] j = 12 - 6 = 6 inputArry[3] = 6
// [2 6]
// [1 6] j = 6 - 4 = 2 inputArry[1] = 4
// [0 2] j = 2 - 2 = 0 inputArry[0] = 2
[tempStateArry enumerateObjectsUsingBlock:^(TempState * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { TempState *temp = tempStateArry[idx];
NSLog(@"index:%d & temp.iIdx:%d & temp.iIdx:%d",index,temp.iIdx,temp.jIdx); if (index == temp.iIdx && jdx == temp.jIdx) {
NSLog(@"temp.iIdx:%d & temp.iIdx:%d & inputArry[idx]:%@",temp.iIdx,temp.jIdx,inputArry[index]);
[outputArry addObject:inputArry[index]];
jdx -= [inputArry[index] integerValue];
NSLog(@"jdx:%d",jdx);
*stop = YES;
}
}];
}
NSLog(@"最后结果outputArry:%@",_outputArry);
}
最后结果:
【2,,6】 【5,7】

实现子数组和绝对值差最小 - Objective-C的更多相关文章

  1. 连续子数组的和的绝对值的最大值、最小值(非绝对值的话直接dp动态规划)

    前缀和的思路: sum[i] = num[0]+num[1]+......+num[i-1] sum[j] = num[0]+num[1]+......+num[j-1] 那么:num[i]+num[ ...

  2. [Swift]LeetCode209. 长度最小的子数组 | Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  3. 《剑指Offer》- 连续子数组的最大和或最小和

    前言 本文是<剑指Offer>系列(JavaScript版)的第一篇,题目是"连续子数组的最大和或最小和". 话不多说,开始"打怪"修炼... 一. ...

  4. 双指针之滑动窗口(长度最小的子数组 和 和为s的连续正数序列)

    双指针之滑动窗口 (长度最小的子数组:和为s的连续正数序列) 1, 什么时候使用? (与子数组/字符串 有关的题目)~如果给了某个具体值的target,即用滑动窗口 不然就双指针(一般做法,左边< ...

  5. 209. 长度最小的子数组--LeetCode

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/minimum-size-subarray-sum 著作权归领扣网络所有.商业转载请联系官方授权,非商业 ...

  6. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. lintcode循环数组之连续子数组求和

    v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...

  8. 【剑指offer】连续子数组的最大和

    个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...

  9. 最大值减去最小值小于或等于num的子数组数量

    [说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“最大值减去最小值小于或等于num的子数组数量”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路, ...

随机推荐

  1. NWERC 2015

    2015-2016 Northwestern European Regional Contest (NWERC 2015) F H没做 似乎只有 B 题有点意思 D:数论分块枚举所有上取整区间,只需要 ...

  2. 刘永富的Office/VBA/VSTO开发资源分享

    各种常用安装包下载:https://share.weiyun.com/5PCvqY4 简称 文件名称 描述信息 视频课程 虚拟光驱软件Daemon DAEMON_Tools_Lite_V10.1.0. ...

  3. js获取前n天或后n天的天数

    1获取n天后的某天的日期 <!DOCTYPE html> js获取前n天或者后n天的天数 </style> <body > <h1 id="date ...

  4. UML- 其他需求制品有哪些?

     1.其他需求 补充性规格说明(非功能性需求):性能/稳定性.文档.报表.许可授权等. 词汇表 设想:执行摘要. 业务规则(领域规则):如税法 2.准则 初始阶段无需对其他需求彻底分析.但花费一定时间 ...

  5. elasticsearch 大集群,双重别名,滚动更新分词方案

    elasticsearch 滚动更新分词 国内用ik.hanlp.ansj或基于其二次开发的比较多 必然有分词变更的操作(主要是是加词) reindex+别名可以解决一部分问题,但在大集群上会影响业务 ...

  6. maven 设置pom 指定jdk版本

    <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</acti ...

  7. [LC] 273. Integer to English Words

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  8. C# 接口练习

    #define debug using System; using System.Collections; namespace ConsoleApp1 { interface IAnimal { in ...

  9. SpringBoot1.X /2.X使用@Component注解注入为java.lang.NullPointException 问题小结

    问题简述: 我们在使用Spring系的产品的时候总是想当然的以为所有指定的Bean都会让Spring来管理,其实不然,即使是Spring出品的产品也不全是的哦,以下是我遇到的一个使用@Componen ...

  10. maven中指定build一个project中几个特定的子modules

    问题由来: 一个项目可能会有多个子module,在特定情况下可能只需要build其中几个module. 例如我的项目的目录结构如下 myproject |------------module_one ...