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>
This commit is contained in:
Alexander Pantyukhin 2022-11-26 03:38:33 +04:00 committed by GitHub
parent 8d28f1d36f
commit defd82dda1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -100,6 +100,7 @@
| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy |
| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy |
| 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C](./src/1524.c) | Medium |
| 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [C](./src/1653.c) | Medium |
| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy |
| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium |
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |

29
leetcode/src/1653.c Normal file
View File

@ -0,0 +1,29 @@
#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);
}