diff --git a/Leecode/pom.xml b/Leecode/pom.xml index e45ab83..ede280c 100644 --- a/Leecode/pom.xml +++ b/Leecode/pom.xml @@ -54,6 +54,12 @@ RELEASE compile + + + cn.hutool + hutool-all + 5.8.15 + diff --git a/Leecode/src/main/java/com/markilue/leecode/Test1.java b/Leecode/src/main/java/com/markilue/leecode/Test1.java new file mode 100644 index 0000000..c49f82d --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/Test1.java @@ -0,0 +1,23 @@ +package com.markilue.leecode; + +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.cqu.phmapiclientsdk.client.PhmApiClient; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode + *@Author: markilue + *@CreateTime: 2023-06-01 17:36 + *@Description: TODO + *@Version: 1.0 + */ +public class Test1 { + + public static void main(String[] args) { + PhmApiClient phmApiClient = new PhmApiClient("875f0554b4e041b3ba4be50cb68eb94f", "438fdae618794937a5333cfb6856a601"); + JSONObject jsonObject = phmApiClient.invokeByURL("http://172.28.9.61:8090/api/gas-turbine/things/search/", "{\"page_index\": 1, \"page_size\": 10, \"thing_group_uuid\":\"c1b3eb711fbd4475b262fe84fc965ea1\"}", "POST"); + String s = JSONUtil.toJsonStr(jsonObject); + System.out.println(s); + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T83_337_Rob.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T83_337_Rob.java new file mode 100644 index 0000000..8136a17 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T83_337_Rob.java @@ -0,0 +1,41 @@ +package com.markilue.leecode.hot100.second; + +import com.markilue.leecode.tree.TreeNode; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-06-01 19:30 + *@Description: TODO 力扣337 打家劫舍III + *@Version: 1.0 + */ +public class T83_337_Rob { + + public int rob(TreeNode root) { + + int[] dp = subRob(root); + return Math.max(dp[0], dp[1]); + } + + /** + * + * @param node 当前节点的状态可以从子节点的状态推断出来 + * @return [抢当前节点,不抢当前节点] + */ + public int[] subRob(TreeNode node) { + if (node == null) { + return new int[]{0, 0}; + } + + int[] left = subRob(node.left); + int[] right = subRob(node.right); + + int steal = left[0] + right[0] + node.val; + int noSteal = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); + + return new int[]{noSteal, steal}; + + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T84_338_CountBits.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T84_338_CountBits.java new file mode 100644 index 0000000..4ce778a --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T84_338_CountBits.java @@ -0,0 +1,36 @@ +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 19:40 + *@Description: TODO 力扣338 比特位计数 + *@Version: 1.0 + */ +public class T84_338_CountBits { + + @Test + public void test(){ + System.out.println(Arrays.toString(countBits(5))); + } + + //n^(n-1)会减少一个1 + public int[] countBits(int n) { + + int[] dp = new int[n + 1]; + + for (int i = 1; i < dp.length; i++) { + int j = i; + while (j != 0) { + j &= (j - 1); + dp[i]++; + } + } + return dp; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T85_347_TopKFrequent.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T85_347_TopKFrequent.java new file mode 100644 index 0000000..976db30 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T85_347_TopKFrequent.java @@ -0,0 +1,53 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +import java.util.*; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-06-01 19:52 + *@Description: TODO 力扣347 前K个高频元素 + *@Version: 1.0 + */ +public class T85_347_TopKFrequent { + + @Test + public void test() { + int[] nums = {1, 1, 1, 2, 2, 3}; + int k = 2; + System.out.println(Arrays.toString(topKFrequent(nums,k))); + } + + + //计算次数,构建最小堆,挨个放入 + public int[] topKFrequent(int[] nums, int k) { + + HashMap map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + PriorityQueue heap = new PriorityQueue<>((o1, o2) -> { + return o1[1] > o2[1] ? 1 : -1; + }); + + for (Map.Entry entry : map.entrySet()) { + if (heap.size() < k) { + heap.offer(new int[]{entry.getKey(), entry.getValue()}); + } else if (entry.getValue() > heap.peek()[1]) { + heap.poll(); + heap.offer(new int[]{entry.getKey(), entry.getValue()}); + } + } + + int[] result = new int[k]; + + for (int i = 0; i < result.length; i++) { + result[i] = heap.poll()[0]; + } + return result; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T86_394_DecodeString.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T86_394_DecodeString.java new file mode 100644 index 0000000..64f5f29 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T86_394_DecodeString.java @@ -0,0 +1,58 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-06-01 20:09 + *@Description: TODO 力扣394 字符串解码 + *@Version: 1.0 + */ +public class T86_394_DecodeString { + + @Test + public void test() { + String s = "3[a]2[bc]"; + System.out.println(decodeString(s)); + } + + public String decodeString(String s) { + return sub(s); + } + + int start=0; + public String sub(String s) { + if (start >= s.length() || s.charAt(start) == ']') return ""; + StringBuilder result = new StringBuilder(); + + char cur = s.charAt(start); + if (Character.isDigit(cur)) { + int num = 0; + while (start < s.length() && Character.isDigit(s.charAt(start))) { + num = num * 10 + s.charAt(start) - '0'; + start++; + } + // 过滤左括号 + ++start; + // 解析 String + String str = sub(s); + // 过滤右括号 + ++start; + // 构造字符串 + while (num-- > 0) { + result.append(str); + } + + } else if (Character.isLetter(cur)) { + result.append(cur); + start++; + } + result.append(sub(s));//继续剩下的 + + return result.toString(); + + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/interview/huawei/Question3.java b/Leecode/src/main/java/com/markilue/leecode/interview/huawei/Question3.java new file mode 100644 index 0000000..ceacc32 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/interview/huawei/Question3.java @@ -0,0 +1,73 @@ +package com.markilue.leecode.interview.huawei; + +import org.junit.Test; + +import java.util.Scanner; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.interview.huawei + *@Author: markilue + *@CreateTime: 2023-06-01 17:13 + *@Description: TODO 华为笔试第三题 + *@Version: 1.0 + */ +public class Question3 { + + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int m = sc.nextInt();//行数 + int n = sc.nextInt();//列数 + + int[][] income = new int[m][n];//发电收入矩阵 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + income[j][i] = sc.nextInt() - 5;//纯利润 + } + } + + calculateMaxRectangleSum(m, n, income); + + } + + @Test + public void test() { + int[][] income = { + {-1, 5, 2}, + {-2, -3, 4}, + }; + calculateMaxRectangleSum(2, 3, income); + } + + private static void calculateMaxRectangleSum(int m, int n, int[][] matrix) { + int[][] predixSum = new int[m + 1][n + 1];//前缀和矩阵 + int maxSum = Integer.MIN_VALUE; + int edge = 0; + //计算前缀和 + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + predixSum[i][j] = predixSum[i - 1][j] + predixSum[i][j - 1] - predixSum[i - 1][j - 1] + matrix[i - 1][j - 1]; + } + } + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + for (int k = i; k <= m; k++) { + for (int l = j; l <= n; l++) { + int sum = predixSum[k][l] - predixSum[k][j - 1] - predixSum[i - 1][l] + predixSum[i - 1][j - 1]; + if (sum > maxSum) { + maxSum = sum; + System.out.println(i + " " + j + " " + k + " " + l); + edge = (k - i + 1) * (l - j + 1); + } + + } + } + } + } + System.out.println(edge + " " + maxSum); + } + + +} diff --git a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question2.java b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question2.java similarity index 97% rename from Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question2.java rename to Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question2.java index 47d5835..b33052d 100644 --- a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question2.java +++ b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question2.java @@ -1,4 +1,4 @@ -package com.markilue.leecode.interview.meituan; +package com.markilue.leecode.interview.meituan.T0325; import org.junit.Test; diff --git a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question3.java b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question3.java similarity index 95% rename from Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question3.java rename to Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question3.java index 5f61de1..6aa616a 100644 --- a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question3.java +++ b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question3.java @@ -1,8 +1,7 @@ -package com.markilue.leecode.interview.meituan; +package com.markilue.leecode.interview.meituan.T0325; import org.junit.Test; -import java.util.Arrays; import java.util.Scanner; /** diff --git a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question4.java b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question4.java similarity index 96% rename from Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question4.java rename to Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question4.java index 386ca96..29c9490 100644 --- a/Leecode/src/main/java/com/markilue/leecode/interview/meituan/Question4.java +++ b/Leecode/src/main/java/com/markilue/leecode/interview/meituan/T0325/Question4.java @@ -1,4 +1,4 @@ -package com.markilue.leecode.interview.meituan; +package com.markilue.leecode.interview.meituan.T0325; import java.util.Scanner; diff --git a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/pom.xml b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/pom.xml index 2863f3f..897eac6 100644 --- a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/pom.xml +++ b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/pom.xml @@ -207,6 +207,14 @@ 3.12.0 + + + + org.apache.bahir + flink-connector-redis_${scala.version} + 1.1.0 + + diff --git a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/app/dwm/WindCMSAsyncApp.java b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/app/dwm/WindCMSAsyncApp.java index 3029d01..9b480fa 100644 --- a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/app/dwm/WindCMSAsyncApp.java +++ b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/app/dwm/WindCMSAsyncApp.java @@ -8,6 +8,7 @@ import com.cqu.warehouse.realtime.app.func.DataSamplingFunction; import com.cqu.warehouse.realtime.app.func.FFTSamplingAsyncFunction; import com.cqu.warehouse.realtime.app.func.FFTSamplingFunction; import com.cqu.warehouse.realtime.utils.MyKafkaUtils; +import com.cqu.warehouse.realtime.utils.RedisUtils; import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner; import org.apache.flink.api.common.eventtime.WatermarkStrategy; import org.apache.flink.api.common.functions.RichMapFunction; @@ -20,6 +21,9 @@ import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction; import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows; import org.apache.flink.streaming.api.windowing.time.Time; import org.apache.flink.streaming.api.windowing.windows.TimeWindow; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommand; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper; import org.apache.flink.util.Collector; import org.apache.flink.util.OutputTag; import org.apache.lucene.util.RamUsageEstimator; @@ -235,7 +239,7 @@ public class WindCMSAsyncApp extends BaseStreamApp { return obj.getJSONArray("x").toJavaList(Double.class); } }, - 60,TimeUnit.SECONDS + 60, TimeUnit.SECONDS ); @@ -245,16 +249,62 @@ public class WindCMSAsyncApp extends BaseStreamApp { fftDS.print("fft:"); //TODO 5.写回kafka - mergeDS - .map(jsonObject -> jsonObject.toJSONString()) - .addSink( - MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_downsampling") - ); - fftDS - .map(jsonObject -> jsonObject.toJSONString()) - .addSink( - MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_frequency") - ); +// mergeDS +// .map(jsonObject -> jsonObject.toJSONString()) +// .addSink( +// MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_downsampling") +// ); +// fftDS +// .map(jsonObject -> jsonObject.toJSONString()) +// .addSink( +// MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_frequency") +// ); + + //TODO 6.写到Redis + mergeDS.addSink( + RedisUtils.getRedisSink( + new RedisMapper() { + + @Override + public RedisCommandDescription getCommandDescription() { + return new RedisCommandDescription(RedisCommand.SETEX,"cmsLTTB",1000*60*4);//四个小时过期 + } + + @Override + public String getKeyFromData(JSONObject jsonObject) { + return jsonObject.getString("windfarm")+":"+jsonObject.getString("wt_no")+":cmsLTTB"; + } + + @Override + public String getValueFromData(JSONObject jsonObject) { + return jsonObject.toJSONString(); + } + } + ) + ); + + fftDS.addSink( + RedisUtils.getRedisSink( + new RedisMapper() { + + @Override + public RedisCommandDescription getCommandDescription() { + return new RedisCommandDescription(RedisCommand.SETEX,"cmsFFT",1000*60*4);//四个小时过期 + } + + @Override + public String getKeyFromData(JSONObject jsonObject) { + return jsonObject.getString("windfarm")+":"+jsonObject.getString("wt_no")+":cmsFFT"; + } + + @Override + public String getValueFromData(JSONObject jsonObject) { + //方便后续Python读取,全部存放,而不是一个x + return jsonObject.toJSONString(); + } + } + ) + ); } } diff --git a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/utils/RedisUtils.java b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/utils/RedisUtils.java index 5bc00d1..125a18a 100644 --- a/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/utils/RedisUtils.java +++ b/phm_rotate/backend/phm_backend/warehouse/rt-warehouse/src/main/java/com/cqu/warehouse/realtime/utils/RedisUtils.java @@ -1,5 +1,10 @@ package com.cqu.warehouse.realtime.utils; +import org.apache.flink.streaming.connectors.redis.RedisSink; +import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommand; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription; +import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @@ -16,6 +21,9 @@ public class RedisUtils { private static JedisPool jedisPool; + private static final String REDIS_HOST ="Ding202"; + private static final Integer PORT = 6379; + public static Jedis getJedisPool() { if (jedisPool == null) { @@ -43,6 +51,23 @@ public class RedisUtils { jedisPool = new JedisPool(jedisPoolConfig, "Ding202", 6379, 10000); } + + public static RedisSink getRedisSink(RedisMapper redisMapper){ + + FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig + .Builder() + .setHost(REDIS_HOST) + .setPort(PORT) + .setMaxIdle(5) + .setMinIdle(5) + .setMaxTotal(100) + .setTestOnBorrow(true) + .build(); + + return new RedisSink(conf,redisMapper); + + } + public static void main(String[] args) { Jedis jedis = getJedisPool(); String pong = jedis.ping();