Skip to content

Commit bac8ee4

Browse files
added solution to the problem 1963 of leetcode
1 parent e5dad3f commit bac8ee4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

leetcode/src/1963.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
int minSwaps(char* s)
5+
{
6+
int n = strlen(s);
7+
int sum = 0;
8+
int minmSum = INT_MAX;
9+
10+
for (int i = 0; i < n; i++)
11+
{
12+
if (s[i] == '[')
13+
{
14+
// if opening bracket comes increase the sum
15+
sum = sum + 1;
16+
}
17+
else
18+
{
19+
sum = sum - 1;
20+
// in case of closing bracket decrease the sum
21+
}
22+
23+
// find the minimum sum
24+
if (sum < minmSum)
25+
{
26+
minmSum = sum;
27+
}
28+
}
29+
// if minimum sum is greater than 0 then no need of swap
30+
// in one swap we can increase the minimum swap by 2
31+
if (minmSum > 0)
32+
{
33+
return 0;
34+
}
35+
else
36+
{
37+
return (abs(minmSum) + 1) / 2;
38+
}
39+
}

0 commit comments

Comments
 (0)