左程云大厂算法刷题班——02

本文介绍了四种基于滑动窗口和双指针的算法问题解决方案,包括寻找最大连续子数组长度、字符串颜色块最少交换步数、目标和的子数组个数以及最大化组合收入。此外,还提出了一种支持动态更新键值对并记录更改次数的数据结构设计,并给出了查找最长无重复字符子串的两种方法。这些算法在解决数组和字符串处理问题中展现出高效性和灵活性。
摘要由CSDN通过智能技术生成

第一题

在这里插入图片描述
思路:滑动窗口(左右两指针)

public class 第一题01 {
    public static void main(String[] args) {
        int[] arr = {1,3,4,5,7,13,15,20};
        int res = maxPoint(arr, 5);
        System.out.println(res);
    }
    public static int maxPoint(int[] arr,int k){
        int n = arr.length;
        int L = 0;
        int R = 0;
        int maxlen = 0;
        while (R < n){
            while (R < n && arr[R] - arr[L] <= k){
                R++;
            }
            maxlen = Math.max(maxlen,R - L);
            L++;
        }
        return maxlen;
    }
}

第二题

在这里插入图片描述
思路:双指针


public class 第二题02 {
    public static void main(String[] args) {
        int res = minSteps("BBGGBBGBG");
        System.out.println(res);
    }
    public static int minSteps(String s){
        if (s == null || s.equals(" ")){
            return 0;
        }
        char[] chars = s.toCharArray();
        int length = s.length();
        int L = 0;
        int R = 0;
        int step_1 = 0;
        int step_2 = 0;
        while (R < length){
            if (chars[R] == 'G'){
                step_1 += R - L;
                L++;
            }
            R++;
        }
        L = 0;
        R = 0;
        while (R < length){
            if (chars[R] == 'B'){
                step_2 += R - L;
                L++;
            }
            R++;
        }
        return Math.min(step_1,step_2);
    }
}

第三题

leetcode494
在这里插入图片描述
暴力递归——>记忆化搜索——>动态规划


public class 第三题02 {
    public static void main(String[] args) {
        int[] nums = {0,0,0,0,0,0,0,0,1};
/*        int res1 = findTargetSumWays1(nums, 3);
        System.out.println(res1);
        int res2 = findTargetSumWays2(nums, 3);
        System.out.println(res2);*/
        int res3 = findTargetSumWays3(nums, 1);
        System.out.println(res3);
    }
    //暴力递归
    public static int findTargetSumWays1(int[] nums, int target) {
         return process1(nums,0,target);
    }
    private static int process1(int[] nums, int index, int target) {
        if (index == nums.length){
            return target == 0 ? 1 : 0;
        }
        return process1(nums,index + 1,target - nums[index]) + process1(nums,index + 1,target + nums[index]);
    }
    //记忆化搜索
    public static int findTargetSumWays2(int[] nums, int target) {
        return process2(nums,0,target,new HashMap<Integer,HashMap<Integer,Integer>>());
    }
    private static int process2(int[] nums, int index, int target, HashMap<Integer, HashMap<Integer, Integer>> map) {
        if (map.containsKey(index) && map.get(index).containsKey(target)){
            return map.get(index).get(target);
        }
        if (index == nums.length){
            return target == 0 ? 1 : 0;
        }
        int ans = process2(nums,index + 1,target - nums[index],map) + process2(nums,index + 1,target + nums[index],map);
        if (!map.containsKey(index)){
            map.put(index,new HashMap<>());
        }
        map.get(index).put(target,ans);
        return ans;
    }
    //动态规划(优化版)
    public static int findTargetSumWays3(int[] nums, int target) {
        int n = nums.length;
        int sum = 0;
        for (int i = 0;i < n;i++){
            sum += nums[i];
        }
        if (sum < Math.abs(target) || (sum - target) % 2 != 0){
            return 0;
        }
        int newTarget = (sum + target) / 2;
        int[][] dp = new int[n + 1][newTarget + 1];
        dp[0][0] = 1;
        for (int i = 1;i <= n;i++){
            for (int j = 0;j <= newTarget;j++){
                if (j - nums[i - 1] >= 0){
                    dp[i][j] += dp[i - 1][j - nums[i - 1]];
                }
                dp[i][j] += dp[i - 1][j];
            }
        }
        return dp[n][newTarget];
    }
}

在这里插入图片描述
思路:1.暴力递归——>记忆化搜索
2.小根堆


public class 第四题02 {
    public static void main(String[] args) {
        int[][] incomes = {{5,7},{1,6},{5,2},{7,6},{9,10},{2,6},{3,11},{6,8}};
        int res1 = maxIncome1(incomes);
        System.out.println(res1);
        int res2 = maxIncome2(incomes);
        System.out.println(res2);
        int res3 = maxIncome3(incomes);
        System.out.println(res3);
    }
    //暴力递归
    public static int maxIncome1(int[][] incomes){
        if (incomes == null || incomes.length < 2){
            return 0;
        }
        int N = incomes.length;
        int rest = N / 2;
        return process1(incomes,0,rest);
    }
    private static int process1(int[][] incomes, int index, int rest) {
        if (index == incomes.length){
            return 0;
        }
        if (incomes.length - index == rest){
            return incomes[index][0] + process1(incomes,index + 1,rest - 1);
        }
        if (rest == 0){
            return incomes[index][1] + process1(incomes,index + 1,rest);
        }
        int p1 = incomes[index][0] + process1(incomes,index + 1,rest - 1);
        int p2 = incomes[index][1] + process1(incomes,index + 1,rest);
        return Math.max(p1,p2);
    }
    //记忆化搜索
    public static int maxIncome2(int[][] incomes){
        if (incomes == null || incomes.length < 2){
            return 0;
        }
        int N = incomes.length;
        int rest = N / 2;
        return process2(incomes,0,rest,new HashMap<Integer,HashMap<Integer,Integer>>());
    }
    private static int process2(int[][] incomes, int index, int rest, HashMap<Integer, HashMap<Integer, Integer>> map) {
        if (map.containsKey(index) && map.get(index).containsKey(rest)){
            return map.get(index).get(rest);
        }
        if (index == incomes.length){
            return 0;
        }
        if (incomes.length - index == rest){
            return incomes[index][0] + process2(incomes,index + 1,rest - 1,map);
        }
        if (rest == 0){
            return incomes[index][1] + process2(incomes,index + 1,rest,map);
        }
        int p1 = incomes[index][0] + process2(incomes,index + 1,rest - 1,map);
        int p2 = incomes[index][1] + process2(incomes,index + 1,rest,map);
        int max = Math.max(p1, p2);
        if (!map.containsKey(index)){
            map.put(index,new HashMap<>());
        }
        map.get(index).put(rest,max);
        return max;
    }
    //
    public static class Node{
        public int A;
        public int B;
        public int del;
        public Node(int A,int B,int del){
            this.A = A;
            this.B = B;
            this.del = del;
        }
    }
    //小根堆
    public static int maxIncome3(int[][] incomes){
        int n = incomes.length;
        int sum_A = 0;
        int sum_B = 0;
        PriorityQueue<Node> queue = new PriorityQueue<>((a,b) -> a.del - b.del);
        for (int i = 0;i < n;i++){
            queue.add(new Node(incomes[i][0],incomes[i][1],incomes[i][0] - incomes[i][1]));
            sum_A += incomes[i][0];
        }
        for (int i = 0;i < n / 2;i++){
            Node poll = queue.poll();
            sum_A -= poll.A;
            sum_B += poll.B;
        }
        return sum_A + sum_B;
    }
}

第五题

数据结构设计:Hashmap满足以下条件:
在这里插入图片描述
思路:两个map,一个保存key和value,一个保存value和time(time为改变次数)

public class 第五题02 {
    public static class MyHashmap{
        private HashMap<Integer,Integer> keyMap = new HashMap<>();
        private HashMap<Integer,Integer> valueMap = new HashMap<>();
        private int setAllTime = Integer.MIN_VALUE;
        private int time = 0;
        private int all = 0;
        public void put(Integer key,Integer value){
            keyMap.put(key,value);
            valueMap.put(value,time);
            time++;
        }
        public Integer get(Integer key){
            if (!keyMap.containsKey(key)){
                return null;
            }
            Integer value = keyMap.get(key);
            Integer valueTime = valueMap.get(value);
            if (valueTime > setAllTime){
                return value;
            }
            return all;
        }
        public void setAll(Integer value){
            setAllTime = time;
            time++;
            all = value;
        }
    }

    public static void main(String[] args) {
        MyHashmap myHashmap = new MyHashmap();
        myHashmap.put(1,9);
        /*myHashmap.put(2,8);*/
        myHashmap.setAll(7);
        /*myHashmap.put(3,7);*/
        myHashmap.put(4,6);
        /*myHashmap.put(5,5);*/
        System.out.println(myHashmap.get(1) + " " + myHashmap.get(4));;
    }
}

第六题

leetcode3

public class 第六题02 {
    //滑动窗口
    public static int lengthOfLongestSubstring1(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int n = s.length();
        int start = 0;
        int ans = 0;
        for (int i = 0;i < n;i++){
            char c = s.charAt(i);
            if (map.containsKey(c)){
                start = Math.max(start,map.get(c));
            }
            ans = Math.max(ans,i - start + 1);
            map.put(c,i + 1);
        }
        return ans;
    }
    //动态规划
    public static int lengthOfLongestSubstring(String s) {
        if (s == null || s.equals("")){
            return 0;
        }
        int[] map = new int[256];
        for (int i = 0;i < 256;i++){
            map[i] = -1;
        }
        char[] str = s.toCharArray();
        int n = s.length();
        int ans = 1;
        int pre = 0;
        for (int i = 0;i < n;i++){
            int p1 = i - map[str[i]];
            int p2 = pre + 1;
            int p = Math.min(p1,p2);
            ans = Math.max(p,ans);
            pre = p;
            map[str[i]] = i;
        }
        return ans;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值