Files
C/leetcode/src/1653.c
Alexander Pantyukhin defd82dda1 feat: add Minimum Deletions LeetCode problem (#1138)
* 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>
2022-11-25 17:38:33 -06:00

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);
}