-
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.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
4 changed files
with
151 additions
and
2 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,24 @@ | ||
package com.gatsby; | ||
|
||
/** | ||
* @author -- gatsby | ||
* @date -- 2022/7/22 | ||
* @description -- leetcode 1529 最少的后缀翻转次数 | ||
*/ | ||
|
||
|
||
public class _1529MinimumSuffixFlips { | ||
public int minFlips(String target) { | ||
if (target.length() == 0) return 0; | ||
int count = 0; | ||
char base = target.charAt(0); | ||
if (base == '1') count++; | ||
for (int i = 1; i < target.length(); ++i) { | ||
if (target.charAt(i) != base) { | ||
count++; | ||
base = target.charAt(i); | ||
} | ||
} | ||
return count; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/com/gatsby/_2171RemovingMinimumNumberOIfMagicBeans.java
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,33 @@ | ||
package com.gatsby; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author -- gatsby | ||
* @date -- 2022/7/9 | ||
* @description -- 2171. 拿出最少数目的魔法豆 | ||
*/ | ||
|
||
|
||
public class _2171RemovingMinimumNumberOIfMagicBeans { | ||
public long minimumRemoval(int[] beans) { | ||
Arrays.sort(beans); | ||
long res = Integer.MAX_VALUE; | ||
long sum = 0; | ||
// Set<Integer> beanSet = new HashSet<>(); | ||
for (int bean : beans) { | ||
sum += bean; | ||
// beanSet.add(bean); | ||
} | ||
int n = beans.length; | ||
// for (int bean: beanSet) { | ||
// res = Math.min(res, sum - ()) | ||
// } | ||
for (int i = 0; i < n; ++i) { | ||
System.out.println(beans[i]); | ||
if (i != n - 1 && beans[i] == beans[i + 1]) continue; | ||
res = Math.min(res, sum - (long) beans[i] * (beans.length - i)); | ||
} | ||
return res; | ||
} | ||
} |
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,46 @@ | ||
package com.gatsby.offerII; | ||
|
||
/** | ||
* @author -- gatsby | ||
* @date -- 2022/7/16 | ||
* @description -- 剑指 Offer II 099. 最小路径之和 | ||
*/ | ||
|
||
|
||
public class _99MinPathSum { | ||
public int minPathSum(int[][] grid) { | ||
int row = grid.length; | ||
int col = grid[0].length; | ||
if (row == 1) { | ||
int sum = 0; | ||
for (int i : grid[0]) { | ||
sum += i; | ||
} | ||
return sum; | ||
} | ||
|
||
if (col == 1) { | ||
int sum = 0; | ||
for (int[] i : grid) { | ||
sum += i[0]; | ||
} | ||
return sum; | ||
} | ||
|
||
int[][] dp = new int[row][col]; | ||
dp[0][0] = grid[0][0]; | ||
for (int i = 1; i < row; ++i) { | ||
dp[i][0] = grid[i][0] + dp[i - 1][0]; | ||
} | ||
for (int i = 1; i < col; ++i) { | ||
dp[0][i] = dp[0][i - 1] + grid[0][i]; | ||
} | ||
|
||
for (int i = 1; i < row; ++i) { | ||
for (int j = 1; j < col; ++j) { | ||
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; | ||
} | ||
} | ||
return dp[row - 1][col - 1]; | ||
} | ||
} |
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