实现子数组和绝对值差最小 - Objective-C
类似于背包问题,前提条件是数组全是正整数和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的更多相关文章
- 连续子数组的和的绝对值的最大值、最小值(非绝对值的话直接dp动态规划)
前缀和的思路: sum[i] = num[0]+num[1]+......+num[i-1] sum[j] = num[0]+num[1]+......+num[j-1] 那么:num[i]+num[ ...
- [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 ...
- 《剑指Offer》- 连续子数组的最大和或最小和
前言 本文是<剑指Offer>系列(JavaScript版)的第一篇,题目是"连续子数组的最大和或最小和". 话不多说,开始"打怪"修炼... 一. ...
- 双指针之滑动窗口(长度最小的子数组 和 和为s的连续正数序列)
双指针之滑动窗口 (长度最小的子数组:和为s的连续正数序列) 1, 什么时候使用? (与子数组/字符串 有关的题目)~如果给了某个具体值的target,即用滑动窗口 不然就双指针(一般做法,左边< ...
- 209. 长度最小的子数组--LeetCode
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/minimum-size-subarray-sum 著作权归领扣网络所有.商业转载请联系官方授权,非商业 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- lintcode循环数组之连续子数组求和
v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...
- 【剑指offer】连续子数组的最大和
个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...
- 最大值减去最小值小于或等于num的子数组数量
[说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“最大值减去最小值小于或等于num的子数组数量”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路, ...
随机推荐
- AQS: 什么是AQS?
AQS定义了一套多线程访问共享资源的同步器框架. 许多同步类实现都依赖于它,如常用的ReentrantLock/ReentrantReadWriterLock/CountDownLatch这些类里面都 ...
- 利用CSS制作背景变色的横向导航栏
1.表单 页面如下: <html> <head> <title>注册表单页面</title> </head> <body> &l ...
- Linux 创建静态库.a
gcc -c 只编译不连接 -o *.o(生成.o文件) ar crv name.a *.o *.o (ar 命令把 .o文件打包成 name.a 静态库) 测试 name.a -L 紧跟链 ...
- 线程池-进程池-io模型
一.线程池与进程池 什么是池?简单的说就是一个容器,一个范围 在保证计算机硬件安全的情况下最大限度的充分利用计算机, 池其实是降低了程序的运行效率,但是保证了计算机硬件的安全,也是实现了一个并发的效果 ...
- Nginx_安全1
Nginx 安全 nginx隐藏版本号 # 在Nginx的配置文件中进行修改,增加下面这个. server_tokens on; nginx对IP和目录限速 # Nginx可以通过HTTPLimitZ ...
- day36-进程操作实例,守护进程,方法,属性
#1.server端跟多个client端聊天: #异步操作,主进程负责接收client的连接,子进程负责跟client聊天. #每接收一个连接,就创建一个子进程,子进程之间的数据是隔离的,互不影响,所 ...
- text-overflow属性
text-overflow属性有两个值, 默认值是clip:当对象内文本溢出时不显示裁切掉. 另一个就是:ellipsis:对象内文本溢出时显示省略标记(...). 使用text-overflow:e ...
- python-django电商项目-需求分析架构设计数据库设计_20191115
python-django电商项目需求分析 1.用户模块 1)注册页 注册时校验用户名是否已被注册. 完成用户信息的注册. 给用户的注册邮箱发送邮件,用户点击邮件中的激活链接完成用户账户的激活. 2) ...
- 扩增|feather evolution
Wool vs feather 扩增方法:1.Gene Duplication2.Genome Duplication3.Cluster 哺乳动物毛发和鸟类的羽毛,都来自于角蛋白. 羽毛进化图 DNA ...
- TPO4-1 Deer Populations of the Puget Sound
The causes of this population rebound are consequences of other human actions. First, the major pred ...