follow up2-20190426

本文深入解析双指针技术在解决最小子数组、最长无重复子串等问题的应用,以及堆在处理多数组中第K大元素、矩阵中第K小元素等场景的高效算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

406. Minimum Size Subarray 

同向双指针

https://www.lintcode.com/problem/minimum-size-subarray-sum/description?_from=ladder&&fromId=4

 1     public class Solution {
 2     /**
 3      * @param nums: an array of integers
 4      * @param s: An integer
 5      * @return: an integer representing the minimum size of subarray
 6      */
 7     public int minimumSize(int[] nums, int s) {
 8         // write your code here
 9         if(nums==null || nums.length ==0){
10             return -1;
11         }
12         
13         int sum = 0;
14         int res = Integer.MAX_VALUE;
15         for(int l =0,r=0;r<nums.length;r++){
16             sum +=nums[r];
17             
18             while(sum>=s){
19                 res = Math.min(res,r-l+1);
20                 sum = sum -nums[l];
21                 l++;
22             }
23         }
24         
25         return res==Integer.MAX_VALUE?-1:res;
26     }
27 }
View Code

 

384. Longest Substring Without Repeating Characters

同向双指针

 https://www.lintcode.com/problem/longest-substring-without-repeating-characters/description?_from=ladder&&fromId=4

 1 public class Solution {
 2     /**
 3      * @param s: a string
 4      * @return: an integer
 5      */
 6     public int lengthOfLongestSubstring(String s) {
 7         // write your code here
 8         if(s==null || s.length()==0){
 9             return 0;
10         }
11         
12         Set<Character> set = new HashSet<>();
13         int left =0;
14         int right =0;
15         int ans = Integer.MIN_VALUE;
16         
17         while(left<s.length() && right<s.length()){
18             while(right<s.length() && !set.contains(s.charAt(right))){
19                 set.add(s.charAt(right));
20                 ans = Math.max(ans,right-left+1);
21                 right++;
22             }
23             
24             set.remove(s.charAt(left));
25             left++;
26         }
27         
28         return ans == Integer.MIN_VALUE ? -1 : ans;
29     }
30 }
View Code

 

32. Minimum Window Substring

同向双指针

https://www.lintcode.com/problem/minimum-window-substring/description

 1 public class Solution {
 2     /**
 3      * @param source : A string
 4      * @param target: A string
 5      * @return: A string denote the minimum window, return "" if there is no such a string
 6      */
 7     public String minWindow(String source , String target) {
 8         // write your code here    
 9         if(source==null || source.length()==0){
10             return source;
11         }
12         
13         Map<Character,Integer> map = new HashMap<>();
14         for(int i=0;i<target.length();i++){
15             char c = target.charAt(i);
16             if(map.containsKey(c)){
17                 map.put(c,map.get(c)+1);
18             }else{
19                 map.put(c,1);
20             }
21         }
22         
23         int i=0;
24         int j =0;
25         String res= "";
26         int min = Integer.MAX_VALUE;
27         int countT = target.length();
28         int countS = 0;
29         
30         //while 循环不要加 j<source.length 的条件
31         while(i<source.length()){
32             while(j<source.length() && countS<countT){
33                 char c = source.charAt(j);
34                 if(map.containsKey(c)){
35                     if(map.get(c)>0) countS++;
36                     map.put(c,map.get(c)-1);
37                 }
38                 
39                 j++;
40             }
41             
42             if(countS>=countT){
43                 if(j-i<min){
44                     min = j-i;
45                     res = source.substring(i,j);
46                 }
47             }
48             
49             char cc = source.charAt(i);
50             if(map.containsKey(cc)){
51                 if(map.get(cc)>=0) countS--;
52                 map.put(cc,map.get(cc)+1);
53             }
54             i++;
55         }
56         
57         return res;
58     }
59 }
View Code

 

386. Longest Substring with At Most K Distinct Characters

同向双指针

https://www.lintcode.com/problem/longest-substring-with-at-most-k-distinct-characters/description?_from=ladder&&fromId=4

 1 public class Solution {
 2     /**
 3      * @param s: A string
 4      * @param k: An integer
 5      * @return: An integer
 6      */
 7     public int lengthOfLongestSubstringKDistinct(String s, int k) {
 8         // write your code here
 9         if(s==null || s.length()==0 || k==0){
10             return 0;
11         }
12         
13         int l = 0;
14         int r = 0;
15         int ans = 0;
16         int sum = 0;
17         
18         int[] cnt = new int[256];
19         for(r=0;r<s.length();r++){
20             cnt[s.charAt(r)]++;
21             if(cnt[s.charAt(r)]==1){
22                 sum++;
23             }
24             
25             while(sum>k){
26                 cnt[s.charAt(l)]--;
27                 if(cnt[s.charAt(l)]==0){
28                     sum--;
29                 }
30                 l++;
31             }
32             
33             ans = Math.max(ans,r-l+1);
34         }
35         
36         return ans;
37         
38     }
39 }
View Code

 


465. Kth Smallest Sum In Two Sorted Arrays

heap

 https://www.lintcode.com/problem/kth-smallest-sum-in-two-sorted-arrays/description?_from=ladder&&fromId=4

class Pair{
    int x;
    int y;
    int sum;
    Pair(int x, int y,int sum){
        this.x = x;
        this.y = y;
        this.sum = sum;
    }
}
public class Solution {
    /**
     * @param A: an integer arrays sorted in ascending order
     * @param B: an integer arrays sorted in ascending order
     * @param k: An integer
     * @return: An integer
     */
    public int kthSmallestSum(int[] A, int[] B, int k) {
        // write your code here
         if (A == null || A.length == 0) {
             return 0;
         }
         if (B == null || B.length == 0) {
             return 0;
         }
         if (k == 0) {
           return 0;
         }
         
         Comparator<Pair> comparator = new Comparator<Pair>(){
             @Override
             public int compare(Pair o1,Pair o2){
                 return o1.sum-o2.sum;
             }
         };
         PriorityQueue<Pair> minHeap = new PriorityQueue<Pair>(comparator);
         boolean[][] visit = new boolean[A.length][B.length];
         minHeap.add(new Pair(0,0,A[0]+B[0]));
         visit[0][0] = true;
         int[] dx = {0,1};
         int[] dy = {1,0};
         
         for(int i=1;i<k;i++){
             Pair center = minHeap.poll();;
             for(int j = 0;j<2;j++){
                 if(center.x + dx[j]>A.length-1 || center.y + dy[j]>B.length-1 || visit[center.x+dx[j]][center.y+dy[j]]){
                     continue;
                 }
                 minHeap.add(new Pair(center.x+dx[j],center.y+dy[j],A[center.x+dx[j]]+B[center.y+dy[j]]));
                 visit[center.x+dx[j]][center.y+dy[j]] = true;
             }
         }
         
         return minHeap.peek().sum;
    }
}
View Code

 


401. Kth Smallest Number in Sorted Matrix

 heap

https://www.lintcode.com/problem/kth-smallest-number-in-sorted-matrix/description?_from=ladder&&fromId=4

 1 class Pair {
 2     private int x;
 3     private int y;
 4     private int val;
 5 
 6     public Pair(int x, int y, int val) {
 7         this.x = x;
 8         this.y = y;
 9         this.val = val;
10     }
11 
12     public int getX() {
13         return x;
14     }
15 
16     public void setX(int x) {
17         this.x = x;
18     }
19 
20     public int getY() {
21         return y;
22     }
23 
24     public void setY(int y) {
25         this.y = y;
26     }
27 
28     public int getVal() {
29         return val;
30     }
31 
32     public void setVal(int val) {
33         this.val = val;
34     }
35 }
36 
37 public class Solution {
38     /**
39      * @param matrix: a matrix of integers
40      * @param k:      An integer
41      * @return: the kth smallest number in the matrix
42      */
43     public int kthSmallest(int[][] matrix, int k) {
44         // write your code here
45         if (matrix == null || matrix.length == 0 || matrix.length * matrix[0].length < k) {
46             return -1;
47         }
48         Comparator<Pair> comparator = new Comparator<Pair>() {
49             @Override
50             public int compare(Pair o1, Pair o2) {
51                 return o1.getVal() - o2.getVal();
52             }
53         };
54         int r = matrix.length;
55         int c = matrix[0].length;
56         PriorityQueue<Pair> minHeap = new PriorityQueue<>(comparator);
57         boolean[][] visited = new boolean[r][c];
58 
59         minHeap.add(new Pair(0, 0, matrix[0][0]));
60         visited[0][0] = true;
61 
62         for (int i = 1; i <= k - 1; i++) {
63             Pair cur = minHeap.poll();
64             if (cur.getX() + 1 < r && !visited[cur.getX() + 1][cur.getY()]) {
65                 minHeap.add(new Pair(cur.getX() + 1, cur.getY(), matrix[cur.getX() + 1][cur.getY()]));
66                 visited[cur.getX() + 1][cur.getY()] = true;
67             }
68             if (cur.getY() + 1 < c && !visited[cur.getX()][cur.getY() + 1]) {
69                 minHeap.add(new Pair(cur.getX(), cur.getY() + 1, matrix[cur.getX()][cur.getY() + 1]));
70                 visited[cur.getX()][cur.getY() + 1] = true;
71             }
72         }
73 
74         return minHeap.peek().getVal();
75     }
76 }
View Code

 

543. Kth Largest in N Arrays

heap

https://www.lintcode.com/problem/kth-largest-in-n-arrays/description?_from=ladder&&fromId=4

 1 class Node{
 2     int value;
 3     int fromId;
 4     int index;
 5     public Node(int value,int fromId,int index){
 6         this.value = value;
 7         this.fromId = fromId;
 8         this.index = index;
 9     }
10 }
11 public class Solution {
12     /**
13      * @param arrays: a list of array
14      * @param k: An integer
15      * @return: an integer, K-th largest element in N arrays
16      */
17     public int KthInArrays(int[][] arrays, int k) {
18         // write your code here
19         if(arrays==null || arrays.length==0 ||k<=0){
20             return 0;
21         }
22         
23         Comparator<Node> comparator = new Comparator<Node>(){
24             @Override
25             public int compare(Node o1,Node o2){
26                 return o2.value - o1.value;
27             }
28         };
29         
30         PriorityQueue<Node> maxHeap = new PriorityQueue<Node>(comparator);
31         
32         int n = arrays.length;
33         
34         //sort and put first column into heap
35         for(int i =0;i<n;i++){
36             Arrays.sort(arrays[i]);
37             
38             if(arrays[i].length>0){
39                 int fromId = i;
40                 int index = arrays[i].length-1;
41                 int value = arrays[i][index];
42                 maxHeap.add(new Node(value,fromId,index));
43             }
44         }
45         
46         for(int i=1;i<k;i++){
47             Node curr = maxHeap.poll();
48             
49             if(curr.index>0){
50                 curr.index--;
51                 maxHeap.add(new Node(arrays[curr.fromId][curr.index],curr.fromId,curr.index));
52             }
53         }
54         
55         return maxHeap.peek().value;
56         
57         
58     }
59 }
View Code

 

转载于:https://www.cnblogs.com/lizzyluvcoding/p/10772136.html

内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值