leecode,rt_phm更新更新

This commit is contained in:
kevinding1125 2023-06-01 20:53:27 +08:00
parent a9c69ee064
commit 321961372d
13 changed files with 387 additions and 15 deletions

View File

@ -54,6 +54,12 @@
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>
</dependencies>

View File

@ -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);
}
}

View File

@ -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};
}
}

View File

@ -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;
}
}

View File

@ -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<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> {
return o1[1] > o2[1] ? 1 : -1;
});
for (Map.Entry<Integer, Integer> 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;
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -1,4 +1,4 @@
package com.markilue.leecode.interview.meituan;
package com.markilue.leecode.interview.meituan.T0325;
import org.junit.Test;

View File

@ -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;
/**

View File

@ -1,4 +1,4 @@
package com.markilue.leecode.interview.meituan;
package com.markilue.leecode.interview.meituan.T0325;
import java.util.Scanner;

View File

@ -207,6 +207,14 @@
<version>3.12.0</version>
</dependency>
<!--Flink - redis Sink的依赖-->
<!-- https://mvnrepository.com/artifact/org.apache.bahir/flink-connector-redis -->
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_${scala.version}</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

View File

@ -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,15 +249,61 @@ public class WindCMSAsyncApp extends BaseStreamApp {
fftDS.print("fft:");
//TODO 5.写回kafka
mergeDS
.map(jsonObject -> jsonObject.toJSONString())
.addSink(
MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_downsampling")
// 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.<JSONObject>getRedisSink(
new RedisMapper<JSONObject>() {
@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
.map(jsonObject -> jsonObject.toJSONString())
.addSink(
MyKafkaUtils.getKafkaSink("dwm_wind_cms_10_frequency")
fftDS.addSink(
RedisUtils.<JSONObject>getRedisSink(
new RedisMapper<JSONObject>() {
@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();
}
}
)
);
}

View File

@ -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 <T> RedisSink<T> 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<T>(conf,redisMapper);
}
public static void main(String[] args) {
Jedis jedis = getJedisPool();
String pong = jedis.ping();