类似于背包问题,前提条件是数组全是正整数和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. 洛谷P1435 回文子串

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

  2. Python3+Pycharm+PyQt5环境搭建

    操作系统:Windows 10 Python版本:3.7及以上版本均可 PyCharm:PyCharm 2019.3 1.安装 PyQt5 及其拓展工具. pip install pyqt5 pip ...

  3. Huffman编码实验

    一. 实验目的 熟练掌握哈夫曼树的建立和哈夫曼编码的算法实现. 二. 实验内容 根据哈夫曼编码的原理,编写一个程序,在用户输入结点权值的基础上求赫夫曼编码,并能把给定的编码进行译码. 三. 实验要求 ...

  4. 问题:pip命令安装好的库,pycharm却显示没有这个库

    问题: 今天发现pycharm内部安装库出了问题,导致无法安装各种库,我就在cmd下用自己安装好的pip安装各个库,成功安装后发现各个库在idle中可以成功的import,但在pycharm里却显示没 ...

  5. iOS之NSString类型为什么要用copy修饰

    在开发的过程中,只知道NSString类型最好用copy修饰而不能用strong,但是不知道为什么,今天了解了下,总算搞明白了. 如下所示,当修饰符为copy时,因为NSMutableString是N ...

  6. JavaWeb过滤器(Filter)

    参考:https://blog.csdn.net/yuzhiqiang_1993/article/details/81288912 原理: 一般实现流程: 1.新建一个类,实现Filter接口2.实现 ...

  7. crontab不执行service命令

    我这里的需求是每30分钟重启一次 写成下面的格式就可以正确执行了,后面执行的命令写绝对路径 */30 * * * * /usr/bin/supervisorctl restart all

  8. KMP匹配(模板)

    先粘上我入门KMP时看的大佬的博客:orz orz 从头到尾彻底理解KMP 我觉得这篇已经讲的很详细了,希望大家能坚持看下去. 步骤 ①寻找前缀后缀最长公共元素长度对于P = p0 p1 ...pj- ...

  9. 吴裕雄--天生自然C语言开发:循环

    while(condition) { statement(s); } #include <stdio.h> int main () { /* 局部变量定义 */ ; /* while 循环 ...

  10. TPO4-1 Deer Populations of the Puget Sound

    The causes of this population rebound are consequences of other human actions. First, the major pred ...