leet-code/src/main/java/leetcode/editor/cn/GetMaximumInGeneratedArray.java

93 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个整数 n 。按下述规则生成一个长度为 n + 1 的数组 nums
//
//
// nums[0] = 0
// nums[1] = 1
// 当 2 <= 2 * i <= n 时nums[2 * i] = nums[i]
// 当 2 <= 2 * i + 1 <= n 时nums[2 * i + 1] = nums[i] + nums[i + 1]
//
//
// 返回生成数组 nums 中的 最大 值。
//
//
//
// 示例 1
//
//
//输入n = 7
//输出3
//解释:根据规则:
// nums[0] = 0
// nums[1] = 1
// nums[(1 * 2) = 2] = nums[1] = 1
// nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
// nums[(2 * 2) = 4] = nums[2] = 1
// nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
// nums[(3 * 2) = 6] = nums[3] = 2
// nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
//因此nums = [0,1,1,2,1,3,2,3],最大值 3
//
//
// 示例 2
//
//
//输入n = 2
//输出1
//解释根据规则nums[0]、nums[1] 和 nums[2] 之中的最大值是 1
//
//
// 示例 3
//
//
//输入n = 3
//输出2
//解释根据规则nums[0]、nums[1]、nums[2] 和 nums[3] 之中的最大值是 2
//
//
//
//
// 提示:
//
//
// 0 <= n <= 100
//
// Related Topics 数组 动态规划 模拟 👍 40 👎 0
package leetcode.editor.cn;
//1646:获取生成数组中的最大值
class GetMaximumInGeneratedArray {
public static void main(String[] args) {
//测试代码
Solution solution = new GetMaximumInGeneratedArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int getMaximumGenerated(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int max = 1;
int[] arr = new int[n + 1];
arr[0] = 0;
arr[1] = 1;
for (int i = 2; i <= n; i++) {
int index = i / 2;
if (i % 2 == 0) {
arr[i] = arr[index];
} else {
arr[i] = arr[index] + arr[index + 1];
max = Math.max(max, arr[i]);
}
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}