Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution to the problem 1963 of leetcode #1426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions leetcode/src/1963.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time complexity: O(n)
// Space complexity: O(1)

int minSwaps(char* s)
{
int n = strlen(s);
int sum = 0;
int minmSum = INT_MAX;

for (int i = 0; i < n; i++)
{
if (s[i] == '[')
{
// if opening bracket comes increase the sum
sum = sum + 1;
}
else
{
sum = sum - 1;
// in case of closing bracket decrease the sum
}

// find the minimum sum
if (sum < minmSum)
{
minmSum = sum;
}
}
// if minimum sum is greater than 0 then no need of swap
// in one swap we can increase the minimum swap by 2
if (minmSum > 0)
{
return 0;
}
else
{
return (abs(minmSum) + 1) / 2;
}
}
Loading