leecode,rt_phm更新更新
This commit is contained in:
parent
321961372d
commit
74afd287d8
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.markilue.leecode.hot100.second;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-06-02 11:05
|
||||||
|
*@Description: TODO 力扣208 实现Trie(前缀树)
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class T65_208_Trie_2 {
|
||||||
|
|
||||||
|
boolean isEnd;
|
||||||
|
T65_208_Trie_2[] children;
|
||||||
|
|
||||||
|
public T65_208_Trie_2() {
|
||||||
|
children = new T65_208_Trie_2[26];
|
||||||
|
isEnd = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(String word) {
|
||||||
|
T65_208_Trie_2 cur = this;
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char c = word.charAt(i);
|
||||||
|
if (cur.children[c - 'a'] == null) {
|
||||||
|
cur.children[c - 'a'] = new T65_208_Trie_2();
|
||||||
|
}
|
||||||
|
cur = cur.children[c - 'a'];
|
||||||
|
}
|
||||||
|
cur.isEnd = true;//最优一个是终点
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean search(String word) {
|
||||||
|
T65_208_Trie_2 node = searchPredix(word);
|
||||||
|
return node != null && node.isEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean startsWith(String prefix) {
|
||||||
|
return searchPredix(prefix) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T65_208_Trie_2 searchPredix(String word) {
|
||||||
|
T65_208_Trie_2 cur = this;
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char c = word.charAt(i);
|
||||||
|
if (cur.children[c - 'a'] == null) {
|
||||||
|
return null;//没有
|
||||||
|
}
|
||||||
|
cur = cur.children[c - 'a'];
|
||||||
|
}
|
||||||
|
return cur;//最优一个是终点
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,7 @@ public class T81_312_MaxCoins {
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
int[] nums = {3, 1, 5, 8};
|
int[] nums = {3, 1, 5, 8};
|
||||||
System.out.println(maxCoins(nums));
|
System.out.println(maxCoins1(nums));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -62,4 +62,33 @@ public class T81_312_MaxCoins {
|
||||||
}
|
}
|
||||||
return memo[left][right];
|
return memo[left][right];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//由于戳气球会导致区间变成零散,所以可以考虑变成填气球;或者可以理解为一个区间最后一次戳的气球
|
||||||
|
public int maxCoins1(int[] nums) {
|
||||||
|
int n = nums.length;
|
||||||
|
int[] numNums = new int[n + 2];
|
||||||
|
for (int i = 1; i < n + 1; i++) {
|
||||||
|
numNums[i] = nums[i - 1];
|
||||||
|
}
|
||||||
|
numNums[0] = numNums[n + 1] = 1;
|
||||||
|
int[][] dp = new int[n + 2][n + 2];//dp[i][j]表示区间[i,j]的最大硬币数
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
for (int j = i + 2; j <= n + 1; j++) {//区间至少大于3才能戳
|
||||||
|
for (int k = i + 1; k < j; k++) {
|
||||||
|
//在左区间i和右区间j中间戳
|
||||||
|
int sum = numNums[i] * numNums[k] * numNums[j];
|
||||||
|
//加上左区间的和右区间的
|
||||||
|
sum += dp[i][k] + dp[k][j];
|
||||||
|
dp[i][j] = Math.max(dp[i][j], sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[0][n + 1];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public class T86_394_DecodeString {
|
||||||
}
|
}
|
||||||
|
|
||||||
int start = 0;
|
int start = 0;
|
||||||
|
|
||||||
public String sub(String s) {
|
public String sub(String s) {
|
||||||
if (start >= s.length() || s.charAt(start) == ']') return "";
|
if (start >= s.length() || s.charAt(start) == ']') return "";
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
@ -55,4 +56,37 @@ public class T86_394_DecodeString {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String subGet(String s) {
|
||||||
|
if (start > s.length() || s.charAt(start) == ']') return "";
|
||||||
|
|
||||||
|
char cur = s.charAt(start);
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
if (Character.isDigit(cur)) {
|
||||||
|
int num = getNum(s);
|
||||||
|
//左括号
|
||||||
|
start++;
|
||||||
|
String next = subGet(s);
|
||||||
|
while (num-- > 0) {
|
||||||
|
result.append(next);
|
||||||
|
}
|
||||||
|
start++;//右括号
|
||||||
|
}else if(Character.isLetter(cur)){
|
||||||
|
result.append(cur);
|
||||||
|
start++;//下一个
|
||||||
|
}
|
||||||
|
//拼接剩下的
|
||||||
|
result.append(subGet(s));
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNum(String s) {
|
||||||
|
int num = 0;
|
||||||
|
while (start < s.length() && Character.isDigit(s.charAt(start))) {
|
||||||
|
num = num * 10 + s.charAt(start) - '0';
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.markilue.leecode.interview.huawei;
|
package com.markilue.leecode.interview.huawei.T0531;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.markilue.leecode.interview.huawei;
|
package com.markilue.leecode.interview.huawei.T0531;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.markilue.leecode.interview.huawei;
|
package com.markilue.leecode.interview.huawei.T0531;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -37,7 +37,8 @@ public class Question3 {
|
||||||
{-1, 5, 2},
|
{-1, 5, 2},
|
||||||
{-2, -3, 4},
|
{-2, -3, 4},
|
||||||
};
|
};
|
||||||
calculateMaxRectangleSum(2, 3, income);
|
// calculateMaxRectangleSum(2, 3, income);
|
||||||
|
calculate(2, 3, income);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void calculateMaxRectangleSum(int m, int n, int[][] matrix) {
|
private static void calculateMaxRectangleSum(int m, int n, int[][] matrix) {
|
||||||
|
|
@ -70,4 +71,37 @@ public class Question3 {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void calculate(int m, int n, int[][] matrix) {
|
||||||
|
|
||||||
|
//构造前缀和数组,计算前缀和
|
||||||
|
int[][] prefix = new int[m + 1][n + 1];
|
||||||
|
|
||||||
|
for (int i = 1; i < prefix.length; i++) {
|
||||||
|
for (int j = 1; j < prefix[0].length; j++) {
|
||||||
|
prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + matrix[i - 1][j - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int result = Integer.MIN_VALUE;
|
||||||
|
int edge = 0;
|
||||||
|
//利用前缀和来判断那个矩形最大
|
||||||
|
for (int i = 1; i < m + 1; i++) {
|
||||||
|
for (int j = 1; j < n + 1; j++) {
|
||||||
|
//左上角
|
||||||
|
for (int k = i; k < m + 1; k++) {
|
||||||
|
for (int l = j; l < n + 1; l++) {
|
||||||
|
//右下角
|
||||||
|
int cur = prefix[k][l] - prefix[i - 1][l] - prefix[k][j - 1] + prefix[i - 1][j - 1];
|
||||||
|
if (result < cur) {
|
||||||
|
result = cur;
|
||||||
|
edge = (k - i + 1) * (l - j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(edge + " " + result);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue