题解如下:

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>的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. go源码阅读 - container/ring

    相比于List,环的结构有些特殊,环的头部就是尾部,所以每个元素可以代表自身这个环.环其实是一个双向回环链表.type Ring struct { next, prev *Ring Value int ...

  2. Mozi.HttpEmbedded嵌入式Web服务器

    Mozi.HttpEmbedded 嵌入式Web服务器 项目介绍 Mozi.HttpEmbedded是一个基于.Net构建的嵌入式Web服务器,为.Net App提供web服务功能. 嵌入式的目标不是 ...

  3. Jquery_效果-隐藏显示、淡入淡出、滑动面板、简单的动画队列

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  4. Fastjson tomcat-dhcp链

    Fastjson tomcat-dbcp链 这条链可直接回显,可以解决fastjson在内网的情况,因为很多实战的时候,fastjson的应用部署在内网,只映射一个端口出来,导致前面学习的jdbcRo ...

  5. 《手把手教你》系列基础篇(九十二)-java+ selenium自动化测试-框架设计基础-POM设计模式简介(详解教程)

    1.简介 页面对象模型(Page Object Model)在Selenium Webdriver自动化测试中使用非常流行和受欢迎,作为自动化测试工程师应该至少听说过POM这个概念.本篇介绍POM的简 ...

  6. Python抽象基类:ABC谢谢你,因为有你,温暖了四季!

    Python抽象基类:ABC谢谢你,因为有你,温暖了四季! Python抽象基类:ABC谢谢你,因为有你,温暖了四季! 实例方法.类方法和静态方法 抽象类 具名元组 参考资料 最近阅读了<Pyt ...

  7. Oracle 错误表

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  8. day01-从一个基础的socket服务说起

    首发地址:day01-从一个基础的socket服务说起 教程说明:C++高性能网络服务保姆级教程 本节目的 实现一个基于socket的echo服务端和客户端 服务端监听流程 第一步:使用socket函 ...

  9. windows 安装 kalfka 并快速启动

    1.安装Java 环境 https://www.java.com/zh_CN/ 直接下载安装即可 (如果之前有配置过java环境 可以先跳过此步骤,但是如果运行的时候报错就需要把之前的jdk环境变量删 ...

  10. Ubuntu环境Docker+K8s+Dashboard的安装配置(无坑亲测)

    安装之前的准备: 安装docker 使用国内 daocloud 一键安装命令: curl -sSL https://get.daocloud.io/docker | sh 直接从dockerhub下载 ...