diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 3adcf8d2..7a6dcc5d 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -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 | diff --git a/leetcode/src/1653.c b/leetcode/src/1653.c new file mode 100644 index 00000000..04ac0c16 --- /dev/null +++ b/leetcode/src/1653.c @@ -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); +}