【算法套路】(数组中)等价转换⭐

例题——2488. 统计中位数为 K 的子数组⭐

https://leetcode.cn/problems/count-subarrays-with-median-k/description/
在这里插入图片描述
提示:

n == nums.length
1 <= n <= 10^5
1 <= nums[i], k <= n
nums 中的整数互不相同

【套路】子数组统计问题常用技巧:等价转换

https://leetcode.cn/problems/count-subarrays-with-median-k/solutions/1993439/deng-jie-zhuan-huan-pythonjavacgo-by-end-5w11/
注意,每个数字各不相同,整个数组中只有一个 k。

在这里插入图片描述
在这里插入图片描述

class Solution {
    public int countSubarrays(int[] nums, int k) {
        int pos = 0, n = nums.length;
        // 求出 k 的 pos
        while (nums[pos] != k) ++pos;
        Map<Integer, Integer> cnt = new HashMap<>();
        cnt.put(0, 1);
        for (int i = pos - 1, x = 0; i >= 0; --i) {
            x += nums[i] < k? 1: -1;
            cnt.merge(x, 1, Integer::sum);
        }

        int ans = cnt.get(0) + cnt.getOrDefault(-1, 0);
        for (int i = pos + 1, x = 0; i < n; ++i) {
            x += nums[i] > k? 1: -1;
            // x 和 x - 1 对应 奇数长度和偶数长度
            ans += cnt.getOrDefault(x, 0) + cnt.getOrDefault(x - 1, 0);
        }
        return ans;
    }
}

本质上是「中心前后缀 + 哈希表计数」

核心在于「个数」,能得到一个和个数有关的数学式子,就可以转换成 1(或者 -1),毕竟在讨论个数的时候,每个元素的贡献就是 1 了。

相似题目列表

面试题 17.05. 字母与数字

https://leetcode.cn/problems/find-longest-subarray-lcci/description/
在这里插入图片描述
前缀和设置为 字母与数字数量之差,相同前缀和的一对可以组成一个子字符串。

class Solution {
    public String[] findLongestSubarray(String[] array) {
        int st = 0, ml = 0;
        int n = array.length, d = 0;
        Map<Integer, Integer> m = new HashMap<>();
        m.put(0, 0);
        for (int i = 0; i < n; ++i) {
            char ch = array[i].charAt(0);
            if (ch >= '0' && ch <= '9') d++;
            else d--;
            if (m.containsKey(d)) {
                int j = m.get(d);
                if (i - j + 1 > ml) {
                    st = j;
                    ml = i - j + 1;
                }
            } else m.put(d, i + 1);
        }
        String[] ans = new String[ml];
        System.arraycopy(array, st, ans, 0, ml);
        return ans;
    }
}

525. 连续数组

https://leetcode.cn/problems/contiguous-array/description/
在这里插入图片描述
跟上面那道题几乎一模一样。

class Solution {
    public int findMaxLength(int[] nums) {
        int d = 0, ans = 0;
        Map<Integer, Integer> m = new HashMap<>();
        m.put(0, 0);
        for (int i = 0; i < nums.length; ++i) {
            if (nums[i] == 1) d++;
            else d--;
            if (m.containsKey(d)) {
                int id = m.get(d);
                if (i - id + 1 > ans) ans = i - id + 1;
            } else m.put(d, i + 1);
        }
        return ans;
    }
}

1124. 表现良好的最长时间段

https://leetcode.cn/problems/longest-well-performing-interval/description/
在这里插入图片描述
提示:

1 <= hours.length <= 10^4
0 <= hours[i] <= 16

解法1

class Solution {
    public int longestWPI(int[] hours) {
        int n = hours.length, d = 0, ans = 0;
        Map<Integer, Integer> m = new HashMap<>();
        m.put(0, 0);
        for (int i = 0; i < n; ++i) {
            if (hours[i] > 8) d++;
            else d--;
            if (d > 0) ans = Math.max(ans, i + 1);
            else if (m.containsKey(d - 1)) {
                ans = Math.max(ans, i - m.get(d - 1) + 1);
            }
            if (!m.containsKey(d)) m.put(d, i + 1);
        }
        return ans;
    }
}

解法2——利用单调栈

https://leetcode.cn/problems/longest-well-performing-interval/solutions/2110211/liang-chong-zuo-fa-liang-zhang-tu-miao-d-hysl/

class Solution {
    public int longestWPI(int[] hours) {
        int n = hours.length;
        int[] s = new int[n + 1];
        ArrayDeque<Integer> stk = new ArrayDeque<>();
        stk.push(0);
        // 单调递减的单调栈
        for (int i = 1; i <= n; ++i) {
            s[i] = s[i - 1] + (hours[i - 1] > 8? 1: -1);
            if (s[i] < s[stk.peek()]) stk.push(i);
        }
        int ans = 0;
        for (int i = n; i > 0; --i) {
            while (!stk.isEmpty() && s[i] > s[stk.peek()]) {
                ans = Math.max(ans, i - stk.pop());
            }
        }
        return ans;
    }
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wei *

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值