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 ...
随机推荐
- vue--vuex 状态管理模式
前言 vuex作为vue的核心插件,同时在开发中也是必不可少的基础模块,本文来总结一下相关知识点. 正文 1.基于单向数据流问题而产生了Vuex 单向数据流是vue 中父子组件的核心概念,props ...
- 十、包机制与JavaDoc
一.包机制 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间. 包语句的语句格式为: package pkg1[. pkg2[. pkg3...]]; 一般使用公司域名倒置作为包名:例如 ...
- grpc流模式-go实现
目录 1. 什么是数据流 2. grpc的四种数据流 2.1 简单模式 2.2 服务端数据流模式 2.3 客户端数据流模式 2.4 双向数据流 3. 上代码 3.1 代码目录 3.2 编写stream ...
- Python标准库tempfile的使用总结
Python标准库tempfile的使用总结 临时文件是计算机程序存储临时数据的文件,它的扩展名通常是".temp".本文用于记录使用Python提供的临时文件API解决实际问题的 ...
- Python工程打包
Python项目打包 我是自己写了一个项目,然后需要打包成问一个exe文件,这样直接打开这个文件就可以运行,而不需要在pycharm中打开相应文件才能运行,也可以将打包好的文件发给其他人,不需要pyc ...
- Visual Studio 修改NuGet 包缓存路径
Visual Studio 下载的NuGet包默认会缓存到 C:\Users{Windows用户名}.nuget\packages 下,时间一长就会导致 C盘空间严重不足. 那么怎样去设置,让包缓存文 ...
- UART串口及Linux实现
UART,全称Universal Asynchronous Receiver Transmitter,通用异步收发器,俗称串口.作为最常用的通信接口之一,从8位单片机到64位SoC,一般都会提供UAR ...
- ansible模块的介绍与使用
ansible-doc的使用 1.ansible-doc -h可以看见ansible-doc的所有参数 2.ansible-doc 命令格式:ansible-doc [-l|-F|-s] [optio ...
- 理解 Angular 服务
理解 Angular 服务 本文写于 2021 年 3 月 29 日 理解 Angular 服务 什么是服务 服务写法 原理简述 提供服务 1. 在服务中注册 2. 在 module 中注册 3. 在 ...
- arts-week11
Algorithm 69. Sqrt(x) - LeetCode Review Building a network attached storage device with a Raspberry ...