leecode,rt_phm更新更新

This commit is contained in:
kevinding1125 2023-06-01 14:01:11 +08:00
parent 220ffec10d
commit a9c69ee064
12 changed files with 435 additions and 33 deletions

View File

@ -0,0 +1,12 @@
package com.markilue.leecode.hot100.interviewHot.dynamic;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.dynamic
*@Author: markilue
*@CreateTime: 2023-06-01 11:50
*@Description: TODO 力扣312 戳气球
*@Version: 1.0
*/
public class LC_312_MaxCoins {
}

View File

@ -1,5 +1,7 @@
package com.markilue.leecode.hot100.interviewHot.graph; package com.markilue.leecode.hot100.interviewHot.graph;
import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -13,6 +15,13 @@ import java.util.List;
*/ */
public class LC_207_CanFinish { public class LC_207_CanFinish {
@Test
public void test() {
int numCourses = 2;
int[][] prerequisites = {{1, 0}};
System.out.println(canFinish1(numCourses, prerequisites));
}
List<List<Integer>> edge;// List<List<Integer>> edge;//
//本质上就是图的深度优先搜索:检测这个图是否存在环路 //本质上就是图的深度优先搜索:检测这个图是否存在环路
@ -29,4 +38,52 @@ public class LC_207_CanFinish {
return false; return false;
} }
boolean valid = true;
public boolean canFinish1(int numCourses, int[][] prerequisites) {
//构建图
List<List<Integer>> edge = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
edge.add(new ArrayList<>());
}
for (int[] prerequisite : prerequisites) {
edge.get(prerequisite[0]).add(prerequisite[1]);
}
int[] used = new int[edge.size()];
for (int i = 0; i < edge.size() && valid; i++) {
if (used[i] == 0) {
dfs(edge, i, used);
}
}
return valid;
}
private void dfs(List<List<Integer>> edge, int i, int[] used) {
used[i] = 1;
List<Integer> curList = edge.get(i);
for (Integer next : curList) {
if (used[next] == 1) {
//正在用,不行
valid = false;
return;
} else if (used[next] == 0) {
dfs(edge, next, used);
if (!valid) {
return;
}
}
}
used[i] = 2;
}
} }

View File

@ -47,4 +47,30 @@ public class T78_300_LengthOfLIS {
return index + 1; return index + 1;
} }
//使用一个单调数组记录如果比最大的一个小就找一个合适的位置进行替换而不影响原来的长度
public int lengthOfLIS1(int[] nums) {
int[] stack = new int[nums.length];
stack[0] = nums[0];
int index = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > stack[index]) {
//单调递增,顺利加入
stack[++index] = nums[i];
} else {
//需要找一个合适的位置加入
for (int j = 0; j <= index; j++) {
if (nums[i] < stack[j]) {
stack[j] = nums[i];
break;
}
}
}
}
return index + 1;//返回的是长度
}
} }

View File

@ -18,7 +18,7 @@ public class T79_301_RemoveInvalidParentheses {
@Test @Test
public void test() { public void test() {
String s = "(a)())()"; String s = "(a)())()";
System.out.println(removeInvalidParentheses(s)); System.out.println(removeInvalidParentheses1(s));
} }
@ -84,4 +84,54 @@ public class T79_301_RemoveInvalidParentheses {
} }
//朴素的想法:删除无效的括号,本质上就是要判断怎么样的是无效然后将无效的部分删除并判断合不合适即可
public List<String> removeInvalidParentheses1(String s) {
int left = 0;
int right = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
left++;
} else if (s.charAt(i) == ')') {
if (left > 0) {
left--;
} else {
right++;
}
}
}
//判断完之后就需要进行删除同时不可能存在left,right都大于0的情况
List<String> result = new ArrayList<>();
remove(s, left, right, result,0);
return result;
}
public void remove(String s, int left, int right, List<String> res,int start) {
if (left == 0 && right == 0) {
if (isValid(s)) {
res.add(s);
}
return;
}
//必须在删除的后面继续删才可以
for (int i = start; i < s.length(); i++) {
//去重
if(i>start&&s.charAt(i)==s.charAt(i-1))continue;
//不够了
if (left + right > s.length() - i) return;
if (left > 0 && s.charAt(i) == '(') {
//可以移除左边
remove(s.substring(0, i) + s.substring(i + 1), left - 1, right, res,i);
}
if (right > 0 && s.charAt(i) == ')') {
//可以移除左边
remove(s.substring(0, i) + s.substring(i + 1), left, right - 1, res,i);
}
}
}
} }

View File

@ -0,0 +1,65 @@
package com.markilue.leecode.hot100.second;
import org.junit.Test;
import java.util.Arrays;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-06-01 11:13
*@Description: TODO 力扣312 戳气球
*@Version: 1.0
*/
public class T81_312_MaxCoins {
@Test
public void test() {
int[] nums ={3,1,5,8};
System.out.println(maxCoins(nums));
}
int[][] memo;
//朴素的想法:判断那一步是最大值为了节省时间需要将值记录下来
//考虑到区间的方式,这里可以使用二维数组来记录
public int maxCoins(int[] nums) {
int n = nums.length;
//构造一个完整数组
int[] newNums = new int[n + 2];
for (int i = 1; i <= n; i++) {
newNums[i] = nums[i - 1];
}
newNums[0] = newNums[n + 1] = 1;
//初始化记忆搜索矩阵
memo = new int[n + 2][n + 2];
for (int[] ints : memo) {
Arrays.fill(ints, -1);
}
//开始状态转移
return dp(newNums, 0, n + 1);
}
//判断left和right为区间的最大值
public int dp(int[] nums, int left, int right) {
if (left + 1 >= right) {//区间小于三个值
return 0;
}
if (memo[left][right] != -1) {//记忆过了
return memo[left][right];
}
//挨个找left和right区间类戳破气球的最大值;i表示最后戳谁
for (int i = left + 1; i < right; i++) {//只能戳中间,不能戳两边
int sum = nums[left] * nums[i] * nums[right];
sum += dp(nums, left, i) + dp(nums, i, right);//把左边的和右边的都添加上
memo[left][right] = Math.max(memo[left][right], sum);
}
return memo[left][right];
}
}

View File

@ -0,0 +1,76 @@
package com.markilue.leecode.hot100.second;
import org.junit.Test;
import java.util.Arrays;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-06-01 11:55
*@Description: TODO 力扣322 零钱兑换
*@Version: 1.0
*/
public class T82_322_CoinChange {
@Test
public void test() {
int[] coins = {5, 2, 1};
int amount = 11;
System.out.println(coinChange(coins, amount));
}
@Test
public void test1() {
int[] coins = {186, 419, 83, 408};
int amount = 6249;
System.out.println(coinChange(coins, amount));
}
public int coinChange(int[] coins, int amount) {
int[][] dp = new int[coins.length][amount + 1];//dp[i][j]表示 使用coin[0-i]装满amount最小要多少个硬币
Arrays.sort(coins);
for (int i = 1; i < dp[0].length; i++) {
dp[0][i] = i % coins[0] == 0 ? i / coins[0] : Integer.MAX_VALUE;
}
for (int i = 1; i < coins.length; i++) {
for (int j = 1; j < amount + 1; j++) {
if (j >= coins[i] && dp[i][j - coins[i]] != Integer.MAX_VALUE) {
dp[i][j] = Math.min(dp[i][j - coins[i]] + 1, dp[i - 1][j]);//用当前的硬币;不用当前的硬币
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[coins.length - 1][amount] == Integer.MAX_VALUE ? -1 : dp[coins.length - 1][amount];
}
//滚动数组优化:
public int coinChange1(int[] coins, int amount) {
int[] dp = new int[amount + 1];//dp[i][j]表示 使用coin[0-i]装满amount最小要多少个硬币
Arrays.sort(coins);
for (int i = 1; i < dp.length; i++) {
dp[i] = i % coins[0] == 0 ? i / coins[0] : Integer.MAX_VALUE;
}
for (int i = 1; i < coins.length; i++) {
for (int j = coins[i]; j < amount + 1; j++) {
if (dp[j - coins[i]] != Integer.MAX_VALUE) {
dp[j] = Math.min(dp[j - coins[i]] + 1, dp[j]);//用当前的硬币;不用当前的硬币
}
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}

View File

@ -0,0 +1,65 @@
package com.markilue.leecode.interview.huawei;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.interview.huawei
*@Author: markilue
*@CreateTime: 2023-05-31 19:09
*@Description: TODO
*@Version: 1.0
*/
public class Question1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
@Test
public void test() {
int[][] nums = {
{1, 5},
{6, 3},
{2, 2},
{5, 4},
{1, 6},
};
solve(3, nums);
}
public void solve(int max, int[][] nums) {
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
for (int[] num : nums) {
StringBuilder sb = new StringBuilder();
int A = num[0];
int B = num[1];
for (int i = 0; i < A; i++) {
if (queue.size() < max) {
queue.offer(B);
} else if (queue.peek() < B) {
queue.poll();
queue.offer(B);
}
}
Integer[] n = queue.toArray(new Integer[0]);
Arrays.sort(n);
for (int i = n.length - 1; i >= 0; i--) {
sb.append(n[i]);
}
System.out.println(sb);
}
}
}

View File

@ -0,0 +1,51 @@
package com.markilue.leecode.interview.huawei;
import org.junit.Test;
import java.util.Arrays;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.interview.huawei
*@Author: markilue
*@CreateTime: 2023-05-31 19:47
*@Description: TODO
*@Version: 1.0
*/
public class Question2 {
public void solve(int m, int n) {
}
int[][] memo;
@Test
public void test() {
int m = 13;
int n = 11;
memo = new int[m + 1][n + 1];
for (int[] ints : memo) {
Arrays.fill(ints, -1);
}
int result = find(m, n);
System.out.println(result);
}
public int find(int m, int n) {
if (m < n) return find(n, m);
if (memo[m][n] != -1) return memo[m][n];
if (m == 0 || n == 0) {
memo[m][n] = 0;
return 0;
}
int result = Integer.MAX_VALUE;
for (int i = n; i >= 1; i--) {
result = Math.min(result, 1 + find(m - i, n) + find(n - i, m));
}
memo[m][n] = result;
return result;
}
}

View File

@ -17,26 +17,26 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public class GasTypeState { public class GasTypeState {
String stt; private String stt;
String edt; private String edt;
String type_id; private String type_id;
Long rated_temp; private Long rated_temp;
Long rated_press; private Long rated_press;
Long rated_flow_rate; private Long rated_flow_rate;
Long rated_speed; private Long rated_speed;
Long rated_power; private Long rated_power;
Long rated_load; private Long rated_load;
Long rated_duration; private Long rated_duration;
Double avg_LubeReturnT2; private Double avg_LubeReturnT2;
Double avg_T5TC1; private Double avg_T5TC1;
Double avg_GFFlow; private Double avg_GFFlow;
Double avg_LFFlow; private Double avg_LFFlow;
Double avg_NHP_1; private Double avg_NHP_1;
Double avg_AirInletDP_1; private Double avg_AirInletDP_1;
Double avg_T1_1; private Double avg_T1_1;
Double avg_LubeHaderP; private Double avg_LubeHaderP;
Double avg_LubeFilterDP; private Double avg_T5TC9;
Double avg_TankT; private Double avg_TankT;
Double avg_GrBxAccel; private Double avg_LFT;
Long ts; private Long ts;
} }

View File

@ -60,7 +60,7 @@ public class GasTypeStateApp extends BaseStreamApp {
" LFT DOUBLE, " + " LFT DOUBLE, " +
" realtime STRING, " + " realtime STRING, " +
" row_time as TO_TIMESTAMP(SUBSTRING(realtime from 0 for 19), 'yyyy-MM-dd HH:mm:ss'), " + " row_time as TO_TIMESTAMP(SUBSTRING(realtime from 0 for 19), 'yyyy-MM-dd HH:mm:ss'), " +
" WATERMARK FOR row_time as row_time - INTERVAL '5' SECOND" + " WATERMARK FOR row_time as row_time - INTERVAL '1' SECOND" +
") " + ") " +
"with(" + MyKafkaUtils.getKafkaSourceByDDL(topic, groupId) + ")"; "with(" + MyKafkaUtils.getKafkaSourceByDDL(topic, groupId) + ")";
@ -106,7 +106,7 @@ public class GasTypeStateApp extends BaseStreamApp {
Table table = tableEnv.sqlQuery(selectSQL); Table table = tableEnv.sqlQuery(selectSQL);
// tableEnv.executeSql(selectSQL).print(); // tableEnv.executeSql(selectSQL).print();
DataStream<GasTypeState> windowDS = tableEnv.toAppendStream(table, GasTypeState.class); DataStream<GasTypeState> windowDS = tableEnv.toAppendStream(table, GasTypeState.class);
windowDS.print(">>>"); windowDS.print(">>>");//很久没打印可能是正常的,大概会慢10多个分钟的窗口暂时不知道为什么
windowDS.addSink( windowDS.addSink(
ClickhouseUtils.getJDBCSink("insert into gas_type_state values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)") ClickhouseUtils.getJDBCSink("insert into gas_type_state values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")

View File

@ -87,9 +87,9 @@ create table gas_type_state
avg_AirInletDP_1 Decimal64(3), avg_AirInletDP_1 Decimal64(3),
avg_T1_1 Decimal64(3), avg_T1_1 Decimal64(3),
avg_LubeHaderP Decimal64(3), avg_LubeHaderP Decimal64(3),
avg_LubeFilterDP Decimal64(3), avg_T5TC9 Decimal64(3),
avg_TankT Decimal64(3), avg_TankT Decimal64(3),
avg_GrBxAccel Decimal64(3), avg_LFT Decimal64(3),
ts UInt64 ts UInt64
) engine = ReplacingMergeTree(ts) ) engine = ReplacingMergeTree(ts)
partition by toYYYYMMDD(stt) partition by toYYYYMMDD(stt)

View File

@ -51,7 +51,7 @@ public class ClickhouseUtils {
new JdbcExecutionOptions.Builder() new JdbcExecutionOptions.Builder()
// .withMaxRetries(3) // .withMaxRetries(3)
// .withBatchIntervalMs() // .withBatchIntervalMs()
.withBatchSize(1)//每5次输出到clickhouse .withBatchSize(5)//每5次输出到clickhouse
.build(), .build(),
new JdbcConnectionOptions.JdbcConnectionOptionsBuilder() new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
.withDriverName("ru.yandex.clickhouse.ClickHouseDriver") .withDriverName("ru.yandex.clickhouse.ClickHouseDriver")