1. Two-dimensional prefix sum algorithm

prefixes and templates

One-dimensional prefix sums:

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(),q = scan.nextInt();
        int[] arr = new int[n + 1];
        long[] dp = new long[n + 1];
        for(int i = 1;i <= n;i++) {
    
    
            arr[i] = scan.nextInt();
            dp[i] = dp[i - 1] + arr[i];
        }
        while(q > 0) {
    
    
            int l = scan.nextInt(),r = scan.nextInt();
            System.out.println(dp[r] - dp[l - 1]);
            q--;
        }
    }
}

Two-dimensional prefix sum:

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(),m = scan.nextInt(),q = scan.nextInt();
        int[][] arr = new int[n + 1][m + 1];
        long[][] dp = new long[n + 1][m + 1];
        for(int i = 1;i <= n;i++) {
    
    
            for(int j = 1;j <= m;j++) {
    
    
                arr[i][j] = scan.nextInt();
                dp[i][j] = arr[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            }
        }
        while(q > 0) {
    
    
            int x1 = scan.nextInt(),y1 = scan.nextInt();
            int x2 = scan.nextInt(),y2 = scan.nextInt();
            System.out.println(dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1]);
            q--;
        }
    }
}

724. Find the Center Index of an Array

Given an integer array nums, please calculate the center index of the array .

The array center subscript is a subscript of the array, and the sum of all elements on the left is equal to the sum of all elements on the right.

If the center subscript is at the leftmost end of the array, the sum of the numbers on the left is considered to be 0, since there are no elements to the left of the subscript. This also applies to the center subscript at the far right of the array.

If the array has multiple center subscripts, the one closest to the left should be returned. Returns -1 if the array does not have a central subscript.
insert image description here

class Solution {
    
    
    public int pivotIndex(int[] nums) {
    
    
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];

        for(int i = 1;i < n;i ++) {
    
    
            f[i] = f[i - 1] + nums[i - 1];
        }
        for(int j = n - 2;j >= 0;j--) {
    
    
            g[j] = g[j + 1] + nums[j + 1];
        }
        for(int i = 0;i < n;i++) {
    
    
            if(f[i] == g[i]) {
    
    
                return i;
            }
        }
        return -1;
    }
}

238. Product of an array other than itself

Given an integer array nums, return the array answer, where answer[i] is equal to the product of all elements in nums except nums[i].

The title data guarantees that the product of all prefix elements and suffixes of any element in the array nums is within the range of 32-bit integers.

Please don't use division and solve this problem in O(n) time complexity.
insert image description here

class Solution {
    
    
    public int[] productExceptSelf(int[] nums) {
    
    
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = 1;
        g[n - 1] = 1;
        for(int i = 1;i < n;i++) {
    
    
            f[i] = f[i - 1] * nums[i - 1];
        }
        for(int i = n - 2;i >= 0;i--) {
    
    
            g[i] = g[i + 1] * nums[i + 1];
        }
        int[] ret = new int[n];
        for(int i = 0;i < n;i++) {
    
    
            ret[i] = f[i] * g[i];
        }
        return ret;
    }
}

560. Subarrays Sum of K

Given an integer array nums and an integer k, please count and return the number of continuous subarrays whose sum is k in this array.
insert image description here

class Solution {
    
    
    public int subarraySum(int[] nums, int k) {
    
    
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,1);
        int sum = 0, ret = 0;
        for(int x : nums) {
    
    
            sum += x;
            ret += hash.getOrDefault(sum - k,0);
            hash.put(sum,hash.getOrDefault(sum,0) + 1);
        }
        return ret;
    }
}

974. Sum Subarrays Divisible by K

Given an integer array nums and an integer k , returns the number of (contiguous, non-empty) subarrays whose sum is divisible by k.

A subarray is a contiguous part of an array.
insert image description here

class Solution {
    
    
    public int subarraysDivByK(int[] nums, int k) {
    
    
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,1);
        int sum = 0,ret = 0;
        for(int x : nums) {
    
    
            sum += x;
            int r = (sum % k + k) % k;
            ret += hash.getOrDefault(r,0);
            hash.put(r,hash.getOrDefault(r,0) + 1);
        }
        return ret;
    }
}

525. Contiguous Arrays

Given a binary array nums , find the longest contiguous subarray containing the same number of 0s and 1s, and return the length of the subarray.
insert image description here

class Solution {
    
    
    public int findMaxLength(int[] nums) {
    
    
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,-1); //默认存在一个前缀和为0的情况

        int sum = 0,ret = 0;
        for(int i = 0; i < nums.length;i++) {
    
    
            sum += (nums[i] == 0 ? -1 : 1);
            if(hash.containsKey(sum)) {
    
    
                ret = Math.max(ret,i - hash.get(sum));
            } else {
    
    
                hash.put(sum,i);
            }
        }
        return ret;
    }
}

1314. Matrix Regions and

Given an mxn matrix mat and an integer k, please return a matrix answer, where each answer[i][j] is the sum of all elements mat[r][c] that satisfy the following conditions:

i - k <= r <= i + k, j - k <= c <= j + k and (r, c) is in the matrix.
insert image description here

class Solution {
    
    
    public int[][] matrixBlockSum(int[][] mat, int k) {
    
    
        int m = mat.length,n = mat[0].length;

        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1;i <= m;i++) {
    
    
            for(int j = 1;j <= n;j++) {
    
    
                dp[i][j] = mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] 
                - dp[i - 1][j - 1];
            }
        }
        int[][] ret = new int[m][n];
        for(int i = 0;i < m;i++) {
    
    
            for(int j = 0;j < n;j++) {
    
    
                int x1 = Math.max(0,i - k) + 1;
                int y1 = Math.max(0,j - k) + 1;
                int x2 = Math.min(m - 1,i + k) + 1;
                int y2 = Math.min(n - 1,j + k) + 1;
                ret[i][j] = dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1-1][y1-1];
            }
        }
        return ret;
    }
}

Guess you like

Origin blog.csdn.net/buhuisuanfa/article/details/131843864