mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-13 21:42:29 +00:00
* add Minimum Deletions to Make String Balanced * Update 1653.c add new line at the end * Rename README.md to DIRECTORY.md * chore: apply suggestions from code review * Update leetcode/src/1653.c Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Update leetcode/src/1653.c Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Update leetcode/src/1653.c Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Update 1653.c remove redundant define * Update leetcode/src/1653.c Co-authored-by: John Law <johnlaw.po@gmail.com> Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> Co-authored-by: John Law <johnlaw.po@gmail.com>
30 lines
725 B
C
30 lines
725 B
C
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
|
|
|
|
// Dynamic programming approach. Down -> Up.
|
|
// Runtime: O(n)
|
|
// Space: O(1)
|
|
int minimumDeletions(char * s){
|
|
int len = strlen(s);
|
|
|
|
int aStateValue = s[0] == 'b';
|
|
|
|
int bStateValue = 0;
|
|
|
|
int newAStateValue;
|
|
int newBStateValue;
|
|
|
|
for(int i = 1; i < len; i++){
|
|
newAStateValue = aStateValue + (s[i] == 'b');
|
|
|
|
newBStateValue = min(
|
|
aStateValue,
|
|
bStateValue + (s[i] == 'a')
|
|
);
|
|
|
|
aStateValue = newAStateValue;
|
|
bStateValue = newBStateValue;
|
|
}
|
|
|
|
return min(aStateValue, bStateValue);
|
|
}
|