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

changed 494. Target Sum & Added solution 1963. Minimum Number of Swaps to Make the String Balanced #252

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
60 changes: 39 additions & 21 deletions leetcode/0043.Multiply-Strings/43. Multiply Strings.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
/*
* Author: Sk Shahriar Ahmed Raka
* Email: [email protected]
* Telegram: https://t.me/shahriarraka
* Github: https://github.com/skshahriarahmedraka
* StackOverflow: https://stackoverflow.com/users/12216779/
* Linkedin: https://linkedin.com/in/shahriarraka
* -----
* Last Modified:
* Modified By:
* -----
* Copyright (c) 2022 Your Company
* -----
* HISTORY:
*/

// 311 / 311 test cases passed.
// Status: Accepted
// Runtime: 0 ms
// Memory Usage: 2.3 MB

// You are here!
// Your runtime beats 100.00 % of golang submissions.

// You are here!
// Your memory usage beats 77.90 % of golang submissions.
package leetcode

import (
"math/big"

)


func multiply(num1 string, num2 string) string {
if num1 == "0" || num2 == "0" {
return "0"
}
b1, b2, tmp := []byte(num1), []byte(num2), make([]int, len(num1)+len(num2))
for i := 0; i < len(b1); i++ {
for j := 0; j < len(b2); j++ {
tmp[i+j+1] += int(b1[i]-'0') * int(b2[j]-'0')
}
}
for i := len(tmp) - 1; i > 0; i-- {
tmp[i-1] += tmp[i] / 10
tmp[i] = tmp[i] % 10
}
if tmp[0] == 0 {
tmp = tmp[1:]
}
res := make([]byte, len(tmp))
for i := 0; i < len(tmp); i++ {
res[i] = '0' + byte(tmp[i])
}
return string(res)
var x big.Int
var y big.Int
var z big.Int
x.SetString(num1,10)
y.SetString(num2,10)
z.Mul(&x,&y)
return z.String()
}
32 changes: 32 additions & 0 deletions leetcode/0494.Target-Sum/494. Target Sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,35 @@ func dfsFindTargetSumWays(nums []int, index int, curSum int, S int, res *int, su
dfsFindTargetSumWays(nums, index+1, curSum+nums[index], S, res, sums)
dfsFindTargetSumWays(nums, index+1, curSum-nums[index], S, res, sums)
}

// recursive
func RecursivefindTargetSumWays(nums []int, target int) int {
// Runtime: 611 ms, faster than 19.64% of Go online submissions for Target Sum.
// Memory Usage: 2 MB, less than 97.26% of Go online submissions for Target Sum.
ans :=0
CurrentValue:=0
var sum func(int,[]int)
sum =func (CurrentValue int, arr []int) {
if len(arr)==0 && CurrentValue==target {
ans+=1
return
}else if len(arr)==0 {
return
}
temp:=arr
if len(arr)==1{
temp=[]int{}
}else {
temp=arr[1:]
}

sum(CurrentValue+arr[0],temp)
sum(CurrentValue-arr[0],temp)


}

sum(CurrentValue,nums)
return ans
}

36 changes: 35 additions & 1 deletion leetcode/0494.Target-Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,38 @@ func dfsFindTargetSumWays(nums []int, index int, curSum int, S int, res *int, su
dfsFindTargetSumWays(nums, index+1, curSum-nums[index], S, res, sums)
}

```
```

// recursive
```
func RecursivefindTargetSumWays(nums []int, target int) int {
// Runtime: 611 ms, faster than 19.64% of Go online submissions for Target Sum.
// Memory Usage: 2 MB, less than 97.26% of Go online submissions for Target Sum.
ans :=0
CurrentValue:=0
var sum func(int,[]int)
sum =func (CurrentValue int, arr []int) {
if len(arr)==0 && CurrentValue==target {
ans+=1
return
}else if len(arr)==0 {
return
}
temp:=arr
if len(arr)==1{
temp=[]int{}
}else {
temp=arr[1:]
}

sum(CurrentValue+arr[0],temp)
sum(CurrentValue-arr[0],temp)


}

sum(CurrentValue,nums)
return ans
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package leetcode
/*
* Author: Sk Shahriar Ahmed Raka
* Email: [email protected]
* Telegram: https://t.me/shahriarraka
* Github: https://github.com/skshahriarahmedraka
* StackOverflow: https://stackoverflow.com/users/12216779/
* Linkedin: https://linkedin.com/in/shahriarraka
* -----
* Last Modified:
* Modified By:
* -----
* Copyright (c) 2022 Your Company
* -----
* HISTORY:
*/



// 58 / 58 test cases passed.
// Status: Accepted
// Runtime: 42 ms
// Memory Usage: 9.8 MB

// Submitted: 0 minutes ago
// Runtime: 42 ms, faster than 87.50% of Go online submissions for Minimum Number of Swaps to Make the String Balanced.
// Memory Usage: 9.8 MB, less than 81.25% of Go online submissions for Minimum Number of Swaps to Make the String Balanced.





func minSwaps(s string) int {
maxClose:=0
close:=0
for _,i:= range s {
if i=='['{
close-=1
}else {
close+=1
}
maxClose=MAX(maxClose,close)


}
return (maxClose+1)/2

}

func MAX(i,j int)int{
if i>j{
return i
}
return j
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## 1963. Minimum Number of Swaps to Make the String Balanced
### Medium

You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.

A string is called balanced if and only if:

It is the empty string, or
It can be written as AB, where both A and B are balanced strings, or
It can be written as [C], where C is a balanced string.

You may swap the brackets at any two indices any number of times.

Return the minimum number of swaps to make s balanced.



Example 1:
Input: s = "][]["
Output: 1
Explanation: You can make the string balanced by swapping index 0 with index 3.
The resulting string is "[[]]".

Example 2:
Input: s = "]]][[["
Output: 2
Explanation: You can do the following to make the string balanced:
- Swap index 0 with index 4. s = "[]][][".
- Swap index 1 with index 5. s = "[[][]]".
The resulting string is "[[][]]".

Example 3:

Input: s = "[]"
Output: 0
Explanation: The string is already balanced.



Constraints:

n == s.length
2 <= n <= 106
n is even.
s[i] is either '[' or ']'.
The number of opening brackets '[' equals n / 2, and the number of closing brackets ']' equals n / 2.

Accepted
32,547
Submissions
47,800


## solution
```
func minSwaps(s string) int {
maxClose:=0
close:=0
for _,i:= range s {
if i=='['{
close-=1
}else {
close+=1
}
maxClose=MAX(maxClose,close)


}
return (maxClose+1)/2

}

func MAX(i,j int)int{
if i>j{
return i
}
return j
}
```