[problem solution] lecture 100 on zero basics of algorithms (Lecture 31) multidimensional enumeration - Introduction

Keywords: C Back-end

1, Recommended column

Lecture 100 on zero basis of algorithms (Lecture 31) multidimensional enumeration (I) - Introduction
  
  
  

2, Multidimensional enumeration

  multidimensional enumeration is actually nesting loops, such as two-dimensional enumeration: nesting a for loop in a for loop.

#incldue <stdio.h>

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			pritnf("%d%d ", i,j);
		}
		printf("\n");
	}
	return 0;
}

Among them, the first level loop usually represents columns and the second level loop represents rows.
  
  
  

2, After class practice

2.1. Find different

2.1.1 title link:

389. Find a different

2.1.2 train of thought analysis

  

   the title requires us to find out the different letters of the two sequences. It should be said that the string s lacks a character relative to the string T. in fact, it is to arrange s corresponding to T. s needs to add a character to get t.

So we just need to add up the characters of t, then subtract the characters in s, and the remaining value is the ASCL code value of the missing letter.

The code is as follows:

char findTheDifference(char * s, char * t){
    int sum = 0;
    for(int i = 0; t[i]; i++){
        sum += t[i];
    }
    for(int i = 0; s[i]; i++){
        sum -= s[i];
    }
    char ret = sum;
    return ret;
}

2.2 children with the most candy

2.2.1 title link

1431. Children with the most candy

2.2.2 train of thought analysis

The title means that given an array of candies, candies[i] means the candy shared by the ith child, and also a variable extractions, the amount of additional candy. Now we want to allocate the additional candy to these children. Is there a scheme for each child to become the child with the largest number of candy? If so, Is true.

We only need to judge all children once, candies [i] + extracts > = maxnum.

The code is as follows:

bool* kidsWithCandies(int* candies, int candiesSize, int extraCandies, int* returnSize){
    bool* ret = (bool*)malloc(sizeof(bool) * (candiesSize + 1));
    int max = 0;
    for(int i = 0; i < candiesSize; i++){
        if(max < candies[i])max = candies[i];
    }
    for(int i = 0; i < candiesSize; i++){
        if(candies[i] + extraCandies >= max){
            ret[i] = true;
        }
        else{
            ret[i] = false;
        }
    }
    *returnSize = candiesSize;
    return ret;
}

2.3 sum of all odd length subarrays

2.3.1 title link

1588. Sum of all odd length subarrays

2.3.2 method I violence

   we can enumerate subsequences with odd length. Assuming that the length is n, the starting point of the subsequence is start, the length is length, and the end subscript is end, then 0 < = start < = end < n, length = end - length + 1 is odd.

The code is as follows:

int sumOddLengthSubarrays(int* arr, int arrSize) {
    int sum = 0;
    for (int start = 0; start < arrSize; start++) {
        for (int length = 1; start + length <= arrSize; length += 2) {
            int end = start + length - 1;
            for (int i = start; i <= end; i++) {
                sum += arr[i];
            }
        }
    }
    return sum;
}

2.3.3 method II prefix and

   we define an array prefixSums [], prefixsum [i] represents the sum of numbers from 0 to i - 1. After obtaining the prefix and array, for 0 < = start < = end < n, the sum of subarrays of the subscript range [start,end] of array arr is prefixSums[end + 1] - prefixSums[start].

Under the code diagram:

int sumOddLengthSubarrays(int* arr, int arrSize){
    int prefixSum[arrSize + 1];
    prefixSum[0] = 0;
    for(int i = 0; i < arrSize; i++){
        prefixSum[i + 1] = prefixSum[i] + arr[i]; 
    }
    int sum = 0;
    for(int start = 0; start < arrSize; start++){
        for(int length = 1; length + start <= arrSize; length += 2){
            int end = start + length;
            sum += (prefixSum[end] - prefixSum[start]);
        }
    }
    return sum;
}

2.3.4 mathematical method 3

Mathematical derivation

The code is as follows:

int sumOddLengthSubarrays(int* arr, int arrSize){
    int sum = 0;
    for(int i = 0; i < arrSize; i++){
        int left = i, right = arrSize - left - 1;
        int leftodd = (left + 1) / 2;
        int rightodd = (right + 1) / 2;
        int lefteven = left / 2 + 1;
        int righteven = right / 2 + 1;
        sum += arr[i] * (leftodd * rightodd + lefteven * righteven);
    }
    return sum;
}

2.4 count triples

1534. Count triples

We only need to use three-layer traversal to judge and find out the ones that meet the conditions.

The code is as follows:

int countGoodTriplets(int* arr, int arrSize, int a, int b, int c){
    int count = 0;

    for(int i = 0; i < arrSize - 2; i++){
        for(int j = i + 1; j < arrSize - 1; j++){
            for(int k = j + 1; k < arrSize; k++){
                if(abs(arr[i]-arr[j])<=a && abs(arr[j]-arr[k])<=b && 
                						abs(arr[i] - arr[k]) <= c){
                        count++;
                    }
            }
        }
    }
    return count;
}

2.5 create the target array in the established order

2.5.1 title link

1389. Create the target array in the established order

In this problem, we only need to record the subscript reached by the current number in one pass. When we want to insert data in front, we need to move the following data back.

The code is as follows:

int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize){
    int* target = (int*)malloc(sizeof(int) * numsSize);
    int tail = -1;
    for(int i = 0; i < numsSize; i++){
        tail++;
        for(int j = tail; j > index[i]; j--){
            target[j] = target[j - 1];
        }
        target[index[i]] = nums[i];
    }
    *returnSize = numsSize;
    return target;
}

2.6 count the number of square sum triples

2.6.1 title link

1925. Count the number of square sum triples
This problem is very simple. Let's go through three layers to find out all the conditions.

The code is as follows:

int countTriples(int n){
    int count = 0;
    for(int i = 1; i < n - 1; i++){
        for(int j = i + 1; j < n; j++){
            for(int k = j + 1; k <= n; k++){
                if(i * i + j * j == k * k){
                    count++;
                }
            }
        }
    }
    return count * 2;
}

Posted by CerealBH on Sat, 20 Nov 2021 18:29:05 -0800