Leetcode----<Diving Board LCCI>

题解如下:
public class DivingBoardLCCI {
/**
* 暴力解法,遍历每一种可能性 时间复杂度:O(2*N)
* @param shorter
* @param longer
* @param k
* @return
*/
public int[] divingBoard(int shorter, int longer, int k) {
if (k==0) {
return new int[0];
}
TreeSet<Integer> result = new TreeSet<>();
for (int i = 0; i <= k; i++) {
result.add(shorter * i + longer * k - i);
}
int[] ints = new int[result.size()];
AtomicInteger tag = new AtomicInteger(0);
result.stream().forEach(ele -> {
ints[tag.get()] = ele;
tag.getAndIncrement();
});
return ints;
}
/**优化
* 解法二:优化逻辑思维
* 理解:
* 总共有k块木板
* 1. 当shorter==longer 只会有一种情况
* 2. 当shorter!=longer 我们可以从shorter或longer长度的木板中的一个来看,他们可能被使用的情况有0、1、2、、、n 总共(n+1)种情况
* 而且这n种情况下获得到值也不一样,因为每种情况shorter和longer的个数都不一样[当shorter!=longer时,shorter和longer的个数不一样那么最终的值也会不一样]
* @param shorter
* @param longer
* @param k
* @return
*/
public int[] divingBoard2(int shorter, int longer, int k) {
if (k == 0) {
return new int[0];
}
int len;
if (shorter == longer) {
len = 1;
} else {
len = k + 1;
}
int[] result = new int[len];
for (int i = 0; i < len; i++) {
result[i] = shorter * (k - i) + longer * i;
}
return result;
}
}
Leetcode----<Diving Board LCCI>的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 1.Markdown语法
Markdown学习 一.标题:(# +标题名字) 标题 三级标题 四级标题 二.字体 (空格内容前后的空格删掉) Hello,World! **粗体** Hello,World! *斜体* Hell ...
- Abp 实现通过手机号注册用户
前言 Abp 的 Identity 模块,实现了用户的管理,但是对于国内来讲,很多场景不能很好适配.比如:通过手机号进行注册的场景. Abp vnext Identity 以及 asp.net cor ...
- 2021.07.17 题解 CF1385E Directing Edges(拓扑排序)
2021.07.17 题解 CF1385E Directing Edges(拓扑排序) CF1385E Directing Edges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) ...
- Math内置对象 常用的方法
属性: Math.Pi 方法: Math.max() 最大值 Math.min() 最小值 Math.ceil() 向上取整 Math.floor() 向下取整 Math.random() ...
- 6.1 SHELL脚本
6.1 SHELL脚本元素 第一行的脚本声明(#!)用来告诉系统使用哪种Shell解释器来执行该脚本: 第二行的注释信息(#)是对脚本功能和某些命令的介绍信息,使得自己或他人在日后看到这个脚本内容时, ...
- 跟我读CVPR 2022论文:基于场景文字知识挖掘的细粒度图像识别算法
摘要:本文通过场景文字从人类知识库(Wikipedia)中挖掘其背后丰富的上下文语义信息,并结合视觉信息来共同推理图像内容. 本文分享自华为云社区<[CVPR 2022] 基于场景文字知识挖掘的 ...
- Go学习-基本语法(一)
前言 一直对Service Mesh相关内容比较感兴趣,后面一路学习了Dcoker.Kubernetes等相关内容,可以说是对基本概念和使用有一定了解,随着开始学习一些相关的组件的时候,发现基本上全部 ...
- PTA刷题笔记
PTA刷题记录 仓库地址: https://github.com/Haorical/Code/tree/master/PTA/GPLT 两周之内刷完GPLT L2和L3的题,持续更新,包括AK代码,坑 ...
- 生命周期和作用域 & mybatis执行流程
流程 sqlSessionFactory 实例化后 --> transactional事务管理-->创建executor执行器-->创建SqlSession-->实现增删改查 ...
- 基于SqlSugar的数据库访问处理的封装,在.net6框架的Web API上开发应用
我前面几篇随笔介绍了关于几篇关于SqlSugar的基础封装,已经可以直接应用在Winform项目开发上,并且基础接口也通过了单元测试,同时测试通过了一些Winform功能页面:本篇随笔继续深化应用开发 ...