mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
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:
parent
8d28f1d36f
commit
defd82dda1
@ -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
29
leetcode/src/1653.c
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user