We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e5dad3f commit bac8ee4Copy full SHA for bac8ee4
leetcode/src/1963.c
@@ -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
36
37
+ return (abs(minmSum) + 1) / 2;
38
39
+}
0 commit comments