题解如下:

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. 安卓记账本开发学习day5之版本兼容问题

    安卓5.0以上版本想要隐藏DatePicker头布局的写法比较复杂,需要一层一层隐藏 int headerId = getContext().getResources().getIdentifier( ...

  2. 将border 边框换成图片 border-image

    <template>   <div class="heart"></div> </template> <script> ...

  3. Educational Codeforces Round 116 (Rated for Div. 2), problem: (C) Banknotes

    传送门 Problem - C - Codeforces 题目 题目重点内容手打翻译:(欢迎批评指正) 在柏林, 使用着n套不同的货币(banknotes).第i个货币面额为10ai 元,货币的第一种 ...

  4. Apache Doris 通过ODBC连接SQL Server

    社区有小伙伴有使用Doris ODBC外表连接SQL Server数据库,使用中遇到不知道驱动怎么安装,苦于我这边也没有SQL Server的环境,正好社区有用户使用了这个数据库,也安装ODBC驱动测 ...

  5. 技术管理进阶——一线Leader与大Leader的差异是什么?

    原创不易,求分享.求一键三连 ​ 前几年,因为下属的一些骚操作,Leader对我产生了误会,这导致了一些"不信任",这种不信任在短时间会让人丧失安全感,很多人会因此患得患失. 这种 ...

  6. XCTF练习题---MISC---坚持60S

    XCTF练习题---MISC---坚持60S flag:flag{DajiDali_JinwanChiji} 解题步骤: 1.观察题目,下载附件,是一个java文件 2.打开玩了一会,真鸡儿难,试着反 ...

  7. ViewFlipper 简单应用

    AdvserView.java package com.earen.viewflipper; import android.content.Context; import android.graphi ...

  8. Ajax学习笔记demo

    AJAX学习 AJAX简介: 全称:Asynchronous JavaScript and XML (JavaScript执行异步网络请求(JS和XML)),通过AJAX可以在浏览器向服务器发送异步请 ...

  9. 红旗 Linux 桌面操作系统11来了:支持国产自主CPU,全新UI风格设计,兼容面广...

    链接:https://reurl.cc/g8ke9X 红旗Linux桌面操作系统11将于1月10日开放预览版的下载,新版本具有良好的硬件兼容,支持多款国产自主CPU品牌,同时还具有丰富的外设支持及海量 ...

  10. vue3 vite 系统标题 系统名称统一配置

    想要统一配置系统名称 或者其他的,需要在vue3中使用 vite 的环境变量 vite 的环境变量 需要创建两个文件(和 vite.config.js 文件同一目录) .env.development ...