Skip to content

Commit

Permalink
add 16_11
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Jul 8, 2020
1 parent 81ed96d commit 3f5fb9c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| 9 | [Palindrome Number][0009] | Math |
| 13 | [Roman to Integer][0013] | Math, String |
| 14 | [Longest Common Prefix][0014] | String |
| 16.11| [跳水板(Diving Board LCCI)][16_11] | 递归、记忆化 |
| 20 | [Valid Parentheses][0020] | Stack, String |
| 21 | [Merge Two Sorted Lists][0021] | Linked List |
| 26 | [Remove Duplicates from Sorted Array][0026] | Array, Two Pointers |
Expand Down Expand Up @@ -121,6 +122,7 @@
[0009]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0009/README.md
[0013]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0013/README.md
[0014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0014/README.md
[16_11]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/16_11/README.md
[0020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0020/README.md
[0021]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0021/README.md
[0026]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0026/README.md
Expand Down
68 changes: 68 additions & 0 deletions note/16_11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# [跳水板(Diving Board LCCI)][title]

## 题目描述

你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为`shorter`,长度较长的木板长度为`longer`。你必须正好使用`k`块木板。编写一个方法,生成跳水板所有可能的长度。

返回的长度需要从小到大排列。

**示例:**

```
输入:
shorter = 1
longer = 2
k = 3
输出: {3,4,5,6}
```

**提示:**

* 0 < shorter <= longer
* 0 <= k <= 100000

**标签:** 递归、记忆化


## 思路

这题乍一看,好像得用递归或动态规划来解,仔细一想,其实就是高中数学学过的等差数列知识。

`k == 0` 时,返回 `[]` 即可;

`shorter == longer` 时,返回 `[k * shorter]` 即可;

`shorter != longer` 时,那么其实就是一个首项为 `k * shorter`,末项为 `k * longer`,公差为 `longer - shorter` 的等差数列么;

我们根据以上情况就可以写出如下代码了:


```java
public class Solution {
public int[] divingBoard(int shorter, int longer, int k) {
if (k == 0) {
return new int[0];
}
if (shorter == longer) {
return new int[]{shorter * k};
}
int[] ans = new int[k + 1];
int st = k * shorter;// 等差数列的首项
int delta = longer - shorter;// 公差
for (int i = 0; i <= k; i++) {
ans[i] = st + i * delta;
}
return ans;
}
}
```


## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]



[title]: https://leetcode-cn.com/problems/recover-a-tree-from-preorder-traversal
[ajl]: https://github.com/Blankj/awesome-java-leetcode
34 changes: 34 additions & 0 deletions src/com/blankj/easy/_16_11/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.blankj.easy._16_11;

import java.util.Arrays;

/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2020/07/08
* desc :
* </pre>
*/
public class Solution {
public int[] divingBoard(int shorter, int longer, int k) {
if (k == 0) {
return new int[0];
}
if (shorter == longer) {
return new int[]{shorter * k};
}
int[] ans = new int[k + 1];
int st = k * shorter;// 等差数列的首项
int delta = longer - shorter;// 公差
for (int i = 0; i <= k; i++) {
ans[i] = st + i * delta;
}
return ans;
}

public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.divingBoard(1, 2, 3)));
}
}

0 comments on commit 3f5fb9c

Please sign in to comment.