1.4 Learning Blog

1.4 Learning Blog
Today I learned the basic idea of ​​the greedy algorithm, and used it in two basic topics, combining the characteristics of the topic itself and the use of dual pointers.
First question: https://leetcode-cn.com/problems/partition-labels/
Since the same letter can only appear in the same segment, it is obvious that the subscript position of the first occurrence of the same letter and the last occurrence of the same letter The subscript position must appear in the same segment. Therefore, it is necessary to traverse the string to get the subscript position of the last occurrence of each letter.
After getting the subscript position of the last occurrence of each letter, the greedy algorithm and double pointer method can be used to divide the string into as many segments as possible
int max(int ​​a,int b){ if(a<b)return b; else{return a;} } int* partitionLabels(char * S, int* returnSize){ int start=0,end=0; int i,abc[26]; for(i=0;i<26;i++ )abc[i]=0; for(i=0;i<strlen(S);i++)abc[S[i]-'a']=i; returnSize=0; int p=(int*)malloc( sizeof(int)*strlen(S)); for(i=0;i<strlen(S);i++){ end=max(end,abc[S[i]-'a']); if(i= =end){













p[(*returnSize)++]=end-start+1;
start=end+1;
}
}
return p;
}
Second question: https://leetcode-cn.com/problems/score-after-flipping- matrix/
According to the meaning of the question, you can first consider all the row flips, and then consider all the column flips. To get the highest score, the leftmost number in each row of the matrix must be 1. The original 1 is kept, and the original 0 is reversed. After changing the leftmost number of each row to 1, you can only perform column flipping. In order to maximize the total score, we want to make the number of 1s in each column as many as possible. Therefore, we scan every column except for the leftmost column. If the number of 0 in this column is more than the number of 1, then the column is flipped, and the other columns remain unchanged.
Part of the code:

int max(int ​​a,int b){ if(a>b)return a; else{return b;} } int matrixScore(int** A, int ASize, int* AColSize){ int result=0; int m, n,k; m=ASize; n=AColSize[0]; result=m*(1<<(n-1));//The maximum bit 1 of m column can provide m 2 n-1 times for( int j=1;j<n;j++){//Counting from left to right, the first column is numbered 0, and the second column is numbered 1 k=0; for(int i=0;i<m;i++){ if(A[i][0]==1)k+=A[i][j]; else{k+=1-A[i][j];} } k=max(k,mk);// Take the largest result of 0 and 1 +=k*(1<<(nj-1));//The value provided in the jth column is added to the previous value } return result; } Summary: The use of greedy algorithm requires the question Only a thorough understanding of the meaning of the question can use the greedy algorithm in the right place, that is, combining the characteristics of the question itself; it can be combined with other techniques to improve efficiency, such as double pointers.




















Guess you like

Origin blog.csdn.net/weixin_47529865/article/details/112208721