0%

leetcode每日一题-跳水板

跳水板

image1

思想很简单,没啥好说的了,就直接放代码吧。

1
class Solution {
2
    public int[] divingBoard(int shorter, int longer, int k) {
3
        if(k==0){
4
            return new int[0];
5
        }
6
        if(shorter == longer){
7
            return new int[]{k*shorter};
8
        }
9
        int[] ans = new int[k+1];
10
        for(int i=0; i<=k; i++){
11
            ans[i] = shorter*(k-i) + longer*i;
12
        }
13
        return ans;
14
    }
15
}

复杂度分析

  • 时间复杂度:$O(k)$
  • 空间复杂度:$O(k)$