comments | difficulty | edit_url |
---|---|---|
true |
Easy |
You are building a diving board by placing a bunch of planks of wood end-to-end. There are two types of planks, one of length shorter
and one of length longer
. You must use exactly K
planks of wood. Write a method to generate all possible lengths for the diving board.
return all lengths in non-decreasing order.
Example:
Input: shorter = 1 longer = 2 k = 3 Output: {3,4,5,6}
Note:
- 0 < shorter <= longer
- 0 <= k <= 100000
If
If
Otherwise, we can use a board with length
The time complexity is
class Solution:
def divingBoard(self, shorter: int, longer: int, k: int) -> List[int]:
if k == 0:
return []
if longer == shorter:
return [longer * k]
ans = []
for i in range(k + 1):
ans.append(longer * i + shorter * (k - i))
return ans
class Solution {
public int[] divingBoard(int shorter, int longer, int k) {
if (k == 0) {
return new int[0];
}
if (longer == shorter) {
return new int[] {longer * k};
}
int[] ans = new int[k + 1];
for (int i = 0; i < k + 1; ++i) {
ans[i] = longer * i + shorter * (k - i);
}
return ans;
}
}
class Solution {
public:
vector<int> divingBoard(int shorter, int longer, int k) {
if (k == 0) return {};
if (longer == shorter) return {longer * k};
vector<int> ans;
for (int i = 0; i < k + 1; ++i)
ans.push_back(longer * i + shorter * (k - i));
return ans;
}
};
func divingBoard(shorter int, longer int, k int) []int {
if k == 0 {
return []int{}
}
if longer == shorter {
return []int{longer * k}
}
var ans []int
for i := 0; i < k+1; i++ {
ans = append(ans, longer*i+shorter*(k-i))
}
return ans
}
function divingBoard(shorter: number, longer: number, k: number): number[] {
if (k === 0) {
return [];
}
if (longer === shorter) {
return [longer * k];
}
const ans: number[] = [k + 1];
for (let i = 0; i <= k; ++i) {
ans[i] = longer * i + shorter * (k - i);
}
return ans;
}
class Solution {
func divingBoard(_ shorter: Int, _ longer: Int, _ k: Int) -> [Int] {
if k == 0 {
return []
}
if shorter == longer {
return [shorter * k]
}
var ans = [Int](repeating: 0, count: k + 1)
for i in 0...k {
ans[i] = longer * i + shorter * (k - i)
}
return ans
}
}