摘要: 贪心 import java.util.Arrays; class Solution { public int largestSumAfterKNegations(int[] nums, int k) { /** * 先将数组排序,让负数在前面,然后只反转负数 */ Arrays.sort(nums 阅读全文
posted @ 2022-02-25 17:48 振袖秋枫问红叶 阅读(19) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public int jump(int[] nums) { /** * 如果只有一个元素,那不用走 */ if (nums.length == 1){ return 0; } /** * curMax记录在第i个元素能走到的最远距离 * max记录在curMa 阅读全文
posted @ 2022-02-25 17:16 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public boolean canJump(int[] nums) { if (nums.length == 1){ return true; } int max= 0; /** * 遍历数组,实时更新所能达到的最大距离 * 如果遇到nums[i] == 0 阅读全文
posted @ 2022-02-25 10:57 振袖秋枫问红叶 阅读(7) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int maxProfit(int[] prices) { int[][] dp = new int[prices.length][2]; dp[0][0] = -prices[0]; dp[0][1] = 0; /** * 和《121. 买 阅读全文
posted @ 2022-02-25 10:25 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public int maxSubArray(int[] nums) { /** * 元素中有负数,因此max初始值取负数最小值 */ int max = -Integer.MAX_VALUE; int sum = 0; for (int i = 0; i < 阅读全文
posted @ 2022-02-25 09:55 振袖秋枫问红叶 阅读(19) 评论(0) 推荐(0)