“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

takeaway

Fat friends in order to better help new students adapt to the algorithm and questions, recently we began a special assault step by step. Last time we finished dynamic programming in twenty-one days and now we’re going to move on to a little summary of twenty-eight days of various algorithms. What are you waiting for? Come on!!

Algorithm training for 28 days

Given two integer arrays nums1 and nums2, please return the intersection of the two arrays as an array. The number of occurrences of each element in the returned result should be the same as the number of occurrences of the element in both arrays (if not, consider a smaller value). You can ignore the order of the output.

The sample1: Input: nums1 = [1.2.2.1], nums2 = [2.2] output: [2.2]
Copy the code
The sample2: Input: nums1 = [4.9.5], nums2 = [9.4.9.8.4] output: [4.9]
Copy the code

Since the same number can occur multiple times in both arrays, we need a hash table to store the number of occurrences of each number. For a number, the number of occurrences in the intersection set is equal to the minimum number of occurrences in both arrays.

Traverse the first array, the first and record in the hash table each number in the first array, and corresponding to the number of occurrences of, then traverse the second array, for each number in the second array, if this number is found in the hash table, then add the number to the answer, and reduce the frequency of the Numbers in the hash table.

To reduce spatial complexity, we first iterate over the shorter array and record each number and its corresponding occurrence in the hash table, and then iterate over the longer array to get the intersection.

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1.length > nums2.length) {
            return intersect(nums2, nums1);
        }
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int num : nums1) {
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        int[] intersection = new int[nums1.length];
        int index = 0;
        for (int num : nums2) {
            int count = map.getOrDefault(num, 0);
            if (count > 0) {
                intersection[index++] = num;
                count--;
                if (count > 0) {
                    map.put(num, count);
                } else{ map.remove(num); }}}return Arrays.copyOfRange(intersection, 0, index); }}Copy the code

In MATLAB, there is a very useful function 0 that remolds an M x N matrix into another new matrix of a different size (R x C), but preserves its original data.

You are given an M x N matrix represented by a two-dimensional array mat, and two positive integers r and C, representing the number of rows and columns of the matrix you want to reconstruct.

The reconstructed matrix requires that all elements of the original matrix be filled in the same row traversal order.

0 0 If the 0 0 operation with the given parameters is 0 0 0 0 0 Otherwise, output the original matrix.

The sample1: input: mat = [[1.2], [3.4]], r = 1, c = 4Output: [[1.2.3.4]]
Copy the code

The sample2: input: mat = [[1.2], [3.4]], r = 2, c = 4Output: [[1.2], [3.4]]
Copy the code
class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int m = nums.length;
        int n = nums[0].length;
        if(m * n ! = r * c) {return nums;
        }

        int[][] ans = new int[r][c];
        for (int x = 0; x < m * n; ++x) {
            ans[x / c][x % c] = nums[x / n][x % n];
        }
        returnans; }}Copy the code

The interview questions

How does undo log roll back transactions? For example, forgetting to buy a ticket for INSERT type SQL, you log the ID of the data you just inserted into the undo log, which allows you to precisely delete data according to the ID when you want to roll back. Anyway, for DELETE type SQL, you log the data you just deleted in undo log, which inserts the data into the system when you roll back. Virtual Gateway For UPDATe-type SQL, data is recorded in the Undo log before modification, and a rollback requires only a reverse update. Anyway, for SELect-type SQL, don't bother; select doesn't need to be rolled back. How do I query the causes of slow SQL? Buy a ticket to analyze THE SQL execution plan explain Extended, thinking about possible optimization points, hitting indexes, and more. Either no indexes or no indexes are used at all (this is the most common problem with slow queries and is a programming flaw). Fault The memory runs out. Virtual gateway The network speeds slowly. Regardless of whether the amount of data is queried is too large (you can run multiple queries, reducing the amount of data using other methods). Queue whether returns unnecessary rows and columns. Buy a lock or deadlock. Queue I/O throughput is low, creating a bottleneck effect. Sp_lock,sp_who, acts for an active user to view, because reads and writes compete for resourcesCopy the code