当前位置:网站首页>数组的子集不能累加出的最小正数
数组的子集不能累加出的最小正数
2022-07-29 06:05:00 【小卢要刷力扣题】
前言
给定一个正数数组arr,
返回arr的子集不能累加出的最小正数
1)正常怎么做?
2)如果arr中肯定有1这个值,怎么做?
问题一解决思路

arr所有值的累加和从一个负数到一个整数做出一张表,然后看最后一行
arr 0~N-1最后一行的值能不能搞定1, 2, 3…哪一个最早不行的,返回就是答案
public static int unformedSum2(int[] arr) {
if (arr == null || arr.length == 0) {
return 1;
}
int sum = 0;
int min = Integer.MAX_VALUE;
for (int i = 0; i != arr.length; i++) {
sum += arr[i];
min = Math.min(min, arr[i]);
}
// boolean[][] dp ...
int N = arr.length;
boolean[][] dp = new boolean[N][sum + 1];
for (int i = 0; i < N; i++) {
// arr[0..i] 0
dp[i][0] = true;
}
dp[0][arr[0]] = true;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= sum; j++) {
dp[i][j] = dp[i - 1][j] || ((j - arr[i] >= 0) ? dp[i - 1][j - arr[i]] : false);
}
}
for (int j = min; j <= sum; j++) {
if (!dp[N - 1][j]) {
return j;
}
}
return sum + 1;
}
题目二题解
先把array排序,正数数组排完序,左边0位置肯定是1
定义变量range =1,表示从1~ 1范围上的正数都能累加出来
range=k,代表1~k上的所有正数都能搞出来
当arr 0位置是1的情况下,range=1,代表1~ 1范围的正数都可以搞出来
如果1位置也是1, range变成2,代表1 ~2范围的正数都可以搞出来
如果2位置也是2, range变成4,代表1 ~4范围的正数都可以搞出来

如果0~ i-1是0~100, range=100
i位置17,可以让range扩充到117
如果0~ i-1能搞定的数是1~100,此时i位置是102,那么101不可以搞定
如果0~ i搞定1~a,如果位置上是b:
1如果b<=a+1,能扩充范围到1~a+b
2咖如果b> a+1. a+1就是搞定不了的最小正整数
public static int unformedSum3(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
Arrays.sort(arr); // O (N * logN)
int range = 1;
// arr[0] == 1
for (int i = 1; i != arr.length; i++) {
if (arr[i] > range + 1) {
return range + 1;
} else {
range += arr[i];
}
}
return range + 1;
}
边栏推荐
- Difference between CNAME record and a record
- Windows 上 php 7.4 连接 oracle 配置
- Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
- Teacher wangshuyao wrote the notes of operations research course 00 in the front
- 王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
- Share some tips for better code, smooth coding and improve efficiency
- pytest合集(7)— 参数化
- 上采样之反卷积操作
- SSH免密登录-两台虚拟机建立免密通道 双向信任
- Jetpack Compose 中的键盘处理
猜你喜欢

IDEA找不到Database解决方法

剑指 Offer II 115:重建序列

Windows 上 php 7.4 连接 oracle 配置

Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)

Is online legend software testing training really so black hearted? Are they all scams?

Basic knowledge of MySQL (high frequency interview questions)

Unity探索地块通路设计分析 & 流程+代码具体实现

Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码

vim文本编辑器的一些使用小技巧

vscode通过remotessh结合xdebug远程调试php解决方案
随机推荐
mysql查询区分大小写
Cvpr2022oral special series (I): low light enhancement
DM data guard cluster setup
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
How to write controller layer code gracefully?
buck电路boot电容短路和断路实测波形
Software definition boundary SDP
MySql基础知识(高频面试题)
我的创业邻居们
Teacher wangshuyao's notes on operations research 01 guidance and introduction
[cf1054h] epic Revolution -- number theory, convolution, arbitrary modulus NTT
leetcode-1331:数组序号转换
DM数据守护集群搭建
Summary of 2022 SQL classic interview questions (with analysis)
Teacher Wu Enda's machine learning course notes 02 univariate linear regression
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
Unity exploration plot access design analysis & process + code specific implementation
【C语言刷LeetCode】67. 二进制求和(E)
吴恩达老师机器学习课程笔记 01 引言