From 4d0687097b455271bfa2fd38d41e7256bea6c027 Mon Sep 17 00:00:00 2001 From: sk shahriar ahmed raka Date: Sun, 14 Aug 2022 14:43:29 +0600 Subject: [PATCH 1/3] changed 494. Target Sum --- leetcode/0494.Target-Sum/494. Target Sum.go | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/leetcode/0494.Target-Sum/494. Target Sum.go b/leetcode/0494.Target-Sum/494. Target Sum.go index 804152328..0f225e534 100644 --- a/leetcode/0494.Target-Sum/494. Target Sum.go +++ b/leetcode/0494.Target-Sum/494. Target Sum.go @@ -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 +} + From 1d83ca31af2b9d4679470a98e58f0007cc1086fc Mon Sep 17 00:00:00 2001 From: sk shahriar ahmed raka Date: Sun, 14 Aug 2022 14:52:04 +0600 Subject: [PATCH 2/3] added solution 1963. Minimum Number of Swaps to Make the String Balanced --- leetcode/0494.Target-Sum/README.md | 36 ++++++++- ...er of Swaps to Make the String Balanced.go | 55 +++++++++++++ .../README.md | 79 +++++++++++++++++++ 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 leetcode/1963. Minimum Number of Swaps to Make the String Balanced/Minimum Number of Swaps to Make the String Balanced.go create mode 100644 leetcode/1963. Minimum Number of Swaps to Make the String Balanced/README.md diff --git a/leetcode/0494.Target-Sum/README.md b/leetcode/0494.Target-Sum/README.md index 94234e7b9..6eba5c283 100644 --- a/leetcode/0494.Target-Sum/README.md +++ b/leetcode/0494.Target-Sum/README.md @@ -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) } -``` \ No newline at end of file +``` + +// 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 +} +``` + diff --git a/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/Minimum Number of Swaps to Make the String Balanced.go b/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/Minimum Number of Swaps to Make the String Balanced.go new file mode 100644 index 000000000..ce13c0681 --- /dev/null +++ b/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/Minimum Number of Swaps to Make the String Balanced.go @@ -0,0 +1,55 @@ +package leetcode +/* + * Author: Sk Shahriar Ahmed Raka + * Email: skshahriarahmedraka@gmail.com + * 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 +} \ No newline at end of file diff --git a/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/README.md b/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/README.md new file mode 100644 index 000000000..1330a07d9 --- /dev/null +++ b/leetcode/1963. Minimum Number of Swaps to Make the String Balanced/README.md @@ -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 +} +``` \ No newline at end of file From ef3663e5d3d55c081e1ae085546418ead3822447 Mon Sep 17 00:00:00 2001 From: Sk Shahriar Ahmed Raka Date: Fri, 19 Aug 2022 22:41:39 +0600 Subject: [PATCH 3/3] changed 43. Multiply Strings --- .../43. Multiply Strings.go | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/leetcode/0043.Multiply-Strings/43. Multiply Strings.go b/leetcode/0043.Multiply-Strings/43. Multiply Strings.go index 88ab4b4c7..c6893837a 100644 --- a/leetcode/0043.Multiply-Strings/43. Multiply Strings.go +++ b/leetcode/0043.Multiply-Strings/43. Multiply Strings.go @@ -1,25 +1,43 @@ +/* +* Author: Sk Shahriar Ahmed Raka +* Email: skshahriarahmedraka@gmail.com + * 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() }