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 ...
随机推荐
- ionic系列教程 2 ---- 安装
开发平台注意点首先,我们需要注意构建Ionic App需要的最低配置:Ionic只支持iOS6 +和Android 4.0 + ,(虽然2.3可以工作,但会有点卡).但是,Android设备众多,可能 ...
- 使用 shell 脚本自动获取发版指标数据
问题背景 大一点的公司都会建立一套规章流程来避免低级错误,例如合入代码前必需经过同行评审:上线前必需提测且通过 QA 验证:全量前必需经过 1%.5%.10%.20%.50% 的灰度过程.尤其是最后一 ...
- pgpool-II 4.3 中文手册-前言
什么是 Pgpool-II? Pgpool II 管理一个 PostgreSQL 服务器池,以实现单个 PostgreSQL 安装无法实现的一些功能.这些功能包括: 高可用 Pgpool-II 通过使 ...
- 手机USB共享网络是个啥
智能手机一般都提供了USB共享网络的功能,将手机通过USB线与电脑连接,手机端开启『USB共享网络』,电脑就能通过手机上网. 手机端开启『USB共享网络』: 电脑端出现新的网络连接: 通过设备管理器看 ...
- 请收藏,Linux 运维必备的 40 个命令总结,收好了~
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 1.删除0字节文件 find -type f -size ...
- grafana v8.0+ 隐藏表格字段
Select panel title → Inspect → Panel JSON Set "type" to "table-old" Apply The vi ...
- JS 加载
DOM 加载完毕后执行,不需要等待image.js.css.iframe等加载 1.$(function() {}) 2.$(document).ready(function() {}) 不要写成 ...
- vue - Vue路由
至此基本上vue2.0的内容全部结束,后面还有点elementUI和vue3.0的内容过几天再来更新. 这几天要回学校去参加毕业答辩,断更几天 一.相关理解 是vue的一个插件库,专门用来实现spa( ...
- 论文解读(ClusterSCL)《ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs》
论文信息 论文标题:ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs论文作者:Yanling Wang, Jing ...
- install dns server on ubuntu
参考 CSDN/Ubuntu环境下安装和配置DNS服务器 在 Ubuntu 上安裝 DNS server Install BIND 9 on Ubuntu and Configure It for U ...