-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package leetcode; | ||
|
||
/** | ||
* 1413. 逐步求和得到正数的最小值 | ||
* 给你一个整数数组 nums 。你可以选定任意的 正数 startValue 作为初始值。 | ||
* | ||
* 你需要从左到右遍历 nums 数组,并将 startValue 依次累加上 nums 数组中的值。 | ||
* | ||
* 请你在确保累加和始终大于等于 1 的前提下,选出一个最小的 正数 作为 startValue 。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* 输入:nums = [-3,2,-3,4,2] | ||
* 输出:5 | ||
* 解释:如果你选择 startValue = 4,在第三次累加时,和小于 1 。 | ||
* 累加求和 | ||
* startValue = 4 | startValue = 5 | nums | ||
* (4 -3 ) = 1 | (5 -3 ) = 2 | -3 | ||
* (1 +2 ) = 3 | (2 +2 ) = 4 | 2 | ||
* (3 -3 ) = 0 | (4 -3 ) = 1 | -3 | ||
* (0 +4 ) = 4 | (1 +4 ) = 5 | 4 | ||
* (4 +2 ) = 6 | (5 +2 ) = 7 | 2 | ||
* 示例 2: | ||
* | ||
* 输入:nums = [1,2] | ||
* 输出:1 | ||
* 解释:最小的 startValue 需要是正数。 | ||
* 示例 3: | ||
* | ||
* 输入:nums = [1,-2,-3] | ||
* 输出:5 | ||
* | ||
* | ||
* 提示: | ||
* | ||
* 1 <= nums.length <= 100 | ||
* -100 <= nums[i] <= 100 | ||
* @author chenzw | ||
* @date 2022/8/9 | ||
*/ | ||
public class Solution1413 { | ||
public int minStartValue(int[] nums) { | ||
int min = Integer.MAX_VALUE; | ||
int sum = 0; | ||
for(int i:nums){ | ||
sum+=i; | ||
min = Math.min(min,sum); | ||
} | ||
return min>=0?1:1-min; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package leetcode; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 174. 地下城游戏 | ||
* 一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。 | ||
* | ||
* 骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡。 | ||
* | ||
* 有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数);其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔法球(若房间里的值为正整数,则表示骑士将增加健康点数)。 | ||
* | ||
* 为了尽快到达公主,骑士决定每次只向右或向下移动一步。 | ||
* | ||
* | ||
* | ||
* 编写一个函数来计算确保骑士能够拯救到公主所需的最低初始健康点数。 | ||
* | ||
* 例如,考虑到如下布局的地下城,如果骑士遵循最佳路径 右 -> 右 -> 下 -> 下,则骑士的初始健康点数至少为 7。 | ||
* | ||
* -2 (K) -3 3 | ||
* -5 -10 1 | ||
* 10 30 -5 (P) | ||
* | ||
* | ||
* 说明: | ||
* | ||
* 骑士的健康点数没有上限。 | ||
* | ||
* 任何房间都可能对骑士的健康点数造成威胁,也可能增加骑士的健康点数,包括骑士进入的左上角房间以及公主被监禁的右下角房间。 | ||
* @author chenzw | ||
* @date 2022/8/8 | ||
*/ | ||
public class Solution174 { | ||
public int calculateMinimumHP(int[][] dungeon) { | ||
int m = dungeon.length; | ||
int n = dungeon[0].length; | ||
int[][] dp = new int[m+1][n+1];//dp[i][j]表示从(i,j)到(m-1,n-1)点需要的最小初始值 | ||
Arrays.fill(dp[m],Integer.MAX_VALUE); | ||
for(int i=0;i<=m;i++){ | ||
dp[i][n] = Integer.MAX_VALUE; | ||
} | ||
dp[m][n-1] = 1; | ||
dp[m-1][n] = 1; | ||
for(int i=m-1;i>=0;i--){ | ||
for(int j=n-1;j>=0;j--){ | ||
dp[i][j] = Math.max(Math.min(dp[i+1][j],dp[i][j+1])-dungeon[i][j],1); | ||
} | ||
} | ||
return dp[0][0]; | ||
} | ||
} |