This is the 27th day of my participation in the August More Text Challenge

1975. Maximum square matrix sum

Thought analysis

The problem I see diagram and the subject name will have a strong sense of prefix and both view, carefully to see if is not, however, is to calculate the maximum and square matrix elements of the first purpose, at the same time, we also can be multiplied by the side of infinite time to public – 1 operation, for the two positive – 1 operation must be lost, for the two negative, 1 it into two positive operation, In the case of one plus one minus, the -1 operation will swap the -1 position.

So we take a bold guess that if there are even numbers of -1’s in the problem, the maximum sum is the sum of abs(arr[I]), and if there are an odd number of -1’s, the maximum sum is the sum of abs(arr[I]) minus the minimum of abs(arr[I]) *2

long long maxMatrixSum(vector<vector<int>>& matrix) {
        int numfushu = 0;
        long long  ret = 0;
        int mi = 100000;
        for(int i = 0; i < matrix.size(a); i++){for(int j = 0; j < matrix[i].size(a); j++){if(matrix[i][j] < 0 ){
                    numfushu++;
                }
                int ab = abs(matrix[i][j]);
                ret += ab;
                mi = min(ab, mi); }}if (numfushu % 2= =0)return ret;
        return ret - mi * 2;
    }
Copy the code

Sure enough, I can’t understand why I have to use longlong card.

12. Convert integers to Roman numerals

Thought analysis

This problem for ordinary use cases, like 27, is just a constant determination of the size of the number and the division to take the remainder, like 27 is less than 50, greater than 10, 27/10 = 2, 27/10 = 7, less than 10 is greater than 5, 7/5 = 1, 7% 5 = 2.

It’s only the special cases that matter.

The answer is a hard coded solution to the problem in a different way (the author deleted his own junk code after reading the answer)

const string thousands[] = {""."M"."MM"."MMM"};
const string hundreds[]  = {""."C"."CC"."CCC"."CD"."D"."DC"."DCC"."DCCC"."CM"};
const string tens[]      = {""."X"."XX"."XXX"."XL"."L"."LX"."LXX"."LXXX"."XC"};
const string ones[]      = {""."I"."II"."III"."IV"."V"."VI"."VII"."VIII"."IX"};

class Solution {
public:
    string intToRoman(int num) {
        return thousands[num / 1000] + hundreds[num % 1000 / 100] + tens[num % 100 / 10] + ones[num % 10]; }};Copy the code