Skip to content
Closed
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