From 489403916e71167b3b45952281d360521c830c30 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Jun 2021 17:30:04 -0700 Subject: [PATCH 001/969] Create L1465.go --- Medium/L1465.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L1465.go diff --git a/Medium/L1465.go b/Medium/L1465.go new file mode 100644 index 0000000..1b2d517 --- /dev/null +++ b/Medium/L1465.go @@ -0,0 +1,28 @@ +package Medium + +func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int { + sort.Ints(horizontalCuts) + sort.Ints(verticalCuts) + + maxX := max(h - horizontalCuts[len(horizontalCuts) - 1], + horizontalCuts[0]) + + maxY := max(w - verticalCuts[len(verticalCuts) - 1], + verticalCuts[0]) + + for i := 0; i < len(horizontalCuts) - 1; i++ { + maxX = max(maxX, horizontalCuts[i + 1]-horizontalCuts[i]) + } + for i := 0; i < len(verticalCuts) - 1; i++ { + maxY = max(maxY, verticalCuts[i + 1]-verticalCuts[i]) + } + + return (maxX * maxY) % 1000000007 +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} From 8e6edd3b902b68dcb7725006805f3a7554b9666b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Jun 2021 17:30:38 -0700 Subject: [PATCH 002/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3842d82..e2e09ef 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Please give this repo a ⭐ if it inspires you. |[1268](https://leetcode.com/problems/search-suggestions-system/)| Search Suggestions System| |[695](https://leetcode.com/problems/max-area-of-island/)| Max Area of Island| |[97](https://leetcode.com/problems/interleaving-string/)| Interleaving String| +|[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| ## Hard |LC #|Description| From 880feb41e1cf2b36445eea872782fa1f7539fe0a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 6 Jun 2021 12:36:04 -0700 Subject: [PATCH 003/969] Create L128.go --- Medium/L128.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L128.go diff --git a/Medium/L128.go b/Medium/L128.go new file mode 100644 index 0000000..8b2f67a --- /dev/null +++ b/Medium/L128.go @@ -0,0 +1,32 @@ +package Medium + +func longestConsecutive(nums []int) int { + numMap := map[int]bool{} + consecutiveNumCnt := 0 + + for _, num := range nums{ + numMap[num] = true + } + + for num := range numMap{ + if numMap[num - 1]{ + continue + } + + cur := num + for numMap[cur+1]{ + cur++ + } + + consecutiveNumCnt = max(consecutiveNumCnt, cur - num+1) + } + + return consecutiveNumCnt +} + +func max(a, b int)int{ + if a > b{ + return a + } + return b +} From 8a9616cc2eb2c527a0eb1365003445e6471ef21b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 6 Jun 2021 12:36:41 -0700 Subject: [PATCH 004/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e2e09ef..076d32d 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Please give this repo a ⭐ if it inspires you. |[695](https://leetcode.com/problems/max-area-of-island/)| Max Area of Island| |[97](https://leetcode.com/problems/interleaving-string/)| Interleaving String| |[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| +|[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| ## Hard |LC #|Description| From a6d22c59a10c9f923d1f3f8b28a25d44eab18c44 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Jun 2021 06:35:15 -0700 Subject: [PATCH 005/969] Create L105.go --- Medium/L105.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L105.go diff --git a/Medium/L105.go b/Medium/L105.go new file mode 100644 index 0000000..e46dfbd --- /dev/null +++ b/Medium/L105.go @@ -0,0 +1,33 @@ +package Medium + +func buildTree(preorder []int, inorder []int) *TreeNode { + if len(preorder) == 0 || len(inorder) == 0 { + return nil + } + + mp := make(map[int]int) + for i := range inorder { + mp[inorder[i]] = i + } + + return solve(preorder, inorder, mp, 0, len(preorder)-1, 0, len(inorder)-1) +} + +func solve(preorder, inorder []int, mp map[int]int, preStart, preEnd, inStart, inEnd int) *TreeNode { + if preStart > preEnd || inStart > inEnd { + return nil + } + + root := &TreeNode{ + Val: preorder[preStart], + Left: nil, + Right: nil, + } + inRoot := mp[root.Val] + numLeft := inRoot - inStart + + root.Left = solve(preorder, inorder, mp, preStart+1, preStart+numLeft, inStart, inRoot-1) + root.Right = solve(preorder, inorder, mp, preStart+numLeft+1, preEnd, inRoot+1, inEnd) + + return root +} From fb66776a8e3e6c573c3623b742b08cc33b8f1a2f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Jun 2021 06:35:44 -0700 Subject: [PATCH 006/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 076d32d..ede9062 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Please give this repo a ⭐ if it inspires you. |[97](https://leetcode.com/problems/interleaving-string/)| Interleaving String| |[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| |[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| +|[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| ## Hard |LC #|Description| From 70498dab73525b75f4f8820b8ae97226fcaf95e8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Jun 2021 07:04:21 -0700 Subject: [PATCH 007/969] Create L1696.go --- Medium/L1696.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L1696.go diff --git a/Medium/L1696.go b/Medium/L1696.go new file mode 100644 index 0000000..ac11742 --- /dev/null +++ b/Medium/L1696.go @@ -0,0 +1,18 @@ +package Medium + +func maxResult(nums []int, k int) int { + var dq []int + dq = append(dq, 0) + for i := 1; i < len(nums); i++ { + if dq[0]+k < i { + dq = dq[1:] + } + nums[i] += nums[dq[0]] + for len(dq) > 0 && nums[dq[len(dq)-1]] <= nums[i] { + dq = dq[:len(dq)-1] + } + dq = append(dq, i) + } + + return nums[len(nums)-1] +} From b5459ad17aaa485ff606adec04cc84c888af6d4f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Jun 2021 07:04:55 -0700 Subject: [PATCH 008/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ede9062..1a3529d 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ Please give this repo a ⭐ if it inspires you. |[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| |[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| |[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| +|[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| ## Hard |LC #|Description| From e8943a59a888fb5b93bc2d62bf6dcf60b2c23790 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 14 Jun 2021 07:29:49 -0700 Subject: [PATCH 009/969] Create L1710.go --- Easy/L1710.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L1710.go diff --git a/Easy/L1710.go b/Easy/L1710.go new file mode 100644 index 0000000..da8a7d0 --- /dev/null +++ b/Easy/L1710.go @@ -0,0 +1,22 @@ +package Easy + +func maximumUnits(boxTypes [][]int, truckSize int) int { + sort.Slice(boxTypes, func(i, j int) bool { + return boxTypes[i][1] > boxTypes[j][1] + }) + + sum, trucks := 0, 0 + for _, b := range boxTypes { + if trucks < truckSize { + if trucks+b[0] > truckSize { + sum += (truckSize - trucks) * b[1] + break + } else { + sum += b[0] * b[1] + trucks += b[0] + } + } + } + + return sum +} From 65ce828f30bd5289f543a9f6a2a072d3fcd49198 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 14 Jun 2021 07:30:21 -0700 Subject: [PATCH 010/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1a3529d..2f07e6a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Please give this repo a ⭐ if it inspires you. |[204](https://leetcode.com/problems/count-primes/)| Count Primes| |[453](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| Minimum Moves to Equal Array Elements| |[709](https://leetcode.com/problems/to-lower-case/)| To Lower Case| +|[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| ## Medium |LC #|Description| From 25b181dea7400809618cecdc5f24ac32edae00c3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Jun 2021 08:36:00 -0700 Subject: [PATCH 011/969] Create L698.go --- Medium/L698.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L698.go diff --git a/Medium/L698.go b/Medium/L698.go new file mode 100644 index 0000000..7cd1e4c --- /dev/null +++ b/Medium/L698.go @@ -0,0 +1,39 @@ +package Medium + +func makesquare(matchsticks []int) bool { + sum, max, k, vis := 0, 0, 4, make([]bool, len(matchsticks)) + for _, n := range matchsticks { + sum += n + if n > max { + max = n + } + } + + if sum%k != 0 || max > sum/k { + return false + } + + return solve(matchsticks, &vis, k, sum/k, 0, 0) +} + +func solve(nums []int, vis *[]bool, k, targetSubsetSum, curSubsetSum, nextIndexToCheck int) bool { + if k == 0 { + return true + } + + if curSubsetSum == targetSubsetSum { + return solve(nums, vis, k-1, targetSubsetSum, 0, 0) + } + + for i := nextIndexToCheck; i < len(nums); i++ { + if !(*vis)[i] && curSubsetSum+nums[i] <= targetSubsetSum { + (*vis)[i] = true + if solve(nums, vis, k, targetSubsetSum, curSubsetSum+nums[i], i+1) { + return true + } + (*vis)[i] = false + } + } + + return false +} From 2c9a675d71ab749fed164f2ca7fd469145cd3c3f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Jun 2021 08:36:30 -0700 Subject: [PATCH 012/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2f07e6a..1163b2b 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ Please give this repo a ⭐ if it inspires you. |[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| |[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| |[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| +|[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| ## Hard |LC #|Description| From 71599d48a50a45e6a8e19d0bd02e65c9197bbf22 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Jun 2021 06:53:55 -0700 Subject: [PATCH 013/969] Create L22.go --- Medium/L22.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L22.go diff --git a/Medium/L22.go b/Medium/L22.go new file mode 100644 index 0000000..c3d90de --- /dev/null +++ b/Medium/L22.go @@ -0,0 +1,32 @@ +package Medium + +func generateParenthesis(n int) []string { + var ans []string + var tmp string + if n == 0 { + return ans + } + + dfs(&ans, &tmp, n, 0, 0) + + return ans +} + +func dfs(ans *[]string, str *string, n, open, close int) { + if len(*str) == 2*n { + *ans = append(*ans, *str) + return + } + + if open < n { + *str += "(" + dfs(ans, str, n, open+1, close) + *str = (*str)[:len(*str)-1] + } + + if close < open { + *str += ")" + dfs(ans, str, n, open, close+1) + *str = (*str)[:len(*str)-1] + } +} From 52004254876d9fc1efe66e3a4b42fc5086166503 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Jun 2021 06:54:28 -0700 Subject: [PATCH 014/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1163b2b..82d6062 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ Please give this repo a ⭐ if it inspires you. |[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| |[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| |[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| +|[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| ## Hard |LC #|Description| From 015377073e22ad037162f39cd4c7d4428e5680b2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Jun 2021 14:32:13 -0700 Subject: [PATCH 015/969] Create L795.go --- Medium/L795.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L795.go diff --git a/Medium/L795.go b/Medium/L795.go new file mode 100644 index 0000000..310ba75 --- /dev/null +++ b/Medium/L795.go @@ -0,0 +1,19 @@ +package Medium + +func numSubarrayBoundedMax(nums []int, left int, right int) int { + start, last, res := -1, -1, 0 + for i := range nums { + if nums[i] > right { + start, last = i, i + continue + } + + if nums[i] >= left { + last = i + } + + res += last - start + } + + return res +} From f7e0c2adbe908c2b1a7747d706d3a9195c4134f3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Jun 2021 14:32:42 -0700 Subject: [PATCH 016/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 82d6062..f8eb164 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Please give this repo a ⭐ if it inspires you. |[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| |[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| |[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| +|[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| ## Hard |LC #|Description| From b67160d837f51e63db0bc6684876b82a87310f3c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 20 Jun 2021 13:01:43 -0700 Subject: [PATCH 017/969] Create L778.go --- Hard/L778.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Hard/L778.go diff --git a/Hard/L778.go b/Hard/L778.go new file mode 100644 index 0000000..3ed0cae --- /dev/null +++ b/Hard/L778.go @@ -0,0 +1,40 @@ +package Hard + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func swimInWater(grid [][]int) int { + time, m := 0, len(grid) + + for { + vis := make(map[int]bool) + dfs(grid, vis, 0, 0, time) + time++ + if _, ok := vis[m*m-1]; ok { + break + } + } + + return time - 1 +} + +func isValid(grid [][]int, x, y, time int) bool { + m, n := len(grid), len(grid[0]) + + return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] <= time +} + +func dfs(grid [][]int, vis map[int]bool, i, j, time int) { + if !isValid(grid, i, j, time) { + return + } + + if _, ok := vis[i*len(grid)+j]; ok { + return + } + + vis[i*len(grid)+j] = true + + for _, d := range dirs { + dfs(grid, vis, i+d[0], j+d[1], time) + } +} From 3a3eca32845e146ecb3984fc855cc1cc46799f5f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 20 Jun 2021 13:02:16 -0700 Subject: [PATCH 018/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f8eb164..51f3b21 100644 --- a/README.md +++ b/README.md @@ -157,3 +157,4 @@ Please give this repo a ⭐ if it inspires you. |[1473](https://leetcode.com/problems/paint-house-iii/)| Paint House III| |[51](https://leetcode.com/problems/n-queens/)| N-Queens| |[52](https://leetcode.com/problems/n-queens-ii/)| N-Queens II| +|[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| From c84ac7d6a05255ecd42dbc3f8d4d52c83d338f43 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Jun 2021 13:36:59 -0700 Subject: [PATCH 019/969] Create L118.go --- Easy/L118.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Easy/L118.go diff --git a/Easy/L118.go b/Easy/L118.go new file mode 100644 index 0000000..38027de --- /dev/null +++ b/Easy/L118.go @@ -0,0 +1,14 @@ +package Easy + +func generate(numRows int) [][]int { + a := make([][]int, numRows) + for i := 0; i Date: Mon, 21 Jun 2021 13:37:32 -0700 Subject: [PATCH 020/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 51f3b21..1f5501f 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ Please give this repo a ⭐ if it inspires you. |[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| |[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| |[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| +|[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| ## Hard |LC #|Description| From bb7aab397e525d1ffae8baa42a8f7dadcfcffe49 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Jun 2021 06:59:39 -0700 Subject: [PATCH 021/969] Create L576.go --- Medium/L576.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Medium/L576.go diff --git a/Medium/L576.go b/Medium/L576.go new file mode 100644 index 0000000..8bd6ea0 --- /dev/null +++ b/Medium/L576.go @@ -0,0 +1,47 @@ +package Medium + +const MOD = 1000000007 + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func findPaths(m int, n int, maxMove int, startRow int, startColumn int) int { + dp := make([][][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([][]int, n) + for j := 0; j < n; j++ { + dp[i][j] = make([]int, maxMove+1) + for k := 0; k < maxMove+1; k++ { + dp[i][j][k] = -1 + } + } + } + + return dfs(&dp, m, n, startRow, startColumn, maxMove) +} + +func isValid(m, n, x, y int) bool { + return x >= 0 && x < m && y >= 0 && y < n +} + +func dfs(dp *[][][]int, m, n, i, j, maxMove int) int { + if maxMove < 0 { + return 0 + } + + if !isValid(m, n, i, j) { + return 1 + } + + if (*dp)[i][j][maxMove] != -1 { + return (*dp)[i][j][maxMove] + } + + sum := 0 + for _, d := range dirs { + sum += dfs(dp, m, n, i+d[0], j+d[1], maxMove-1) % MOD + } + + (*dp)[i][j][maxMove] = sum % MOD + + return (*dp)[i][j][maxMove] % MOD +} From 25227d42afa697581c6991e9d94c84eab5291b1b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Jun 2021 07:00:19 -0700 Subject: [PATCH 022/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1f5501f..09866a9 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ Please give this repo a ⭐ if it inspires you. |[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| |[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| |[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| +|[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| ## Hard |LC #|Description| From 6330f7b1a92fbb80aac3dd0df2f43720cc8cd6e9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Jun 2021 07:36:53 -0700 Subject: [PATCH 023/969] Create L684.go --- Medium/L684.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L684.go diff --git a/Medium/L684.go b/Medium/L684.go new file mode 100644 index 0000000..0c8b10f --- /dev/null +++ b/Medium/L684.go @@ -0,0 +1,38 @@ +package Medium + +func findRedundantConnection(edges [][]int) []int { + if len(edges) == 0 { + return []int{} + } + parent := make([]int, 5000) + for i := 0; i < len(parent); i++ { + parent[i] = i + } + + for _, e := range edges { + if find(parent, e[0]) == find(parent, e[1]) { + return e + } + union(e[0], e[1], &parent) + } + + return []int{} +} + +func find(parent []int, x int) int { + if parent[x] == x { + return x + } + + return find(parent, parent[x]) +} + +func union(p, q int, parent *[]int) { + x, y := find(*parent, p), find(*parent, q) + + if x == y { + return + } + + (*parent)[y] = x +} From cf25b6713e2bddcc4d42ac0a821eed73eb39ac39 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Jun 2021 07:37:29 -0700 Subject: [PATCH 024/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 09866a9..4582ee9 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,7 @@ Please give this repo a ⭐ if it inspires you. |[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| |[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| |[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| +|[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| ## Hard |LC #|Description| From 4aac131d86f33bd9b2b59a99a9716c4d554063d5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Jun 2021 16:58:09 -0700 Subject: [PATCH 025/969] Create L315.go --- Hard/L315.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Hard/L315.go diff --git a/Hard/L315.go b/Hard/L315.go new file mode 100644 index 0000000..d9518d2 --- /dev/null +++ b/Hard/L315.go @@ -0,0 +1,52 @@ +package Hard + +func countSmaller(nums []int) []int { + res, index := make([]int, len(nums)), make([]int, len(nums)) + for i := 0; i < len(res); i++ { + index[i] = i + } + + mergeSort(&res, &index, 0, len(nums)-1, nums) + return res +} + +func mergeSort(res, index *[]int, l, r int, nums []int) { + if l >= r { + return + } + + mid := (l + r) / 2 + mergeSort(res, index, l, mid, nums) + mergeSort(res, index, mid+1, r, nums) + merge(res, index, l, mid, mid+1, r, nums) +} + +func merge(res, index *[]int, l1, r1, l2, r2 int, nums []int) { + tmp := make([]int, r2-l1+1) + start, count, p := l1, 0, 0 + for l1 <= r1 || l2 <= r2 { + if l1 > r1 { + tmp[p] = (*index)[l2] + p += 1 + l2 += 1 + } else if l2 > r2 { + (*res)[(*index)[l1]] += count + tmp[p] = (*index)[l1] + p += 1 + l1 += 1 + } else if nums[(*index)[l1]] > nums[(*index)[l2]] { + tmp[p] = (*index)[l2] + p += 1 + l2 += 1 + count++ + } else { + (*res)[(*index)[l1]] += count + tmp[p] = (*index)[l1] + p += 1 + l1 += 1 + } + } + for i := 0; i < len(tmp); i++ { + (*index)[start+i] = tmp[i] + } +} From 2a375d57fa9436e55dd201960c3445898b9af414 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Jun 2021 16:58:47 -0700 Subject: [PATCH 026/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4582ee9..a8cc03f 100644 --- a/README.md +++ b/README.md @@ -161,3 +161,4 @@ Please give this repo a ⭐ if it inspires you. |[51](https://leetcode.com/problems/n-queens/)| N-Queens| |[52](https://leetcode.com/problems/n-queens-ii/)| N-Queens II| |[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| +|[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| From 677da38b2f8734d4eed1f0979cc85f2147420026 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Jun 2021 07:25:54 -0700 Subject: [PATCH 027/969] Create L135.go --- Hard/L135.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Hard/L135.go diff --git a/Hard/L135.go b/Hard/L135.go new file mode 100644 index 0000000..e0b6ed1 --- /dev/null +++ b/Hard/L135.go @@ -0,0 +1,33 @@ +package Hard + +func candy(ratings []int) int { + candies := make([]int, len(ratings)) + for i := range candies { + candies[i] = 1 + } + + for i := range candies { + if i == 0 { + continue + } + + if ratings[i] > ratings[i-1] { + candies[i] = candies[i-1] + 1 + } + } + + for i := len(candies) - 2; i >= 0; i-- { + if ratings[i] > ratings[i+1] { + if candies[i+1]+1 > candies[i] { + candies[i] = candies[i+1] + 1 + } + } + } + + sum := 0 + for _, c := range candies { + sum += c + } + + return sum +} From 4123ec7ad1b67a4a3d6231eb330be9b7e48fd1ef Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Jun 2021 07:26:22 -0700 Subject: [PATCH 028/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a8cc03f..81e10ae 100644 --- a/README.md +++ b/README.md @@ -162,3 +162,4 @@ Please give this repo a ⭐ if it inspires you. |[52](https://leetcode.com/problems/n-queens-ii/)| N-Queens II| |[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| |[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| +|[135](https://leetcode.com/problems/candy/)| Candy| From 389a3963e37da5b120e1e283685688e96b046241 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Jun 2021 07:12:09 -0700 Subject: [PATCH 029/969] Create L1047.go --- Easy/L1047.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Easy/L1047.go diff --git a/Easy/L1047.go b/Easy/L1047.go new file mode 100644 index 0000000..8b80657 --- /dev/null +++ b/Easy/L1047.go @@ -0,0 +1,60 @@ +package Easy + +func removeDuplicates(s string) string { + st := NewStack() + + for _, c := range s { + if !st.isEmpty() { + if st.peek() == byte(c) { + st.pop() + continue + } else { + st.push(byte(c)) + } + } else { + st.push(byte(c)) + } + } + + var sb strings.Builder + for !st.isEmpty() { + sb.WriteByte(st.pop()) + } + + return reverse(sb.String()) +} + +type Stack struct { + st []byte +} + +func NewStack() *Stack { + return &Stack{st: []byte{}} +} + +func (s *Stack) pop() byte { + ret := s.st[len(s.st)-1] + s.st = s.st[:len(s.st)-1] + + return ret +} + +func (s *Stack) peek() byte { + return s.st[len(s.st)-1] +} + +func (s *Stack) push(c byte) { + s.st = append(s.st, c) +} + +func (s *Stack) isEmpty() bool { + return len(s.st) == 0 +} + +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} From ae44b9f9ee9b0d30f7a8b52b7505870cda3eaca2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Jun 2021 07:12:38 -0700 Subject: [PATCH 030/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 81e10ae..f2f6817 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Please give this repo a ⭐ if it inspires you. |[453](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| Minimum Moves to Equal Array Elements| |[709](https://leetcode.com/problems/to-lower-case/)| To Lower Case| |[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| +|[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| ## Medium |LC #|Description| From 92efd9bf7d834bc087b38cf525f826215d15908f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Jun 2021 06:47:38 -0700 Subject: [PATCH 031/969] Create L1004.go --- Medium/L1004.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/L1004.go diff --git a/Medium/L1004.go b/Medium/L1004.go new file mode 100644 index 0000000..92326e2 --- /dev/null +++ b/Medium/L1004.go @@ -0,0 +1,22 @@ +package Medium + +func longestOnes(nums []int, k int) int { + max, zeroCnt, i, j := 0, 0, 0, 0 + for ; j < len(nums); j++ { + if nums[j] == 0 { + zeroCnt++ + } + + for zeroCnt > k { + if nums[i] == 0 { + zeroCnt-- + } + i++ + } + if (j - i + 1) > max { + max = j - i + 1 + } + } + + return max +} From 4e2288e9fcc81d02bd9440524e6d3f5f54e5e60f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Jun 2021 06:48:10 -0700 Subject: [PATCH 032/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2f6817..7247076 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ Please give this repo a ⭐ if it inspires you. |[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| |[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| |[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| +|[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| ## Hard |LC #|Description| From df851f5689cd3ed62d79bb6257e4bebffe99ec35 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Jun 2021 07:29:18 -0700 Subject: [PATCH 033/969] Create L236.go --- Medium/L236.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L236.go diff --git a/Medium/L236.go b/Medium/L236.go new file mode 100644 index 0000000..3ed89de --- /dev/null +++ b/Medium/L236.go @@ -0,0 +1,25 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { + if root == p || root == q || root == nil { + return root + } + + left, right := lowestCommonAncestor(root.Left, p, q), lowestCommonAncestor(root.Right, p, q) + + if left == nil { + return right + } else if right == nil { + return left + } else { + return root + } +} From d245ee57e5294a6c56b32c5b3ab0a70971ce40fe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Jun 2021 07:29:48 -0700 Subject: [PATCH 034/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7247076..6668a60 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ Please give this repo a ⭐ if it inspires you. |[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| |[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| |[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| +|[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| ## Hard |LC #|Description| From 485e7083361b4c61b3339d748685551dc2d61a04 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 1 Jul 2021 06:25:51 -0700 Subject: [PATCH 035/969] Create L89.go --- Medium/L89.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Medium/L89.go diff --git a/Medium/L89.go b/Medium/L89.go new file mode 100644 index 0000000..cf51ba8 --- /dev/null +++ b/Medium/L89.go @@ -0,0 +1,14 @@ +package Medium + +func grayCode(n int) []int { + var res []int + res = append(res, 0) + for i := 0; i < n; i++ { + size := len(res) + for k := size - 1; k >= 0; k-- { + res = append(res, res[k]|1< Date: Thu, 1 Jul 2021 06:26:23 -0700 Subject: [PATCH 036/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6668a60..f152636 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ Please give this repo a ⭐ if it inspires you. |[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| |[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| |[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| +|[89](https://leetcode.com/problems/gray-code/)| Gray Code| ## Hard |LC #|Description| From 05cd576af04c35aeb5db36485d1b9274df1dc3ac Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 7 Jul 2021 12:39:33 -0700 Subject: [PATCH 037/969] Create L378.go --- Medium/L378.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L378.go diff --git a/Medium/L378.go b/Medium/L378.go new file mode 100644 index 0000000..b5f0d47 --- /dev/null +++ b/Medium/L378.go @@ -0,0 +1,31 @@ +package Medium + +func kthSmallest(matrix [][]int, k int) int { + lo, hi := matrix[0][0], matrix[len(matrix)-1][len(matrix)-1] + for lo <= hi { + mid := lo + (hi-lo)/2 + cnt := getLessEqual(matrix, mid) + if cnt < k { + lo = mid + 1 + } else { + hi = mid - 1 + } + } + + return lo +} + +func getLessEqual(matrix [][]int, val int) int { + res, n, i, j := 0, len(matrix), len(matrix)-1, 0 + + for i >= 0 && j < n { + if matrix[i][j] > val { + i-- + } else { + res += i + 1 + j++ + } + } + + return res +} From c318a597635928d714dec05dc388271c923be9b9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 7 Jul 2021 12:40:05 -0700 Subject: [PATCH 038/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f152636..40882a3 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ Please give this repo a ⭐ if it inspires you. |[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| |[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| |[89](https://leetcode.com/problems/gray-code/)| Gray Code| +|[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| ## Hard |LC #|Description| From ce7d5da60736ea9e60167dd6f70ac06aee20de22 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 8 Jul 2021 17:17:16 -0700 Subject: [PATCH 039/969] Create L718.go --- Medium/L718.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L718.go diff --git a/Medium/L718.go b/Medium/L718.go new file mode 100644 index 0000000..445966f --- /dev/null +++ b/Medium/L718.go @@ -0,0 +1,40 @@ +package Medium + +func findLength(nums1 []int, nums2 []int) int { + res, m, n := 0, len(nums1), len(nums2) + + memo := make([][]int, m+1) + for i := range memo { + memo[i] = make([]int, n+1) + for j := range memo[i] { + memo[i][j] = -1 + } + } + + solve(nums1, nums2, m, n, &memo, &res) + return res +} + +func solve(n1, n2 []int, m, n int, memo *[][]int, res *int) int { + if m == 0 || n == 0 { + return 0 + } + + if (*memo)[m][n] != -1 { + return (*memo)[m][n] + } + + solve(n1, n2, m, n-1, memo, res) + solve(n1, n2, m-1, n, memo, res) + + if n1[m-1] == n2[n-1] { + (*memo)[m][n] = 1 + solve(n1, n2, m-1, n-1, memo, res) + if (*memo)[m][n] > *res { + *res = (*memo)[m][n] + } + return (*memo)[m][n] + } + (*memo)[m][n] = 0 + + return (*memo)[m][n] +} From 5ee6104bcae494a0880760e6ae11bd17605aeee3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 8 Jul 2021 17:17:49 -0700 Subject: [PATCH 040/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 40882a3..e2bd9da 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ Please give this repo a ⭐ if it inspires you. |[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| |[89](https://leetcode.com/problems/gray-code/)| Gray Code| |[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| +|[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| ## Hard |LC #|Description| From b752c1b9542d7d517a11a81fcfe927a77ca4c7c9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 9 Jul 2021 15:15:07 -0700 Subject: [PATCH 041/969] Create L300.go --- Medium/L300.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L300.go diff --git a/Medium/L300.go b/Medium/L300.go new file mode 100644 index 0000000..4afdd90 --- /dev/null +++ b/Medium/L300.go @@ -0,0 +1,38 @@ +package Medium + +func lengthOfLIS(nums []int) int { + if len(nums) == 1 { + return 1 + } + + dp := make([]int, len(nums)) + for i := range dp { + dp[i] = math.MinInt64 + } + + return solve(nums, 0, math.MinInt64, &dp) +} + +func solve(nums []int, idx, prev int, dp *[]int) int { + if idx >= len(nums) { + return 0 + } + + curr := nums[idx] + left, right := 0, 0 + + if curr > prev { + if (*dp)[idx] == math.MinInt64 { + (*dp)[idx] = 1 + solve(nums, idx+1, curr, dp) + } + left = (*dp)[idx] + } + + right = solve(nums, idx+1, prev, dp) + + if left > right { + return left + } + + return right +} From 6b71e6fb7a13a2de87ff4315c8ae3db239f5186a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 9 Jul 2021 15:15:41 -0700 Subject: [PATCH 042/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e2bd9da..fcb90cb 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ Please give this repo a ⭐ if it inspires you. |[89](https://leetcode.com/problems/gray-code/)| Gray Code| |[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| |[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| +|[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| ## Hard |LC #|Description| From b5d0257d48da16a959058fa5e66c78b8f5f8fb87 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 13 Jul 2021 15:12:01 -0700 Subject: [PATCH 043/969] Create L162.go --- Medium/L162.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Medium/L162.go diff --git a/Medium/L162.go b/Medium/L162.go new file mode 100644 index 0000000..24dccac --- /dev/null +++ b/Medium/L162.go @@ -0,0 +1,17 @@ +package Medium + +func findPeakElement(nums []int) int { + l, h, mid := 0, len(nums)-1, 0 + + for l < h { + mid = l + (h-l)/2 + + if nums[mid] < nums[mid+1] { + l = mid + 1 + } else { + h = mid + } + } + + return l +} From 8453f87b5fb2db82c1466c4cf1c85a53c1448efe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 13 Jul 2021 15:12:33 -0700 Subject: [PATCH 044/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fcb90cb..4ac9d8a 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ Please give this repo a ⭐ if it inspires you. |[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| |[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| |[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| +|[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| ## Hard |LC #|Description| From efab4b8eed54b11634a73420b5ab4163a693dddc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Jul 2021 16:19:50 -0700 Subject: [PATCH 045/969] Create L791.go --- Medium/L791.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L791.go diff --git a/Medium/L791.go b/Medium/L791.go new file mode 100644 index 0000000..a8dd6a4 --- /dev/null +++ b/Medium/L791.go @@ -0,0 +1,25 @@ +package Medium + +func customSortString(order string, str string) string { + arr := make([]int, 26) + for i := 0; i < len(str); i++ { + arr[str[i]-'a']++ + } + + var sb strings.Builder + for i := 0; i < len(order); i++ { + for arr[order[i]-'a'] > 0 { + sb.WriteByte(order[i]) + arr[order[i]-'a']-- + } + } + + for i := 0; i < len(arr); i++ { + for arr[i] > 0 { + sb.WriteByte(byte(i) + 'a') + arr[i]-- + } + } + + return sb.String() +} From 0cb2413a0d1a755ed3d267ca3a8b5ffe0b08b8b0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Jul 2021 16:20:22 -0700 Subject: [PATCH 046/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4ac9d8a..0658173 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ Please give this repo a ⭐ if it inspires you. |[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| |[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| |[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| +|[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| ## Hard |LC #|Description| From 7aa2b1f39f15f92570f329e5ab964f68c3b17ec3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 15 Jul 2021 07:33:00 -0700 Subject: [PATCH 047/969] Create L611.go --- Medium/L611.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Medium/L611.go diff --git a/Medium/L611.go b/Medium/L611.go new file mode 100644 index 0000000..6681276 --- /dev/null +++ b/Medium/L611.go @@ -0,0 +1,23 @@ +package Medium + +func triangleNumber(nums []int) int { + res := 0 + if len(nums) < 3 { + return res + } + + sort.Ints(nums) + for i := 2; i < len(nums); i++ { + left, right := 0, i-1 + for left < right { + if nums[left]+nums[right] > nums[i] { + res += right - left + right-- + } else { + left++ + } + } + } + + return res +} From 47115ad28e275517f70a600e53979d4b4578dbf5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 15 Jul 2021 07:34:31 -0700 Subject: [PATCH 048/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0658173..7dcd895 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,7 @@ Please give this repo a ⭐ if it inspires you. |[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| |[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| |[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| +|[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| ## Hard |LC #|Description| From 485327186e5e310158272abaa5b5751ea7f87f29 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 17 Jul 2021 09:47:10 -0700 Subject: [PATCH 049/969] Create L927.go --- Hard/L927.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Hard/L927.go diff --git a/Hard/L927.go b/Hard/L927.go new file mode 100644 index 0000000..4dbb86b --- /dev/null +++ b/Hard/L927.go @@ -0,0 +1,54 @@ +func threeEqualParts(arr []int) []int { + numOne := 0 + for _, i := range arr { + if i == 1 { + numOne++ + } + } + + noRes := []int{-1, -1} + if numOne == 0 { + return []int{0, 2} + } + if numOne%3 != 0 { + return noRes + } + + idxThird, temp := 0, 0 + for i := len(arr) - 1; i >= 0; i-- { + if arr[i] == 1 { + temp++ + if temp == numOne/3 { + idxThird = i + break + } + } + } + + res1 := findEndIdx(arr, 0, idxThird) + if res1 < 0 { + return noRes + } + + res2 := findEndIdx(arr, res1+1, idxThird) + if res2 < 0 { + return noRes + } + + return []int{res1, res2 + 1} +} + +func findEndIdx(A []int, left, right int) int { + for A[left] == 0 { + left++ + } + for right < len(A) { + if A[left] != A[right] { + return -1 + } + left++ + right++ + } + + return left - 1 +} From 6b3488a1a711d3cde59947a0009396f94c6f9d0b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 17 Jul 2021 09:47:39 -0700 Subject: [PATCH 050/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7dcd895..3e37b23 100644 --- a/README.md +++ b/README.md @@ -173,3 +173,4 @@ Please give this repo a ⭐ if it inspires you. |[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| |[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| |[135](https://leetcode.com/problems/candy/)| Candy| +|[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| From 253e68bc29965e6ff682154a31113254501bfa61 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 18 Jul 2021 10:30:59 -0700 Subject: [PATCH 051/969] Create L25.go --- Hard/L25.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Hard/L25.go diff --git a/Hard/L25.go b/Hard/L25.go new file mode 100644 index 0000000..ad849fa --- /dev/null +++ b/Hard/L25.go @@ -0,0 +1,23 @@ +package Hard + +func reverseKGroup(head *ListNode, k int) *ListNode { + node, cnt := head, 0 + for cnt < k { + if node == nil { + return head + } + node = node.Next + cnt++ + } + + prev := reverseKGroup(node, k) + for cnt > 0 { + next := head.Next + head.Next = prev + prev = head + head = next + cnt-- + } + + return prev +} From 73c1c8b5bfd7b287e15de4be10665f454ce8f9cc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 18 Jul 2021 10:31:28 -0700 Subject: [PATCH 052/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3e37b23..1da299e 100644 --- a/README.md +++ b/README.md @@ -174,3 +174,4 @@ Please give this repo a ⭐ if it inspires you. |[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| |[135](https://leetcode.com/problems/candy/)| Candy| |[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| +|[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| From 096539cd68723d81372b895ea9c59476c6e2d4fd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 19 Jul 2021 14:50:16 -0700 Subject: [PATCH 053/969] Create L235.go --- Easy/L235.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L235.go diff --git a/Easy/L235.go b/Easy/L235.go new file mode 100644 index 0000000..60ebcff --- /dev/null +++ b/Easy/L235.go @@ -0,0 +1,20 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { + if root.Val > p.Val && root.Val > q.Val { + return lowestCommonAncestor(root.Left, p, q) + } else if root.Val < p.Val && root.Val < q.Val { + return lowestCommonAncestor(root.Right, p, q) + } else { + return root + } +} From 192d90274448f1e031bbe5e70449f1fa6aa28b8c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 19 Jul 2021 14:50:50 -0700 Subject: [PATCH 054/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1da299e..6af2ef7 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ Please give this repo a ⭐ if it inspires you. |[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| |[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| |[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| +|[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| ## Hard |LC #|Description| From af3bbd5a14abb75b691285bfe225b87f9cc8fb71 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Jul 2021 16:38:32 -0700 Subject: [PATCH 055/969] Create L838.go --- Medium/L838.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L838.go diff --git a/Medium/L838.go b/Medium/L838.go new file mode 100644 index 0000000..1702452 --- /dev/null +++ b/Medium/L838.go @@ -0,0 +1,36 @@ +package Medium + +func pushDominoes(dominoes string) string { + arr := []byte(dominoes) + L, R := -1, -1 + for i := 0; i <= len(dominoes); i++ { + if i == len(arr) || arr[i] == 'R' { + if R > L { + for R < i { + arr[R] = 'R' + R++ + } + } + R = i + } else if arr[i] == 'L' { + if L > R || R == -1 && L == -1 { + L++ + for L < i { + arr[L] = 'L' + L++ + } + } else { + L = i + lo, hi := R+1, L-1 + for lo < hi { + arr[lo] = 'R' + arr[hi] = 'L' + lo++ + hi-- + } + } + } + } + + return string(arr) +} From 98687f9f17199ab5f3e28c2a7f3bc300fc15c6fe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Jul 2021 16:39:07 -0700 Subject: [PATCH 056/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6af2ef7..7ac9a0b 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ Please give this repo a ⭐ if it inspires you. |[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| |[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| |[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| +|[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| ## Hard |LC #|Description| From 8d3115845b5feee06a986beff28933af17ec5828 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 22 Jul 2021 16:35:04 -0700 Subject: [PATCH 057/969] Rename L915.go to L916.go --- Medium/{L915.go => L916.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Medium/{L915.go => L916.go} (100%) diff --git a/Medium/L915.go b/Medium/L916.go similarity index 100% rename from Medium/L915.go rename to Medium/L916.go From a1055998872559b4bc8f1114570833e4a076519f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 22 Jul 2021 16:35:22 -0700 Subject: [PATCH 058/969] Create L915.go --- Medium/L915.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L915.go diff --git a/Medium/L915.go b/Medium/L915.go new file mode 100644 index 0000000..f9ab7af --- /dev/null +++ b/Medium/L915.go @@ -0,0 +1,19 @@ +package Medium + +func partitionDisjoint(nums []int) int { + localMax, partitionIdx := nums[0], 0 + max := localMax + + for i := range nums { + if localMax > nums[i] { + localMax = max + partitionIdx = i + } else { + if nums[i] > max { + max = nums[i] + } + } + } + + return partitionIdx + 1 +} From 5f75cc3f2ee891c83df44fab11e9d51e372d4415 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 22 Jul 2021 16:36:00 -0700 Subject: [PATCH 059/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7ac9a0b..b2e0c57 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ Please give this repo a ⭐ if it inspires you. |[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| |[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| |[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| +|[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| ## Hard |LC #|Description| From cb012a14f151301db594fcae953488a06b3d8266 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 23 Jul 2021 07:08:26 -0700 Subject: [PATCH 060/969] Create L814.go --- Medium/L814.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L814.go diff --git a/Medium/L814.go b/Medium/L814.go new file mode 100644 index 0000000..b029417 --- /dev/null +++ b/Medium/L814.go @@ -0,0 +1,20 @@ +package Medium + +func pruneTree(root *TreeNode) *TreeNode { + return dfs(root) +} + +func dfs(root *TreeNode) *TreeNode { + if root == nil { + return root + } + + root.Left = dfs(root.Left) + root.Right = dfs(root.Right) + + if root.Left == nil && root.Right == nil && root.Val == 0 { + return nil + } + + return root +} From 169e6944cc4c77b1d036539a228355629c4ebebe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 23 Jul 2021 07:08:59 -0700 Subject: [PATCH 061/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b2e0c57..784ab02 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ Please give this repo a ⭐ if it inspires you. |[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| |[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| |[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| +|[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| ## Hard |LC #|Description| From 62873b339e238fc6ef8323ec0cd523011f3caa4a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Jul 2021 09:46:00 -0700 Subject: [PATCH 062/969] Create L233.go --- Hard/L233.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Hard/L233.go diff --git a/Hard/L233.go b/Hard/L233.go new file mode 100644 index 0000000..2f883ec --- /dev/null +++ b/Hard/L233.go @@ -0,0 +1,26 @@ +package Hard + +func findIntegers(n int) int { + f := make([]int, 32) + f[0], f[1] = 1, 2 + + for i := 2; i < 32; i++ { + f[i] = f[i-1] + f[i-2] + } + + ans, k, preBit := 0, 30, 0 + for k >= 0 { + if (n & (1 << k)) > 0 { + ans += f[k] + if preBit > 0 { + return ans + } + preBit = 1 + } else { + preBit = 0 + } + k-- + } + + return ans + 1 +} From 900e083489359e18118fbe2365c737cfc4550662 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Jul 2021 09:46:29 -0700 Subject: [PATCH 063/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 784ab02..3dcaafe 100644 --- a/README.md +++ b/README.md @@ -179,3 +179,4 @@ Please give this repo a ⭐ if it inspires you. |[135](https://leetcode.com/problems/candy/)| Candy| |[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| |[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| +|[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| From e258255dd5e7ddfaa7fba3fbb76b8013955d7a87 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Jul 2021 16:20:56 -0700 Subject: [PATCH 064/969] Create L108.go --- Easy/L108.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Easy/L108.go diff --git a/Easy/L108.go b/Easy/L108.go new file mode 100644 index 0000000..e096fec --- /dev/null +++ b/Easy/L108.go @@ -0,0 +1,23 @@ +package Easy + +func sortedArrayToBST(nums []int) *TreeNode { + if len(nums) == 0 { + return nil + } + + head := solve(nums, 0, len(nums) - 1) + return head +} + +func solve(nums []int, lo, hi int) *TreeNode{ + if lo > hi { + return nil + } + + mid := lo + (hi - lo)/2 + node := new(TreeNode) + node.Val = nums[mid] + node.Left, node.Right = solve(nums, lo, mid -1), solve(nums, mid + 1, hi) + + return node +} From 1e19f820904fd0d0fbc77cb3e7625d65b92ac586 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Jul 2021 16:21:29 -0700 Subject: [PATCH 065/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3dcaafe..d0a0c56 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Please give this repo a ⭐ if it inspires you. |[709](https://leetcode.com/problems/to-lower-case/)| To Lower Case| |[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| |[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| +|[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| ## Medium |LC #|Description| From c4005155fb473ae78fdf705f8646ddbe1908527b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 31 Jul 2021 13:27:02 -0700 Subject: [PATCH 066/969] Create L42.go --- Hard/L42.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Hard/L42.go diff --git a/Hard/L42.go b/Hard/L42.go new file mode 100644 index 0000000..431b20f --- /dev/null +++ b/Hard/L42.go @@ -0,0 +1,48 @@ +package Hard + +func trap(height []int) int { + if height == nil || len(height) == 0 { + return 0 + } + + left, right, sum := leftbound(height), rightbound(height), 0 + + for i := range height { + min := left[i] + if right[i] < left[i] { + min = right[i] + } + + sum += min - height[i] + } + + return sum +} + +func leftbound(arr []int) []int { + ret := make([]int, len(arr)) + + ret[0] = arr[0] + for i := 1; i < len(arr); i++ { + ret[i] = ret[i-1] + if arr[i] > ret[i-1] { + ret[i] = arr[i] + } + } + + return ret +} + +func rightbound(arr []int) []int { + ret := make([]int, len(arr)) + + ret[len(arr)-1] = arr[len(arr)-1] + for i := len(arr) - 2; i >= 0; i-- { + ret[i] = ret[i+1] + if arr[i] > ret[i+1] { + ret[i] = arr[i] + } + } + + return ret +} From 151baf5ee631fb23b97a8522f891b833521d3c59 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 31 Jul 2021 13:27:32 -0700 Subject: [PATCH 067/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d0a0c56..aec232c 100644 --- a/README.md +++ b/README.md @@ -181,3 +181,4 @@ Please give this repo a ⭐ if it inspires you. |[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| |[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| |[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| +|[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| From 4d29c8104e7bd029d56f8930c4bfc34fe967c81f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 3 Aug 2021 07:36:26 -0700 Subject: [PATCH 068/969] Create L90.go --- Medium/L90.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L90.go diff --git a/Medium/L90.go b/Medium/L90.go new file mode 100644 index 0000000..137ac89 --- /dev/null +++ b/Medium/L90.go @@ -0,0 +1,29 @@ +package Medium + +func subsetsWithDup(nums []int) [][]int { + var ans [][]int + var tmp []int + + if len(nums) == 0 { + return ans + } + + sort.Ints(nums) + bt(&ans, tmp, nums, 0) + + return ans +} + +func bt(ans *[][]int, tmp []int, nums []int, start int) { + newRow := make([]int, len(tmp)) + copy(newRow, tmp) + *ans = append(*ans, newRow) + for i := start; i < len(nums); i++ { + if i > 0 && i > start && nums[i] == nums[i-1] { + continue + } + tmp = append(tmp, nums[i]) + bt(ans, tmp, nums, i+1) + tmp = (tmp)[:len(tmp)-1] + } +} From 767fb95fa154a7bd5fa383d18cf953b887a1e792 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 3 Aug 2021 07:36:54 -0700 Subject: [PATCH 069/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aec232c..156ef17 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,7 @@ Please give this repo a ⭐ if it inspires you. |[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| |[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| |[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| +|[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| ## Hard |LC #|Description| From 33d3c9281c58c52a5473a0b222df4140d6ed5db8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 5 Aug 2021 11:04:38 -0700 Subject: [PATCH 070/969] Create L877.go --- Medium/L877.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Medium/L877.go diff --git a/Medium/L877.go b/Medium/L877.go new file mode 100644 index 0000000..1dfaa29 --- /dev/null +++ b/Medium/L877.go @@ -0,0 +1,54 @@ +package Medium + +func stoneGame(piles []int) bool { + m, n := len(piles), len(piles) + memo := make([][]int, m*n) + for i := 0; i < m; i++ { + memo[i] = make([]int, n) + for j := 0; j < n; j++ { + memo[i][j] = -1 + } + } + sum := 0 + for _, v := range piles { + sum += v + } + + return solve(piles, &memo, 0, len(piles)-1) > sum/2 +} + +func solve(piles []int, memo *[][]int, startIdx, endIdx int) int { + if startIdx > endIdx { + return 0 + } + + if (*memo)[startIdx][endIdx] > 0 { + return (*memo)[startIdx][endIdx] + } + + if startIdx+1 == endIdx { + (*memo)[startIdx][endIdx] = max(piles[startIdx], piles[endIdx]) + } else { + a := piles[startIdx] + min(solve(piles, memo, startIdx+2, endIdx), solve(piles, memo, startIdx+1, endIdx-1)) + b := piles[endIdx] + min(solve(piles, memo, startIdx+1, endIdx-1), solve(piles, memo, startIdx, endIdx-2)) + (*memo)[startIdx][endIdx] = max(a, b) + } + + return (*memo)[startIdx][endIdx] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From eadffb4d715f08cb7c89434545a4ab060b98a6b6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 5 Aug 2021 11:05:07 -0700 Subject: [PATCH 071/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 156ef17..30f5f42 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ Please give this repo a ⭐ if it inspires you. |[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| |[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| |[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| +|[877](https://leetcode.com/problems/stone-game/)| Stone Game| ## Hard |LC #|Description| From a48e3f1f6c6eb8ef66d5cfda2daf1255720b7b94 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 6 Aug 2021 10:45:11 -0700 Subject: [PATCH 072/969] Create L429.go --- Medium/L429.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L429.go diff --git a/Medium/L429.go b/Medium/L429.go new file mode 100644 index 0000000..76dc963 --- /dev/null +++ b/Medium/L429.go @@ -0,0 +1,28 @@ +package Medium + +func levelOrder(root *Node) [][]int { + var res [][]int + if root == nil { + return res + } + + var q []*Node + q = append(q, root) + + for len(q) > 0 { + size := len(q) + var tmp []int + for i := 0; i < size; i++ { + n := q[0] + q = q[1:] + tmp = append(tmp, n.Val) + + for _, v := range n.Children { + q = append(q, v) + } + } + res = append(res, tmp) + } + + return res +} From 72984d70f72dfc775d9b86582643f6c49dd4590c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 6 Aug 2021 10:45:40 -0700 Subject: [PATCH 073/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 30f5f42..8f17e6d 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ Please give this repo a ⭐ if it inspires you. |[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| |[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| |[877](https://leetcode.com/problems/stone-game/)| Stone Game| +|[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| ## Hard |LC #|Description| From 8e79f35459ed6a282a917deaf3144c21e35f13a3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Aug 2021 15:47:08 -0700 Subject: [PATCH 074/969] Create L132.go --- Hard/L132.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Hard/L132.go diff --git a/Hard/L132.go b/Hard/L132.go new file mode 100644 index 0000000..e808ada --- /dev/null +++ b/Hard/L132.go @@ -0,0 +1,28 @@ +package Hard + +func minCut(s string) int { + cut, n := make([]int, len(s)), len(s) + pal := make([][]bool, n*n) + for i := 0; i < n; i++ { + pal[i] = make([]bool, n) + } + + for i := 0; i < n; i++ { + min := i + for j := 0; j <= i; j++ { + if s[j] == s[i] && (j+1 > i-1 || pal[j+1][i-1]) { + pal[j][i] = true + if j == 0 { + min = 0 + } else { + if cut[j-1]+1 < min { + min = cut[j-1] + 1 + } + } + } + } + cut[i] = min + } + + return cut[n-1] +} From a8c0937f639a126ad3253603afd2abee2a12c19d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Aug 2021 15:47:41 -0700 Subject: [PATCH 075/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8f17e6d..2beab40 100644 --- a/README.md +++ b/README.md @@ -185,3 +185,4 @@ Please give this repo a ⭐ if it inspires you. |[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| |[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| |[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| +|[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| From 434f0a2aa00e893f5335c75542b7cfa4449c5c3f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 9 Aug 2021 09:48:10 -0700 Subject: [PATCH 076/969] Create L415.go --- Easy/L415.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Easy/L415.go diff --git a/Easy/L415.go b/Easy/L415.go new file mode 100644 index 0000000..6c49b3f --- /dev/null +++ b/Easy/L415.go @@ -0,0 +1,34 @@ +package Easy + +func addStrings(num1 string, num2 string) string { + carry, i, j := 0, len(num1)-1, len(num2)-1 + var sb strings.Builder + + for i >= 0 || j >= 0 { + n1, n2 := 0, 0 + if i >= 0 { + n1 = int(num1[i] - '0') + } + if j >= 0 { + n2 = int(num2[j] - '0') + } + sum := n1 + n2 + carry + carry = sum / 10 + sb.WriteByte(byte(sum%10 + '0')) + i-- + j-- + } + + if carry != 0 { + sb.WriteByte(byte(carry + '0')) + } + + return reverse(sb.String()) +} + +func reverse(s string) (result string) { + for _, v := range s { + result = string(v) + result + } + return +} From 30d531318fed641ab8a72dac2ab4d8a48d32d57d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 9 Aug 2021 09:48:43 -0700 Subject: [PATCH 077/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2beab40..fdabb43 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Please give this repo a ⭐ if it inspires you. |[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| |[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| |[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| +|[415](https://leetcode.com/problems/add-strings/)| Add Strings| ## Medium |LC #|Description| From ac1e5bdf322ac2f68d609fefe4617d7b30a30774 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 10 Aug 2021 20:15:11 -0700 Subject: [PATCH 078/969] Create L926.go --- Medium/L926.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L926.go diff --git a/Medium/L926.go b/Medium/L926.go new file mode 100644 index 0000000..c965cbb --- /dev/null +++ b/Medium/L926.go @@ -0,0 +1,18 @@ +package Medium + +func minFlipsMonoIncr(s string) int { + oneCounter, flipCounter := 0, 0 + for i := 0; i < len(s); i++ { + if s[i] == '1' { + oneCounter++ + } else { + flipCounter++ + } + + if oneCounter < flipCounter { + flipCounter = oneCounter + } + } + + return flipCounter +} From 685321489e93a285922bd95c5b7c269ffd59c1ee Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 10 Aug 2021 20:15:42 -0700 Subject: [PATCH 079/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fdabb43..516dedc 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ Please give this repo a ⭐ if it inspires you. |[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| |[877](https://leetcode.com/problems/stone-game/)| Stone Game| |[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| +|[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| ## Hard |LC #|Description| From 43e86d2111f1f4593a5febbc817118dfda399981 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 11 Aug 2021 07:18:07 -0700 Subject: [PATCH 080/969] Create L954.go --- Medium/L954.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L954.go diff --git a/Medium/L954.go b/Medium/L954.go new file mode 100644 index 0000000..8652b3c --- /dev/null +++ b/Medium/L954.go @@ -0,0 +1,39 @@ +package Medium + +func canReorderDoubled(arr []int) bool { + if len(arr) == 0 { + return true + } + sort.Ints(arr) + mp := make(map[int]int) + for _, i := range arr { + if i == 0 { + continue + } + mp[i]++ + } + + for i := range arr { + if arr[i] != 0 { + if mp[arr[i]] > 0 { + tgt := arr[i] * 2 + if arr[i] < 0 { + if arr[i]%2 != 0 { + return false + } else { + tgt = arr[i] / 2 + } + } + val, exists := mp[tgt] + if !exists || val < mp[arr[i]] { + return false + } else { + mp[tgt] = mp[tgt] - mp[arr[i]] + mp[arr[i]] = 0 + } + } + } + } + + return true +} From 832ca91e4845625ea2f3c4ee756a2580647b86c2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 11 Aug 2021 07:18:44 -0700 Subject: [PATCH 081/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 516dedc..f2e5530 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ Please give this repo a ⭐ if it inspires you. |[877](https://leetcode.com/problems/stone-game/)| Stone Game| |[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| |[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| +|[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| ## Hard |LC #|Description| From f17b4ad5fefd1ecf914580f85670d0fb3f42534a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 Aug 2021 07:45:18 -0700 Subject: [PATCH 082/969] Create L49.go --- Medium/L49.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L49.go diff --git a/Medium/L49.go b/Medium/L49.go new file mode 100644 index 0000000..143ef2d --- /dev/null +++ b/Medium/L49.go @@ -0,0 +1,36 @@ +package Medium + +func groupAnagrams(strs []string) [][]string { + var ans [][]string + if len(strs) == 0 { + return ans + } + + mp := make(map[string][]string) + + for _, s := range strs { + k := getKey(s) + mp[k] = append(mp[k], s) + } + + for _, v := range mp { + ans = append(ans, v) + } + + return ans +} + +func getKey(s string) string { + arr := make([]byte, 26) + + for i := 0; i < len(s); i++ { + arr[s[i]-'a']++ + } + + var sb strings.Builder + for _, v := range arr { + sb.WriteByte(v) + } + + return sb.String() +} From 9fec2bf5cb682f7b146a7d63df03aeaee6ab5238 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 Aug 2021 07:45:56 -0700 Subject: [PATCH 083/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2e5530..d0a335d 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ Please give this repo a ⭐ if it inspires you. |[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| |[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| |[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| +|[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| ## Hard |LC #|Description| From 8048298338c6c7f5f373b8a8998732f42ddec69c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Aug 2021 16:32:22 -0700 Subject: [PATCH 084/969] Create L73.go --- Medium/L73.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L73.go diff --git a/Medium/L73.go b/Medium/L73.go new file mode 100644 index 0000000..33a1677 --- /dev/null +++ b/Medium/L73.go @@ -0,0 +1,39 @@ +package Medium + +func setZeroes(matrix [][]int) { + fr, fc, m, n := false, false, len(matrix), len(matrix[0]) + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if matrix[i][j] == 0 { + if i == 0 { + fr = true + } + if j == 0 { + fc = true + } + matrix[0][j] = 0 + matrix[i][0] = 0 + } + } + } + for i := 1; i < m; i++ { + for j := 1; j < n; j++ { + if matrix[i][0] == 0 || matrix[0][j] == 0 { + matrix[i][j] = 0 + } + } + } + + if fr { + for j := 0; j < n; j++ { + matrix[0][j] = 0 + } + } + + if fc { + for i := 0; i < m; i++ { + matrix[i][0] = 0 + } + } +} From 3b7a2c9ecf9e5319c2230e735e08f4845ecaf57b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Aug 2021 16:32:53 -0700 Subject: [PATCH 085/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d0a335d..fd71209 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ Please give this repo a ⭐ if it inspires you. |[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| |[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| |[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| +|[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| ## Hard |LC #|Description| From 41eb394b4eb0db5911cb5a360b84bd3c1e250cc2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Aug 2021 16:48:19 -0700 Subject: [PATCH 086/969] Create L546.go --- Hard/L546.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Hard/L546.go diff --git a/Hard/L546.go b/Hard/L546.go new file mode 100644 index 0000000..0659827 --- /dev/null +++ b/Hard/L546.go @@ -0,0 +1,45 @@ +package Hard + +func removeBoxes(boxes []int) int { + n := len(boxes) + dp := make([][][]int, n) + for i := 0; i < n; i++ { + dp[i] = make([][]int, n) + for j := 0; j < n; j++ { + dp[i][j] = make([]int, n) + for k := 0; k < n; k++ { + dp[i][j][k] = -1 + } + } + } + + return solve(boxes, &dp, 0, n-1, 0) +} + +func solve(boxes []int, dp *[][][]int, i, j, k int) int { + if i > j { + return 0 + } + + if (*dp)[i][j][k] > 0 { + return (*dp)[i][j][k] + } + + i0, k0 := i, k + for ; i+1 <= j && boxes[i+1] == boxes[i]; i, k = i+1, k+1 { + + } + + res := (k+1)*(k+1) + solve(boxes, dp, i+1, j, 0) + for m := i + 1; m <= j; m++ { + if boxes[i] == boxes[m] { + val := solve(boxes, dp, i+1, m-1, 0) + solve(boxes, dp, m, j, k+1) + if val > res { + res = val + } + } + } + + (*dp)[i0][j][k0] = res + return res +} From 155c765f5ccedf736fee9d975f15adcb484ba977 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Aug 2021 16:48:47 -0700 Subject: [PATCH 087/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fd71209..ecf3d09 100644 --- a/README.md +++ b/README.md @@ -191,3 +191,4 @@ Please give this repo a ⭐ if it inspires you. |[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| |[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| |[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| +|[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| From 72da592afae651cc10a697ab78b3de0294f89f9b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 17 Aug 2021 15:33:33 -0700 Subject: [PATCH 088/969] Create L1448.go --- Medium/L1448.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L1448.go diff --git a/Medium/L1448.go b/Medium/L1448.go new file mode 100644 index 0000000..6ac2667 --- /dev/null +++ b/Medium/L1448.go @@ -0,0 +1,28 @@ +package Medium + +func goodNodes(root *TreeNode) int { + cnt := 0 + if root == nil { + return cnt + } + dfs(root, root.Val, &cnt) + return cnt +} + +func dfs(root *TreeNode, val int, cnt *int) { + if root == nil { + return + } + + if root.Val >= val { + *cnt = (*cnt) + 1 + } + + max := val + if val < root.Val { + max = root.Val + } + + dfs(root.Left, max, cnt) + dfs(root.Right, max, cnt) +} From 5065f20c70c537a6aff4b0f6b0f1b0102c3da7f6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 17 Aug 2021 15:33:59 -0700 Subject: [PATCH 089/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ecf3d09..ab1fd83 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ Please give this repo a ⭐ if it inspires you. |[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| |[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| |[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| +|[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| ## Hard |LC #|Description| From 1cb0024c716c56a2a6393b8ea0cbc714db0c8152 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 20 Aug 2021 15:06:29 -0700 Subject: [PATCH 090/969] Create L36.go --- Medium/L36.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L36.go diff --git a/Medium/L36.go b/Medium/L36.go new file mode 100644 index 0000000..4abe9f1 --- /dev/null +++ b/Medium/L36.go @@ -0,0 +1,46 @@ +package Medium + +func isValidSudoku(board [][]byte) bool { + if len(board) == 0 { + return false + } + + index := 0 + for row := 0; row < len(board); row++ { + rowCheck, colCheck, boxCheck := make([]bool, 9), make([]bool, 9), make([]bool, 9) + for col := 0; col < len(board[row]); col++ { + if board[row][col] != '.' { + index = int((board[row][col] - '0') - 1) + + if rowCheck[index] { + return false + } + + rowCheck[index] = true + } + + if board[col][row] != '.' { + index = int((board[col][row] - '0') - 1) + + if colCheck[index] { + return false + } + + colCheck[index] = true + } + + boxRow, boxCol := (row/3)*3+col/3, (row%3)*3+col%3 + if board[boxRow][boxCol] != '.' { + index = int((board[boxRow][boxCol] - '0') - 1) + + if boxCheck[index] { + return false + } + + boxCheck[index] = true + } + } + } + + return true +} From 7667669240d5e801bd9333395ba3624b9078b16a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 20 Aug 2021 15:07:01 -0700 Subject: [PATCH 091/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ab1fd83..aa21842 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ Please give this repo a ⭐ if it inspires you. |[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| |[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| |[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| +|[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| ## Hard |LC #|Description| From 2a54b1e3bcb8768589c2d40d154044a4d6e84965 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 21 Aug 2021 17:20:48 -0700 Subject: [PATCH 092/969] Create L37.go --- Hard/L37.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Hard/L37.go diff --git a/Hard/L37.go b/Hard/L37.go new file mode 100644 index 0000000..7634962 --- /dev/null +++ b/Hard/L37.go @@ -0,0 +1,39 @@ +package Hard + +func solveSudoku(board [][]byte) { + solve(board, 0, 0) +} + +func solve(board [][]byte, row, col int) bool { + for i := row; i < 9; i, col = i+1, 0 { + for j := col; j < 9; j++ { + if board[i][j] != '.' { + continue + } + for num := '1'; num <= '9'; num++ { + if isValid(board, i, j, byte(num)) { + board[i][j] = byte(num) + if solve(board, i, j+1) { + return true + } + board[i][j] = '.' + } + } + return false + } + } + + return true +} + +func isValid(board [][]byte, row, col int, num byte) bool { + blkrow, blkcol := (row/3)*3, (col/3)*3 + for i := 0; i < 9; i++ { + if board[i][col] == num || board[row][i] == num || + board[blkrow+i/3][blkcol+i%3] == num { + return false + } + } + + return true +} From cd21016bb3a021a556a4d43d78c497c39a5c5124 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 21 Aug 2021 17:21:20 -0700 Subject: [PATCH 093/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aa21842..53a76be 100644 --- a/README.md +++ b/README.md @@ -194,3 +194,4 @@ Please give this repo a ⭐ if it inspires you. |[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| |[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| |[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| +|[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| From 625d755788aea64390ab800f9c85a3f5b944d1d7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 22 Aug 2021 10:58:34 -0700 Subject: [PATCH 094/969] Create L850.go --- Hard/L850.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Hard/L850.go diff --git a/Hard/L850.go b/Hard/L850.go new file mode 100644 index 0000000..bba5159 --- /dev/null +++ b/Hard/L850.go @@ -0,0 +1,73 @@ +package Hard + +package main + +func main() { + + n := []int{4, -2, 2, -4} + println(canReorderDoubled(n)) +} + +func rectangleArea(rectangles [][]int) int { + mod, res := 1000000007, 0 + var recList [][]int + for _, r := range rectangles { + addRectangle(&recList, &r, 0) + } + + for _, r := range recList { + res = res + ((r[2]-r[0])*(r[3]-r[1]))%mod + } + + return res % mod +} + +func addRectangle(recList *[][]int, curRec *[]int, start int) { + if start >= len(*recList) { + *recList = append(*recList, *curRec) + return + } + + r := (*recList)[start] + + if (*curRec)[2] <= r[0] || (*curRec)[3] <= r[1] || (*curRec)[0] >= r[2] || (*curRec)[1] >= r[3] { + addRectangle(recList, curRec, start+1) + return + } + + if (*curRec)[0] < r[0] { + tmp := []int{(*curRec)[0], (*curRec)[1], r[0], (*curRec)[3]} + addRectangle(recList, &tmp, start+1) + } + + if (*curRec)[2] > r[2] { + tmp := []int{r[2], (*curRec)[1], (*curRec)[2], (*curRec)[3]} + addRectangle(recList, &tmp, start+1) + } + + if (*curRec)[1] < r[1] { + tmp := []int{max(r[0], (*curRec)[0]), (*curRec)[1], min(r[2], (*curRec)[2]), r[1]} + addRectangle(recList, &tmp, start+1) + } + + if (*curRec)[3] > r[3] { + tmp := []int{max(r[0], (*curRec)[0]), r[3], min(r[2], (*curRec)[2]), (*curRec)[3]} + addRectangle(recList, &tmp, start+1) + } +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 4ff988a28319464dcede349b9a2cbddde60b45fb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 22 Aug 2021 10:59:05 -0700 Subject: [PATCH 095/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 53a76be..f7a4544 100644 --- a/README.md +++ b/README.md @@ -195,3 +195,4 @@ Please give this repo a ⭐ if it inspires you. |[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| |[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| |[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| +|[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| From 8e302e653d7208f07c143f59f19faca733d70859 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 23 Aug 2021 16:00:21 -0700 Subject: [PATCH 096/969] Create L653.go --- Easy/L653.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L653.go diff --git a/Easy/L653.go b/Easy/L653.go new file mode 100644 index 0000000..66157af --- /dev/null +++ b/Easy/L653.go @@ -0,0 +1,21 @@ +package Easy + +func findTarget(root *TreeNode, k int) bool { + return dfs(root, root, k) +} + +func dfs(root, cur *TreeNode, k int) bool { + if cur == nil { + return false + } + + return search(root, cur, k-cur.Val) || dfs(root, cur.Left, k) || dfs(root, cur.Right, k) +} + +func search(root, curr *TreeNode, val int) bool { + if root == nil { + return false + } + + return (root.Val == val) && (root != curr) || (root.Val < val) && search(root.Right, curr, val) || (root.Val > val) && search(root.Left, curr, val) +} From 3b298246bccbfec0520eb855941157dde5a89061 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 23 Aug 2021 16:00:55 -0700 Subject: [PATCH 097/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f7a4544..9815c0e 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Please give this repo a ⭐ if it inspires you. |[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| |[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| |[415](https://leetcode.com/problems/add-strings/)| Add Strings| +|[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| ## Medium |LC #|Description| From cefa69319de8c0477d743128a5f55efaff3b1ee4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 29 Aug 2021 16:32:49 -0700 Subject: [PATCH 098/969] Create L330.go --- Hard/L330.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Hard/L330.go diff --git a/Hard/L330.go b/Hard/L330.go new file mode 100644 index 0000000..99ca577 --- /dev/null +++ b/Hard/L330.go @@ -0,0 +1,17 @@ +package Hard + +func minPatches(nums []int, n int) int { + miss, added, i := 1, 0, 0 + + for miss <= n { + if i < len(nums) && nums[i] <= miss { + miss += nums[i] + i++ + } else { + miss += miss + added++ + } + } + + return added +} From d428e9f5caba45eb257f6d1d2ad9b31a2e9ecc6c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 29 Aug 2021 16:33:18 -0700 Subject: [PATCH 099/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9815c0e..f63f790 100644 --- a/README.md +++ b/README.md @@ -197,3 +197,4 @@ Please give this repo a ⭐ if it inspires you. |[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| |[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| |[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| +|[330](https://leetcode.com/problems/patching-array//)| Patching Array| From 60fe30b8a1b7ba4ef9846219733be795de258550 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 31 Aug 2021 16:31:43 -0700 Subject: [PATCH 100/969] Create L153.go --- Medium/L153.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Medium/L153.go diff --git a/Medium/L153.go b/Medium/L153.go new file mode 100644 index 0000000..633ad6b --- /dev/null +++ b/Medium/L153.go @@ -0,0 +1,16 @@ +package Medium + +func findMin(nums []int) int { + l, h, mid, n := 0, len(nums)-1, 0, len(nums) + + for l < h { + mid = l + (h-l)/2 + if nums[mid] < nums[h] { + h = mid + } else { + l = (mid + 1) % n + } + } + + return nums[l] +} From 6841c9cd90788b459a3aa106e7ad8ac8bf6d176b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 31 Aug 2021 16:32:45 -0700 Subject: [PATCH 101/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f63f790..d4ff45f 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ Please give this repo a ⭐ if it inspires you. |[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| |[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| |[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| +|[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| ## Hard |LC #|Description| From 6a9589cae933ad68f215500ebad62c355416951c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Sep 2021 14:46:52 -0700 Subject: [PATCH 102/969] Create L565.go --- Medium/L565.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L565.go diff --git a/Medium/L565.go b/Medium/L565.go new file mode 100644 index 0000000..b448213 --- /dev/null +++ b/Medium/L565.go @@ -0,0 +1,18 @@ +package Medium + +func arrayNesting(nums []int) int { + maxLen, visited := 0, make([]bool, len(nums)) + for _, n := range nums { + currLen := 0 + for !visited[n] { + currLen++ + visited[n] = true + n = nums[n] + } + if currLen > maxLen { + maxLen = currLen + } + } + + return maxLen +} From 9936ce5990be3beb88f8ee185b22ec7422a6ae71 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Sep 2021 14:47:24 -0700 Subject: [PATCH 103/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d4ff45f..aafd428 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ Please give this repo a ⭐ if it inspires you. |[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| |[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| |[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| +|[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| ## Hard |LC #|Description| From 268a759c838d53f1cef2c5ea806aaa6f54659e25 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 5 Sep 2021 16:47:51 -0700 Subject: [PATCH 104/969] Create L899.go --- Hard/L899.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Hard/L899.go diff --git a/Hard/L899.go b/Hard/L899.go new file mode 100644 index 0000000..8b35d29 --- /dev/null +++ b/Hard/L899.go @@ -0,0 +1,50 @@ +package Hard + +func orderlyQueue(s string, k int) string { + if len(s) == 0 { + return s + } + + n, s := len(s), s+s + + if k >= 2 { + cnt := make([]int, 26) + for i := 0; i < n; i++ { + cnt[s[i]-'a']++ + } + var sb strings.Builder + for i := 0; i < 26; i++ { + for j := 0; j < cnt[i]; j++ { + sb.WriteByte(byte(i + 'a')) + } + } + + return sb.String() + } + + res, arr, x := make([]byte, n*2), make([]int, n*2), 0 + res[0], arr[0] = s[0], -1 + + for i := 1; i < n*2; i++ { + for x >= 0 && res[arr[x]+1] > s[i] { + x = arr[x] + } + res[x+1] = s[i] + + if x >= 0 { + idx := arr[x] + for idx >= 0 && res[idx+1] != s[i] { + idx = arr[idx] + } + + if res[idx+1] == s[i] { + idx++ + } + arr[x+1] = idx + } + + x++ + } + + return string(res[:n]) +} From 1ad9153fb1d3c7783358924a36bd31b344f48b5c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 5 Sep 2021 16:48:19 -0700 Subject: [PATCH 105/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aafd428..ec7adfa 100644 --- a/README.md +++ b/README.md @@ -200,3 +200,4 @@ Please give this repo a ⭐ if it inspires you. |[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| |[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| |[330](https://leetcode.com/problems/patching-array//)| Patching Array| +|[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| From 721763fc6b7611b80bd6a5a046c02ef61066fe32 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Sep 2021 16:06:17 -0700 Subject: [PATCH 106/969] Create L1629.go --- Easy/L1629.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L1629.go diff --git a/Easy/L1629.go b/Easy/L1629.go new file mode 100644 index 0000000..d3802d0 --- /dev/null +++ b/Easy/L1629.go @@ -0,0 +1,21 @@ +package Easy + +func slowestKey(releaseTimes []int, keysPressed string) byte { + max, key := releaseTimes[0], keysPressed[0] + + for i := 1; i < len(releaseTimes); i++ { + relTime := releaseTimes[i] - releaseTimes[i-1] + if relTime >= max { + if relTime == max { + if keysPressed[i] > key { + key = keysPressed[i] + } + } else { + max = relTime + key = keysPressed[i] + } + } + } + + return key +} From 71ff6d7b859dae5da8595e71900d1068dcc3964b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Sep 2021 16:06:50 -0700 Subject: [PATCH 107/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ec7adfa..c9901ff 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Please give this repo a ⭐ if it inspires you. |[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| |[415](https://leetcode.com/problems/add-strings/)| Add Strings| |[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| +|[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| ## Medium |LC #|Description| From 5970f822274ef4d9bdeed6299321450430e5aa75 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Sep 2021 16:41:41 -0700 Subject: [PATCH 108/969] Create L206.go --- Easy/L206.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L206.go diff --git a/Easy/L206.go b/Easy/L206.go new file mode 100644 index 0000000..52c0a0e --- /dev/null +++ b/Easy/L206.go @@ -0,0 +1,13 @@ +package Easy + +func reverseList(head *ListNode) *ListNode { + var newHead *ListNode + for head != nil { + next := head.Next + head.Next = newHead + newHead = head + head = next + } + + return newHead +} From c002b9a988b98b594efbc56fb4c60eaccd1875bc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Sep 2021 16:42:28 -0700 Subject: [PATCH 109/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c9901ff..5153c61 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Please give this repo a ⭐ if it inspires you. |[415](https://leetcode.com/problems/add-strings/)| Add Strings| |[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| |[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| +|[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| ## Medium |LC #|Description| From b33957bd4af178c139ddf094c61720e1870815cd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 8 Sep 2021 16:09:51 -0700 Subject: [PATCH 110/969] Create L848.go --- Medium/L848.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L848.go diff --git a/Medium/L848.go b/Medium/L848.go new file mode 100644 index 0000000..4a64e5c --- /dev/null +++ b/Medium/L848.go @@ -0,0 +1,20 @@ +package Medium + +func shiftingLetters(s string, shifts []int) string { + var sb strings.Builder + shift := 0 + for i := len(s) - 1; i >= 0; i-- { + shift = (shift + shifts[i]) % 26 + sb.WriteByte(byte((int(s[i]-'a')+shift)%26 + 'a')) + } + + return reverse(sb.String()) +} + +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} From 83820d5311057c371aa34b94d4145dd6cac49717 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 8 Sep 2021 16:10:27 -0700 Subject: [PATCH 111/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5153c61..7cfc267 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ Please give this repo a ⭐ if it inspires you. |[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| |[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| |[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| +|[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| ## Hard |LC #|Description| From 2cd1b3a30a26838e3f6bd2666c78b45869b81bc5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Sep 2021 22:26:00 -0700 Subject: [PATCH 112/969] Create L224.go --- Hard/L224.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Hard/L224.go diff --git a/Hard/L224.go b/Hard/L224.go new file mode 100644 index 0000000..5353dbd --- /dev/null +++ b/Hard/L224.go @@ -0,0 +1,32 @@ +package Hard + +func calculate(s string) int { + if len(s) == 0 { + return 0 + } + + result, sign, num := 0, 1, 0 + + var st []int + st = append(st, sign) + + for i := range s { + if s[i] >= '0' && s[i] <= '9' { + num = num*10 + int(s[i]-'0') + } else if s[i] == '+' || s[i] == '-' { + result += sign * num + sign = st[len(st)-1] + if s[i] != '+' { + sign *= -1 + } + num = 0 + } else if s[i] == '(' { + st = append(st, sign) + } else if s[i] == ')' { + st = st[:len(st)-1] + } + } + + result += sign * num + return result +} From 9be6d197922bea057f38f5745ac0472bbd6e9b3e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Sep 2021 22:26:34 -0700 Subject: [PATCH 113/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7cfc267..b708b1b 100644 --- a/README.md +++ b/README.md @@ -204,3 +204,4 @@ Please give this repo a ⭐ if it inspires you. |[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| |[330](https://leetcode.com/problems/patching-array//)| Patching Array| |[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| +|[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| From 78e7ec3eafa0ce70b093a821e8acf339e5e0d9c3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Sep 2021 13:25:33 -0700 Subject: [PATCH 114/969] Create L1189.go --- Easy/L1189.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Easy/L1189.go diff --git a/Easy/L1189.go b/Easy/L1189.go new file mode 100644 index 0000000..5b11125 --- /dev/null +++ b/Easy/L1189.go @@ -0,0 +1,51 @@ +package Easy + +func maxNumberOfBalloons(text string) int { + mp := make(map[byte]int) + arr := []byte{'b', 'a', 'l', 'o', 'n'} + + for i := range text { + if isValid(text[i]) { + mp[text[i]]++ + } + } + + min := math.MaxInt64 + for i := range arr { + if val, ok := mp[arr[i]]; ok { + if arr[i] == 'l' || arr[i] == 'o' { + if val < 2 { + return 0 + } + val = val / 2 + } + if val < min { + min = val + } + } else { + return 0 + } + } + + if min == math.MaxInt64 { + return 0 + } + + return min +} + +var validMap = map[byte]bool{ + 'b': true, + 'a': true, + 'l': true, + 'o': true, + 'n': true, +} + +func isValid(ch byte) bool { + if _, ok := validMap[ch]; !ok { + return false + } + + return true +} From fa9cfc9de5d4d23ba9de6df2c92a7f28e30665ee Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Sep 2021 13:25:59 -0700 Subject: [PATCH 115/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b708b1b..1d4e313 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Please give this repo a ⭐ if it inspires you. |[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| |[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| |[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| +|[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| ## Medium |LC #|Description| From c9bce891e1f21354922c82658ecef0070bd41511 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Sep 2021 16:50:55 -0700 Subject: [PATCH 116/969] Create L978.go --- Medium/L978.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Medium/L978.go diff --git a/Medium/L978.go b/Medium/L978.go new file mode 100644 index 0000000..8ac0afc --- /dev/null +++ b/Medium/L978.go @@ -0,0 +1,30 @@ +package Medium + +func maxTurbulenceSize(arr []int) int { + inc, dec, result := 1, 1, 1 + + for i := 1; i < len(arr); i++ { + if arr[i] < arr[i-1] { + dec = inc + 1 + ince = 1 + } else if arr[i] > arr[i-1] { + inc = dec + 1 + dec = 1 + } else { + inc = 1 + dec = 1 + } + + result = max(result, max(dec, inc)) + } + + return result +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From b638b38ebe201869290c1e2d432d7aa5a842392f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Sep 2021 16:51:53 -0700 Subject: [PATCH 117/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1d4e313..e7c4808 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ Please give this repo a ⭐ if it inspires you. |[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| |[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| |[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| +|[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| ## Hard |LC #|Description| From 2b7fe75dd45171c2ff91da4d126e33351b5ea501 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 16 Sep 2021 15:37:23 -0700 Subject: [PATCH 118/969] Create L54.go --- Medium/L54.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L54.go diff --git a/Medium/L54.go b/Medium/L54.go new file mode 100644 index 0000000..c150ea8 --- /dev/null +++ b/Medium/L54.go @@ -0,0 +1,36 @@ +package Medium + +func spiralOrder(matrix [][]int) []int { + var res []int + if len(matrix) == 0 { + return res + } + + n, m := len(matrix), len(matrix[0]) + up, down, left, right := 0, n-1, 0, m-1 + + for len(res) < n*m { + for j := left; j <= right && len(res) < n*m; j++ { + res = append(res, matrix[up][j]) + } + + for i := up + 1; i <= down-1 && len(res) < n*m; i++ { + res = append(res, matrix[i][right]) + } + + for j := right; j >= left && len(res) < n*m; j-- { + res = append(res, matrix[down][j]) + } + + for i:= down-1; i >= up + 1 && len(res) < n*m; i-- { + res = append(res, matrix[i][left]) + } + + left++ + right-- + up++ + down-- + } + + return res +} From 20b71cc368b0ed1d04d1aa1ee9041f06f105e58f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 16 Sep 2021 16:34:33 -0700 Subject: [PATCH 119/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e7c4808..29ff537 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,7 @@ Please give this repo a ⭐ if it inspires you. |[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| |[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| |[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| +|[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| ## Hard |LC #|Description| From 604639d702108f2fe7e47beb23ea5a8f8f01edbc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Sep 2021 12:42:26 -0700 Subject: [PATCH 120/969] Create L350.go --- Easy/L350.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L350.go diff --git a/Easy/L350.go b/Easy/L350.go new file mode 100644 index 0000000..f2e8c27 --- /dev/null +++ b/Easy/L350.go @@ -0,0 +1,28 @@ +package Easy + +func intersect(nums1 []int, nums2 []int) []int { + var res []int + mp1, mp2 := make(map[int]int), make(map[int]int) + + for _, n := range nums1 { + mp1[n]++ + } + + for _, n := range nums2 { + mp2[n]++ + } + + for k, v := range mp2 { + if val, ok := mp1[k]; ok { + minVal := v + if val < minVal { + minVal = val + } + for i := 0; i < minVal; i++ { + res = append(res, k) + } + } + } + + return res +} From 517765a18269d9db9fba6e84f9d829f7af6ef398 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Sep 2021 12:42:52 -0700 Subject: [PATCH 121/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 29ff537..c430899 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Please give this repo a ⭐ if it inspires you. |[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| |[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| |[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| +|[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| ## Medium |LC #|Description| From 46304b6807617cb43061293750888564febe7eb4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Sep 2021 16:54:43 -0700 Subject: [PATCH 122/969] Create L1275.go --- Easy/L1275.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Easy/L1275.go diff --git a/Easy/L1275.go b/Easy/L1275.go new file mode 100644 index 0000000..1162582 --- /dev/null +++ b/Easy/L1275.go @@ -0,0 +1,47 @@ +package Easy + +func tictactoe(moves [][]int) string { + n := 3 + rows, cols := make([]int, n), make([]int, n) + d1, d2, curr := 0, 0, 1 + + for _, currMove := range moves { + rows[currMove[0]] += curr + cols[currMove[1]] += curr + + if currMove[0] == currMove[1] { + d1 += curr + } + + if currMove[0]+currMove[1] == n-1 { + d2 += curr + } + + if abs(rows[currMove[0]]) == n || + abs(cols[currMove[1]]) == n || + abs(d1) == n || + abs(d2) == n { + if curr == 1 { + return "A" + } + return "B" + } + + // 1 is 'A', -1 is 'B' + curr *= -1 + } + + if len(moves) < 9 { + return "Pending" + } + + return "Draw" +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From 381d47a3f8b32118004f4861876444de26ec7654 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Sep 2021 16:55:14 -0700 Subject: [PATCH 123/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c430899..9e05384 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Please give this repo a ⭐ if it inspires you. |[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| |[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| |[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| +|[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| ## Medium |LC #|Description| From 33f6788401cefcf1df3595d8b6235593883ebad0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Sep 2021 07:09:54 -0700 Subject: [PATCH 124/969] Create L485.go --- Easy/L485.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L485.go diff --git a/Easy/L485.go b/Easy/L485.go new file mode 100644 index 0000000..2dc6eee --- /dev/null +++ b/Easy/L485.go @@ -0,0 +1,21 @@ +package Easy + +func findMaxConsecutiveOnes(nums []int) int { + result, max := 0, 0 + + if len(nums) == 0 { + return result + } + + for i := range nums { + max += nums[i] + if nums[i] == 0 { + max = 0 + } else { + if max >= result { + result = max + } + } + } + return result +} From 1e8761b7d16ebebe8180c7d08b3fd56829f6e836 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Sep 2021 07:10:42 -0700 Subject: [PATCH 125/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9e05384..a0e1653 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Please give this repo a ⭐ if it inspires you. |[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| |[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| |[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| +|[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| ## Medium |LC #|Description| From e023d023aebd519e8fb7fa64ab6a0a14dbbb2d5c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Sep 2021 09:16:44 -0700 Subject: [PATCH 126/969] Create L1239.go --- Medium/L1239.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Medium/L1239.go diff --git a/Medium/L1239.go b/Medium/L1239.go new file mode 100644 index 0000000..476bbbe --- /dev/null +++ b/Medium/L1239.go @@ -0,0 +1,44 @@ +package Medium + +func maxLength(arr []string) int { + result := 0 + + if len(arr) == 0 { + return result + } + + dfs(arr, "", 0, &result) + + return result +} + +func dfs(arr []string, path string, idx int, result *int) { + unique := isUnique(path) + + if unique { + if len(path) > *result { + *result = len(path) + } + } + + if idx == len(arr) || !unique { + return + } + + for i := idx; i < len(arr); i++ { + dfs(arr, path+arr[i], i+1, result) + } +} + +func isUnique(s string) bool { + mp := make(map[byte]bool) + + for _, c := range s { + if _, ok := mp[byte(c)]; ok { + return false + } + mp[byte(c)] = true + } + + return true +} From cf37bfbefa8b0a987a1022747cb5b008cccd3d8b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Sep 2021 09:17:25 -0700 Subject: [PATCH 127/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a0e1653..d1e34ba 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ Please give this repo a ⭐ if it inspires you. |[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| |[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| |[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| +|[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| ## Hard |LC #|Description| From 358f9a954981298992064036a7aab5dd8dc662b7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Sep 2021 14:08:33 -0700 Subject: [PATCH 128/969] Create L782.go --- Hard/L782.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Hard/L782.go diff --git a/Hard/L782.go b/Hard/L782.go new file mode 100644 index 0000000..307e4cb --- /dev/null +++ b/Hard/L782.go @@ -0,0 +1,53 @@ +package Hard + +func movesToChessboard(board [][]int) int { + n := len(board) + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + if (board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) == 1 { + return -1 + } + } + } + + rowSum, colSum, rowMisplaced, colMisplaced := 0, 0, 0, 0 + for i := 0; i < n; i++ { + rowSum += board[0][i] + colSum += board[i][0] + if board[i][0] == i%2 { + rowMisplaced++ + } + if board[0][i] == i%2 { + colMisplaced++ + } + } + + if rowSum != n/2 && rowSum != (n+1)/2 { + return -1 + } + if colSum != n/2 && colSum != (n+1)/2 { + return -1 + } + + if n%2 == 1 { + if colMisplaced%2 == 1 { + colMisplaced = n - colMisplaced + } + if rowMisplaced%2 == 1 { + rowMisplaced = n - rowMisplaced + } + } else { + colMisplaced = min(n-colMisplaced, colMisplaced) + rowMisplaced = min(n-rowMisplaced, rowMisplaced) + } + + return (colMisplaced + rowMisplaced) / 2 +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From a67524c475bf3c14a181aa7a523b1bf138560b8a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Sep 2021 14:09:10 -0700 Subject: [PATCH 129/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d1e34ba..138f572 100644 --- a/README.md +++ b/README.md @@ -212,3 +212,4 @@ Please give this repo a ⭐ if it inspires you. |[330](https://leetcode.com/problems/patching-array//)| Patching Array| |[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| |[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| +|[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| From d86752744a6112bee777f9ccda47b617078700bb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Sep 2021 06:54:10 -0700 Subject: [PATCH 130/969] Create L929.go --- Easy/L929.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Easy/L929.go diff --git a/Easy/L929.go b/Easy/L929.go new file mode 100644 index 0000000..15263ba --- /dev/null +++ b/Easy/L929.go @@ -0,0 +1,29 @@ +package Easy + +func numUniqueEmails(emails []string) int { + mp := make(map[string]bool) + + for _, s := range emails { + mp[cleanStrings(s)] = true + } + + return len(mp) +} + +func cleanStrings(s string) string { + arr := strings.Split(s, "@") + var sb strings.Builder + + for _, c := range arr[0] { + if c == '.' { + continue + } else if c == '+' { + break + } else { + sb.WriteByte(byte(c)) + } + } + sb.WriteString(fmt.Sprintf("@%s", arr[1])) + + return sb.String() +} From 284c28f6dff312d8420f75be4cfc8d08e0a8eec9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Sep 2021 06:54:45 -0700 Subject: [PATCH 131/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 138f572..f5be0fa 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Please give this repo a ⭐ if it inspires you. |[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| |[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| |[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| +|[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| ## Medium |LC #|Description| From e8d10407f8c642210cf4b45df28c2847df8a7ac6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Sep 2021 16:31:35 -0700 Subject: [PATCH 132/969] Create L922.go --- Easy/L922.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L922.go diff --git a/Easy/L922.go b/Easy/L922.go new file mode 100644 index 0000000..10d014e --- /dev/null +++ b/Easy/L922.go @@ -0,0 +1,20 @@ +package Easy + +func sortArrayByParityII(nums []int) []int { + i, j, n := 0, 1, len(nums) + + for i < n && j < n { + for i < n && nums[i]%2 == 0 { + i += 2 + } + for j < n && nums[j]%2 == 1 { + j += 2 + } + if i < n && j < n { + // swap + nums[i], nums[j] = nums[j], nums[i] + } + } + + return nums +} From 6b6f45cb20666c6959306dd7940b4d57de69ebea Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Sep 2021 16:32:06 -0700 Subject: [PATCH 133/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f5be0fa..f3c9ca9 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Please give this repo a ⭐ if it inspires you. |[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| |[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| |[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| +|[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| ## Medium |LC #|Description| From 7aff99de2a739273cc6aefcb0bcfac4e5bca3274 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 29 Sep 2021 16:37:14 -0700 Subject: [PATCH 134/969] Create L725.go --- Medium/L725.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L725.go diff --git a/Medium/L725.go b/Medium/L725.go new file mode 100644 index 0000000..253b471 --- /dev/null +++ b/Medium/L725.go @@ -0,0 +1,32 @@ +package Medium + +func splitListToParts(head *ListNode, k int) []*ListNode { + n, curr := 0, head + for curr != nil { + n += 1 + curr = curr.Next + } + m, r, j := n/k, n%k, 0 + if m == 0 { + r -= k + } + + res := make([]*ListNode, k) + curr = head + for curr != nil { + res[j] = curr + j++ + for i := 1; i < m; i++ { + curr = curr.Next + } + if r > 0 { + curr = curr.Next + r -= 1 + } + tmp := curr.Next + curr.Next = nil + curr = tmp + } + + return res +} From 84c26f1681ae709e6adbcea9acd41c9fb1d3fb0e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 29 Sep 2021 16:37:51 -0700 Subject: [PATCH 135/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f3c9ca9..3a0ec60 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ Please give this repo a ⭐ if it inspires you. |[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| |[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| |[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| +|[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| ## Hard |LC #|Description| From 9b348710c139563347f2d4284a220660a6be1438 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Sep 2021 06:15:41 -0700 Subject: [PATCH 136/969] Rename L698.go to L473.go --- Medium/{L698.go => L473.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Medium/{L698.go => L473.go} (100%) diff --git a/Medium/L698.go b/Medium/L473.go similarity index 100% rename from Medium/L698.go rename to Medium/L473.go From 92548bcc6fa2cfcd952d97852aacddb10a3001b2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Sep 2021 06:17:00 -0700 Subject: [PATCH 137/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3a0ec60..1912eb4 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ Please give this repo a ⭐ if it inspires you. |[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| |[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| |[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| +|[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| ## Hard |LC #|Description| From ed385e272ebe499330da4fc2c813c82f8bde6106 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Sep 2021 06:17:24 -0700 Subject: [PATCH 138/969] Create L698.go --- Medium/L698.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L698.go diff --git a/Medium/L698.go b/Medium/L698.go new file mode 100644 index 0000000..9c1c196 --- /dev/null +++ b/Medium/L698.go @@ -0,0 +1,35 @@ +package Medium + +func canPartitionKSubsets(nums []int, k int) bool { + sum := 0 + for _, n := range nums { + sum += n + } + if k <= 0 || sum%k != 0 { + return false + } + visited := make([]bool, len(nums)) + return solve(nums, &visited, 0, k, 0, 0, sum/k) +} + +func solve(nums []int, visited *[]bool, startIdx, k, curSum, curNum, target int) bool { + if k == 1 { + return true + } + + if curSum == target && curNum > 0 { + return solve(nums, visited, 0, k-1, 0, 0, target) + } + + for i := startIdx; i < len(nums); i++ { + if !(*visited)[i] { + (*visited)[i] = true + if solve(nums, visited, i+1, k, curSum+nums[i], curNum+1, target) { + return true + } + (*visited)[i] = false + } + } + + return false +} From 47ad63a97a1608f3687fdca0bf2a8b6a16e27091 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Sep 2021 19:48:56 -0700 Subject: [PATCH 139/969] Create L1143.go --- Medium/L1143.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L1143.go diff --git a/Medium/L1143.go b/Medium/L1143.go new file mode 100644 index 0000000..f49c154 --- /dev/null +++ b/Medium/L1143.go @@ -0,0 +1,40 @@ +package Medium + +func longestCommonSubsequence(text1 string, text2 string) int { + m, n := len(text1), len(text2) + memo := make([][]int, m+1) + for i := 0; i < m+1; i++ { + memo[i] = make([]int, n+1) + for j := 0; j < n+1; j++ { + memo[i][j] = -1 + } + } + + return lcs(text1, text2, m, n, &memo) +} + +func lcs(s1, s2 string, m, n int, memo *[][]int) int { + if m == 0 || n == 0 { + return 0 + } + + if (*memo)[m][n] != -1 { + return (*memo)[m][n] + } + + if s1[m-1] == s2[n-1] { + (*memo)[m][n] = 1 + lcs(s1, s2, m-1, n-1, memo) + } else { + (*memo)[m][n] = max(lcs(s1, s2, m-1, n, memo), lcs(s1, s2, m, n-1, memo)) + } + + return (*memo)[m][n] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 01913d3e629e324da8b427009d2e808bd28eaa08 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Sep 2021 19:49:35 -0700 Subject: [PATCH 140/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1912eb4..e2f53dd 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ Please give this repo a ⭐ if it inspires you. |[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| |[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| |[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| +|[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| ## Hard |LC #|Description| From 7870f371db2cb124f56d3c938cbde2a6d6abeea8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 2 Oct 2021 16:05:07 -0700 Subject: [PATCH 141/969] Create L174.go --- Hard/L174.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Hard/L174.go diff --git a/Hard/L174.go b/Hard/L174.go new file mode 100644 index 0000000..ac38c07 --- /dev/null +++ b/Hard/L174.go @@ -0,0 +1,51 @@ +package Hard + +func calculateMinimumHP(dungeon [][]int) int { + m, n := len(dungeon), len(dungeon[0]) + dp := make([][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([]int, n) + for j := 0; j < n; j++ { + dp[i][j] = math.MaxInt64 + } + } + + return solve(dungeon, 0, 0, &dp) +} + +func solve(mat [][]int, i, j int, dp *[][]int) int { + m, n := len(mat), len(mat[0]) + + if i == m || j == n { + return math.MaxInt64 + } + if i == m-1 && j == n-1 { + if mat[i][j] < 0 { + return -mat[i][j] + 1 + } else { + return 1 + } + } + + if (*dp)[i][j] != math.MaxInt64 { + return (*dp)[i][j] + } + + right, down := solve(mat, i, j+1, dp), solve(mat, i+1, j, dp) + minHpReq := min(right, down) - mat[i][j] + + (*dp)[i][j] = 1 + if minHpReq > 0 { + (*dp)[i][j] = minHpReq + } + + return (*dp)[i][j] +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 6a0187b2411ca441cf7d267756d3fc5f998785af Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 2 Oct 2021 16:06:29 -0700 Subject: [PATCH 142/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e2f53dd..aab3ec2 100644 --- a/README.md +++ b/README.md @@ -218,3 +218,4 @@ Please give this repo a ⭐ if it inspires you. |[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| |[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| |[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| +|[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| From 0625c4a1c8343628c5f0d4246eb658f8617056f9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 3 Oct 2021 06:59:59 -0700 Subject: [PATCH 143/969] Create L55.go --- Medium/L55.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L55.go diff --git a/Medium/L55.go b/Medium/L55.go new file mode 100644 index 0000000..ed3ce39 --- /dev/null +++ b/Medium/L55.go @@ -0,0 +1,35 @@ +package Medium + +func canJump(nums []int) bool { + n, dp := len(nums), make([]int, len(nums)) + for i := 0; i < n; i++ { + dp[i] = -1 + } + return solve(nums, 0, &dp) +} + +func solve(nums []int, idx int, dp *[]int) bool { + if idx >= len(nums)-1 { + return true + } + + if nums[idx] == 0 { + (*dp)[idx] = 0 + return false + } + + if (*dp)[idx] != -1 { + return (*dp)[idx] == 1 + } + + jumps := nums[idx] + for i := 1; i <= jumps; i++ { + if solve(nums, idx+i, dp) { + (*dp)[idx] = 1 + return true + } + } + + (*dp)[idx] = 0 + return false +} From 5680d24f62c97f19eb5ac54fa95d610821e8f6af Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 3 Oct 2021 07:00:34 -0700 Subject: [PATCH 144/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aab3ec2..f826641 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ Please give this repo a ⭐ if it inspires you. |[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| |[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| |[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| +|[55](https://leetcode.com/problems/jump-game/)| Jump Game| ## Hard |LC #|Description| From d5605169c9b923ae0072e85e2a16ce99a2172821 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 3 Oct 2021 17:24:33 -0700 Subject: [PATCH 145/969] Create L463.go --- Easy/L463.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Easy/L463.go diff --git a/Easy/L463.go b/Easy/L463.go new file mode 100644 index 0000000..a6594a8 --- /dev/null +++ b/Easy/L463.go @@ -0,0 +1,25 @@ +package Easy + +func islandPerimeter(grid [][]int) int { + m, n, cnt, dirs := len(grid), len(grid[0]), 0, [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == 1 { + for _, d := range dirs { + x, y := i+d[0], j+d[1] + if isValid(grid, x, y) { + cnt++ + } + } + } + } + } + + return cnt +} + +func isValid(grid [][]int, x, y int) bool { + m, n := len(grid), len(grid[0]) + + return x < 0 || y < 0 || x == m || y == n || grid[x][y] == 0 +} From c02f3ad063db403452ad162e15ebdde51e2192f5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 3 Oct 2021 17:25:02 -0700 Subject: [PATCH 146/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f826641..ad31c7f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Please give this repo a ⭐ if it inspires you. |[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| |[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| |[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| +|[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| ## Medium |LC #|Description| From 0a4cfed105743f17717ea9477c6d50242a58685e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Oct 2021 06:23:49 -0700 Subject: [PATCH 147/969] Create L70.go --- Easy/L70.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L70.go diff --git a/Easy/L70.go b/Easy/L70.go new file mode 100644 index 0000000..a487033 --- /dev/null +++ b/Easy/L70.go @@ -0,0 +1,18 @@ +package Easy + +func climbStairs(n int) int { + mp := make(map[int]int) + + mp[1], mp[2], mp[3] = 1,2,3 + return dp(mp, n) +} + +func dp(mp map[int]int, n int) int { + if _, ok := mp[n]; ok { + return mp[n] + } + + mp[n] = dp(mp, n-1) + dp(mp, n-2) + + return mp[n] +} From 9a8bcc2448fb28f32be07cf97fcdfc89e80f36bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Oct 2021 06:24:23 -0700 Subject: [PATCH 148/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ad31c7f..12c54ba 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Please give this repo a ⭐ if it inspires you. |[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| |[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| |[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| +|[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| ## Medium |LC #|Description| From 18539f3011f1315a5e03771fbc2d47c3ee72d760 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 6 Oct 2021 06:15:48 -0700 Subject: [PATCH 149/969] Create L442.go --- Medium/L442.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L442.go diff --git a/Medium/L442.go b/Medium/L442.go new file mode 100644 index 0000000..61a103a --- /dev/null +++ b/Medium/L442.go @@ -0,0 +1,24 @@ +func findDuplicates(nums []int) []int { + var ret []int + if len(nums) == 0 { + return ret + } + + for _, n := range nums { + index := abs(n) - 1 + if nums[index] < 0 { + ret = append(ret, abs(n)) + } + nums[index] = -nums[index] + } + + return ret +} + +func abs(n int) int { + if n < 0 { + return -n + } + + return n +} From f2ee9a732bc283104b76400132bf3e1abe496e53 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 6 Oct 2021 06:16:22 -0700 Subject: [PATCH 150/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 12c54ba..9999831 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ Please give this repo a ⭐ if it inspires you. |[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| |[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| |[55](https://leetcode.com/problems/jump-game/)| Jump Game| +|[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| ## Hard |LC #|Description| From a26fb244331ebe974d8b25fc91668798fc58e8ce Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Oct 2021 16:52:00 -0700 Subject: [PATCH 151/969] Create L79.go --- Medium/L79.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/L79.go diff --git a/Medium/L79.go b/Medium/L79.go new file mode 100644 index 0000000..18e063a --- /dev/null +++ b/Medium/L79.go @@ -0,0 +1,55 @@ +package Medium + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func exist(board [][]byte, word string) bool { + if len(board) == 0 || len(word) == 0 { + return false + } + m, n := len(board), len(board[0]) + vis := make([][]bool, m) + for i := 0; i < m; i++ { + vis[i] = make([]bool, n) + } + found := false + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + found = false + if board[i][j] == word[0] { + found = bt(board, &vis, i, j, 0, word) + if found { + return found + } + } + } + } + return found +} + +func isValid(board [][]byte, x, y int, ch byte) bool { + m, n := len(board), len(board[0]) + + return x >= 0 && x < m && y >= 0 && y < n && board[x][y] == ch +} + +func bt(board [][]byte, vis *[][]bool, i, j, index int, word string) bool { + if index == len(word) { + return true + } + + if !isValid(board, i, j, word[index]) { + return false + } + + if (*vis)[i][j] { + return false + } + (*vis)[i][j] = true + found := false + for _, d := range dirs { + found = found || bt(board, vis, i+d[0], j+d[1], index+1, word) + } + (*vis)[i][j] = false + + return found +} From 637580c5515dd98dead9e30d90a5d8a9bb5dabfb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Oct 2021 16:52:35 -0700 Subject: [PATCH 152/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9999831..9234671 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ Please give this repo a ⭐ if it inspires you. |[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| |[55](https://leetcode.com/problems/jump-game/)| Jump Game| |[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| +|[79](https://leetcode.com/problems/word-search/)| Word Search| ## Hard |LC #|Description| From 13fa0ba2daec692ff47a0276ca9d963762ad3928 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 10 Oct 2021 13:41:15 -0700 Subject: [PATCH 153/969] Create L201.go --- Medium/L201.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Medium/L201.go diff --git a/Medium/L201.go b/Medium/L201.go new file mode 100644 index 0000000..76576df --- /dev/null +++ b/Medium/L201.go @@ -0,0 +1,9 @@ +package Medium + +func rangeBitwiseAnd(left int, right int) int { + for left < right { + right = right & (right - 1) + } + + return left & right +} From 9e088a7d4119ae590fad009f8a2959479c1380c3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 10 Oct 2021 13:41:46 -0700 Subject: [PATCH 154/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9234671..1941ff9 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,7 @@ Please give this repo a ⭐ if it inspires you. |[55](https://leetcode.com/problems/jump-game/)| Jump Game| |[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| |[79](https://leetcode.com/problems/word-search/)| Word Search| +|[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| ## Hard |LC #|Description| From 789c1ce098c2465750d12d8cf798e4a4bb08f98a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Oct 2021 07:45:00 -0700 Subject: [PATCH 155/969] Create L543.go --- Medium/L543.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L543.go diff --git a/Medium/L543.go b/Medium/L543.go new file mode 100644 index 0000000..89d5401 --- /dev/null +++ b/Medium/L543.go @@ -0,0 +1,25 @@ +package Medium + +func diameterOfBinaryTree(root *TreeNode) int { + max := 0 + maxDepth(root, &max) + return max +} + +func maxDepth(root *TreeNode, max *int) int { + if root == nil { + return 0 + } + + left, right := maxDepth(root.Left, max), maxDepth(root.Right, max) + + if left+right > *max { + *max = left + right + } + + if left > right { + return left + 1 + } else { + return right + 1 + } +} From f6ab63fa60f997ccf66c2fc25f33def154b276f7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Oct 2021 07:45:59 -0700 Subject: [PATCH 156/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1941ff9..fdd21c1 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Please give this repo a ⭐ if it inspires you. |[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| |[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| |[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| +|[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| ## Medium |LC #|Description| From 3f68835b5e46cf96d4f1fc9603c5fb0538213430 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Oct 2021 07:46:44 -0700 Subject: [PATCH 157/969] Move to Easy pkg --- {Medium => Easy}/L543.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Medium => Easy}/L543.go (100%) diff --git a/Medium/L543.go b/Easy/L543.go similarity index 100% rename from Medium/L543.go rename to Easy/L543.go From 022b452375cab1cad5cc1a350d9ede22783cc637 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Oct 2021 07:46:53 -0700 Subject: [PATCH 158/969] Update L543.go --- Easy/L543.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Easy/L543.go b/Easy/L543.go index 89d5401..8414359 100644 --- a/Easy/L543.go +++ b/Easy/L543.go @@ -1,4 +1,4 @@ -package Medium +package Easy func diameterOfBinaryTree(root *TreeNode) int { max := 0 From f28f7cc92b5e25f2ab03919ee14717419563b196 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 12 Oct 2021 22:02:24 -0700 Subject: [PATCH 159/969] Create L1008.go --- Medium/L1008.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Medium/L1008.go diff --git a/Medium/L1008.go b/Medium/L1008.go new file mode 100644 index 0000000..7cbbc0f --- /dev/null +++ b/Medium/L1008.go @@ -0,0 +1,23 @@ +package Medium + +func bstFromPreorder(preorder []int) *TreeNode { + idx := 0 + return dfs(preorder, math.MinInt64, math.MaxInt64, &idx) +} + +func dfs(preorder []int, low, high int, idx *int) *TreeNode { + if *idx >= len(preorder) { + return nil + } + if preorder[*idx] < low || preorder[*idx] > high { + return nil + } + + root := &TreeNode{ + Val: preorder[*idx], + } + *idx += 1 + root.Left, root.Right = dfs(preorder, low, root.Val, idx), dfs(preorder, root.Val, high, idx) + + return root +} From d75184f8a0eed31822bb5f3fe349987b90657c99 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 12 Oct 2021 22:03:00 -0700 Subject: [PATCH 160/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fdd21c1..35db988 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ Please give this repo a ⭐ if it inspires you. |[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| |[79](https://leetcode.com/problems/word-search/)| Word Search| |[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| +|[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| ## Hard |LC #|Description| From 9602d3bf77b81d1f421dca2569f63c966a1a6c73 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 12 Oct 2021 22:07:01 -0700 Subject: [PATCH 161/969] Create L374.go --- Easy/L374.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L374.go diff --git a/Easy/L374.go b/Easy/L374.go new file mode 100644 index 0000000..95e293a --- /dev/null +++ b/Easy/L374.go @@ -0,0 +1,18 @@ +package Easy + +func guessNumber(n int) int { + i, j := 1, n + for i < j { + mid := i + (j-i)/2 + switch guess(mid) { + case 0: + return mid + case 1: + i = mid + 1 + case -1: + j = mid + } + } + + return i +} From e9fc1c0b3e300cdbc9648ef1099519ae1ded72d5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 12 Oct 2021 22:07:37 -0700 Subject: [PATCH 162/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 35db988..c8ab7c1 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Please give this repo a ⭐ if it inspires you. |[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| |[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| |[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| +|[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| ## Medium |LC #|Description| From 6dd6ef9c7e8692f606f956380ffbfd277e4744ae Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Oct 2021 16:08:12 -0700 Subject: [PATCH 163/969] Create L309.go --- Medium/L309.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Medium/L309.go diff --git a/Medium/L309.go b/Medium/L309.go new file mode 100644 index 0000000..caab14d --- /dev/null +++ b/Medium/L309.go @@ -0,0 +1,30 @@ +package Medium + +func maxProfit(prices []int) int { + return solve(prices, 0, make(map[int]int)) +} + +func solve(prices []int, current int, mp map[int]int) int { + if current > len(prices) { + return 0 + } + + if val, ok := mp[current]; ok { + return val + } + + max := 0 + for i := current + 1; i < len(prices); i++ { + sell := prices[i] - prices[current] + solve(prices, i+2, mp) + if sell > max { + max = sell + } + } + + profit := solve(prices, current+1, mp) + if profit > max { + max = profit + } + mp[current] = max + return max +} From 1e874a6d1801094f72bd4a051d51b5fc3d426348 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Oct 2021 16:08:44 -0700 Subject: [PATCH 164/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c8ab7c1..697903f 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ Please give this repo a ⭐ if it inspires you. |[79](https://leetcode.com/problems/word-search/)| Word Search| |[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| |[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| +|[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| ## Hard |LC #|Description| From 9216dc5fc866efaac7fa227c9aafe38cf279b628 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Oct 2021 10:57:05 -0700 Subject: [PATCH 165/969] Create L123.go --- Hard/L123.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Hard/L123.go diff --git a/Hard/L123.go b/Hard/L123.go new file mode 100644 index 0000000..a193852 --- /dev/null +++ b/Hard/L123.go @@ -0,0 +1,36 @@ +package Hard + +func maxProfit(prices []int) int { + return solve(prices, 0, 2, 0, make(map[string]int)) +} + +func solve(prices []int, i, k, buyOrSell int, dp map[string]int) int { + if i >= len(prices) || k == 0 { + return 0 + } + key := fmt.Sprintf("%d#%d#%d", i, buyOrSell, k) + if val, ok := dp[key]; ok { + return val + } + res := 0 + if buyOrSell == 0 { + buy := solve(prices, i+1, k, 1, dp) - prices[i] + noBuy := solve(prices, i+1, k, 0, dp) + res += max(buy, noBuy) + } else { + sell := solve(prices, i+1, k-1, 0, dp) + prices[i] + noSell := solve(prices, i+1, k, 1, dp) + res += max(sell, noSell) + } + + dp[key] = res + return res +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 8e6af43805a467cab7998316db5f3312f1157032 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Oct 2021 10:57:38 -0700 Subject: [PATCH 166/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 697903f..54aaba0 100644 --- a/README.md +++ b/README.md @@ -229,3 +229,4 @@ Please give this repo a ⭐ if it inspires you. |[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| |[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| |[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| +|[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| From bd51e0fad96182b4ad72352d7c9ec4db9aaf2772 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 17 Oct 2021 15:32:27 -0700 Subject: [PATCH 167/969] Create L437.go --- Medium/L437.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L437.go diff --git a/Medium/L437.go b/Medium/L437.go new file mode 100644 index 0000000..f89fda8 --- /dev/null +++ b/Medium/L437.go @@ -0,0 +1,32 @@ +package Medium + +func pathSum(root *TreeNode, targetSum int) int { + mp := make(map[int]int) + mp[0] = 1 + + return solve(root, 0, targetSum, mp) +} + +func solve(root *TreeNode, currSum, target int, mp map[int]int) int { + if root == nil { + return 0 + } + + currSum += root.Val + res := 0 + if val, ok := mp[currSum-target]; ok { + res = val + } + if val, ok := mp[currSum]; ok { + mp[currSum] = val + 1 + } else { + mp[currSum] = 1 + } + + res += solve(root.Left, currSum, target, mp) + solve(root.Right, currSum, target, mp) + if val, ok := mp[currSum]; ok { + mp[currSum] = val - 1 + } + + return res +} From ab007a21e3de7d3ec0e18b4337954d3b07643fdc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 17 Oct 2021 15:33:02 -0700 Subject: [PATCH 168/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 54aaba0..e4da9a2 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,7 @@ Please give this repo a ⭐ if it inspires you. |[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| |[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| |[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| +|[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| ## Hard |LC #|Description| From 2f47c6969d8edf8f140dc424645a6c1f8fa3cbff Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 18 Oct 2021 06:52:09 -0700 Subject: [PATCH 169/969] Update L993.go --- Easy/L993.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Easy/L993.go b/Easy/L993.go index e9f529f..6c175e4 100644 --- a/Easy/L993.go +++ b/Easy/L993.go @@ -5,8 +5,8 @@ func isCousins(root *TreeNode, x int, y int) bool { return false } - q := make([]*TreeNode, 1, 10) - q[0] = root + var q []*TreeNode + q = append(q, root) var xExist, yExist bool for len(q) > 0 { From 6b3e0145c259a5e37191b69a0381c7714be33334 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Oct 2021 16:19:46 -0700 Subject: [PATCH 170/969] Create L496.go --- Easy/L496.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Easy/L496.go diff --git a/Easy/L496.go b/Easy/L496.go new file mode 100644 index 0000000..7aa64f2 --- /dev/null +++ b/Easy/L496.go @@ -0,0 +1,24 @@ +package Easy + +func nextGreaterElement(nums1 []int, nums2 []int) []int { + mp, res := make(map[int]int), make([]int, len(nums1)) + var st []int + for _, n := range nums2 { + for len(st) > 0 && st[len(st)-1] < n { + mp[st[len(st)-1]] = n + st = st[:len(st)-1] + } + st = append(st, n) + } + + for i := range nums1 { + val, ok := mp[nums1[i]] + if !ok { + res[i] = -1 + } else { + res[i] = val + } + } + + return res +} From 6f1854a20defa033b156e3afcc1072bf59f6558c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Oct 2021 16:20:19 -0700 Subject: [PATCH 171/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e4da9a2..b8c3793 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Please give this repo a ⭐ if it inspires you. |[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| |[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| |[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| +|[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| ## Medium |LC #|Description| From 9b0f9d025590f911ce5090b4940d58d25a76b61c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 22 Oct 2021 16:23:21 -0700 Subject: [PATCH 172/969] Create L451.go --- Medium/L451.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L451.go diff --git a/Medium/L451.go b/Medium/L451.go new file mode 100644 index 0000000..543b569 --- /dev/null +++ b/Medium/L451.go @@ -0,0 +1,29 @@ +package Medium + +func frequencySort(s string) string { + mp := make(map[byte]int) + for i := range s { + mp[s[i]]++ + } + + bucket := make([][]byte, len(s)+1) + for i := 0; i < len(mp)+1; i++ { + bucket[i] = make([]byte, 0) + } + for k, freq := range mp { + bucket[freq] = append(bucket[freq], k) + } + + var sb strings.Builder + for i := len(bucket) - 1; i >= 0; i-- { + if len(bucket[i]) != 0 { + for idx := 0; idx < len(bucket[i]); idx++ { + for pos := 0; pos < i; pos++ { + sb.WriteByte(bucket[i][idx]) + } + } + } + } + + return sb.String() +} From cf6b2224facd752469bb44e688dde3b97e4bcfd4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 22 Oct 2021 16:23:55 -0700 Subject: [PATCH 173/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8c3793..46bfc78 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ Please give this repo a ⭐ if it inspires you. |[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| |[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| |[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| +|[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| ## Hard |LC #|Description| From 1c6b620e61fdb82873dabace0fd353fca2c0fb22 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 23 Oct 2021 11:07:23 -0700 Subject: [PATCH 174/969] Create L154.go --- Hard/L154.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Hard/L154.go diff --git a/Hard/L154.go b/Hard/L154.go new file mode 100644 index 0000000..d00947e --- /dev/null +++ b/Hard/L154.go @@ -0,0 +1,19 @@ +package Hard + +func findMin(nums []int) int { + lo, hi, mid := 0, len(nums)-1, 0 + + for lo < hi { + mid = lo + (hi-lo)/2 + + if nums[mid] > nums[hi] { + lo = mid + 1 + } else if nums[mid] < nums[hi] { + hi = mid + } else { + hi-- + } + } + + return nums[lo] +} From 7b9c28c8a7619c1b147f97cc8ec3067511309486 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 23 Oct 2021 11:08:03 -0700 Subject: [PATCH 175/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 46bfc78..02300b0 100644 --- a/README.md +++ b/README.md @@ -233,3 +233,4 @@ Please give this repo a ⭐ if it inspires you. |[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| |[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| |[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| +|[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| From 58f52df07369345955be0cda3edf914423b9af8f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 24 Oct 2021 13:52:15 -0700 Subject: [PATCH 176/969] Create L222.go --- Medium/L222.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L222.go diff --git a/Medium/L222.go b/Medium/L222.go new file mode 100644 index 0000000..821d37d --- /dev/null +++ b/Medium/L222.go @@ -0,0 +1,31 @@ +package Medium + +func countNodes(root *TreeNode) int { + cnt := 0 + if root == nil { + return cnt + } + + var q []*TreeNode + q = append(q, root) + + for len(q) > 0 { + size := len(q) + + for idx := 0; idx < size; idx++ { + node := q[0] + q = q[1:] + cnt++ + + if node.Left != nil { + q = append(q, node.Left) + } + + if node.Right != nil { + q = append(q, node.Right) + } + } + } + + return cnt +} From a5582256bd272f00f7712a5aa6b751443cf96079 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 24 Oct 2021 13:52:48 -0700 Subject: [PATCH 177/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 02300b0..ca7ab02 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,7 @@ Please give this repo a ⭐ if it inspires you. |[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| |[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| |[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| +|[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| ## Hard |LC #|Description| From f9fa2e745218cad79c400198f17660d08d910769 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Oct 2021 08:22:39 -0700 Subject: [PATCH 178/969] Create L155.go --- Easy/L155.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Easy/L155.go diff --git a/Easy/L155.go b/Easy/L155.go new file mode 100644 index 0000000..66af987 --- /dev/null +++ b/Easy/L155.go @@ -0,0 +1,47 @@ +package Easy + +type StackNode struct { + val int + min int + next *StackNode +} + +func NewStackNode(val, min int, next *StackNode) *StackNode { + return &StackNode{ + val: val, + min: min, + next: next, + } +} + +type MinStack struct { + head *StackNode +} + +func Constructor() MinStack { + return MinStack{head: nil} +} + +func (this *MinStack) Push(val int) { + if this.head == nil { + this.head = NewStackNode(val, val, nil) + } else { + min := val + if this.head.min < min { + min = this.head.min + } + this.head = NewStackNode(val, min, this.head) + } +} + +func (this *MinStack) Pop() { + this.head = this.head.next +} + +func (this *MinStack) Top() int { + return this.head.val +} + +func (this *MinStack) GetMin() int { + return this.head.min +} From 11a22819bba45e4134f5c8a6866fa790518b3cc8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Oct 2021 08:23:07 -0700 Subject: [PATCH 179/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ca7ab02..ce40ef9 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Please give this repo a ⭐ if it inspires you. |[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| |[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| |[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| +|[155](https://leetcode.com/problems/min-stack/)| Min Stack| ## Medium |LC #|Description| From aca499b5b4b7bc7f253e4c8c97abe3e9cbb15dc8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 26 Oct 2021 11:06:36 -0700 Subject: [PATCH 180/969] Create L226.go --- Easy/L226.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Easy/L226.go diff --git a/Easy/L226.go b/Easy/L226.go new file mode 100644 index 0000000..ea9b3c3 --- /dev/null +++ b/Easy/L226.go @@ -0,0 +1,17 @@ +package Easy + +func invertTree(root *TreeNode) *TreeNode { + return dfs(root) +} + +func dfs(root *TreeNode) *TreeNode { + if root == nil { + return root + } + + left, right := root.Left, root.Right + root.Left = dfs(right) + root.Right = dfs(left) + + return root +} From 6ef8e2cc3b479acc262f1df4010a3fde2ab517df Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 26 Oct 2021 11:07:05 -0700 Subject: [PATCH 181/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ce40ef9..8a80159 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Please give this repo a ⭐ if it inspires you. |[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| |[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| |[155](https://leetcode.com/problems/min-stack/)| Min Stack| +|[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| ## Medium |LC #|Description| From cd85db6bff16454b13a2b9092eabda585e9a6f81 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 27 Oct 2021 12:31:22 -0700 Subject: [PATCH 182/969] Create L75.go --- Medium/L75.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L75.go diff --git a/Medium/L75.go b/Medium/L75.go new file mode 100644 index 0000000..5942297 --- /dev/null +++ b/Medium/L75.go @@ -0,0 +1,21 @@ +package Medium + +func sortColors(nums []int) { + if len(nums) < 2 { + return + } + + low, high := 0, len(nums)-1 + for i := low; i <= high; { + if nums[i] == 0 { + nums[i], nums[low] = nums[low], nums[i] + i++ + low++ + } else if nums[i] == 2 { + nums[i], nums[high] = nums[high], nums[i] + high-- + } else { + i++ + } + } +} From b6702b4bf179f03727acbb9855c3b74fc8bf92b1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 27 Oct 2021 12:31:54 -0700 Subject: [PATCH 183/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8a80159..231801d 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ Please give this repo a ⭐ if it inspires you. |[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| |[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| |[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| +|[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| ## Hard |LC #|Description| From 5c87b2b80ec956eb48ae68d20e1fb92d2228efc4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 28 Oct 2021 11:51:27 -0700 Subject: [PATCH 184/969] Create L15.go --- Medium/L15.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L15.go diff --git a/Medium/L15.go b/Medium/L15.go new file mode 100644 index 0000000..075232d --- /dev/null +++ b/Medium/L15.go @@ -0,0 +1,31 @@ +package Medium + +func threeSum(nums []int) [][]int { + var res [][]int + sort.Ints(nums) + for i := 0; i+2 < len(nums); i++ { + if i > 0 && nums[i] == nums[i-1] { + continue + } + + j, k, tgt := i+1, len(nums)-1, -nums[i] + for j < k { + if nums[j]+nums[k] == tgt { + res = append(res, []int{nums[i], nums[j], nums[k]}) + j++ + k-- + for j < k && nums[j] == nums[j-1] { + j++ + } + for j < k && nums[k] == nums[k+1] { + k-- + } + } else if nums[j]+nums[k] > tgt { + k-- + } else { + j++ + } + } + } + return res +} From f12aa8784638b30319c5daf0d000308c3cb2c4be Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 28 Oct 2021 11:51:56 -0700 Subject: [PATCH 185/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 231801d..91cf872 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ Please give this repo a ⭐ if it inspires you. |[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| |[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| |[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| +|[15](https://leetcode.com/problems/3sum/)| 3Sum| ## Hard |LC #|Description| From a46a8426477ba638c0fe660f23d825b11eb0b85d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 29 Oct 2021 14:39:32 -0700 Subject: [PATCH 186/969] Create L994.go --- Medium/L994.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Medium/L994.go diff --git a/Medium/L994.go b/Medium/L994.go new file mode 100644 index 0000000..3683a85 --- /dev/null +++ b/Medium/L994.go @@ -0,0 +1,66 @@ +package Medium + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func isValid(grid [][]int, x, y int) bool { + m, n := len(grid), len(grid[0]) + + return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 +} + +func orangesRotting(grid [][]int) int { + if len(grid) == 0 { + return 0 + } + m, n, cnt, lvl := len(grid), len(grid[0]), 0, -1 + vis := make([][]bool, m) + for i := 0; i < m; i++ { + vis[i] = make([]bool, n) + } + + var q [][]int + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == 2 { + q = append(q, []int{i, j}) + vis[i][j] = true + } else if grid[i][j] == 1 { + cnt++ + } + } + } + + if cnt == 0 { + return cnt + } + + if len(q) == 0 || len(q) == m*n { + return -1 + } + + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + c := q[0] + q = q[1:] + + for _, d := range dirs { + newX, newY := c[0]+d[0], c[1]+d[1] + if isValid(grid, newX, newY) && !vis[newX][newY] { + vis[newX][newY] = true + q = append(q, []int{newX, newY}) + cnt-- + } + } + } + lvl++ + } + + if cnt == 0 { + if lvl > 0 { + return lvl + } + } + + return -1 +} From 440118b08a7dc96e17e98c1fe308bbc5c91b272b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 29 Oct 2021 14:40:03 -0700 Subject: [PATCH 187/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 91cf872..92b164b 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ Please give this repo a ⭐ if it inspires you. |[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| |[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| |[15](https://leetcode.com/problems/3sum/)| 3Sum| +|[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| ## Hard |LC #|Description| From c713e5e82ff311e01056384f631abb0216a747a1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Oct 2021 19:14:40 -0700 Subject: [PATCH 188/969] Create L430.go --- Medium/L430.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L430.go diff --git a/Medium/L430.go b/Medium/L430.go new file mode 100644 index 0000000..3528627 --- /dev/null +++ b/Medium/L430.go @@ -0,0 +1,28 @@ +package Medium + +func flatten(root *Node) *Node { + if root == nil { + return root + } + + p := root + for p != nil { + if p.Child == nil { + p = p.Next + continue + } + tmp := p.Child + for tmp.Next != nil { + tmp = tmp.Next + } + tmp.Next = p.Next + if p.Next != nil { + p.Next.Prev = tmp + } + p.Next = p.Child + p.Child.Prev = p + p.Child = nil + } + + return root +} From 952445fbeed1c68936bea38f947648dcc0b3f677 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Oct 2021 19:15:09 -0700 Subject: [PATCH 189/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 92b164b..b692bad 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,7 @@ Please give this repo a ⭐ if it inspires you. |[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| |[15](https://leetcode.com/problems/3sum/)| 3Sum| |[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| +|[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| ## Hard |LC #|Description| From 8d67fe2bbca950ebcfd80853f10d475ba495998b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 1 Nov 2021 12:21:59 -0700 Subject: [PATCH 190/969] Create L130.go --- Medium/L130.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Medium/L130.go diff --git a/Medium/L130.go b/Medium/L130.go new file mode 100644 index 0000000..8dc9aaa --- /dev/null +++ b/Medium/L130.go @@ -0,0 +1,66 @@ +package Medium + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func isValid(board *[][]byte, x, y int) bool { + m, n := len(*board), len((*board)[0]) + + return x >= 0 && x < m && y >= 0 && y < n && (*board)[x][y] == 'O' +} + +func bfs(board *[][]byte, i, j int) { + var q [][]int + q = append(q, []int{i, j}) + + for len(q) > 0 { + tmp := q[0] + q = q[1:] + + x, y := tmp[0], tmp[1] + (*board)[x][y] = '1' + + for _, d := range dirs { + newX, newY := x+d[0], y+d[1] + if isValid(board, newX, newY) { + (*board)[newX][newY] = '1' + q = append(q, []int{newX, newY}) + } + } + } +} + +func solve(board [][]byte) { + for i := 0; i < len(board[0]); i++ { + if board[0][i] == 'O' { + bfs(&board, 0, i) + } + } + + for i := 0; i < len(board[0]); i++ { + if board[len(board)-1][i] == 'O' { + bfs(&board, len(board)-1, i) + } + } + + for i := 0; i < len(board); i++ { + if board[i][0] == 'O' { + bfs(&board, i, 0) + } + } + + for i := 0; i < len(board); i++ { + if board[i][len(board[0])-1] == 'O' { + bfs(&board, i, len(board[0])-1) + } + } + + for i := 0; i < len(board); i++ { + for j := 0; j < len(board[0]); j++ { + if board[i][j] == 'O' { + board[i][j] = 'X' + } else if board[i][j] == '1' { + board[i][j] = 'O' + } + } + } +} From d02a13c15ca790c0902d163149ae9c43c9a2fee2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 1 Nov 2021 12:22:29 -0700 Subject: [PATCH 191/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b692bad..5f0d2f3 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,7 @@ Please give this repo a ⭐ if it inspires you. |[15](https://leetcode.com/problems/3sum/)| 3Sum| |[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| |[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| +|[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| ## Hard |LC #|Description| From 70a4f75286c7dbf5ed588936453e11406b9473f5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 Nov 2021 06:59:10 -0700 Subject: [PATCH 192/969] Create L129.go --- Medium/L129.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L129.go diff --git a/Medium/L129.go b/Medium/L129.go new file mode 100644 index 0000000..189da3e --- /dev/null +++ b/Medium/L129.go @@ -0,0 +1,20 @@ +package Medium + +func sumNumbers(root *TreeNode) int { + return dfs(root, 0) +} + +func dfs(root *TreeNode, currSum int) int { + if root == nil { + return 0 + } + + currSum = currSum*10 + root.Val + if root.Left == nil && root.Right == nil { + return currSum + } + + leftSum, rightSum := dfs(root.Left, currSum), dfs(root.Right, currSum) + + return leftSum + rightSum +} From aa9967eebcf26c8ee99b42f35c1fdb4a5f79639e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 Nov 2021 06:59:47 -0700 Subject: [PATCH 193/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5f0d2f3..b674bd3 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ Please give this repo a ⭐ if it inspires you. |[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| |[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| |[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| +|[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| ## Hard |LC #|Description| From 27e8afb870ae3cb77f1b17b087b0931998885477 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 5 Nov 2021 16:35:41 -0700 Subject: [PATCH 194/969] Create L441.go --- Easy/L441.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L441.go diff --git a/Easy/L441.go b/Easy/L441.go new file mode 100644 index 0000000..68a4453 --- /dev/null +++ b/Easy/L441.go @@ -0,0 +1,15 @@ +package Easy + +func arrangeCoins(n int) int { + i := 0 + for n > 0 { + i += 1 + n -= i + } + + if n == 0 { + return i + } + + return i - 1 +} From 6d66c3dc7361498ce059e84e27b22adf3cf98847 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 5 Nov 2021 16:36:06 -0700 Subject: [PATCH 195/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b674bd3..9f27126 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Please give this repo a ⭐ if it inspires you. |[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| |[155](https://leetcode.com/problems/min-stack/)| Min Stack| |[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| +|[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| ## Medium |LC #|Description| From 0b787f015ee7e13595f82372aabebd5480c1a7f3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Nov 2021 15:30:27 -0700 Subject: [PATCH 196/969] Create L260.go --- Medium/L260.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L260.go diff --git a/Medium/L260.go b/Medium/L260.go new file mode 100644 index 0000000..5c1a1c5 --- /dev/null +++ b/Medium/L260.go @@ -0,0 +1,20 @@ +package Medium + +func singleNumber(nums []int) []int { + diff := 0 + for _, n := range nums { + diff ^= n + } + + diff = diff & -diff + rets := []int{0, 0} + for _, n := range nums { + if n&diff == 0 { + rets[0] ^= n + } else { + rets[1] ^= n + } + } + + return rets +} From edc70ef32745f43cd66b8227bc323a10c049abde Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Nov 2021 15:31:07 -0700 Subject: [PATCH 197/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9f27126..85b8277 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ Please give this repo a ⭐ if it inspires you. |[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| |[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| |[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| +|[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| ## Hard |LC #|Description| From 85d30d7b9cfd2fa319dd3a1e6fc712b2f948c0ec Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Nov 2021 17:43:53 -0700 Subject: [PATCH 198/969] Create L43.go --- Medium/L43.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L43.go diff --git a/Medium/L43.go b/Medium/L43.go new file mode 100644 index 0000000..325b5d6 --- /dev/null +++ b/Medium/L43.go @@ -0,0 +1,29 @@ +package Medium + +func multiply(num1 string, num2 string) string { + n1, n2 := len(num1), len(num2) + products := make([]byte, n1+n2) + for i := n1 - 1; i >= 0; i-- { + for j := n2 - 1; j >= 0; j-- { + d1, d2 := num1[i]-'0', num2[j]-'0' + products[i+j+1] += d1 * d2 + } + } + + carry := 0 + for i := len(products) - 1; i >= 0; i-- { + tmp := (int(products[i]) + carry) % 10 + carry = (int(products[i]) + carry) / 10 + products[i] = byte(tmp) + } + + for len(products) != 0 && products[0] == '0' { + products = products[1:] + } + + if len(products) == 0 { + return "0" + } + + return string(products) +} From c6c019ff484a6abbcfb08e6e1e27ba88bc65e1f8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Nov 2021 17:44:29 -0700 Subject: [PATCH 199/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 85b8277..3cee2cc 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ Please give this repo a ⭐ if it inspires you. |[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| |[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| |[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| +|[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| ## Hard |LC #|Description| From b58bebfe7e4860fd63663e475766c384335a3e97 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Nov 2021 06:46:58 -0800 Subject: [PATCH 200/969] Create L96.go --- Medium/L96.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L96.go diff --git a/Medium/L96.go b/Medium/L96.go new file mode 100644 index 0000000..2073152 --- /dev/null +++ b/Medium/L96.go @@ -0,0 +1,25 @@ +package Medium + +func numTrees(n int) int { + mp := make(map[int]int) + + return solve(n, mp) +} + +func solve(n int, mp map[int]int) int { + if n == 0 || n == 1 { + return 1 + } + + sum := 0 + for i := 1; i <= n; i++ { + if _, ok := mp[i-1]; !ok { + mp[i-1] = solve(i-1, mp) + } + if _, ok := mp[n-i]; !ok { + mp[n-i] = solve(n-i, mp) + } + sum += mp[i-1] * mp[n-i] + } + return sum +} From 2f502b2e330fd197cd75800d6e2cc7be079f2362 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Nov 2021 06:47:29 -0800 Subject: [PATCH 201/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3cee2cc..74d1139 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,7 @@ Please give this repo a ⭐ if it inspires you. |[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| |[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| |[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| +|[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| ## Hard |LC #|Description| From dc97b84f39dd16c92037b7a8756866d5eed5b06f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 10 Nov 2021 06:57:59 -0800 Subject: [PATCH 202/969] Create L122.go --- Medium/L122.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L122.go diff --git a/Medium/L122.go b/Medium/L122.go new file mode 100644 index 0000000..73e2675 --- /dev/null +++ b/Medium/L122.go @@ -0,0 +1,45 @@ +package Medium + +func maxProfit(prices []int) int { + n := len(prices) + + if n < 2 { + return 0 + } + + memo := make([][]int, n+1) + for i := 0; i < n+1; i++ { + memo[i] = make([]int, 2) + for j := 0; j < 2; j++ { + memo[i][j] = -1 + } + } + + return solve(0, 1, prices, &memo) +} + +func solve(index, buy int, prices []int, memo *[][]int) int { + if index >= len(prices) { + return 0 + } + + if (*memo)[index][buy] != -1 { + return (*memo)[index][buy] + } + + if buy == 1 { + (*memo)[index][buy] = max(-prices[index]+solve(index+1, 0, prices, memo), solve(index+1, 1, prices, memo)) + } else { + (*memo)[index][buy] = max(prices[index]+solve(index+1, 1, prices, memo), solve(index+1, 0, prices, memo)) + } + + return (*memo)[index][buy] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 6f68c2f63f29a5ce915d97ef822211fceb69c501 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 10 Nov 2021 06:58:36 -0800 Subject: [PATCH 203/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 74d1139..7fce499 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,7 @@ Please give this repo a ⭐ if it inspires you. |[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| |[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| |[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| +|[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| ## Hard |LC #|Description| From cb548c26f75d65c6585e628d0a37986666ae3e3d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 11 Nov 2021 06:32:04 -0800 Subject: [PATCH 204/969] Create L1413.go --- Easy/L1413.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Easy/L1413.go diff --git a/Easy/L1413.go b/Easy/L1413.go new file mode 100644 index 0000000..114680a --- /dev/null +++ b/Easy/L1413.go @@ -0,0 +1,14 @@ +package Easy + +func minStartValue(nums []int) int { + sum, minSum := 0, 0 + + for _, n := range nums { + sum += n + if sum < minSum { + minSum = sum + } + } + + return 1 - minSum +} From 05d7383f81cf8b709f883070f69de804da0769b2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 11 Nov 2021 06:32:35 -0800 Subject: [PATCH 205/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7fce499..b7f5816 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Please give this repo a ⭐ if it inspires you. |[155](https://leetcode.com/problems/min-stack/)| Min Stack| |[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| |[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| +|[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| ## Medium |LC #|Description| From f3e91623b6b62cc597906a99fae600b3dc893a98 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 11 Nov 2021 16:48:19 -0800 Subject: [PATCH 206/969] Create L203.go --- Easy/L203.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L203.go diff --git a/Easy/L203.go b/Easy/L203.go new file mode 100644 index 0000000..afa05ae --- /dev/null +++ b/Easy/L203.go @@ -0,0 +1,15 @@ +package Easy + +func removeElements(head *ListNode, val int) *ListNode { + if head == nil { + return nil + } + + head.Next = removeElements(head.Next, val) + + if head.Val == val { + return head.Next + } + + return head +} From 1d4ede7f3844e7ebd6ad41883a0e69c0ac33a9e9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 11 Nov 2021 16:48:49 -0800 Subject: [PATCH 207/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b7f5816..af8020d 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Please give this repo a ⭐ if it inspires you. |[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| |[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| |[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| +|[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| ## Medium |LC #|Description| From 1295494b8fdd4f375252209c616409d066e9dca0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 12 Nov 2021 22:30:25 -0800 Subject: [PATCH 208/969] Create L739.go --- Medium/L739.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L739.go diff --git a/Medium/L739.go b/Medium/L739.go new file mode 100644 index 0000000..26c42d6 --- /dev/null +++ b/Medium/L739.go @@ -0,0 +1,19 @@ +package Medium + +func dailyTemperatures(T []int) []int { + ret, n := make([]int, len(T)), len(T) + var st []int + + for i := n - 1; i >= 0; i-- { + cur := T[i] + for len(st) > 0 && cur >= T[st[len(st)-1]] { + st = st[:len(st)-1] + } + if len(st) > 0 { + ret[i] = st[len(st)-1] - i + } + st = append(st, i) + } + + return ret +} From 5ad461a66f32ffb059e9fb6e4d583c86b79fed00 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 12 Nov 2021 22:30:59 -0800 Subject: [PATCH 209/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index af8020d..2b8bdc3 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,7 @@ Please give this repo a ⭐ if it inspires you. |[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| |[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| |[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| +|[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| ## Hard |LC #|Description| From ebb87a625aa04d00835ce70e803eaa4f3615c85c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 15 Nov 2021 07:17:57 -0800 Subject: [PATCH 210/969] Create L368.go --- Medium/L368.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L368.go diff --git a/Medium/L368.go b/Medium/L368.go new file mode 100644 index 0000000..c4027cb --- /dev/null +++ b/Medium/L368.go @@ -0,0 +1,40 @@ +package Medium + +type Solution struct { + ans []int + dp []int + tmp []int +} + +func largestDivisibleSubset(nums []int) []int { + s := &Solution{ + dp: make([]int, 0), + tmp: make([]int, 0), + } + + sort.Ints(nums) + for i := 0; i < len(nums); i++ { + s.dp = append(s.dp, -1) + } + s.solve(0, 1, nums) + + return s.ans +} + +func (s *Solution) solve(i, prev int, nums []int) { + if i >= len(nums) { + if len(s.tmp) > len(s.ans) { + s.ans = make([]int, len(s.tmp)) + copy(s.ans, s.tmp) + } + return + } + + if len(s.tmp) > s.dp[i] && nums[i]%prev == 0 { + s.dp[i] = len(s.tmp) + s.tmp = append(s.tmp, nums[i]) + s.solve(i+1, nums[i], nums) + s.tmp = s.tmp[:len(s.tmp)-1] + } + s.solve(i+1, prev, nums) +} From 3a58990058d2389d8c78565c8869f81ad5d6bcab Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 15 Nov 2021 07:18:33 -0800 Subject: [PATCH 211/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2b8bdc3..2babd7a 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,7 @@ Please give this repo a ⭐ if it inspires you. |[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| |[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| |[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| +|[368](https://leetcode.com/problems/largest-divisible-subset/)| Largest Divisible Subset| ## Hard |LC #|Description| From f16b9cb6bf5090e62014d228b33a263acc34612e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 16 Nov 2021 06:24:14 -0800 Subject: [PATCH 212/969] Create L668.go --- Hard/L668.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Hard/L668.go diff --git a/Hard/L668.go b/Hard/L668.go new file mode 100644 index 0000000..d71253e --- /dev/null +++ b/Hard/L668.go @@ -0,0 +1,34 @@ +package Hard + +func findKthNumber(m int, n int, k int) int { + lo, hi, mid := 0, m*n, 0 + + for lo < hi { + mid = lo + (hi-lo)/2 + + if kSmaller(n, m, mid, k) { + hi = mid + } else { + lo = mid + 1 + } + } + + return lo +} + +func kSmaller(n, m, num, k int) bool { + c := 0 + for i := 0; i <= m; i++ { + c += min(n, num/i) + } + + return c >= k +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 780e85a84ffda918d39771b7763ac01f4bcdd247 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 16 Nov 2021 06:24:54 -0800 Subject: [PATCH 213/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2babd7a..c808fe3 100644 --- a/README.md +++ b/README.md @@ -252,3 +252,4 @@ Please give this repo a ⭐ if it inspires you. |[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| |[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| |[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| +|[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| From 1190a24d13470366252e0a22861ef55d87a399c3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 18 Nov 2021 14:23:14 -0800 Subject: [PATCH 214/969] Create L448.go --- Easy/L448.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L448.go diff --git a/Easy/L448.go b/Easy/L448.go new file mode 100644 index 0000000..ae4e278 --- /dev/null +++ b/Easy/L448.go @@ -0,0 +1,18 @@ +package Easy + +func findDisappearedNumbers(nums []int) []int { + for i := range nums { + for nums[i] != i+1 && nums[i] != nums[nums[i]-1] { + nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i] + } + } + + var res []int + for i := range nums { + if nums[i] != i+1 { + res = append(res, i+1) + } + } + + return res +} From e2624a6a416eeed504b5bf89a448b630d3bdeb34 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 18 Nov 2021 14:23:45 -0800 Subject: [PATCH 215/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c808fe3..cfb4071 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Please give this repo a ⭐ if it inspires you. |[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| |[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| |[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| +|[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| ## Medium |LC #|Description| From 00ebb6a756f3a32029888bf9556fd55ace689789 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 18 Nov 2021 22:36:02 -0800 Subject: [PATCH 216/969] Create L461.go --- Easy/L461.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Easy/L461.go diff --git a/Easy/L461.go b/Easy/L461.go new file mode 100644 index 0000000..3d99e05 --- /dev/null +++ b/Easy/L461.go @@ -0,0 +1,12 @@ +package Easy + +func hammingDistance(x int, y int) int { + xor, res := x^y, 0 + + for xor != 0 { + res += xor & 1 + xor >>= 1 + } + + return res +} From 3f75de8ca7d579877c57ccb378ffadc02663a2e5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 18 Nov 2021 22:36:46 -0800 Subject: [PATCH 217/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cfb4071..afa997e 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Please give this repo a ⭐ if it inspires you. |[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| |[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| |[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| +|[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| ## Medium |LC #|Description| From de485db8444ebeddb51b3717adf6f501ec3ede87 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 20 Nov 2021 08:15:02 -0800 Subject: [PATCH 218/969] Create L540.go --- Medium/L540.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L540.go diff --git a/Medium/L540.go b/Medium/L540.go new file mode 100644 index 0000000..1a47f46 --- /dev/null +++ b/Medium/L540.go @@ -0,0 +1,25 @@ +package Medium + +func singleNonDuplicate(nums []int) int { + lo, hi, mid := 0, len(nums)-1, -1 + + for lo < hi { + mid = lo + (hi-lo)/2 + + if mid%2 == 0 { + if nums[mid] == nums[mid+1] { + lo = mid + 2 + } else { + hi = mid + } + } else { + if nums[mid] == nums[mid-1] { + lo = mid + 1 + } else { + hi = mid + } + } + } + + return nums[lo] +} From f3c84fc27a17513233dbe59a4345d364ec896abd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 20 Nov 2021 08:15:37 -0800 Subject: [PATCH 219/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index afa997e..0968a73 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ Please give this repo a ⭐ if it inspires you. |[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| |[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| |[368](https://leetcode.com/problems/largest-divisible-subset/)| Largest Divisible Subset| +|[540](https://leetcode.com/problems/single-element-in-a-sorted-array/)| Single Element in a Sorted Array| ## Hard |LC #|Description| From 1a34e3c388be00729d087fa6fa02cdfba34c6372 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 21 Nov 2021 16:25:42 -0800 Subject: [PATCH 220/969] Create L450.go --- Medium/L450.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L450.go diff --git a/Medium/L450.go b/Medium/L450.go new file mode 100644 index 0000000..680e451 --- /dev/null +++ b/Medium/L450.go @@ -0,0 +1,33 @@ +package Medium + +func deleteNode(root *TreeNode, key int) *TreeNode { + if root == nil { + return root + } + + if key < root.Val { + root.Left = deleteNode(root.Left, key) + } else if key > root.Val { + root.Right = deleteNode(root.Right, key) + } else { + if root.Left == nil { + return root.Right + } else if root.Right == nil { + return root.Left + } + + minNode := findMin(root.Right) + root.Val = minNode.Val + root.Right = deleteNode(root.Right, root.Val) + } + + return root +} + +func findMin(root *TreeNode) *TreeNode { + for root.Left != nil { + root = root.Left + } + + return root +} From 430621b8c697ba73529129b196f217880889a162 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 21 Nov 2021 16:34:49 -0800 Subject: [PATCH 221/969] Create L106.go --- Medium/L106.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L106.go diff --git a/Medium/L106.go b/Medium/L106.go new file mode 100644 index 0000000..9d21799 --- /dev/null +++ b/Medium/L106.go @@ -0,0 +1,31 @@ +package Medium + +func buildTree(inorder []int, postorder []int) *TreeNode { + if len(inorder) == 0 || len(postorder) == 0 { + return nil + } + + mp := make(map[int]int) + for i := range inorder { + mp[inorder[i]] = i + } + + return helper(postorder, 0, len(postorder)-1, inorder, 0, len(inorder)-1, mp) +} + +func helper(postOrder []int, postStart, postEnd int, inOrder []int, inStart, inEnd int, mp map[int]int) *TreeNode { + if postStart > postEnd || inStart > inEnd { + return nil + } + + root := &TreeNode{ + Val: postOrder[postEnd], + } + inRoot := mp[root.Val] + numsLeft := inRoot - inStart + + root.Left = helper(postOrder, postStart, postStart+numsLeft-1, inOrder, inStart, inRoot-1, mp) + root.Right = helper(postOrder, postStart+numsLeft, postEnd-1, inOrder, inRoot+1, inEnd, mp) + + return root +} From 27cfb11d119b80a6218562290c02ea9c6ccfcbb2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 21 Nov 2021 16:35:49 -0800 Subject: [PATCH 222/969] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0968a73..e91e991 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,8 @@ Please give this repo a ⭐ if it inspires you. |[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| |[368](https://leetcode.com/problems/largest-divisible-subset/)| Largest Divisible Subset| |[540](https://leetcode.com/problems/single-element-in-a-sorted-array/)| Single Element in a Sorted Array| +|[106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| Construct Binary Tree from Inorder and Postorder Traversal| +|[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| ## Hard |LC #|Description| From dca28a255b6e7f5a1beeb44af8d3cd875fbff796 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 23 Nov 2021 16:31:33 -0800 Subject: [PATCH 223/969] Create L952.go --- Hard/L952.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Hard/L952.go diff --git a/Hard/L952.go b/Hard/L952.go new file mode 100644 index 0000000..de42a92 --- /dev/null +++ b/Hard/L952.go @@ -0,0 +1,70 @@ +package Hard + +type UnionFind struct { + Parent []int + Size []int + Max int +} + +func NewUnionFind(n int) *UnionFind { + parent, size, max := make([]int, n), make([]int, n), 1 + for i := 0; i < n; i++ { + parent[i], size[i] = i, 1 + } + + return &UnionFind{ + Parent: parent, + Size: size, + Max: max, + } +} + +func (u *UnionFind) find(x int) int { + if x == u.Parent[x] { + return x + } + u.Parent[x] = u.find(u.Parent[x]) + return u.Parent[x] +} + +func (u *UnionFind) union(x, y int) { + rootX, rootY := u.find(x), u.find(y) + + if rootY != rootX { + u.Parent[rootX] = rootY + u.Size[rootY] += u.Size[rootX] + if u.Size[rootY] > u.Max { + u.Max = u.Size[rootY] + } + } +} + +func largestComponentSize(nums []int) int { + mp, uf := make(map[int]int), NewUnionFind(len(nums)) + + for i := range nums { + a := nums[i] + for j := 2; j*j <= a; j++ { + if a%j == 0 { + if _, ok := mp[j]; !ok { + mp[j] = i + } else { + uf.union(i, mp[j]) + } + + if _, ok := mp[a/j]; !ok { + mp[a/j] = i + } else { + uf.union(i, mp[a/j]) + } + } + } + if _, ok := mp[a]; !ok { + mp[a] = i + } else { + uf.union(i, mp[a]) + } + } + + return uf.Max +} From 6f554366d4145166dd5568914c91f82a8765c49b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 23 Nov 2021 16:42:18 -0800 Subject: [PATCH 224/969] Create L986.go --- Medium/L986.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Medium/L986.go diff --git a/Medium/L986.go b/Medium/L986.go new file mode 100644 index 0000000..8c5ed11 --- /dev/null +++ b/Medium/L986.go @@ -0,0 +1,43 @@ +package Medium + +func intervalIntersection(firstList [][]int, secondList [][]int) [][]int { + var res [][]int + if len(firstList) == 0 || len(secondList) == 0 { + return res + } + + i, j, m, n := 0, 0, len(firstList), len(secondList) + max, min := math.MinInt64, math.MaxInt64 + + for i < m && j < n { + max = maximum(firstList[i][0], secondList[j][0]) + min = minimum(firstList[i][1], secondList[j][1]) + if max <= min { + res = append(res, []int{max, min}) + } + + if firstList[i][1] < secondList[j][1] { + i++ + } else { + j++ + } + } + + return res +} + +func maximum(a, b int) int { + if a > b { + return a + } + + return b +} + +func minimum(a, b int) int { + if a < b { + return a + } + + return b +} From d746207415f30aa220a5e09c00a633008dc96913 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 23 Nov 2021 16:43:22 -0800 Subject: [PATCH 225/969] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e91e991..9458ddc 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ Please give this repo a ⭐ if it inspires you. |[540](https://leetcode.com/problems/single-element-in-a-sorted-array/)| Single Element in a Sorted Array| |[106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| Construct Binary Tree from Inorder and Postorder Traversal| |[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| +|[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| ## Hard |LC #|Description| @@ -258,3 +259,4 @@ Please give this repo a ⭐ if it inspires you. |[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| |[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| |[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| +|[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| From cfca07240b2873a7ed83a3cce54d95bf28fff577 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 24 Nov 2021 20:42:21 -0800 Subject: [PATCH 226/969] Create L53.go --- Easy/L53.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L53.go diff --git a/Easy/L53.go b/Easy/L53.go new file mode 100644 index 0000000..20ea9fa --- /dev/null +++ b/Easy/L53.go @@ -0,0 +1,28 @@ +package Easy + +func maxSubArray(nums []int) int { + max := math.MinInt64 + solve(nums, 0, &max) + + return max +} + +func solve(nums []int, idx int, max *int) int { + maxSoFar := 0 + if idx == len(nums)-1 { + maxSoFar = nums[idx] + } else { + maxSoFar = maximum(nums[idx], nums[idx]+solve(nums, idx+1, max)) + } + *max = maximum(*max, maxSoFar) + + return maxSoFar +} + +func maximum(a, b int) int { + if a > b { + return a + } + + return b +} From 044bfa23a7ba5a7c52b45202019cb7af5e21970f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 24 Nov 2021 20:42:52 -0800 Subject: [PATCH 227/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9458ddc..456adaa 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Please give this repo a ⭐ if it inspires you. |[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| |[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| |[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| +|[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| ## Medium |LC #|Description| From 63d833b0212b7e621baea1e450f7f24c47be18c7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 25 Nov 2021 17:59:47 -0800 Subject: [PATCH 228/969] Create L35.go --- Easy/L35.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L35.go diff --git a/Easy/L35.go b/Easy/L35.go new file mode 100644 index 0000000..7f0a5ed --- /dev/null +++ b/Easy/L35.go @@ -0,0 +1,22 @@ +package Easy + +func searchInsert(nums []int, target int) int { + if len(nums) == 0 { + return 0 + } + + l, r, mid := 0, len(nums)-1, 0 + + for l <= r { + mid = (r-l)/2 + l + if nums[mid] == target { + return mid + } else if nums[mid] < target { + l = mid + 1 + } else { + r = mid - 1 + } + } + + return l +} From 0853bff6382f5423ff49b032aa3cde63855007c9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 25 Nov 2021 18:00:17 -0800 Subject: [PATCH 229/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 456adaa..13b00d1 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Please give this repo a ⭐ if it inspires you. |[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| |[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| |[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| +|[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| ## Medium |LC #|Description| From c6804868f1e2b6d6015a4be7a004ae918cf03ce3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 26 Nov 2021 22:44:24 -0800 Subject: [PATCH 230/969] Create L238.go --- Medium/L238.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L238.go diff --git a/Medium/L238.go b/Medium/L238.go new file mode 100644 index 0000000..975affe --- /dev/null +++ b/Medium/L238.go @@ -0,0 +1,21 @@ +package Medium + +func productExceptSelf(nums []int) []int { + if len(nums) == 0 { + return []int{} + } + + ret := make([]int, len(nums)) + ret[0] = 1 + for i := 1; i < len(nums); i++ { + ret[i] = ret[i-1] * nums[i-1] + } + + right := 1 + for i := len(nums) - 2; i >= 0; i-- { + right *= nums[i+1] + ret[i] = ret[i] * right + } + + return ret +} From 37bce959fb85a72f7c6ce684c0522a00fc605842 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 26 Nov 2021 22:44:58 -0800 Subject: [PATCH 231/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 13b00d1..9733cec 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,7 @@ Please give this repo a ⭐ if it inspires you. |[106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| Construct Binary Tree from Inorder and Postorder Traversal| |[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| |[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| +|[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| ## Hard |LC #|Description| From aab9339d51516a7761326109d50d39ef92d5acd8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Nov 2021 16:48:54 -0800 Subject: [PATCH 232/969] Create L797.go --- Medium/L797.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L797.go diff --git a/Medium/L797.go b/Medium/L797.go new file mode 100644 index 0000000..7914aaa --- /dev/null +++ b/Medium/L797.go @@ -0,0 +1,35 @@ +package Medium + +func allPathsSourceTarget(graph [][]int) [][]int { + var ret, lst [][]int + if len(graph) == 0 { + return ret + } + + for i := range graph { + lst = append(lst, make([]int, 0)) + for _, g := range graph[i] { + lst[i] = append(lst[i], g) + } + } + + bt(&ret, lst, make([]int, 0), 0, len(graph)-1) + return ret +} + +func bt(ret *[][]int, lst [][]int, tmp []int, index, n int) { + if index == n { + tmp = append(tmp, index) + temp := make([]int, len(tmp)) + copy(temp, tmp) + *ret = append(*ret, temp) + tmp = tmp[:len(tmp)-1] + return + } + + tmp = append(tmp, index) + for _, nei := range lst[index] { + bt(ret, lst, tmp, nei, n) + } + tmp = tmp[:len(tmp)-1] +} From 0abbd8c8118f1978884f1b28904c462055dded05 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Nov 2021 16:49:30 -0800 Subject: [PATCH 233/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9733cec..e72f9a4 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,7 @@ Please give this repo a ⭐ if it inspires you. |[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| |[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| |[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| +|[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| ## Hard |LC #|Description| From 8b655c62ca97e32d5c764e58417f226108d865b9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 28 Nov 2021 22:47:42 -0800 Subject: [PATCH 234/969] Create L721.go --- Medium/L721.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Medium/L721.go diff --git a/Medium/L721.go b/Medium/L721.go new file mode 100644 index 0000000..d31f3c5 --- /dev/null +++ b/Medium/L721.go @@ -0,0 +1,52 @@ +package Medium + +func accountsMerge(accounts [][]string) [][]string { + var ans [][]string + if len(accounts) == 0 { + return ans + } + + owner, graph := make(map[string]string), make(map[string]map[string]bool) + + for _, a := range accounts { + own := a[0] + for i := 1; i < len(a); i++ { + owner[a[i]] = own + if _, ok := graph[a[i]]; !ok { + graph[a[i]] = make(map[string]bool) + } + + if i == 1 { + continue + } + graph[a[i]][a[i-1]] = true + graph[a[i-1]][a[i]] = true + } + } + + vis := make(map[string]bool) + for k, _ := range graph { + if _, ok := vis[k]; !ok { + var tmp []string + dfs(graph, k, &tmp, vis) + sort.Strings(tmp) + tmp = append([]string{owner[k]}, tmp...) + ans = append(ans, tmp) + } + } + + return ans +} + +func dfs(graph map[string]map[string]bool, s string, tmp *[]string, vis map[string]bool) { + if _, ok := vis[s]; ok { + return + } + + vis[s] = true + *tmp = append(*tmp, s) + neighbors := graph[s] + for nei, _ := range neighbors { + dfs(graph, nei, tmp, vis) + } +} From c088d63497b0347c42e840fd024abd54322e45b5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 28 Nov 2021 22:48:28 -0800 Subject: [PATCH 235/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e72f9a4..78edb60 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,7 @@ Please give this repo a ⭐ if it inspires you. |[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| |[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| |[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| +|[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| ## Hard |LC #|Description| From 26215018ba2b9b09ce38717ebcb6b207d535c4f9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Nov 2021 06:54:48 -0800 Subject: [PATCH 236/969] Create L85.go --- Hard/L85.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Hard/L85.go diff --git a/Hard/L85.go b/Hard/L85.go new file mode 100644 index 0000000..20e24f2 --- /dev/null +++ b/Hard/L85.go @@ -0,0 +1,38 @@ +package Hard + +func maximalRectangle(matrix [][]byte) int { + if len(matrix) == 0 { + return 0 + } + + ans, m, n, height := 0, len(matrix), len(matrix[0]), make([]int, len(matrix[0])) + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if matrix[i][j] == '0' { + height[j] = 0 + continue + } + height[j]++ + for cur, pre := j-1, height[j]; cur >= 0; cur-- { + if height[cur] == 0 { + break + } + + if height[cur] < pre { + pre = height[cur] + } + + if pre*(j-cur+1) > ans { + ans = pre * (j - cur + 1) + } + } + + if height[j] > ans { + ans = height[j] + } + } + } + + return ans +} From ddb987096bb574b9122b3e1669bd9004e7b79d33 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Nov 2021 06:55:20 -0800 Subject: [PATCH 237/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 78edb60..bfbf9b9 100644 --- a/README.md +++ b/README.md @@ -265,3 +265,4 @@ Please give this repo a ⭐ if it inspires you. |[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| |[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| |[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| +|[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| From 7338821dc2310060e884a8344afd1db1a2e36b7c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Dec 2021 06:42:25 -0800 Subject: [PATCH 238/969] Create L198.go --- Medium/L198.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L198.go diff --git a/Medium/L198.go b/Medium/L198.go new file mode 100644 index 0000000..63b7b4e --- /dev/null +++ b/Medium/L198.go @@ -0,0 +1,29 @@ +package Medium + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func rob(nums []int) int { + mp := make(map[int]int) + n := len(nums) + return max(count(mp, nums, n-1), count(mp, nums, n-2)) +} + +func count(mp map[int]int, nums []int, n int) int { + if _, ok := mp[n]; ok { + return mp[n] + } + + if n < 0 { + return 0 + } + + mp[n] = nums[n] + max(count(mp, nums, n-2), count(mp, nums, n-3)) + + return mp[n] +} From d9940bf407842689bcb0dd8640bbf14a9232ba39 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Dec 2021 06:42:57 -0800 Subject: [PATCH 239/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bfbf9b9..e10681d 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ Please give this repo a ⭐ if it inspires you. |[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| |[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| |[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| +|[198](https://leetcode.com/problems/house-robber/)| House Robber| ## Hard |LC #|Description| From 2bb740e012d1397e92da04341fb52df3a4f2a7e4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Dec 2021 17:06:57 -0800 Subject: [PATCH 240/969] Create L328.go --- Medium/L328.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L328.go diff --git a/Medium/L328.go b/Medium/L328.go new file mode 100644 index 0000000..90dd494 --- /dev/null +++ b/Medium/L328.go @@ -0,0 +1,19 @@ +package Medium + +func oddEvenList(head *ListNode) *ListNode { + if head == nil { + return head + } + + odd, even, evenHead := head, head.Next, head.Next + + for even != nil && even.Next != nil { + odd.Next = odd.Next.Next + even.Next = even.Next.Next + odd = odd.Next + even = even.Next + } + odd.Next = evenHead + + return head +} From 594506a697b42e352b0b5d4dbf5df1f21003720c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Dec 2021 17:07:29 -0800 Subject: [PATCH 241/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e10681d..4d0ffd8 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,7 @@ Please give this repo a ⭐ if it inspires you. |[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| |[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| |[198](https://leetcode.com/problems/house-robber/)| House Robber| +|[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| ## Hard |LC #|Description| From 44c5f0b5269b4ae7617b5c4f1d7fae894ca25cf2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 2 Dec 2021 20:17:02 -0800 Subject: [PATCH 242/969] Create L152.go --- Medium/L152.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L152.go diff --git a/Medium/L152.go b/Medium/L152.go new file mode 100644 index 0000000..28903ef --- /dev/null +++ b/Medium/L152.go @@ -0,0 +1,34 @@ +package Medium + +func maxProduct(nums []int) int { + if len(nums) == 0 { + return 0 + } + + maxi, mini, res := nums[0], nums[0], nums[0] + for i := 1; i < len(nums); i++ { + tmp := maxi + maxi = max(max(maxi*nums[i], mini*nums[i]), nums[i]) + mini = min(min(tmp*nums[i], mini*nums[i]), nums[i]) + + res = max(maxi, res) + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From a9f451f2852a51260afab9015ca3273686e86a8b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 2 Dec 2021 20:17:35 -0800 Subject: [PATCH 243/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4d0ffd8..73a2ee1 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,7 @@ Please give this repo a ⭐ if it inspires you. |[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| |[198](https://leetcode.com/problems/house-robber/)| House Robber| |[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| +|[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| ## Hard |LC #|Description| From ffc299d63fd98919ad90d4d704f8abe0f4e60228 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 4 Dec 2021 06:58:29 -0800 Subject: [PATCH 244/969] Create L1032.go --- Hard/L1032.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Hard/L1032.go diff --git a/Hard/L1032.go b/Hard/L1032.go new file mode 100644 index 0000000..1c1dbe2 --- /dev/null +++ b/Hard/L1032.go @@ -0,0 +1,66 @@ +package Hard + +type TrieNode struct { + children map[byte]*TrieNode + word bool +} + +func NewTrieNode() *TrieNode { + return &TrieNode{ + children: make(map[byte]*TrieNode), + word: false, + } +} + +type StreamChecker struct { + trie *TrieNode + stream []byte +} + +func Constructor(words []string) StreamChecker { + sc := StreamChecker{ + trie: NewTrieNode(), + stream: make([]byte, 0), + } + + for _, w := range words { + node := sc.trie + reversedWord := reverse(w) + for _, c := range reversedWord { + if _, ok := node.children[c]; !ok { + node.children[c] = NewTrieNode() + } + node = node.children[c] + } + node.word = true + } + + return sc +} + +func (this *StreamChecker) Query(letter byte) bool { + this.stream = append([]byte{letter}, this.stream...) + + node := this.trie + for _, c := range this.stream { + if node.word { + return true + } + if _, ok := node.children[c]; !ok { + return false + } + node = node.children[c] + } + + return node.word +} + +func reverse(s string) []byte { + ret := []byte(s) + + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + ret[i], ret[j] = ret[j], ret[i] + } + + return ret +} From 9fc57f05972440a6699484f54cffcf06b149b3b4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 4 Dec 2021 06:59:04 -0800 Subject: [PATCH 245/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 73a2ee1..711fb73 100644 --- a/README.md +++ b/README.md @@ -269,3 +269,4 @@ Please give this repo a ⭐ if it inspires you. |[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| |[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| |[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| +|[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| From 9390b9b2c233b725c96a54a474fe2ba5fa5a8465 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Dec 2021 06:24:36 -0800 Subject: [PATCH 246/969] Create L1217.go --- Easy/L1217.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L1217.go diff --git a/Easy/L1217.go b/Easy/L1217.go new file mode 100644 index 0000000..4ed5158 --- /dev/null +++ b/Easy/L1217.go @@ -0,0 +1,19 @@ +package Easy + +func minCostToMoveChips(position []int) int { + odd, even := 0, 0 + + for _, p := range position { + if p%2 == 0 { + even++ + } else { + odd++ + } + } + + if even < odd { + return even + } + + return odd +} From 008c76b517a235460adeb4ee59325230fc1c7f15 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Dec 2021 06:25:13 -0800 Subject: [PATCH 247/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 711fb73..aa4a0a4 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Please give this repo a ⭐ if it inspires you. |[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| |[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| |[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| +|[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| ## Medium |LC #|Description| From 814a3699a94aae12f216163b08f13dfc4dde6517 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Dec 2021 16:38:02 -0800 Subject: [PATCH 248/969] Create L563.go --- Easy/L563.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L563.go diff --git a/Easy/L563.go b/Easy/L563.go new file mode 100644 index 0000000..7171015 --- /dev/null +++ b/Easy/L563.go @@ -0,0 +1,28 @@ +package Easy + +func findTilt(root *TreeNode) int { + res := 0 + + postOrder(root, &res) + return res +} + +func postOrder(root *TreeNode, res *int) int { + if root == nil { + return 0 + } + + left, right := postOrder(root.Left, res), postOrder(root.Right, res) + + *res += abs(left - right) + + return left + right + root.Val +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From ff6c96923479a2446893bbcc09895a86a5743e37 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Dec 2021 16:38:33 -0800 Subject: [PATCH 249/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aa4a0a4..046f511 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ Please give this repo a ⭐ if it inspires you. |[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| |[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| |[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| +|[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| ## Medium |LC #|Description| From 2cac3f60dc451ea077c67b6e0e91ce6d10cf29b1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 9 Dec 2021 14:12:59 -0800 Subject: [PATCH 250/969] Create L1306.go --- Medium/L1306.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/L1306.go diff --git a/Medium/L1306.go b/Medium/L1306.go new file mode 100644 index 0000000..ed53b2a --- /dev/null +++ b/Medium/L1306.go @@ -0,0 +1,22 @@ +package Medium + +func canReach(arr []int, start int) bool { + return solve(arr, start, make(map[int]bool)) +} + +func solve(arr []int, pos int, seen map[int]bool) bool { + if pos < 0 || pos >= len(arr) { + return false + } + + if _, ok := seen[pos]; ok { + return false + } + + if arr[pos] == 0 { + return true + } + + seen[pos] = true + return solve(arr, pos+arr[pos], seen) || solve(arr, pos-arr[pos], seen) +} From 9bd20062983b0a7d9cf21bfa9f794f12acf7d9ad Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 9 Dec 2021 14:13:34 -0800 Subject: [PATCH 251/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 046f511..38537ec 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,7 @@ Please give this repo a ⭐ if it inspires you. |[198](https://leetcode.com/problems/house-robber/)| House Robber| |[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| |[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| +|[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| ## Hard |LC #|Description| From 8544d2d58e51be50928774d8e23f3a45b513fcd8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Dec 2021 06:30:12 -0800 Subject: [PATCH 252/969] Create L790.go --- Medium/L790.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Medium/L790.go diff --git a/Medium/L790.go b/Medium/L790.go new file mode 100644 index 0000000..1c7e23b --- /dev/null +++ b/Medium/L790.go @@ -0,0 +1,43 @@ +package Medium + +func numTilings(n int) int { + return solve(n, 2, make(map[string]int)) +} + +func solve(n, colState int, memo map[string]int) int { + mod, ways := 1000000007, 0 + if n < 0 { + return 0 + } + + if n == 0 { + if colState == 2 { + return 1 + } else { + return 0 + } + } + + key := fmt.Sprintf("%d|%d", n, colState) + if val, ok := memo[key]; ok { + return val + } + + switch colState { + case 0: + ways += solve(n-2, 2, memo) + ways += solve(n-1, 1, memo) + case 1: + ways += solve(n-2, 2, memo) + ways += solve(n-1, 0, memo) + case 2: + ways += solve(n-1, 0, memo) + ways += solve(n-1, 1, memo) + ways += solve(n-1, 2, memo) + ways += solve(n-2, 2, memo) + } + + ways %= mod + memo[key] = ways + return ways +} From 0d3dfed5a1a45ea94c331d1607e234914e9ca66e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Dec 2021 06:30:48 -0800 Subject: [PATCH 253/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 38537ec..f843a93 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,7 @@ Please give this repo a ⭐ if it inspires you. |[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| |[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| |[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| +|[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| ## Hard |LC #|Description| From 0295c2a7c01a2519f5b6a38807a66907eb5d70f8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Dec 2021 19:04:56 -0800 Subject: [PATCH 254/969] Create L416.go --- Medium/L416.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L416.go diff --git a/Medium/L416.go b/Medium/L416.go new file mode 100644 index 0000000..1c0031b --- /dev/null +++ b/Medium/L416.go @@ -0,0 +1,39 @@ +package Medium + +func canPartition(nums []int) bool { + sum, n := 0, len(nums) + + for _, itm := range nums { + sum += itm + } + + if sum%2 != 0 { + return false + } + + sum /= 2 + dp, visited := make([][]bool, sum+1), make([][]bool, sum+1) + for i := 0; i < sum+1; i++ { + dp[i] = make([]bool, n) + } + for i := 0; i < sum+1; i++ { + visited[i] = make([]bool, n) + } + return solve(nums, dp, visited, sum, n-1) +} + +func solve(nums []int, dp, visited [][]bool, sum, index int) bool { + if index < 0 || sum < 0 { + return false + } + if sum == 0 { + dp[sum][index] = true + return dp[sum][index] + } + if visited[sum][index] { + return dp[sum][index] + } + visited[sum][index] = true + dp[sum][index] = solve(nums, dp, visited, sum, index-1) || solve(nums, dp, visited, sum-nums[index], index-1) + return dp[sum][index] +} From d0a43b49d0e42de12b92c59575a4a54e7d17a875 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Dec 2021 19:05:28 -0800 Subject: [PATCH 255/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f843a93..1810b54 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,7 @@ Please give this repo a ⭐ if it inspires you. |[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| |[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| |[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| +|[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| ## Hard |LC #|Description| From 7251c1cc2f0d0132a1779f68f5bd8868484489de Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Dec 2021 06:47:55 -0800 Subject: [PATCH 256/969] Create L1446.go --- Easy/L1446.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L1446.go diff --git a/Easy/L1446.go b/Easy/L1446.go new file mode 100644 index 0000000..0115682 --- /dev/null +++ b/Easy/L1446.go @@ -0,0 +1,21 @@ +package Easy + +func maxPower(s string) int { + if len(s) == 0 { + return 0 + } + + cnt, ans := 1, 1 + for i := 1; i < len(s); i++ { + if s[i] == s[i-1] { + cnt++ + if cnt > ans { + ans = cnt + } + } else { + cnt = 1 + } + } + + return ans +} From 39a333d6fe8efb5e451179e9b96ed9b1c157a996 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Dec 2021 06:48:30 -0800 Subject: [PATCH 257/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1810b54..8e58bd9 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Please give this repo a ⭐ if it inspires you. |[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| |[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| |[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| +|[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| ## Medium |LC #|Description| From 3445a86f391008977500d367daa90a1b4f033e4a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Dec 2021 06:57:14 -0800 Subject: [PATCH 258/969] Create L878.go --- Hard/L878.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Hard/L878.go diff --git a/Hard/L878.go b/Hard/L878.go new file mode 100644 index 0000000..4030d3e --- /dev/null +++ b/Hard/L878.go @@ -0,0 +1,31 @@ +package Hard + +func nthMagicalNumber(n int, a int, b int) int { + A, B, mod := a, b, 1000000007 + l, r := min(a, b), n*min(a, b) + + for B > 0 { + B, A = A%B, B + } + + lcm := (a * b) / A + for l < r { + mid := l + (r-l)/2 + + if (mid/a)+(mid/b)-(mid/lcm) < n { + l = mid + 1 + } else { + r = mid + } + } + + return l % mod +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 560fa4a60250c491535d889cc7951f40d6faa52c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Dec 2021 06:57:53 -0800 Subject: [PATCH 259/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8e58bd9..293d782 100644 --- a/README.md +++ b/README.md @@ -276,3 +276,4 @@ Please give this repo a ⭐ if it inspires you. |[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| |[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| |[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| +|[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| From f931ab100e524ecfb2769e23ba008268fb332e2b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 14 Dec 2021 10:51:51 -0800 Subject: [PATCH 260/969] Create L938.go --- Easy/L938.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L938.go diff --git a/Easy/L938.go b/Easy/L938.go new file mode 100644 index 0000000..ff3fc7e --- /dev/null +++ b/Easy/L938.go @@ -0,0 +1,20 @@ +package Easy + +func rangeSumBST(root *TreeNode, low int, high int) int { + if root == nil { + return 0 + } + + sum := 0 + if root.Val > low { + sum += rangeSumBST(root.Left, low, high) + } + if root.Val < high { + sum += rangeSumBST(root.Right, low, high) + } + if root.Val >= low && root.Val <= high { + sum += root.Val + } + + return sum +} From 789702156b5aceec1158dd3240f49d1be0dc6eb6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 14 Dec 2021 10:52:21 -0800 Subject: [PATCH 261/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 293d782..d844453 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Please give this repo a ⭐ if it inspires you. |[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| |[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| |[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| +|[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| ## Medium |LC #|Description| From 9f4a231346ce411748b981a61f5dc0e17f042098 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 16 Dec 2021 11:38:03 -0800 Subject: [PATCH 262/969] Create L310.go --- Medium/L310.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L310.go diff --git a/Medium/L310.go b/Medium/L310.go new file mode 100644 index 0000000..376c5ce --- /dev/null +++ b/Medium/L310.go @@ -0,0 +1,40 @@ +package Medium + +func findMinHeightTrees(n int, edges [][]int) []int { + if n == 1 { + return []int{0} + } + + adj := make([]map[int]bool, n) + for i := 0; i < n; i++ { + adj[i] = make(map[int]bool) + } + for _, e := range edges { + adj[e[0]][e[1]] = true + adj[e[1]][e[0]] = true + } + + var leaves []int + for i := 0; i < n; i++ { + if len(adj[i]) == 1 { + leaves = append(leaves, i) + } + } + + for n > 2 { + n -= len(leaves) + var newLeaves []int + for _, i := range leaves { + for v, _ := range adj[i] { + delete(adj[v], i) + if len(adj[v]) == 1 { + newLeaves = append(newLeaves, v) + } + break + } + } + leaves = newLeaves + } + + return leaves +} From 4bb66db74f3d87bcab857cb4dd98c9f8d0943651 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 16 Dec 2021 11:38:38 -0800 Subject: [PATCH 263/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d844453..52cb0be 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,7 @@ Please give this repo a ⭐ if it inspires you. |[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| |[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| |[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| +|[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| ## Hard |LC #|Description| From 4fefc31a8a31545f5936324d6c6795cabb1eb19b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Dec 2021 16:27:32 -0800 Subject: [PATCH 264/969] Create L221.go --- Medium/L221.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Medium/L221.go diff --git a/Medium/L221.go b/Medium/L221.go new file mode 100644 index 0000000..8a6c74f --- /dev/null +++ b/Medium/L221.go @@ -0,0 +1,59 @@ +package Medium + +func maximalSquare(matrix [][]byte) int { + if len(matrix) == 0 { + return 0 + } + + m, n, ans := len(matrix), len(matrix[0]), math.MinInt64 + dp := make([][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([]int, n) + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if matrix[i][j] == '1' { + ans = max(ans, dfs(matrix, i, j, dp)) + } + } + } + + return ans * ans +} + +func isValid(matrix [][]byte, x, y int) bool { + m, n := len(matrix), len(matrix[0]) + + return x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] == '1' +} + +func dfs(matrix [][]byte, i, j int, dp [][]int) int { + if !isValid(matrix, i, j) { + return 0 + } + + if dp[i][j] != 0 { + return dp[i][j] + } + + down, right, diag := dfs(matrix, i+1, j, dp), dfs(matrix, i, j+1, dp), dfs(matrix, i+1, j+1, dp) + dp[i][j] = 1 + min(min(down, right), diag) + return dp[i][j] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 028b3de08e95406b7b1f824b087c92f59b5dc60e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Dec 2021 16:28:05 -0800 Subject: [PATCH 265/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 52cb0be..d4389e0 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,7 @@ Please give this repo a ⭐ if it inspires you. |[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| |[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| |[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| +|[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| ## Hard |LC #|Description| From f1943e1fc369e54732171deb0ded894484f7aa8f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Dec 2021 17:10:27 -0800 Subject: [PATCH 266/969] Create L902.go --- Hard/L902.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Hard/L902.go diff --git a/Hard/L902.go b/Hard/L902.go new file mode 100644 index 0000000..6bb79bb --- /dev/null +++ b/Hard/L902.go @@ -0,0 +1,41 @@ +package Hard + +func atMostNGivenDigitSet(digits []string, n int) int { + if len(digits) == 0 || n == 0 { + return 0 + } + + nStr := fmt.Sprintf("%d", n) + numDigitsN, total, numDigits := len(nStr), 0, len(digits) + + for i := 1; i < numDigitsN; i++ { + total += IntPow(numDigits, i) + } + + for i := 0; i < numDigitsN; i++ { + hasSameNum := false + for _, d := range digits { + if d[0] < nStr[i] { + total += IntPow(numDigits, numDigitsN-i-1) + } else if d[0] == nStr[i] { + hasSameNum = true + } + } + if !hasSameNum { + return total + } + } + + return total + 1 +} + +func IntPow(n, m int) int { + if m == 0 { + return 1 + } + result := n + for i := 2; i <= m; i++ { + result *= n + } + return result +} From 8104e4be3a1e6392024ae5b7aa2f8fabb3bccf7e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 Dec 2021 17:10:58 -0800 Subject: [PATCH 267/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d4389e0..7650c0a 100644 --- a/README.md +++ b/README.md @@ -280,3 +280,4 @@ Please give this repo a ⭐ if it inspires you. |[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| |[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| |[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| +|[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| From 1479895835b07e119a8d559316e4777bcdd95a89 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 19 Dec 2021 15:21:42 -0800 Subject: [PATCH 268/969] Create L394.go --- Medium/L394.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L394.go diff --git a/Medium/L394.go b/Medium/L394.go new file mode 100644 index 0000000..097e592 --- /dev/null +++ b/Medium/L394.go @@ -0,0 +1,41 @@ +package Medium + +func decodeString(s string) string { + index := 0 + return solve(s, &index) +} + +func solve(s string, index *int) string { + var sb strings.Builder + var num string + + for i := *index; i < len(s); i++ { + if s[i] != '[' && s[i] != ']' && !isDigit(s[i]) { + sb.WriteByte(s[i]) + } else if isDigit(s[i]) { + num += string(rune(s[i])) + } else if s[i] == '[' { + *index = i + 1 + next := solve(s, index) + for n := intVal(num); n > 0; n-- { + sb.WriteString(next) + } + num, i = "", *index + } else if s[i] == ']' { + *index = i + return sb.String() + } + } + + return sb.String() +} + +func isDigit(c byte) bool { + return c >= '0' && c <= '9' +} + +func intVal(num string) int { + i, _ := strconv.Atoi(num) + + return i +} From eb2527ef7c37658f01cc8a86a4e4d55b3c77cb8e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 19 Dec 2021 15:22:12 -0800 Subject: [PATCH 269/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7650c0a..1ae9296 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,7 @@ Please give this repo a ⭐ if it inspires you. |[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| |[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| |[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| +|[394](https://leetcode.com/problems/decode-string/)| Decode String| ## Hard |LC #|Description| From 89ea8558545feec5099e9e82e8e4472edc5314a5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Dec 2021 07:14:55 -0800 Subject: [PATCH 270/969] Create L1200.go --- Easy/L1200.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Easy/L1200.go diff --git a/Easy/L1200.go b/Easy/L1200.go new file mode 100644 index 0000000..eb2f1dc --- /dev/null +++ b/Easy/L1200.go @@ -0,0 +1,24 @@ +package Easy + +func minimumAbsDifference(arr []int) [][]int { + var res [][]int + + if len(arr) == 0 { + return res + } + + sort.Ints(arr) + min := math.MaxInt64 + for i := 1; i < len(arr); i++ { + diff := arr[i] - arr[i-1] + if diff <= min { + if diff < min { + min = diff + res = nil + } + res = append(res, []int{arr[i-1], arr[i]}) + } + } + + return res +} From 089a053fde7ec1af04a7e443d8f6bd50e25aed75 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Dec 2021 07:15:34 -0800 Subject: [PATCH 271/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1ae9296..36735da 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ Please give this repo a ⭐ if it inspires you. |[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| |[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| |[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| +|[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| ## Medium |LC #|Description| From 0b7a8de34f8e9dda5ff5cb8a7466c8512652c416 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Dec 2021 16:22:30 -0800 Subject: [PATCH 272/969] Create L231.go --- Medium/L231.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Medium/L231.go diff --git a/Medium/L231.go b/Medium/L231.go new file mode 100644 index 0000000..b09f855 --- /dev/null +++ b/Medium/L231.go @@ -0,0 +1,5 @@ +package Easy + +func isPowerOfTwo(n int) bool { + return n > 0 && (n&(n-1) == 0) +} From c7fc42c5b539495500f039105f14303e0efd9af1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Dec 2021 16:23:04 -0800 Subject: [PATCH 273/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 36735da..280a9ef 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Please give this repo a ⭐ if it inspires you. |[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| |[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| |[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| +|[231](https://leetcode.com/problems/power-of-two/)| Power of Two| ## Medium |LC #|Description| From ec573a89139ab19819fc0a7eeea809120f0afe7b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Dec 2021 12:28:07 -0800 Subject: [PATCH 274/969] Create L143.go --- Medium/L143.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L143.go diff --git a/Medium/L143.go b/Medium/L143.go new file mode 100644 index 0000000..c027972 --- /dev/null +++ b/Medium/L143.go @@ -0,0 +1,29 @@ +package Medium + +func reorderList(head *ListNode) { + if head == nil || head.Next == nil { + return + } + + p1, p2 := head, head + for p2.Next != nil && p2.Next.Next != nil { + p1, p2 = p1.Next, p2.Next.Next + } + + preMiddle, preCurrent := p1, p1.Next + for preCurrent.Next != nil { + current := preCurrent.Next + preCurrent.Next = current.Next + current.Next = preMiddle.Next + preMiddle.Next = current + } + + p1, p2 = head, preMiddle.Next + for p1 != preMiddle { + preMiddle.Next = p2.Next + p2.Next = p1.Next + p1.Next = p2 + p1 = p2.Next + p2 = preMiddle.Next + } +} From 9645420bf59f3b0c0e9dc6af1799bbd8afe985b3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Dec 2021 12:28:38 -0800 Subject: [PATCH 275/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 280a9ef..3003137 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,7 @@ Please give this repo a ⭐ if it inspires you. |[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| |[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| |[394](https://leetcode.com/problems/decode-string/)| Decode String| +|[143](https://leetcode.com/problems/reorder-list/)| Reorder List| ## Hard |LC #|Description| From 96df1e66d810af31ed7c3281c19ea5146c96518d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Dec 2021 07:12:09 -0800 Subject: [PATCH 276/969] Create L210.go --- Medium/L210.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/L210.go diff --git a/Medium/L210.go b/Medium/L210.go new file mode 100644 index 0000000..b7b1ebd --- /dev/null +++ b/Medium/L210.go @@ -0,0 +1,55 @@ +package Medium + +func findOrder(numCourses int, prerequisites [][]int) []int { + if numCourses == 0 { + return []int{} + } + + ret, lst, indDegree, vis, cnt, idx := make([]int, numCourses), make([][]int, numCourses), make([]int, numCourses), make([]bool, numCourses), 0, 0 + if len(prerequisites) == 0 { + for i := 0; i < numCourses; i++ { + ret[i] = i + } + return ret + } + + for i := 0; i < numCourses; i++ { + lst[i] = make([]int, 0) + } + for _, p := range prerequisites { + lst[p[1]] = append(lst[p[1]], p[0]) + indDegree[p[0]]++ + } + + for i := 0; i < numCourses; i++ { + if indDegree[i] == 0 && !vis[i] { + dfs(lst, ret, indDegree, vis, i, &idx, &cnt) + } + } + + if numCourses == cnt { + return ret + } + + return []int{} +} + +func dfs(lst [][]int, ret, inDegree []int, vis []bool, c int, idx, cnt *int) { + if vis[c] { + return + } + + vis[c] = true + if inDegree[c] == 0 { + ret[(*idx)] = c + *idx++ + *cnt++ + } + + for _, nei := range lst[c] { + inDegree[nei]-- + if inDegree[nei] == 0 { + dfs(lst, ret, inDegree, vis, nei, idx, cnt) + } + } +} From 8f617d264439fa8a34ff8deb8ba23b04f9db017c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Dec 2021 07:12:42 -0800 Subject: [PATCH 277/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3003137..e1eee19 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,7 @@ Please give this repo a ⭐ if it inspires you. |[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| |[394](https://leetcode.com/problems/decode-string/)| Decode String| |[143](https://leetcode.com/problems/reorder-list/)| Reorder List| +|[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| ## Hard |LC #|Description| From bc8b1c57cdc92207fe3c8886fc195570d201189c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Dec 2021 17:31:18 -0800 Subject: [PATCH 278/969] Create L56.go --- Medium/L56.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L56.go diff --git a/Medium/L56.go b/Medium/L56.go new file mode 100644 index 0000000..51f0a7b --- /dev/null +++ b/Medium/L56.go @@ -0,0 +1,33 @@ +package Medium + +func merge(intervals [][]int) [][]int { + if len(intervals) == 0 { + return [][]int{} + } + + var ans [][]int + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][0] < intervals[j][0] + }) + + start, end := intervals[0][0], intervals[0][1] + for _, i := range intervals { + if i[0] <= end { + end = max(end, i[1]) + } else { + ans = append(ans, []int{start, end}) + start, end = i[0], i[1] + } + } + ans = append(ans, []int{start, end}) + + return ans +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From b5972618afd300debd2f2549a7c185adc2f1e897 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Dec 2021 17:31:57 -0800 Subject: [PATCH 279/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e1eee19..5afccd5 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ Please give this repo a ⭐ if it inspires you. |[394](https://leetcode.com/problems/decode-string/)| Decode String| |[143](https://leetcode.com/problems/reorder-list/)| Reorder List| |[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| +|[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| ## Hard |LC #|Description| From a444d22d61fdd09f7bf20204d5d78c5b5f0ddfaa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 25 Dec 2021 16:21:29 -0800 Subject: [PATCH 280/969] Create L227.go --- Medium/L227.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Medium/L227.go diff --git a/Medium/L227.go b/Medium/L227.go new file mode 100644 index 0000000..827be0d --- /dev/null +++ b/Medium/L227.go @@ -0,0 +1,112 @@ +package Medium + +type Deque struct { + operands []int + operators []byte +} + +func NewDeque() *Deque { + return &Deque{ + operands: make([]int, 0), + operators: make([]byte, 0), + } +} + +func (d *Deque) PushOperand(val int) { + d.operands = append(d.operands, val) +} + +func (d *Deque) PopOperand() int { + val := d.operands[len(d.operands)-1] + d.operands = d.operands[:len(d.operands)-1] + + return val +} + +func (d *Deque) PushOperator(ch byte) { + d.operators = append(d.operators, ch) +} + +func (d *Deque) PopOperator() byte { + val := d.operators[len(d.operators)-1] + d.operators = d.operators[:len(d.operators)-1] + + return val +} + +func (d *Deque) PeekOperator() byte { + return d.operators[len(d.operators)-1] +} + +func (d *Deque) isOperatorEmpty() bool { + return len(d.operators) == 0 +} + +func (d *Deque) operate() int { + a, b, c := d.PopOperand(), d.PopOperand(), d.PopOperator() + + switch c { + case '+': + return a + b + case '-': + return b - a + case '*': + return a * b + case '/': + return b / a + default: + return 0 + } +} + +var precedenceMap = map[byte]int{ + '(': -1, + '+': 0, + '-': 0, + '*': 1, + '/': 1, +} + +func calculate(s string) int { + q, n := NewDeque(), len(s) + + for i := 0; i < n; i++ { + c := s[i] + if isDigit(c) { + val := int(c - '0') + for i+1 < n && isDigit(s[i+1]) { + val = val*10 + int(s[i+1]-'0') + i++ + } + q.PushOperand(val) + } else if c == ' ' { + continue + } else if c == '(' { + q.PushOperator(c) + } else if c == ')' { + for q.PeekOperator() != '(' { + q.PushOperand(q.operate()) + } + q.PopOperator() + } else { + for !q.isOperatorEmpty() && comparePrecedence(c, q.PeekOperator()) <= 0 { + q.PushOperand(q.operate()) + } + q.PushOperator(c) + } + } + + for !q.isOperatorEmpty() { + q.PushOperand(q.operate()) + } + + return q.PopOperand() +} + +func comparePrecedence(a, b byte) int { + return precedenceMap[a] - precedenceMap[b] +} + +func isDigit(c byte) bool { + return c >= '0' && c <= '9' +} From fee0526cbc32d1ce41a22f6276a1409348aed309 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 25 Dec 2021 16:21:59 -0800 Subject: [PATCH 281/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5afccd5..26e72f8 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ Please give this repo a ⭐ if it inspires you. |[143](https://leetcode.com/problems/reorder-list/)| Reorder List| |[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| |[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| +|[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| ## Hard |LC #|Description| From 76ccba562686c8e97152434b2825a4c68f967844 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 25 Dec 2021 16:46:04 -0800 Subject: [PATCH 282/969] Create L973.go --- Medium/L973.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Medium/L973.go diff --git a/Medium/L973.go b/Medium/L973.go new file mode 100644 index 0000000..90e5ad1 --- /dev/null +++ b/Medium/L973.go @@ -0,0 +1,59 @@ +package Medium + +type priorityQueue [][]int + +func NewPriorityQueue() *priorityQueue { + return &priorityQueue{} +} + +func (pq priorityQueue) Len() int { + return len(pq) +} + +func (pq priorityQueue) Less(i, j int) bool { + x, y := pq.distance(pq[i][0], pq[i][1]), pq.distance(pq[j][0], pq[j][1]) + + return x > y +} + +func (pq priorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +func (pq *priorityQueue) Push(x interface{}) { + *pq = append(*pq, x.([]int)) +} + +func (pq *priorityQueue) Pop() interface{} { + val := (*pq)[len(*pq)-1] + *pq = (*pq)[:len(*pq)-1] + + return val +} + +func (pq priorityQueue) distance(x, y int) int { + return x*x + y*y +} + +func kClosest(points [][]int, k int) [][]int { + var res [][]int + if len(points) == 0 { + return res + } + + pq := NewPriorityQueue() + heap.Init(pq) + + for _, p := range points { + heap.Push(pq, p) + if pq.Len() > k { + heap.Pop(pq) + } + } + + for pq.Len() > 0 { + res = append(res, heap.Pop(pq).([]int)) + } + + return res +} From 3638ec0a5dacc8b3c0ddf5a61cdb4dfc8625f9fb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 25 Dec 2021 16:46:40 -0800 Subject: [PATCH 283/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 26e72f8..ed75d89 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,7 @@ Please give this repo a ⭐ if it inspires you. |[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| |[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| |[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| +|[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| ## Hard |LC #|Description| From cf56eda379c235a5ab4f8b3eada01b4f2dd76a55 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Dec 2021 22:07:57 -0800 Subject: [PATCH 284/969] Create L476.go --- Easy/L476.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Easy/L476.go diff --git a/Easy/L476.go b/Easy/L476.go new file mode 100644 index 0000000..85ecfac --- /dev/null +++ b/Easy/L476.go @@ -0,0 +1,24 @@ +package Easy + +func findComplement(num int) int { + i, j := 0, 0 + + for i < num { + i += pow(j) + j++ + } + + return i - num +} + +func pow(n int) int { + if n == 0 { + return 1 + } + result := 2 + for i := 2; i <= n; i++ { + result *= 2 + } + + return result +} From 9e1e762aebc72ee15e574699d1519e73371769c3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Dec 2021 22:08:41 -0800 Subject: [PATCH 285/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ed75d89..190913a 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Please give this repo a ⭐ if it inspires you. |[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| |[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| |[231](https://leetcode.com/problems/power-of-two/)| Power of Two| +|[476](https://leetcode.com/problems/number-complement/)| Number Complement| ## Medium |LC #|Description| From fa4260f67cc99ab85aa523c6caf99b86413ba72a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Dec 2021 18:26:55 -0800 Subject: [PATCH 286/969] Create L876.go --- Easy/L876.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Easy/L876.go diff --git a/Easy/L876.go b/Easy/L876.go new file mode 100644 index 0000000..b065e03 --- /dev/null +++ b/Easy/L876.go @@ -0,0 +1,11 @@ +package Easy + +func middleNode(head *ListNode) *ListNode { + slow, fast := head, head + + for fast != nil && fast.Next != nil { + slow, fast = slow.Next, fast.Next.Next + } + + return slow +} From 8eb47d469a4b425bd1a5c9cb6f2f73740057338d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Dec 2021 18:27:37 -0800 Subject: [PATCH 287/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 190913a..37916f2 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ Please give this repo a ⭐ if it inspires you. |[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| |[231](https://leetcode.com/problems/power-of-two/)| Power of Two| |[476](https://leetcode.com/problems/number-complement/)| Number Complement| +|[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| ## Medium |LC #|Description| From 4db87457a5f63f8f3b08540ca27e1ce64bf933c6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Dec 2021 17:30:58 -0800 Subject: [PATCH 288/969] Create L116.go --- Medium/L116.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Medium/L116.go diff --git a/Medium/L116.go b/Medium/L116.go new file mode 100644 index 0000000..49b4f18 --- /dev/null +++ b/Medium/L116.go @@ -0,0 +1,52 @@ +// DFS solution +func connect(root *Node) *Node { + dfs(root, nil) + return root +} + +func dfs(curr, next *Node) { + if curr == nil { + return + } + + curr.Next = next + dfs(curr.Left, curr.Right) + + if curr.Next != nil { + dfs(curr.Right, curr.Next.Left) + } else { + dfs(curr.Right, nil) + } +} + +// BFS solution +func connect(root *Node) *Node { + if root == nil { + return root + } + + var q []*Node + q = append(q, root) + + for len(q) != 0 { + size := len(q) + for i := 0; i < size; i++ { + curr := q[0] + q = q[1:] + + if i != size-1 { + curr.Next = q[0] + } + + if curr.Left != nil { + q = append(q, curr.Left) + } + + if curr.Right != nil { + q = append(q, curr.Right) + } + } + } + + return root +} From 5aede8830da5ba48f232f3ff9debdb396fb438d6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Dec 2021 17:31:42 -0800 Subject: [PATCH 289/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 37916f2..6590b40 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,7 @@ Please give this repo a ⭐ if it inspires you. |[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| |[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| |[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| +|[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| ## Hard |LC #|Description| From d570963bb05ff75fe65a22ebf8a92c64660a1974 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Dec 2021 07:32:48 -0800 Subject: [PATCH 290/969] Create L1015.go --- Medium/L1015.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L1015.go diff --git a/Medium/L1015.go b/Medium/L1015.go new file mode 100644 index 0000000..0b12dbb --- /dev/null +++ b/Medium/L1015.go @@ -0,0 +1,34 @@ +package Medium + +// Time Complexity:O(k) +// Space Complexity:O(k) +var exists struct{} + +func smallestRepunitDivByK(k int) int { + n, ans, mp := 0, 0, make(map[int]struct{}) + + for { + ans, n = ans+1, (n*10+1)%k + if n == 0 { + return ans + } + if _, ok := mp[n]; ok { + return -1 + } + mp[n] = exists + } +} + +// Time Complexity:O(k) +// Space Complexity:O(1) +func smallestRepunitDivByK(k int) int { + n := 0 + for i := 0; i < k; i++ { + n = (n*10 + 1) % k + if n == 0 { + return i + 1 + } + } + + return -1 +} From 4dae89b5e8ccf2572cc89c423296ccebdf0a8525 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Dec 2021 07:33:27 -0800 Subject: [PATCH 291/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6590b40..98679e3 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ Please give this repo a ⭐ if it inspires you. |[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| |[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| |[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| +|[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| ## Hard |LC #|Description| From da8788dda99c0cbbbe9ae41d85a1df602bd55184 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 31 Dec 2021 07:26:24 -0800 Subject: [PATCH 292/969] Create L1026.go --- Medium/L1026.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L1026.go diff --git a/Medium/L1026.go b/Medium/L1026.go new file mode 100644 index 0000000..c508696 --- /dev/null +++ b/Medium/L1026.go @@ -0,0 +1,46 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxAncestorDiff(root *TreeNode) int { + res := 0 + if root == nil { + return res + } + dfs(root, root.Val, root.Val, &res) + + return res +} + +func dfs(root *TreeNode, minimum, maximum int, res *int) { + if root == nil { + return + } + minimum, maximum = min(root.Val, minimum), max(root.Val, maximum) + *res = max(*res, maximum - minimum) + + dfs(root.Left, minimum, maximum, res) + dfs(root.Right, minimum, maximum, res) +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 2480fe57306361150a4f1735ef26e9efb1e94d58 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 31 Dec 2021 07:27:04 -0800 Subject: [PATCH 293/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 98679e3..32460eb 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,7 @@ Please give this repo a ⭐ if it inspires you. |[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| |[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| |[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| +|[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| ## Hard |LC #|Description| From 5ac031e71966bcc7af7ef861ceeae62dd88c1330 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 2 Jan 2022 08:32:08 -0800 Subject: [PATCH 294/969] Create L1010.go --- Medium/L1010.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L1010.go diff --git a/Medium/L1010.go b/Medium/L1010.go new file mode 100644 index 0000000..95fe644 --- /dev/null +++ b/Medium/L1010.go @@ -0,0 +1,18 @@ +package Medium + +func numPairsDivisibleBy60(time []int) int { + ans, mp := 0, make(map[int]int) + + for _, t := range time { + other, reducedTime := 0, t%60 + if reducedTime != 0 { + other = 60 - reducedTime + } + if val, ok := mp[other]; ok { + ans += val + } + mp[reducedTime]++ + } + + return ans +} From c7d96f0b6a93f2a9117fb27b65896c914f021763 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 2 Jan 2022 08:32:54 -0800 Subject: [PATCH 295/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 32460eb..42cefb4 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ Please give this repo a ⭐ if it inspires you. |[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| |[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| |[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| +|[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| ## Hard |LC #|Description| From e6c569a2ce75f166b56076a2dadc3373df7e7c0e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 2 Jan 2022 16:20:44 -0800 Subject: [PATCH 296/969] Create L997.go --- Easy/L997.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Easy/L997.go diff --git a/Easy/L997.go b/Easy/L997.go new file mode 100644 index 0000000..14cf9a2 --- /dev/null +++ b/Easy/L997.go @@ -0,0 +1,26 @@ +package Easy + +var exists struct{} + +func findJudge(n int, trust [][]int) int { + mp, inDegree := make(map[int]struct{}), make([]int, n+1) + + for i := 1; i <= n; i++ { + mp[i] = exists + } + + for _, t := range trust { + delete(mp, t[0]) + inDegree[t[1]]++ + } + + if len(mp) == 1 { + for k, _ := range mp { + if inDegree[k] == n-1 { + return k + } + } + } + + return -1 +} From 67fc4d1a43a10b71087cb61a28abda36a3a521c4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 2 Jan 2022 16:21:26 -0800 Subject: [PATCH 297/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 42cefb4..d3800e4 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Please give this repo a ⭐ if it inspires you. |[231](https://leetcode.com/problems/power-of-two/)| Power of Two| |[476](https://leetcode.com/problems/number-complement/)| Number Complement| |[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| +|[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| ## Medium |LC #|Description| From 2aeb5207d05015cbd7e4bd4183d83140a5f1734f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 3 Jan 2022 16:48:21 -0800 Subject: [PATCH 298/969] Create L1009.go --- Easy/L1009.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L1009.go diff --git a/Easy/L1009.go b/Easy/L1009.go new file mode 100644 index 0000000..48a5c56 --- /dev/null +++ b/Easy/L1009.go @@ -0,0 +1,21 @@ +package Easy + +func bitwiseComplement(n int) int { + if n == 0 { + return 1 + } + res, ind := 0, 0 + + for n > 0 { + digit := n % 2 + flippedDigit := 0 + if digit != 1 { + flippedDigit = 1 + } + res += flippedDigit << ind + n /= 2 + ind++ + } + + return res +} From b71abbf708f31a1b61708455151cc583eaa16333 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 3 Jan 2022 16:48:56 -0800 Subject: [PATCH 299/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d3800e4..264487e 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Please give this repo a ⭐ if it inspires you. |[476](https://leetcode.com/problems/number-complement/)| Number Complement| |[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| |[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| +|[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| ## Medium |LC #|Description| From 441e130b5d6956ac3865c116e4ad3ba8f937844e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 5 Jan 2022 15:34:06 -0800 Subject: [PATCH 300/969] Create L131.go --- Medium/L131.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L131.go diff --git a/Medium/L131.go b/Medium/L131.go new file mode 100644 index 0000000..ee69bc5 --- /dev/null +++ b/Medium/L131.go @@ -0,0 +1,33 @@ +func partition(s string) [][]string { + var res [][]string + dfs(s, 0, make([]string, 0), &res) + + return res +} + +func dfs(s string, pos int, list []string, res *[][]string) { + if pos == len(s) { + tmp := make([]string, len(list)) + copy(tmp, list) + *res = append(*res, tmp) + } else { + for i := pos; i < len(s); i++ { + if isPalindrome(s, pos, i) { + list = append(list, s[pos:i+1]) + dfs(s, i+1, list, res) + list = list[:len(list)-1] + } + } + } +} + +func isPalindrome(s string, low, high int) bool { + for low < high { + if s[low] != s[high] { + return false + } + low, high = low+1, high-1 + } + + return true +} From de6c725cef6c4fa77b53d288a0f1c4840f288b49 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 5 Jan 2022 15:34:46 -0800 Subject: [PATCH 301/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 264487e..66db224 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,7 @@ Please give this repo a ⭐ if it inspires you. |[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| |[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| |[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| +|[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| ## Hard |LC #|Description| From a0dd6e7cf0d86c921cf9d60749d9d5648dc26923 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 7 Jan 2022 06:35:34 -0800 Subject: [PATCH 302/969] Create L382.go --- Medium/L382.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Medium/L382.go diff --git a/Medium/L382.go b/Medium/L382.go new file mode 100644 index 0000000..4a085b0 --- /dev/null +++ b/Medium/L382.go @@ -0,0 +1,23 @@ +package Medium + +type Solution struct { + head *ListNode +} + +func Constructor(head *ListNode) Solution { + return Solution{head: head} +} + +func (this *Solution) GetRandom() int { + cnt, node, candidate := 0, this.head, this.head + + for node != nil { + cnt++ + if rand.Intn(cnt) == 0 { + candidate = node + } + node = node.Next + } + + return candidate.Val +} From 37096e15e70845353c4568625aa864134926c896 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 7 Jan 2022 06:36:11 -0800 Subject: [PATCH 303/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 66db224..34799e8 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Please give this repo a ⭐ if it inspires you. |[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| |[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| |[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| +|[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| ## Medium |LC #|Description| From bed7e9a6e55bd4cefff63d0243a3286d4220d2c4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 7 Jan 2022 06:36:36 -0800 Subject: [PATCH 304/969] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 34799e8..e287885 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,6 @@ Please give this repo a ⭐ if it inspires you. |[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| |[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| |[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| -|[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| ## Medium |LC #|Description| @@ -257,6 +256,7 @@ Please give this repo a ⭐ if it inspires you. |[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| |[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| |[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| +|[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| ## Hard |LC #|Description| From 02c46eb34762d200096931184ae5367a5f62f59f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 7 Jan 2022 17:04:36 -0800 Subject: [PATCH 305/969] Create L1463.go --- Hard/L1463.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Hard/L1463.go diff --git a/Hard/L1463.go b/Hard/L1463.go new file mode 100644 index 0000000..a16a5e2 --- /dev/null +++ b/Hard/L1463.go @@ -0,0 +1,51 @@ +package Hard + +func cherryPickup(grid [][]int) int { + m, n := len(grid), len(grid[0]) + dp := make([][][]int, m) + + for i := 0; i < m; i++ { + dp[i] = make([][]int, n) + for j := 0; j < n; j++ { + dp[i][j] = make([]int, n) + for k := 0; k < n; k++ { + dp[i][j][k] = -1 + } + } + } + + return dfs(grid, m, n, 0, 0, n-1, &dp) +} + +func dfs(grid [][]int, m, n, r, c1, c2 int, dp *[][][]int) int { + if r == m { + return 0 + } + + if (*dp)[r][c1][c2] != -1 { + return (*dp)[r][c1][c2] + } + + ans := 0 + for i := -1; i <= 1; i++ { + for j := -1; j <= 1; j++ { + nc1, nc2 := c1+i, c2+j + if nc1 >= 0 && nc1 < n && nc2 >= 0 && nc2 < n { + val := dfs(grid, m, n, r+1, nc1, nc2, dp) + if val > ans { + ans = val + } + } + } + } + + cherries := 0 + if c1 == c2 { + cherries = grid[r][c1] + } else { + cherries = grid[r][c1] + grid[r][c2] + } + + (*dp)[r][c1][c2] = ans + cherries + return (*dp)[r][c1][c2] +} From 3009d7491dec6085f3bc261c95867886211538cc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 7 Jan 2022 17:05:23 -0800 Subject: [PATCH 306/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e287885..07b3e88 100644 --- a/README.md +++ b/README.md @@ -299,3 +299,4 @@ Please give this repo a ⭐ if it inspires you. |[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| |[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| |[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| +|[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| From 1700974dccb971d440541c578c6e6cc17b763dc8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 9 Jan 2022 08:38:49 -0800 Subject: [PATCH 307/969] Create L1041.go --- Medium/L1041.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L1041.go diff --git a/Medium/L1041.go b/Medium/L1041.go new file mode 100644 index 0000000..da6ffd6 --- /dev/null +++ b/Medium/L1041.go @@ -0,0 +1,37 @@ +package Medium + +type Direction int + +const ( + DirectionUp Direction = 0 + DirectionRight Direction = 1 + DirectionDown Direction = 2 + DirectionLeft Direction = 3 + DirectionInvalid Direction = 4 +) + +func isRobotBounded(instructions string) bool { + cur, dirs := []int{0, 0}, [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + dir := DirectionUp + + for _, c := range instructions { + if c == 'G' { + cur[0] += dirs[dir][0] + cur[1] += dirs[dir][1] + } else if c == 'L' { + dir = (dir + 3) % 4 + } else { + dir = (dir + 1) % 4 + } + } + + if cur[0] == 0 && cur[1] == 0 { + return true + } + + if dir == 0 && !(cur[0] == 0 && cur[1] == 0) { + return false + } + + return true +} From 2c62b2a39746e9c722f613753110cc7a0f78c4b5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 9 Jan 2022 08:39:26 -0800 Subject: [PATCH 308/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 07b3e88..268579e 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,7 @@ Please give this repo a ⭐ if it inspires you. |[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| |[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| |[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| +|[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| ## Hard |LC #|Description| From bca453ec43244c2116dc6fcf72c0f68e61864c32 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 10 Jan 2022 07:13:26 -0800 Subject: [PATCH 309/969] Create L67.go --- Easy/L67.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Easy/L67.go diff --git a/Easy/L67.go b/Easy/L67.go new file mode 100644 index 0000000..424bf38 --- /dev/null +++ b/Easy/L67.go @@ -0,0 +1,36 @@ +package Easy + +func addBinary(a string, b string) string { + var sb strings.Builder + i, j, carry := len(a)-1, len(b)-1, 0 + + for i >= 0 || j >= 0 { + sum := carry + if j >= 0 { + sum += int(b[j] - '0') + j-- + } + + if i >= 0 { + sum += int(a[i] - '0') + i-- + } + sb.WriteString(fmt.Sprintf("%d", sum%2)) + carry = sum / 2 + } + + if carry != 0 { + sb.WriteString(fmt.Sprintf("%d", carry)) + } + + ret := sb.String() + return reverse(ret) +} + +func reverse(s string) string { + r := []rune(s) + for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { + r[i], r[j] = r[j], r[i] + } + return string(r) +} From 3eae317c575336603972967ad8bbb363e65dd150 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 10 Jan 2022 07:14:03 -0800 Subject: [PATCH 310/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 268579e..d5776a1 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Please give this repo a ⭐ if it inspires you. |[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| |[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| |[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| +|[67](https://leetcode.com/problems/add-binary/)| Add Binary| ## Medium |LC #|Description| From 2ada6cbab2c37527f1a2ef4f20f7cde9e12b644f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jan 2022 17:43:54 -0800 Subject: [PATCH 311/969] Create L701.go --- Medium/L701.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L701.go diff --git a/Medium/L701.go b/Medium/L701.go new file mode 100644 index 0000000..c3e0e44 --- /dev/null +++ b/Medium/L701.go @@ -0,0 +1,38 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func NewTreeNode(val int) *TreeNode { + return &TreeNode{ + Val: val, + Left: nil, + Right: nil, + } +} + +func insertIntoBST(root *TreeNode, val int) *TreeNode { + if root == nil { + return NewTreeNode(val) + } + + helper(root, val) + return root +} + +func helper(root *TreeNode, val int) { + if root.Val < val && root.Right == nil { + root.Right = NewTreeNode(val) + } else if root.Val > val && root.Left == nil { + root.Left = NewTreeNode(val) + } else if root.Val > val { + helper(root.Left, val) + } else { + helper(root.Right, val) + } +} From 91320e398287c709fd1d21ea1966a066c8fd4d64 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jan 2022 17:44:26 -0800 Subject: [PATCH 312/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d5776a1..8816209 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ Please give this repo a ⭐ if it inspires you. |[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| |[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| |[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| +|[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| ## Hard |LC #|Description| From 7267eb4f66d3c4fd12c65954c6e67f717ce2c895 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 13 Jan 2022 15:06:28 -0800 Subject: [PATCH 313/969] Create L452.go --- Medium/L452.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L452.go diff --git a/Medium/L452.go b/Medium/L452.go new file mode 100644 index 0000000..569c56f --- /dev/null +++ b/Medium/L452.go @@ -0,0 +1,25 @@ +package Medium + +func findMinArrowShots(points [][]int) int { + if len(points) == 0 { + return 0 + } + + sort.Slice(points, func(i, j int) bool { + if points[i][1] < points[j][1] { + return true + } + + return false + }) + + end, res := points[0][1], 1 + for _, p := range points { + if p[0] <= end { + continue + } + res++ + end = p[1] + } + return res +} From 203b928062fe4550b101f4c6d1e7e8225f463d42 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 13 Jan 2022 15:07:07 -0800 Subject: [PATCH 314/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8816209..208e4c6 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,7 @@ Please give this repo a ⭐ if it inspires you. |[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| |[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| |[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| +|[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| ## Hard |LC #|Description| From d60c03bf77dda8414302e043486efd451c0ff698 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jan 2022 10:44:20 -0800 Subject: [PATCH 315/969] Create L8.go --- Medium/L8.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L8.go diff --git a/Medium/L8.go b/Medium/L8.go new file mode 100644 index 0000000..8c46d01 --- /dev/null +++ b/Medium/L8.go @@ -0,0 +1,46 @@ +package Medium + +func myAtoi(s string) int { + index, total, sign := 0, 0, 1 + + if len(s) == 0 { + return 0 + } + + for index < len(s) && s[index] == ' ' { + index++ + } + + if index == len(s) { + return 0 + } + + if s[index] == '+' || s[index] == '-' { + if s[index] == '+' { + sign = 1 + } else { + sign = -1 + } + index++ + } + + for index < len(s) { + digit := int(s[index] - '0') + if digit < 0 || digit > 9 { + break + } + + if math.MaxInt32/10 < total || (math.MaxInt32/10 == total && math.MaxInt32%10 < digit) { + if sign == 1 { + return math.MaxInt32 + } + + return math.MinInt32 + } + + total = total*10 + digit + index++ + } + + return total * sign +} From 854204cef64282cdcbf9f2b90360323ac6faf0df Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jan 2022 10:44:52 -0800 Subject: [PATCH 316/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 208e4c6..5a3ba1c 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ Please give this repo a ⭐ if it inspires you. |[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| |[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| |[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| +|[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| ## Hard |LC #|Description| From ccc0ad83bf4fbd735cded26553a28b0d2baf203b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jan 2022 22:24:56 -0800 Subject: [PATCH 317/969] Create L1345.go --- Hard/L1345.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Hard/L1345.go diff --git a/Hard/L1345.go b/Hard/L1345.go new file mode 100644 index 0000000..d5d6347 --- /dev/null +++ b/Hard/L1345.go @@ -0,0 +1,57 @@ +package Hard + +var exists struct{} + +func minJumps(arr []int) int { + if len(arr) <= 1 { + return 0 + } + + mp := make(map[int][]int) + for i := range arr { + if _, ok := mp[arr[i]]; !ok { + mp[arr[i]] = make([]int, 0) + } + mp[arr[i]] = append(mp[arr[i]], i) + } + visited := make(map[int]struct{}) + var q []int + q = append(q, 0) + visited[0] = exists + cnt := 0 + + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + n := q[0] + q = q[1:] + if n == len(arr)-1 { + return cnt + } + if n > 0 { + if _, ok := visited[n-1]; !ok { + visited[n-1] = exists + q = append(q, n-1) + } + } + if n < len(arr)-1 { + if _, ok := visited[n+1]; !ok { + visited[n+1] = exists + q = append(q, n+1) + } + } + if _, ok := mp[arr[n]]; ok { + for _, v := range mp[arr[n]] { + if _, present := visited[v]; !present { + visited[v] = exists + q = append(q, v) + } + } + delete(mp, arr[n]) + } + } + cnt++ + } + + return -1 +} From baaf8bcf74e10cce6efcedb66e7116147f7a2397 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jan 2022 22:25:29 -0800 Subject: [PATCH 318/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5a3ba1c..f2adec4 100644 --- a/README.md +++ b/README.md @@ -305,3 +305,4 @@ Please give this repo a ⭐ if it inspires you. |[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| |[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| |[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| +|[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| From 21c4ff377a5c3b3652692bc8a093e611eb2d11a3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 16 Jan 2022 08:47:45 -0800 Subject: [PATCH 319/969] Create L849.go --- Medium/L849.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L849.go diff --git a/Medium/L849.go b/Medium/L849.go new file mode 100644 index 0000000..5337839 --- /dev/null +++ b/Medium/L849.go @@ -0,0 +1,34 @@ +package Medium + +func maxDistToClosest(seats []int) int { + dist, j := math.MinInt32, -1 + + if seats[0] == 1 { + j = 0 + } + + for i := 1; i < len(seats); i++ { + if seats[i] == 1 { + if j == -1 { + dist = max(dist, i) + } else { + dist = max(dist, (i-j)/2) + } + j = i + } + } + + if seats[len(seats)-1] == 0 { + dist = max(dist, len(seats)-1-j) + } + + return dist +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From de74ec952e34927791927b12a82eaa32bae032d3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 16 Jan 2022 08:48:15 -0800 Subject: [PATCH 320/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2adec4..eb026ad 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,7 @@ Please give this repo a ⭐ if it inspires you. |[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| |[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| |[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| +|[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| ## Hard |LC #|Description| From c5aad4148948f83fce4130579cdfe97861626510 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 18 Jan 2022 07:21:54 -0800 Subject: [PATCH 321/969] Create L290.go --- Easy/L290.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L290.go diff --git a/Easy/L290.go b/Easy/L290.go new file mode 100644 index 0000000..8e94b1b --- /dev/null +++ b/Easy/L290.go @@ -0,0 +1,28 @@ +package Easy + +var exists struct{} + +func wordPattern(pattern string, s string) bool { + mp, st := make(map[byte]string), make(map[string]struct{}) + strs := strings.Split(s, " ") + + if len(strs) != len(pattern) { + return false + } + + for i := range strs { + if v, ok := mp[pattern[i]]; ok { + if v != strs[i] { + return false + } + } else { + if _, ok := st[strs[i]]; ok { + return false + } + st[strs[i]] = exists + mp[pattern[i]] = strs[i] + } + } + + return true +} From a88946208c34b39e89bb1846c8d6be2504f68985 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 18 Jan 2022 07:22:40 -0800 Subject: [PATCH 322/969] Create L605.go --- Easy/L605.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Easy/L605.go diff --git a/Easy/L605.go b/Easy/L605.go new file mode 100644 index 0000000..cf283e3 --- /dev/null +++ b/Easy/L605.go @@ -0,0 +1,25 @@ +package Easy + +func canPlaceFlowers(flowerbed []int, n int) bool { + cnt, res, size := 0, 0, len(flowerbed) + + for i := 0; i <= size; i++ { + if i < size && flowerbed[i] == 0 { + cnt++ + if i == 0 { + cnt++ + } + if i == size - 1 { + cnt++ + } + } else { + res += (cnt - 1) / 2 + if res >= n { + return true + } + cnt = 0 + } + } + + return false +} From 1d8308446c461333396745dcc2f335d21ec885b3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 18 Jan 2022 07:23:33 -0800 Subject: [PATCH 323/969] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index eb026ad..45c5d59 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ Please give this repo a ⭐ if it inspires you. |[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| |[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| |[67](https://leetcode.com/problems/add-binary/)| Add Binary| +|[290](https://leetcode.com/problems/word-pattern/)| Word Pattern| +|[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| ## Medium |LC #|Description| From 040e64b68ffbd4ce5b22f286b6e6fc1ac1b577a4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 19 Jan 2022 09:30:58 -0800 Subject: [PATCH 324/969] Create L142.go --- Medium/L142.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L142.go diff --git a/Medium/L142.go b/Medium/L142.go new file mode 100644 index 0000000..0c045e5 --- /dev/null +++ b/Medium/L142.go @@ -0,0 +1,18 @@ +package Medium + +func detectCycle(head *ListNode) *ListNode { + slow, fast := head, head + + for fast != nil && fast.Next != nil { + slow, fast = slow.Next, fast.Next.Next + if slow == fast { + slow = head + for slow != fast { + slow, fast = slow.Next, fast.Next + } + return slow + } + } + + return nil +} From ca5b02bbcf18e038c9bf830e2063c8a8f5276f0b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 19 Jan 2022 09:31:33 -0800 Subject: [PATCH 325/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 45c5d59..7e60fe0 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,7 @@ Please give this repo a ⭐ if it inspires you. |[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| |[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| |[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| +|[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| ## Hard |LC #|Description| From 50e55959cf5461a7b94706cc17885bf606d38234 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 20 Jan 2022 14:40:39 -0800 Subject: [PATCH 326/969] Create L875.go --- Medium/L875.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Medium/L875.go diff --git a/Medium/L875.go b/Medium/L875.go new file mode 100644 index 0000000..ab683b7 --- /dev/null +++ b/Medium/L875.go @@ -0,0 +1,43 @@ +package Medium + +func minEatingSpeed(piles []int, h int) int { + if len(piles) == 0 { + return 0 + } + min, max, sum := math.MaxInt32, math.MinInt32, 0 + for _, p := range piles { + if p < min { + min = p + } + if p > max { + max = p + } + sum += p + } + + if h == 1 { + return sum + } + + lo, high, mid := 1, 1000000000, 0 + for lo < high { + mid = lo + (high-lo)/2 + if feasible(piles, mid, h) { + high = mid + } else { + lo = mid + 1 + } + } + + return lo +} + +func feasible(arr []int, maxSpeed, h int) bool { + time := 0 + + for _, a := range arr { + time += int(math.Ceil(float64(a) / float64(maxSpeed))) + } + + return time <= h +} From 9cc51aa0a29735a684934262c46e1b31a4d4ed95 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 20 Jan 2022 14:41:15 -0800 Subject: [PATCH 327/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7e60fe0..80b9b95 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,7 @@ Please give this repo a ⭐ if it inspires you. |[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| |[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| |[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| +|[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| ## Hard |LC #|Description| From cbd144cf6e710845b8a973c6407a15f214cf3ca9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jan 2022 15:05:20 -0800 Subject: [PATCH 328/969] Create L134.go --- Medium/L134.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L134.go diff --git a/Medium/L134.go b/Medium/L134.go new file mode 100644 index 0000000..ca16a1c --- /dev/null +++ b/Medium/L134.go @@ -0,0 +1,19 @@ +package Medium + +func canCompleteCircuit(gas []int, cost []int) int { + sumGas, sumCost, start, tank := 0, 0, 0, 0 + for i := range gas { + sumGas += gas[i] + sumCost += cost[i] + tank += gas[i] - cost[i] + if tank < 0 { + start, tank = i+1, 0 + } + } + + if sumGas < sumCost { + return -1 + } + + return start +} From 7f4bde69f92779a13e423b6573d82c1a0ca6f48b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jan 2022 15:06:00 -0800 Subject: [PATCH 329/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 80b9b95..b42aaf2 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,7 @@ Please give this repo a ⭐ if it inspires you. |[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| |[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| |[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| +|[134](https://leetcode.com/problems/gas-station/)| Gas Station| ## Hard |LC #|Description| From b70cb46cdabb6775aa7b395aa9726037b5e9d173 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jan 2022 22:36:10 -0800 Subject: [PATCH 330/969] Create L1510.go --- Hard/L1510.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Hard/L1510.go diff --git a/Hard/L1510.go b/Hard/L1510.go new file mode 100644 index 0000000..72ca39f --- /dev/null +++ b/Hard/L1510.go @@ -0,0 +1,25 @@ +package Hard + +func winnerSquareGame(n int) bool { + return solve(n, make(map[int]bool)) +} + +func solve(n int, mp map[int]bool) bool { + if n == 0 { + return false + } + + if v, ok := mp[n]; ok { + return v + } + + res := false + for i := 1; i*i <= n; i++ { + if !solve(n-i*i, mp) { + res = true + break + } + } + mp[n] = res + return res +} From 04eb790e0663a00878e6cc30dab968f65c6d449c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jan 2022 22:36:45 -0800 Subject: [PATCH 331/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b42aaf2..0c95f60 100644 --- a/README.md +++ b/README.md @@ -312,3 +312,4 @@ Please give this repo a ⭐ if it inspires you. |[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| |[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| |[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| +|[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| From 6a9e3bb1a63025597c1ce53be4c6d468c3a4088f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 22 Jan 2022 21:42:12 -0800 Subject: [PATCH 332/969] Create L1291.go --- Medium/L1291.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L1291.go diff --git a/Medium/L1291.go b/Medium/L1291.go new file mode 100644 index 0000000..ebba04f --- /dev/null +++ b/Medium/L1291.go @@ -0,0 +1,27 @@ +package Medium + +func sequentialDigits(low int, high int) []int { + var q []int + for i := 1; i <= 9; i++ { + q = append(q, i) + } + + var ret []int + for len(q) > 0 { + n := q[0] + q = q[1:] + if n <= high && n >= low { + ret = append(ret, n) + } + + if n > high { + break + } + num := n % 10 + if num < 9 { + q = append(q, n*10+(num+1)) + } + } + + return ret +} From 2ea2dc0a59e52bc507bb1b0ac0e9e08a71014c2b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 22 Jan 2022 21:42:56 -0800 Subject: [PATCH 333/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0c95f60..ab00bc6 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,7 @@ Please give this repo a ⭐ if it inspires you. |[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| |[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| |[134](https://leetcode.com/problems/gas-station/)| Gas Station| +|[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| ## Hard |LC #|Description| From cdd36e581330eff0dcb3112c05dd4d0412eff35a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 24 Jan 2022 15:53:23 -0800 Subject: [PATCH 334/969] Create L520.go --- Easy/L520.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Easy/L520.go diff --git a/Easy/L520.go b/Easy/L520.go new file mode 100644 index 0000000..66c72f3 --- /dev/null +++ b/Easy/L520.go @@ -0,0 +1,23 @@ +package Easy + +func detectCapitalUse(word string) bool { + cnt := 0 + for i := 0; i < len(word); i++ { + if isUpper(word[i]) { + cnt++ + } + } + + if cnt == 0 || cnt == len(word) { + return true + } + + if cnt == 1 && isUpper(word[0]) { + return true + } + return false +} + +func isUpper(b byte) bool { + return b >= 'A' && b <= 'Z' +} From 13ec582e3c58a246daf73775feb024cdbd0f9917 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 24 Jan 2022 15:53:55 -0800 Subject: [PATCH 335/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ab00bc6..02f660b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Please give this repo a ⭐ if it inspires you. |[67](https://leetcode.com/problems/add-binary/)| Add Binary| |[290](https://leetcode.com/problems/word-pattern/)| Word Pattern| |[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| +|[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| ## Medium |LC #|Description| From 4574f8fef168dcb54ebb475540c68fe86b8037ff Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Jan 2022 06:50:35 -0800 Subject: [PATCH 336/969] Create L941.go --- Easy/L941.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L941.go diff --git a/Easy/L941.go b/Easy/L941.go new file mode 100644 index 0000000..752d31b --- /dev/null +++ b/Easy/L941.go @@ -0,0 +1,20 @@ +package Easy + +func validMountainArray(arr []int) bool { + if len(arr) < 3 { + return false + } + + start, end := 0, len(arr)-1 + for start < end { + if arr[start+1] > arr[start] { + start++ + } else if arr[end-1] > arr[end] { + end-- + } else { + break + } + } + + return start != 0 && end != len(arr)-1 && start == end +} From 11262d0eea349e5c0e273921ecd666e9e546a991 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Jan 2022 06:51:09 -0800 Subject: [PATCH 337/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 02f660b..350af7e 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Please give this repo a ⭐ if it inspires you. |[290](https://leetcode.com/problems/word-pattern/)| Word Pattern| |[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| |[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| +|[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| ## Medium |LC #|Description| From 39593fe2b7a06d685d7d96bc2a98b6a2b4a9f0b6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Jan 2022 07:04:36 -0800 Subject: [PATCH 338/969] Update L1305.go --- Medium/L1305.go | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/Medium/L1305.go b/Medium/L1305.go index 7da5c01..03ccbfc 100644 --- a/Medium/L1305.go +++ b/Medium/L1305.go @@ -9,41 +9,38 @@ package Medium * } */ func getAllElements(root1 *TreeNode, root2 *TreeNode) []int { - var res []int - if root1 == nil && root2 == nil { - return res - } + var q1, q2, ans []int - var st1, st2 []*TreeNode - pushLeft(&st1, root1) - pushLeft(&st2, root2) + inorder(root1, &q1) + inorder(root2, &q2) - for len(st1) != 0 || len(st2) != 0 { - var st *[]*TreeNode - if len(st1) == 0 { - st = &st2 - } else if len(st2) == 0 { - st = &st1 + for len(q1) > 0 || len(q2) > 0 { + if len(q2) == 0 { + ans = append(ans, q1[0]) + q1 = q1[1:] + } else if len(q1) == 0 { + ans = append(ans, q2[0]) + q2 = q2[1:] } else { - if st1[len(st1)-1].Val < st2[len(st2)-1].Val { - st = &st1 + if q1[0] < q2[0] { + ans = append(ans, q1[0]) + q1 = q1[1:] } else { - st = &st2 + ans = append(ans, q2[0]) + q2 = q2[1:] } } - - curr := (*st)[len(*st)-1] - *st = (*st)[:len(*st)-1] - res = append(res, curr.Val) - pushLeft(st, curr.Right) } - return res + return ans } -func pushLeft(st *[]*TreeNode, node *TreeNode) { - for node != nil { - *st = append(*st, node) - node = node.Left +func inorder(root *TreeNode, q *[]int) { + if root == nil { + return } + + inorder(root.Left, q) + *q = append(*q, root.Val) + inorder(root.Right, q) } From ef65d513ec6b1250ad0d72fb0497d592919026ce Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Jan 2022 07:23:24 -0800 Subject: [PATCH 339/969] Create L421.go --- Medium/L421.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Medium/L421.go diff --git a/Medium/L421.go b/Medium/L421.go new file mode 100644 index 0000000..8b6f4a1 --- /dev/null +++ b/Medium/L421.go @@ -0,0 +1,61 @@ +package Medium + +type Trie struct { + children []*Trie +} + +func NewTrie() *Trie { + return &Trie{ + children: make([]*Trie, 2), + } +} + +func findMaximumXOR(nums []int) int { + if len(nums) == 0 { + return 0 + } + + root := NewTrie() + for _, n := range nums { + curNode := root + for i := 30; i >= 0; i-- { + curBit := getIthBit(n, i) + if curNode.children[curBit^1] == nil { + curNode.children[curBit^1] = NewTrie() + } + + curNode = curNode.children[curBit^1] + } + } + + ans := math.MinInt32 + for _, n := range nums { + curNode, rst := root, 0 + for i := 30; i >= 0; i-- { + bit := getIthBit(n, i) + + if curNode.children[bit] != nil { + curNode = curNode.children[bit] + rst += 1 << i + } else { + curNode = curNode.children[bit^1] + } + } + if rst > ans { + ans = rst + } + if ans == math.MaxInt32 { + break + } + } + + return ans +} + +func getIthBit(num, i int) int { + if (num & (1 << i)) == 0 { + return 0 + } + + return 1 +} From f71846f91cfbb4408483c5bced66c4cccf195690 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Jan 2022 07:24:06 -0800 Subject: [PATCH 340/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 350af7e..4e2978b 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,7 @@ Please give this repo a ⭐ if it inspires you. |[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| |[134](https://leetcode.com/problems/gas-station/)| Gas Station| |[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| +|[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| ## Hard |LC #|Description| From 1629e125461783b8ec2f93b169a66944abb03a72 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jan 2022 06:56:51 -0800 Subject: [PATCH 341/969] Create L211.go --- Medium/L211.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/L211.go diff --git a/Medium/L211.go b/Medium/L211.go new file mode 100644 index 0000000..9053f3c --- /dev/null +++ b/Medium/L211.go @@ -0,0 +1,55 @@ +package Medium + +type Trie struct { + isWord bool + child []*Trie +} + +func NewTrie() *Trie { + return &Trie{ + child: make([]*Trie, 26), + } +} + +type WordDictionary struct { + root *Trie +} + +func Constructor() WordDictionary { + return WordDictionary{ + root: NewTrie(), + } +} + +func (this *WordDictionary) AddWord(word string) { + currNode := this.root + for i := 0; i < len(word); i++ { + if currNode.child[word[i]-'a'] == nil { + currNode.child[word[i]-'a'] = NewTrie() + } + currNode = currNode.child[word[i]-'a'] + } + currNode.isWord = true +} + +func (this *WordDictionary) Search(word string) bool { + return match(word, 0, this.root) +} + +func match(word string, idx int, root *Trie) bool { + if idx >= len(word) { + return root.isWord + } + + if word[idx] != '.' { + return root.child[word[idx]-'a'] != nil && match(word, idx+1, root.child[word[idx]-'a']) + } + + for ch := 'a'; ch <= 'z'; ch++ { + if root.child[ch-'a'] != nil && match(word, idx+1, root.child[ch-'a']) { + return true + } + } + + return false +} From d2d16cbb45952fcbf36721648c6242f31cac2d7c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jan 2022 06:57:29 -0800 Subject: [PATCH 342/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4e2978b..e04e9b3 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,7 @@ Please give this repo a ⭐ if it inspires you. |[134](https://leetcode.com/problems/gas-station/)| Gas Station| |[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| |[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| +|[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| ## Hard |LC #|Description| From 492cb2d6ef0ee27bf4410fe88002c5629c0fa239 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Feb 2022 12:51:53 -0800 Subject: [PATCH 343/969] Create L438.go --- Medium/L438.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L438.go diff --git a/Medium/L438.go b/Medium/L438.go new file mode 100644 index 0000000..c299090 --- /dev/null +++ b/Medium/L438.go @@ -0,0 +1,41 @@ +package Medium + +func findAnagrams(s string, p string) []int { + var ans []int + if len(p) > len(s) { + return ans + } + + mp := make(map[byte]int) + for i := 0; i < len(p); i++ { + mp[p[i]]++ + } + + begin, end, counter := 0, 0, len(mp) + + for end < len(s) { + if _, ok := mp[s[end]]; ok { + mp[s[end]]-- + if mp[s[end]] == 0 { + counter-- + } + } + end++ + + for counter == 0 { + if _, ok := mp[s[begin]]; ok { + mp[s[begin]]++ + if mp[s[begin]] > 0 { + counter++ + } + } + + if end-begin == len(p) { + ans = append(ans, begin) + } + begin++ + } + } + + return ans +} From 530392c9d47840f337e0d8455a939c13dc6abcf6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Feb 2022 12:52:30 -0800 Subject: [PATCH 344/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e04e9b3..8a57bc9 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,7 @@ Please give this repo a ⭐ if it inspires you. |[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| |[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| |[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| +|[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| ## Hard |LC #|Description| From ca8920c6a2291d04066fff96367db182c038b197 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Feb 2022 07:12:35 -0800 Subject: [PATCH 345/969] Create L454.go --- Medium/L454.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L454.go diff --git a/Medium/L454.go b/Medium/L454.go new file mode 100644 index 0000000..40f0feb --- /dev/null +++ b/Medium/L454.go @@ -0,0 +1,24 @@ +package Medium + +func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { + mp, n, res := make(map[int]int), len(nums1), 0 + + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + sum := nums3[i] + nums4[j] + mp[sum]++ + } + } + + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + sum := nums1[i] + nums2[j] + + if val, ok := mp[-sum]; ok { + res += val + } + } + } + + return res +} From 7a4c7a4962749816724b64bab6112f1ddb970192 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Feb 2022 07:13:07 -0800 Subject: [PATCH 346/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8a57bc9..f650d27 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,7 @@ Please give this repo a ⭐ if it inspires you. |[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| |[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| |[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| +|[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| ## Hard |LC #|Description| From 97afd5795b37c7c60d493efd3db0cbe6cad2b2d4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 4 Feb 2022 06:23:55 -0800 Subject: [PATCH 347/969] Create L525.go --- Medium/L525.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L525.go diff --git a/Medium/L525.go b/Medium/L525.go new file mode 100644 index 0000000..9d67ef6 --- /dev/null +++ b/Medium/L525.go @@ -0,0 +1,24 @@ +package Medium + +func findMaxLength(nums []int) int { + mp, sum, max := make(map[int]int), 0, 0 + mp[0] = -1 + + for i := range nums { + if nums[i] == 0 { + sum++ + } else { + sum-- + } + + if val, ok := mp[sum]; ok { + if (i - val) > max { + max = i - val + } + } else { + mp[sum] = i + } + } + + return max +} From 649134fc4a630cfc0fb91f55918257e98999e283 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 4 Feb 2022 06:24:39 -0800 Subject: [PATCH 348/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f650d27..65b78cb 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,7 @@ Please give this repo a ⭐ if it inspires you. |[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| |[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| |[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| +|[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| ## Hard |LC #|Description| From 53398632d3c6f91c6790462d6e87f0b88af97cd2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 5 Feb 2022 19:49:41 -0800 Subject: [PATCH 349/969] Create L80.go --- Medium/L80.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Medium/L80.go diff --git a/Medium/L80.go b/Medium/L80.go new file mode 100644 index 0000000..70b990c --- /dev/null +++ b/Medium/L80.go @@ -0,0 +1,13 @@ +package Medium + +func removeDuplicates(nums []int) int { + i := 0 + for _, n := range nums { + if i < 2 || n > nums[i-2] { + nums[i] = n + i++ + } + } + + return i +} From 41ac3ff5a2384f30a6abbba894a4c5fb83822f11 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 5 Feb 2022 19:50:14 -0800 Subject: [PATCH 350/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 65b78cb..3604442 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ Please give this repo a ⭐ if it inspires you. |[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| |[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| |[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| +|[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| ## Hard |LC #|Description| From 1cc35e954b97b345f59a052dd879159789ad5962 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 7 Feb 2022 07:15:17 -0800 Subject: [PATCH 351/969] Create L389.go --- Easy/L389.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L389.go diff --git a/Easy/L389.go b/Easy/L389.go new file mode 100644 index 0000000..bed066d --- /dev/null +++ b/Easy/L389.go @@ -0,0 +1,15 @@ +package Easy + +func findTheDifference(s string, t string) byte { + sumS, sumT := 0, 0 + + for i := 0; i < len(s); i++ { + sumS += int(s[i]) + } + + for i := 0; i < len(t); i++ { + sumT += int(t[i]) + } + + return byte(sumT - sumS - 'a') +} From 676291c5a2456d821f25eda2fae18ef6f94aad4a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 7 Feb 2022 07:15:52 -0800 Subject: [PATCH 352/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3604442..f385482 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ Please give this repo a ⭐ if it inspires you. |[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| |[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| |[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| +|[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| ## Medium |LC #|Description| From 3f40873712144a802c8f41235c2bbcce16b57c66 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Feb 2022 06:27:17 -0800 Subject: [PATCH 353/969] Create L258.go --- Easy/L258.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L258.go diff --git a/Easy/L258.go b/Easy/L258.go new file mode 100644 index 0000000..251963b --- /dev/null +++ b/Easy/L258.go @@ -0,0 +1,13 @@ +package Easy + +func addDigits(num int) int { + if num == 0 { + return num + } + + if num%9 == 0 { + return 9 + } + + return num % 9 +} From e917fc5c3bcd532b225f2227e929d111bd59ddc5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Feb 2022 06:27:53 -0800 Subject: [PATCH 354/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f385482..d817909 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Please give this repo a ⭐ if it inspires you. |[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| |[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| |[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| +|[258](https://leetcode.com/problems/add-digits/)| Add Digits| ## Medium |LC #|Description| From 79b6c4c4d54f99d395b5f64f8d4d1896ef831052 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Feb 2022 10:54:18 -0800 Subject: [PATCH 355/969] Create L532.go --- Medium/L532.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L532.go diff --git a/Medium/L532.go b/Medium/L532.go new file mode 100644 index 0000000..e2aea8f --- /dev/null +++ b/Medium/L532.go @@ -0,0 +1,26 @@ +package Medium + +func findPairs(nums []int, k int) int { + if len(nums) == 0 || k < 0 { + return 0 + } + + mp, cnt := make(map[int]int), 0 + for _, n := range nums { + mp[n]++ + } + + for key, val := range mp { + if k == 0 { + if val >= 2 { + cnt++ + } + } else { + if _, ok := mp[key+k]; ok { + cnt++ + } + } + } + + return cnt +} From 70db26ed191fa79bb1908bc9ee42671a59ab56f0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Feb 2022 10:54:54 -0800 Subject: [PATCH 356/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d817909..e15be18 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,7 @@ Please give this repo a ⭐ if it inspires you. |[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| |[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| |[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| +|[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| ## Hard |LC #|Description| From 88a59bc36e07a7065ed81bd8410ea342d9d7c52e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 10 Feb 2022 14:39:37 -0800 Subject: [PATCH 357/969] Create L560.go --- Medium/L560.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Medium/L560.go diff --git a/Medium/L560.go b/Medium/L560.go new file mode 100644 index 0000000..d42fc16 --- /dev/null +++ b/Medium/L560.go @@ -0,0 +1,16 @@ +package Medium + +func subarraySum(nums []int, k int) int { + sum, res, mp := 0, 0, make(map[int]int) + mp[0] = 1 + + for i := range nums { + sum += nums[i] + if val, ok := mp[sum-k]; ok { + res += val + } + mp[sum]++ + } + + return res +} From cf6194d5e5a2212b0cfe129b87b087005234c9a5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 10 Feb 2022 14:40:16 -0800 Subject: [PATCH 358/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e15be18..4a2e32d 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,7 @@ Please give this repo a ⭐ if it inspires you. |[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| |[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| |[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| +|[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| ## Hard |LC #|Description| From 94da9215a031c62a9cc8e14dfd50da9ef0a9c840 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Feb 2022 06:26:50 -0800 Subject: [PATCH 359/969] Create L567.go --- Medium/L567.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L567.go diff --git a/Medium/L567.go b/Medium/L567.go new file mode 100644 index 0000000..fdc2cff --- /dev/null +++ b/Medium/L567.go @@ -0,0 +1,41 @@ +package Medium + +func checkInclusion(s1 string, s2 string) bool { + if len(s1) > len(s2) { + return false + } + + mp := make(map[byte]int) + for i := 0; i < len(s1); i++ { + mp[s1[i]]++ + } + + counter, begin, end := len(mp), 0, 0 + for end < len(s2) { + ch := s2[end] + + if _, ok := mp[ch]; ok { + mp[ch]-- + if mp[ch] == 0 { + counter-- + } + } + end++ + + for counter == 0 { + tmp := s2[begin] + if _, ok := mp[tmp]; ok { + mp[tmp]++ + if mp[tmp] > 0 { + counter++ + } + } + if end-begin == len(s1) { + return true + } + begin++ + } + } + + return false +} From 7eebbd54956edca8dd77ae6f9f14497d6058e100 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Feb 2022 06:27:22 -0800 Subject: [PATCH 360/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4a2e32d..4385ed2 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,7 @@ Please give this repo a ⭐ if it inspires you. |[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| |[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| |[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| +|[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| ## Hard |LC #|Description| From 18608f06faa3024bb2dd416c287ca80beb84c93d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Feb 2022 06:53:36 -0800 Subject: [PATCH 361/969] Create L1108.go --- Easy/L1108.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L1108.go diff --git a/Easy/L1108.go b/Easy/L1108.go new file mode 100644 index 0000000..9ff5ab1 --- /dev/null +++ b/Easy/L1108.go @@ -0,0 +1,15 @@ +package easy + +func defangIPaddr(address string) string { + var s strings.Builder + + for i := 0; i < len(address); i++ { + if address[i] == '.' { + s.WriteString("[.]") + } else { + s.WriteByte(address[i]) + } + } + + return s.String() +} From 5428df757eb770a93ad889d1f368615d0b129fec Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Feb 2022 06:54:14 -0800 Subject: [PATCH 362/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4385ed2..0209b9e 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Please give this repo a ⭐ if it inspires you. |[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| |[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| |[258](https://leetcode.com/problems/add-digits/)| Add Digits| +|[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| ## Medium |LC #|Description| From 1f019d79833413215735f84fb154de022239f6c6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 09:34:05 -0800 Subject: [PATCH 363/969] Create L127.go --- Hard/L127.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Hard/L127.go diff --git a/Hard/L127.go b/Hard/L127.go new file mode 100644 index 0000000..2938821 --- /dev/null +++ b/Hard/L127.go @@ -0,0 +1,52 @@ +package Hard + +var exists struct{} + +func ladderLength(beginWord string, endWord string, wordList []string) int { + beginSet, endSet, visited, dict := make(map[string]struct{}), make(map[string]struct{}), make(map[string]struct{}), make(map[string]struct{}) + + for _, w := range wordList { + dict[w] = exists + } + + if _, ok := dict[endWord]; !ok { + return 0 + } + + beginSet[beginWord], endSet[endWord] = exists, exists + length := 1 + + for len(beginSet) > 0 && len(endSet) > 0 { + if len(beginSet) > len(endSet) { + beginSet, endSet = endSet, beginSet + } + + tmp := make(map[string]struct{}) + for s, _ := range beginSet { + word := []byte(s) + for i := 0; i < len(word); i++ { + orig := word[i] + for ch := 'a'; ch <= 'z'; ch++ { + if byte(ch) == orig { + continue + } + word[i] = byte(ch) + target := string(word) + if _, ok := endSet[target]; ok { + return length + 1 + } + if _, ok := visited[target]; !ok { + if _, contains := dict[target]; contains { + visited[target] = exists + tmp[target] = exists + } + } + word[i] = orig + } + } + } + length, beginSet = length+1, tmp + } + + return 0 +} From e408491f10f5b7ce62dba2d2547df2680e530130 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 09:34:41 -0800 Subject: [PATCH 364/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0209b9e..d825220 100644 --- a/README.md +++ b/README.md @@ -328,3 +328,4 @@ Please give this repo a ⭐ if it inspires you. |[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| |[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| |[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| +|[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| From 36c354569a6275f5fe08c591d4ee6a1d96795ec3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 18:40:49 -0800 Subject: [PATCH 365/969] Create L78.go --- Medium/L78.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L78.go diff --git a/Medium/L78.go b/Medium/L78.go new file mode 100644 index 0000000..9ef3b75 --- /dev/null +++ b/Medium/L78.go @@ -0,0 +1,24 @@ +package Medium + +func subsets(nums []int) [][]int { + ans := make([][]int, 0) + if len(nums) == 0 { + return ans + } + + bt(&ans, make([]int, 0), nums, 0) + + return ans +} + +func bt(ans *[][]int, tmp, nums []int, start int) { + copyTmp := make([]int, len(tmp)) + copy(copyTmp, tmp) + *ans = append(*ans, copyTmp) + + for i := start; i < len(nums); i++ { + tmp = append(tmp, nums[i]) + bt(ans, tmp, nums, i+1) + tmp = tmp[:len(tmp)-1] + } +} From e4ddbced92b2879d5d3da2d46bda059b589162e8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 18:41:20 -0800 Subject: [PATCH 366/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d825220..3bbc92d 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,7 @@ Please give this repo a ⭐ if it inspires you. |[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| |[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| |[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| +|[78](https://leetcode.com/problems/subsets/)| Subsets| ## Hard |LC #|Description| From 5bfa1173305e38a0015286341f3722d30acc3d28 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 19:13:19 -0800 Subject: [PATCH 367/969] Create L1832.go --- Easy/L1832.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L1832.go diff --git a/Easy/L1832.go b/Easy/L1832.go new file mode 100644 index 0000000..1581e59 --- /dev/null +++ b/Easy/L1832.go @@ -0,0 +1,13 @@ +package Easy + +var exists struct{} + +func checkIfPangram(sentence string) bool { + mp := make(map[byte]struct{}) + + for i := 0; i < len(sentence); i++ { + mp[sentence[i]] = exists + } + + return len(mp) == 26 +} From d98c5b3092d816c0a48a4d507a64c6521b1b9fd6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Feb 2022 19:13:55 -0800 Subject: [PATCH 368/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3bbc92d..d459678 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Please give this repo a ⭐ if it inspires you. |[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| |[258](https://leetcode.com/problems/add-digits/)| Add Digits| |[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| +|[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| ## Medium |LC #|Description| From a1db02be92a9e6313b197384094a5243eb375ff2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Feb 2022 22:24:16 -0800 Subject: [PATCH 369/969] Create L104.go --- Easy/L104.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L104.go diff --git a/Easy/L104.go b/Easy/L104.go new file mode 100644 index 0000000..a3f6560 --- /dev/null +++ b/Easy/L104.go @@ -0,0 +1,21 @@ +package Easy + +func maxDepth(root *TreeNode) int { + maxLevel := 0 + dfs(root, 1, &maxLevel) + + return maxLevel +} + +func dfs(root *TreeNode, currLevel int, maxLevel *int) { + if root == nil { + return + } + + if currLevel > *maxLevel { + *maxLevel = currLevel + } + + dfs(root.Left, currLevel+1, maxLevel) + dfs(root.Right, currLevel+1, maxLevel) +} From 81ffee6661d163fe070fb25f9e6857bbe14588a6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Feb 2022 22:24:49 -0800 Subject: [PATCH 370/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d459678..2e6779a 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Please give this repo a ⭐ if it inspires you. |[258](https://leetcode.com/problems/add-digits/)| Add Digits| |[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| |[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| +|[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| ## Medium |LC #|Description| From b9b419497f61bc1196c0ae52595dac468da164f9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Feb 2022 16:19:59 -0800 Subject: [PATCH 371/969] Create L136.go --- Easy/L136.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Easy/L136.go diff --git a/Easy/L136.go b/Easy/L136.go new file mode 100644 index 0000000..b439413 --- /dev/null +++ b/Easy/L136.go @@ -0,0 +1,11 @@ +package Easy + +func singleNumber(nums []int) int { + val := 0 + + for _, n := range nums { + val ^= n + } + + return val +} From 1433d111282396908b6604f57fc56c9107bcd534 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Feb 2022 16:20:47 -0800 Subject: [PATCH 372/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2e6779a..917e9d4 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ Please give this repo a ⭐ if it inspires you. |[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| |[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| |[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| +|[136](https://leetcode.com/problems/single-number/)| Single Number| ## Medium |LC #|Description| From a8ef57fb8529609d841b5e07c056522c18dfc1ca Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Feb 2022 16:28:52 -0800 Subject: [PATCH 373/969] Create L39.go --- Medium/L39.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L39.go diff --git a/Medium/L39.go b/Medium/L39.go new file mode 100644 index 0000000..b863c98 --- /dev/null +++ b/Medium/L39.go @@ -0,0 +1,31 @@ +package Medium + +func combinationSum(candidates []int, target int) [][]int { + var ans [][]int + + if len(candidates) == 0 { + return ans + } + + bt(&ans, make([]int, 0), candidates, 0, target) + + return ans +} + +func bt(ans *[][]int, tmp, nums []int, idx, tgt int) { + if tgt < 0 || idx > len(nums) { + return + } + + if tgt == 0 { + cpyTmp := make([]int, len(tmp)) + copy(cpyTmp, tmp) + *ans = append(*ans, cpyTmp) + } + + for i := idx; i < len(nums); i++ { + tmp = append(tmp, nums[i]) + bt(ans, tmp, nums, i, tgt-nums[i]) + tmp = tmp[:len(tmp)-1] + } +} From 96941bf00100430fef0847202fb7cdd004dd4e4e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Feb 2022 16:29:30 -0800 Subject: [PATCH 374/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 917e9d4..c25bc47 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,7 @@ Please give this repo a ⭐ if it inspires you. |[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| |[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| |[78](https://leetcode.com/problems/subsets/)| Subsets| +|[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| ## Hard |LC #|Description| From ca2f2294c18821e649b578ea9ef7401d37fcf5ab Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Feb 2022 10:02:24 -0800 Subject: [PATCH 375/969] Create L402.go --- Medium/L402.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Medium/L402.go diff --git a/Medium/L402.go b/Medium/L402.go new file mode 100644 index 0000000..4bb91b4 --- /dev/null +++ b/Medium/L402.go @@ -0,0 +1,83 @@ +package Medium + +type Stack struct { + arr []byte +} + +func NewStack() *Stack { + return &Stack{ + arr: make([]byte, 0), + } +} + +func (s *Stack) Push(c byte) { + s.arr = append(s.arr, c) +} + +func (s *Stack) Pop() byte { + if s.IsEmpty() { + return 0 + } + + ret := s.arr[len(s.arr)-1] + s.arr = s.arr[:len(s.arr)-1] + + return ret +} + +func (s *Stack) IsEmpty() bool { + return len(s.arr) <= 0 +} + +func (s *Stack) Peek() byte { + if s.IsEmpty() { + return 0 + } + + return s.arr[len(s.arr)-1] +} + +func removeKdigits(num string, k int) string { + if k == 0 { + return num + } + if len(num) == k { + return "0" + } + + st := NewStack() + index := 0 + + for index < len(num) { + for k > 0 && !st.IsEmpty() && st.Peek() > num[index] { + st.Pop() + k-- + } + st.Push(num[index]) + index = index + 1 + } + + for k > 0 { + k-- + st.Pop() + } + + var sb strings.Builder + for !st.IsEmpty() { + sb.WriteByte(st.Pop()) + } + smallest := reverse(sb.String()) + for len(smallest) > 1 && smallest[0] == '0' { + smallest = smallest[1:] + } + + return smallest +} + +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} From 5607e8989063157e2d744bbbc02c4737ebc507f1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Feb 2022 10:03:00 -0800 Subject: [PATCH 376/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c25bc47..03591fa 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ Please give this repo a ⭐ if it inspires you. |[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| |[78](https://leetcode.com/problems/subsets/)| Subsets| |[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| +|[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| ## Hard |LC #|Description| From 97a2df75a5c904d11e60995b70d0118b75d1bf50 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Feb 2022 15:51:17 -0800 Subject: [PATCH 377/969] Create L171.go --- Easy/L171.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Easy/L171.go diff --git a/Easy/L171.go b/Easy/L171.go new file mode 100644 index 0000000..1cabf7e --- /dev/null +++ b/Easy/L171.go @@ -0,0 +1,10 @@ +package Easy + +func titleToNumber(columnTitle string) int { + res := 0 + for i := 0; i < len(columnTitle); i++ { + res = res*26 + int(columnTitle[i]) - 'A' + 1 + } + + return res +} From 19c3f8299dcc89253a608251b283a5a692c53db0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Feb 2022 15:51:54 -0800 Subject: [PATCH 378/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03591fa..d8f7a38 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Please give this repo a ⭐ if it inspires you. |[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| |[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| |[136](https://leetcode.com/problems/single-number/)| Single Number| +|[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| ## Medium |LC #|Description| From 875127fb307ee135d9d329e0129259967cff13b5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Feb 2022 16:01:01 -0800 Subject: [PATCH 379/969] Create L133.go --- Medium/L133.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L133.go diff --git a/Medium/L133.go b/Medium/L133.go new file mode 100644 index 0000000..e2e8de6 --- /dev/null +++ b/Medium/L133.go @@ -0,0 +1,26 @@ +package Medium + +func cloneGraph(node *Node) *Node { + return dfs(node, make(map[int]*Node)) +} + +func dfs(node *Node, mp map[int]*Node) *Node { + if node == nil { + return nil + } + + if val, ok := mp[node.Val]; ok { + return val + } + + newNode := &Node{ + Val: node.Val, + Neighbors: make([]*Node, 0), + } + mp[newNode.Val] = newNode + for _, n := range node.Neighbors { + newNode.Neighbors = append(newNode.Neighbors, dfs(n, mp)) + } + + return newNode +} From dea161c8befe9b46f13eab47eea83de2c9208517 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Feb 2022 16:01:37 -0800 Subject: [PATCH 380/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d8f7a38..fbca3a5 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,7 @@ Please give this repo a ⭐ if it inspires you. |[78](https://leetcode.com/problems/subsets/)| Subsets| |[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| |[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| +|[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| ## Hard |LC #|Description| From 9a26b26596fe8735558593aef54801cd37c7cf15 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Feb 2022 07:24:06 -0800 Subject: [PATCH 381/969] Create L148.go --- Medium/L148.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L148.go diff --git a/Medium/L148.go b/Medium/L148.go new file mode 100644 index 0000000..54f4f6b --- /dev/null +++ b/Medium/L148.go @@ -0,0 +1,46 @@ +package Medium + +func sortList(head *ListNode) *ListNode { + if head == nil || head.Next == nil { + return head + } + + // step 1. cut the list to two halves + var prev *ListNode + slow, fast := head, head + + for fast != nil && fast.Next != nil { + prev, slow, fast = slow, slow.Next, fast.Next.Next + } + + prev.Next = nil + + // step 2. sort each half + l1, l2 := sortList(head), sortList(slow) + + // step 3. merge l1 and l2 + return merge(l1, l2) +} + +func merge(l1, l2 *ListNode) *ListNode { + l := &ListNode{} + p := l + + for l1 != nil && l2 != nil { + if l1.Val < l2.Val { + p.Next, l1 = l1, l1.Next + } else { + p.Next, l2 = l2, l2.Next + } + p = p.Next + } + + if l1 != nil { + p.Next = l1 + } + if l2 != nil { + p.Next = l2 + } + + return l.Next +} From f8aab7fd9135c3de5555fcd04c5c5bfac2508b5a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Feb 2022 07:24:45 -0800 Subject: [PATCH 382/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fbca3a5..c1d7b20 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,7 @@ Please give this repo a ⭐ if it inspires you. |[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| |[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| |[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| +|[148](https://leetcode.com/problems/sort-list/)| Sort List| ## Hard |LC #|Description| From 5a73e4911b00362fbf48c3e346c032cf3f8bc8de Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Feb 2022 22:20:53 -0800 Subject: [PATCH 383/969] Create L662.go --- Medium/L662.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L662.go diff --git a/Medium/L662.go b/Medium/L662.go new file mode 100644 index 0000000..5ab5ae8 --- /dev/null +++ b/Medium/L662.go @@ -0,0 +1,28 @@ +package Medium + +func widthOfBinaryTree(root *TreeNode) int { + maxWidth := 0 + var lst []int + dfs(root, 1, 0, &lst, &maxWidth) + + return maxWidth +} + +func dfs(root *TreeNode, id, depth int, lst *[]int, maxWidth *int) { + if root == nil { + return + } + if depth >= len(*lst) { + *lst = append(*lst, id) + } + if (id + 1 - (*lst)[depth]) > *maxWidth { + *maxWidth = id + 1 - (*lst)[depth] + } + + if root.Left != nil { + dfs(root.Left, id*2, depth+1, lst, maxWidth) + } + if root.Right != nil { + dfs(root.Right, id*2+1, depth+1, lst, maxWidth) + } +} From f12bfb3e4236135cc1ac04337d99e65ad0205649 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Feb 2022 22:21:25 -0800 Subject: [PATCH 384/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c1d7b20..35e928a 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,7 @@ Please give this repo a ⭐ if it inspires you. |[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| |[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| |[148](https://leetcode.com/problems/sort-list/)| Sort List| +|[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| ## Hard |LC #|Description| From d9881056ddb6f8adf6641e479bc3f27d0e5f3cbc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Feb 2022 09:41:17 -0800 Subject: [PATCH 385/969] Create L228.go --- Easy/L228.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L228.go diff --git a/Easy/L228.go b/Easy/L228.go new file mode 100644 index 0000000..4f7343d --- /dev/null +++ b/Easy/L228.go @@ -0,0 +1,21 @@ +package Easy + +func summaryRanges(nums []int) []string { + start, end := 0, 0 + var list []string + + for i := 0; i < len(nums); i++ { + for i < len(nums)-1 && nums[i]+1 == nums[i+1] { + end, i = end+1, i+1 + } + if start == end { + list = append(list, fmt.Sprintf("%d", nums[start])) + } else { + list = append(list, fmt.Sprintf("%d->%d", nums[start], nums[end])) + } + end++ + start = end + } + + return list +} From ef61d6941a8b4b1f65939b067c5292605601e136 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Feb 2022 09:41:44 -0800 Subject: [PATCH 386/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 35e928a..084bd3f 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ Please give this repo a ⭐ if it inspires you. |[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| |[136](https://leetcode.com/problems/single-number/)| Single Number| |[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| +|[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| ## Medium |LC #|Description| From 386094d4e811ce7e317c3f1a5f0858e20769486b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 1 Mar 2022 12:13:00 -0800 Subject: [PATCH 387/969] Create L338.go --- Easy/L338.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Easy/L338.go diff --git a/Easy/L338.go b/Easy/L338.go new file mode 100644 index 0000000..d232aee --- /dev/null +++ b/Easy/L338.go @@ -0,0 +1,24 @@ +package Easy + +func countBits(num int) []int { + arr := make([]int, num+1) + + for i := 1; i < num+1; i++ { + arr[i] = solve(i&(i-1), &arr) + 1 + } + + return arr +} + +func solve(n int, arr *[]int) int { + if n == 0 { + return n + } + + if (*arr)[n&(n-1)] != 0 { + return (*arr)[n&(n-1)] + 1 + } + (*arr)[n] = solve(n&(n-1), arr) + 1 + + return (*arr)[n] +} From 8e6c834bd4e48267314143e923164cd9cde25713 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 1 Mar 2022 12:13:42 -0800 Subject: [PATCH 388/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 084bd3f..f56f78c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ Please give this repo a ⭐ if it inspires you. |[136](https://leetcode.com/problems/single-number/)| Single Number| |[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| |[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| +|[338](https://leetcode.com/problems/counting-bits/)| Counting Bits| ## Medium |LC #|Description| From cab69598dee2da6ca9a1411db649d2193582d04a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Mar 2022 07:06:38 -0800 Subject: [PATCH 389/969] Create L392.go --- Easy/L392.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L392.go diff --git a/Easy/L392.go b/Easy/L392.go new file mode 100644 index 0000000..f2549a5 --- /dev/null +++ b/Easy/L392.go @@ -0,0 +1,20 @@ +package Easy + +func isSubsequence(s string, t string) bool { + if len(s) == 0 { + return true + } + + indexS, indexT := 0, 0 + for indexT < len(t) { + if t[indexT] == s[indexS] { + indexS++ + if indexS == len(s) { + return true + } + } + indexT++ + } + + return false +} From 37c0787a4c589ac44b82e3881902c9e128d9a3b1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Mar 2022 07:07:17 -0800 Subject: [PATCH 390/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f56f78c..0032668 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ Please give this repo a ⭐ if it inspires you. |[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| |[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| |[338](https://leetcode.com/problems/counting-bits/)| Counting Bits| +|[392](https://leetcode.com/problems/is-subsequence/)| Is Subsequence| ## Medium |LC #|Description| From 0b97daeed3c617fe72d64400a79284505e101e81 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Mar 2022 14:41:52 -0800 Subject: [PATCH 391/969] Create L413.go --- Medium/L413.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/L413.go diff --git a/Medium/L413.go b/Medium/L413.go new file mode 100644 index 0000000..fe620d6 --- /dev/null +++ b/Medium/L413.go @@ -0,0 +1,22 @@ +package Medium + +func numberOfArithmeticSlices(nums []int) int { + if len(nums) < 3 { + return 0 + } + + cnt := 0 + for i := 0; i < len(nums)-2; i++ { + d := nums[i+1] - nums[i] + cnt += helper(nums, i+2, nums[i+1], d, len(nums)) + } + return cnt +} + +func helper(nums []int, idx, prev, d, n int) int { + if idx == n || nums[idx] - prev != d { + return 0 + } + + return 1 + helper(nums, idx+1, nums[idx], d, n) +} From a08dfea9904e6da03b1e3968d88b35e53c7fc588 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Mar 2022 14:42:27 -0800 Subject: [PATCH 392/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0032668..0b4f931 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,7 @@ Please give this repo a ⭐ if it inspires you. |[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| |[148](https://leetcode.com/problems/sort-list/)| Sort List| |[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| +|[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| ## Hard |LC #|Description| From 0c0e90500fa005f45f844112f764ce9ad1bc58dd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 4 Mar 2022 06:22:28 -0800 Subject: [PATCH 393/969] Create L799.go --- Medium/L799.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Medium/L799.go diff --git a/Medium/L799.go b/Medium/L799.go new file mode 100644 index 0000000..ae98018 --- /dev/null +++ b/Medium/L799.go @@ -0,0 +1,44 @@ +package Medium + +func champagneTower(poured int, query_row int, query_glass int) float64 { + dp := make([][]float64, query_row+1) + for i := 0; i < query_row+1; i++ { + dp[i] = make([]float64, query_glass+1) + for j := 0; j < query_glass+1; j++ { + dp[i][j] = -1 + } + } + + ans := solve(query_row, query_glass, poured, &dp) + + if ans > 1 { + return 1 + } + + return ans +} + +func solve(row, col, poured int, dp *[][]float64) float64 { + if col < 0 || col > row { + return 0 + } + if row == 0 && col == 0 { + return float64(poured) + } + + if (*dp)[row][col] != -1 { + return (*dp)[row][col] + } + + (*dp)[row][col] = max(solve(row-1, col-1, poured, dp)-1, 0)/2 + max(solve(row-1, col, poured, dp)-1, 0)/2 + + return (*dp)[row][col] +} + +func max(a, b float64) float64 { + if a > b { + return a + } + + return b +} From 7c03547c539f29c540b92856f879e7cff7f836e6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 4 Mar 2022 06:23:07 -0800 Subject: [PATCH 394/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0b4f931..11441dc 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ Please give this repo a ⭐ if it inspires you. |[148](https://leetcode.com/problems/sort-list/)| Sort List| |[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| |[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| +|[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| ## Hard |LC #|Description| From 5b4b0ba0b47285526e3fc33d52b2ec190f5a04db Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 5 Mar 2022 19:30:14 -0800 Subject: [PATCH 395/969] Create L1359.go --- Hard/L1359.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Hard/L1359.go diff --git a/Hard/L1359.go b/Hard/L1359.go new file mode 100644 index 0000000..6b496fd --- /dev/null +++ b/Hard/L1359.go @@ -0,0 +1,44 @@ +package Hard + +func countOrders(n int) int { + dp := make([][]int64, 501) + for i := 0; i < 501; i++ { + dp[i] = make([]int64, 501) + for j := 0; j < 501; j++ { + dp[i][j] = -1 + } + } + + return int(solve(n, n, &dp)) +} + +func solve(pick, del int, dp *[][]int64) int64 { + if del < 0 || pick < 0 { + return 0 + } + + if del < pick { + return 0 + } + + if pick == 0 && del == 0 { + return 1 + } + + if (*dp)[pick][del] != -1 { + return (*dp)[pick][del] + } + + var ans int64 + mod := 1000000007 + + ans += int64(pick) * solve(pick-1, del, dp) + ans %= int64(mod) + + ans += int64(del-pick) * solve(pick, del-1, dp) + ans %= int64(mod) + + (*dp)[pick][del] = ans + + return (*dp)[pick][del] +} From 24fd19b14dbc74ee06ccfa728fab803693ac3fdf Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 5 Mar 2022 19:36:13 -0800 Subject: [PATCH 396/969] Update L1359.go --- Hard/L1359.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Hard/L1359.go b/Hard/L1359.go index 6b496fd..d14337a 100644 --- a/Hard/L1359.go +++ b/Hard/L1359.go @@ -14,31 +14,37 @@ func countOrders(n int) int { func solve(pick, del int, dp *[][]int64) int64 { if del < 0 || pick < 0 { + //base case as it can't be negative return 0 } if del < pick { + // condition to insure we are first picking then delivering return 0 } if pick == 0 && del == 0 { + //everything finishes return 1 return 1 } if (*dp)[pick][del] != -1 { + //memoization return (*dp)[pick][del] } var ans int64 mod := 1000000007 + // if there are pick number of orders we have [pick]C1 = pick number of choices for selecting and we have picked one so we do pick-1 ans += int64(pick) * solve(pick-1, del, dp) ans %= int64(mod) - + + // delivering : the number of items we have picked but not delivered is (del-pick) so we can select one item from these to deliver [del-pick]C1 =del-pick and we have delivered so del-1 ans += int64(del-pick) * solve(pick, del-1, dp) ans %= int64(mod) (*dp)[pick][del] = ans - + return (*dp)[pick][del] } From 038052dc2d2dffd8513c866f52a61ea2ce353438 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Mar 2022 06:50:20 -0800 Subject: [PATCH 397/969] Create L21.go --- Easy/L21.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L21.go diff --git a/Easy/L21.go b/Easy/L21.go new file mode 100644 index 0000000..fe79c1c --- /dev/null +++ b/Easy/L21.go @@ -0,0 +1,19 @@ +package Easy + +func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { + if l1 == nil { + return l2 + } + + if l2 == nil { + return l1 + } + + if l1.Val < l2.Val { + l1.Next = mergeTwoLists(l1.Next, l2) + return l1 + } else { + l2.Next = mergeTwoLists(l1, l2.Next) + return l2 + } +} From e89117c296e3f10538f183fdccba64a80d495b2d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Mar 2022 06:50:49 -0800 Subject: [PATCH 398/969] Create L141.go --- Easy/L141.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Easy/L141.go diff --git a/Easy/L141.go b/Easy/L141.go new file mode 100644 index 0000000..90d9e3a --- /dev/null +++ b/Easy/L141.go @@ -0,0 +1,14 @@ +package Easy + +func hasCycle(head *ListNode) bool { + walker, runner := head, head + + for runner != nil && runner.Next != nil { + walker, runner = walker.Next, runner.Next.Next + if walker == runner { + return true + } + } + + return false +} From 4216973df4e4854d70a820bf512063d5284f0f25 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Mar 2022 06:51:46 -0800 Subject: [PATCH 399/969] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 11441dc..357cdee 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,8 @@ Please give this repo a ⭐ if it inspires you. |[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| |[338](https://leetcode.com/problems/counting-bits/)| Counting Bits| |[392](https://leetcode.com/problems/is-subsequence/)| Is Subsequence| +|[21](https://leetcode.com/problems/merge-two-sorted-lists/)| Merge Two Sorted Lists| +|[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| ## Medium |LC #|Description| From 3bcb36e51bb383eb8a519360de63cfe32a8db5ab Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Mar 2022 10:51:07 -0800 Subject: [PATCH 400/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 357cdee..26f56d3 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,7 @@ Please give this repo a ⭐ if it inspires you. |[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| |[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| |[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| +|[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| ## Hard |LC #|Description| From b744bb98193972d7cce72f6380f4285d4ac80440 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Mar 2022 10:51:43 -0800 Subject: [PATCH 401/969] Create L82.go --- Medium/L82.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L82.go diff --git a/Medium/L82.go b/Medium/L82.go new file mode 100644 index 0000000..64abd1a --- /dev/null +++ b/Medium/L82.go @@ -0,0 +1,31 @@ +package Medium + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func deleteDuplicates(head *ListNode) *ListNode { + var pre *ListNode + cur := head + + for cur != nil && cur.Next != nil { + if cur.Val != cur.Next.Val { + pre = cur + } else { + for cur.Next != nil && cur.Next.Val == cur.Val { + cur = cur.Next + } + if pre != nil { + pre.Next = cur.Next + } else { + head = cur.Next + } + } + cur = cur.Next + } + + return head +} From 2e4a1a0bd02501bd62abb21cf3a9b76bc1e85178 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Mar 2022 15:20:43 -0800 Subject: [PATCH 402/969] Create L61.go --- Medium/L61.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L61.go diff --git a/Medium/L61.go b/Medium/L61.go new file mode 100644 index 0000000..6d33b62 --- /dev/null +++ b/Medium/L61.go @@ -0,0 +1,25 @@ +package Medium + +func rotateRight(head *ListNode, k int) *ListNode { + if head == nil { + return head + } + + listNum, tail := 1, head + + for tail.Next != nil { + listNum++ + tail = tail.Next + } + tail.Next = head + newHeadIndex := listNum - k%listNum + + for i := 0; i < newHeadIndex; i++ { + tail = tail.Next + } + + head = tail.Next + tail.Next = nil + + return head +} From 4a81ae7da38b47dfc63b1f49c97b634ce93dc42c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 11 Mar 2022 15:21:20 -0800 Subject: [PATCH 403/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 26f56d3..200b56f 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,7 @@ Please give this repo a ⭐ if it inspires you. |[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| |[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| |[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| +|[61](https://leetcode.com/problems/rotate-list/)| Rotate List| ## Hard |LC #|Description| From 12fef3da795e02cc815c8377b6b24a3fc57ef162 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Mar 2022 07:51:56 -0800 Subject: [PATCH 404/969] Create L138.go --- Medium/L138.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L138.go diff --git a/Medium/L138.go b/Medium/L138.go new file mode 100644 index 0000000..2edf4d3 --- /dev/null +++ b/Medium/L138.go @@ -0,0 +1,33 @@ +package Medium + +/** + * Definition for a Node. + * type Node struct { + * Val int + * Next *Node + * Random *Node + * } + */ + +func copyRandomList(head *Node) *Node { + if head == nil { + return nil + } + + mp, node := make(map[*Node]*Node), head + // loop 1. copy all the nodes + for node != nil { + mp[node] = &Node{Val: node.Val} + node = node.Next + } + + node = head + // loop 2. assign next and random pointers + for node != nil { + mp[node].Next = mp[node.Next] + mp[node].Random = mp[node.Random] + node = node.Next + } + + return mp[head] +} From 2759bad15053415dfe0539676be371d5d7f12c9e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Mar 2022 07:52:33 -0800 Subject: [PATCH 405/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 200b56f..652cf20 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ Please give this repo a ⭐ if it inspires you. |[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| |[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| |[61](https://leetcode.com/problems/rotate-list/)| Rotate List| +|[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| ## Hard |LC #|Description| From 8bbadb78e5d743d02bf0390936ed33399f26be5c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Mar 2022 20:01:53 -0800 Subject: [PATCH 406/969] Create 617.go --- Easy/617.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Easy/617.go diff --git a/Easy/617.go b/Easy/617.go new file mode 100644 index 0000000..bda188c --- /dev/null +++ b/Easy/617.go @@ -0,0 +1,32 @@ +package Easy + +func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode { + return dfs(root1, root2) +} + +func dfs(t1, t2 *TreeNode) *TreeNode { + if t1 == nil && t2 == nil { + return nil + } + if t1 == nil { + return t2 + } + if t2 == nil { + return t1 + } + + var root *TreeNode + + if t1 != nil && t2 == nil { + root = &TreeNode{Val: t1.Val} + } else if t1 == nil && t2 != nil { + root = &TreeNode{Val: t2.Val} + } else { + root = &TreeNode{Val: t1.Val + t2.Val} + } + + root.Left = dfs(t1.Left, t2.Left) + root.Right = dfs(t1.Right, t2.Right) + + return root +} From 151f1bcd4dab584b2553437bbd05786299a93966 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Mar 2022 20:02:30 -0800 Subject: [PATCH 407/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 652cf20..99ffc43 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Please give this repo a ⭐ if it inspires you. |[392](https://leetcode.com/problems/is-subsequence/)| Is Subsequence| |[21](https://leetcode.com/problems/merge-two-sorted-lists/)| Merge Two Sorted Lists| |[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| +|[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| ## Medium |LC #|Description| From 178e70a50dff159b7db4f2bc07b7899ee12ff63c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Mar 2022 20:31:12 -0700 Subject: [PATCH 408/969] Create L20.go --- Easy/L20.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Easy/L20.go diff --git a/Easy/L20.go b/Easy/L20.go new file mode 100644 index 0000000..a029bc1 --- /dev/null +++ b/Easy/L20.go @@ -0,0 +1,59 @@ +package Easy + +type Stack struct { + arr []byte +} + +func NewStack() *Stack { + return &Stack{arr: make([]byte, 0)} +} + +func (s *Stack) Push(b byte) { + s.arr = append(s.arr, b) +} + +func (s *Stack) Pop() byte { + if s.IsEmpty() { + return 0 + } + + ret := s.arr[len(s.arr)-1] + s.arr = s.arr[:len(s.arr)-1] + return ret +} + +func (s *Stack) Top() byte { + if s.IsEmpty() { + return 0 + } + + return s.arr[len(s.arr)-1] +} + +func (s *Stack) IsEmpty() bool { + return len(s.arr) == 0 +} + +func isValid(s string) bool { + if len(s) == 0 { + return false + } + st := NewStack() + + for i := 0; i < len(s); i++ { + switch s[i] { + case '(': + st.Push(')') + case '{': + st.Push('}') + case '[': + st.Push(']') + default: + if st.IsEmpty() || st.Pop() != s[i] { + return false + } + } + } + + return st.IsEmpty() +} From 515fe5944427863753edb28b22bb38ec8d8d535f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Mar 2022 20:31:43 -0700 Subject: [PATCH 409/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 99ffc43..1ca4721 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ Please give this repo a ⭐ if it inspires you. |[21](https://leetcode.com/problems/merge-two-sorted-lists/)| Merge Two Sorted Lists| |[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| |[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| +|[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| ## Medium |LC #|Description| From c36abc742815059066fa86acb21bc0439912cafd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Mar 2022 20:48:54 -0700 Subject: [PATCH 410/969] Create L71.go --- Medium/L71.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Medium/L71.go diff --git a/Medium/L71.go b/Medium/L71.go new file mode 100644 index 0000000..d21d5e5 --- /dev/null +++ b/Medium/L71.go @@ -0,0 +1,65 @@ +package Medium + +type Stack struct { + arr []string +} + +func NewStack() *Stack { + return &Stack{arr: make([]string, 0)} +} + +func (s *Stack) Push(str string) { + s.arr = append(s.arr, str) +} + +func (s *Stack) Pop() string { + if s.IsEmpty() { + return "" + } + + ret := s.arr[len(s.arr)-1] + s.arr = s.arr[:len(s.arr)-1] + return ret +} + +func (s *Stack) Size() int { + return len(s.arr) +} + +func (s *Stack) Top() string { + if s.IsEmpty() { + return "" + } + + return s.arr[len(s.arr)-1] +} + +func (s *Stack) IsEmpty() bool { + return len(s.arr) == 0 +} + +func simplifyPath(path string) string { + var sb strings.Builder + st := NewStack() + + for _, s := range strings.Split(path, "/") { + if s == ".." { + if !st.IsEmpty() { + st.Pop() + } + } else if s != "" && s != "." { + st.Push(s) + } + } + + if st.Size() == 0 { + return "/" + } + + for i := 0; i < st.Size(); i++ { + sb.WriteString("/") + sb.WriteString(st.arr[i]) + } + + return sb.String() +} From e1003588321ab05c9e09591823ffefa0bd9fc2fe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 13 Mar 2022 20:49:29 -0700 Subject: [PATCH 411/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1ca4721..ff8e3d2 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,7 @@ Please give this repo a ⭐ if it inspires you. |[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| |[61](https://leetcode.com/problems/rotate-list/)| Rotate List| |[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| +|[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| ## Hard |LC #|Description| From e0d52bc0e75453ecd6e8cd7b56495ecc15d57b4d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 14 Mar 2022 12:08:52 -0700 Subject: [PATCH 412/969] Update L1748.go --- Easy/L1748.go | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Easy/L1748.go b/Easy/L1748.go index 0f5b6ad..04374f5 100644 --- a/Easy/L1748.go +++ b/Easy/L1748.go @@ -1,22 +1,14 @@ package Easy func sumOfUnique(nums []int) int { - if len(nums) == 0 { - return 0 - } - - mp := make(map[int]bool) + mp, sum := make(map[int]int), 0 - sum := 0 - for _, v := range nums { - if _, ok := mp[v]; ok { - if mp[v] { - sum -= v - mp[v] = false - } - } else { - mp[v] = true - sum += v + for _, n := range nums { + mp[n]++ + if mp[n] == 1 { + sum += n + } else if mp[n] == 2 { + sum -= n } } From 5b5e81325aad99e3b3b440990f19841efbfb82e5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Mar 2022 15:23:52 -0700 Subject: [PATCH 413/969] Create L1249.go --- Medium/L1249.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Medium/L1249.go diff --git a/Medium/L1249.go b/Medium/L1249.go new file mode 100644 index 0000000..7a00eac --- /dev/null +++ b/Medium/L1249.go @@ -0,0 +1,68 @@ +package Medium + +type Stack struct { + arr []int +} + +func NewStack() *Stack { + return &Stack{arr: make([]int, 0)} +} + +func (s *Stack) Push(c int) { + s.arr = append(s.arr, c) +} + +func (s *Stack) IsEmpty() bool { + return len(s.arr) == 0 +} + +func (s *Stack) Pop() int { + if s.IsEmpty() { + return 0 + } + + ret := s.arr[len(s.arr)-1] + s.arr = s.arr[:len(s.arr)-1] + return ret +} + +func (s *Stack) Top() int { + if s.IsEmpty() { + return 0 + } + + return s.arr[len(s.arr)-1] +} + +func minRemoveToMakeValid(s string) string { + var sb strings.Builder + st := NewStack() + match := make([]bool, len(s)) + + for i := 0; i < len(s); i++ { + if s[i] == '(' { + st.Push(i) + } else if s[i] == ')' { + if !st.IsEmpty() { + // match these pairs, all unmatched are false anyway + match[i] = true + match[st.Pop()] = true + } + } else { + // any character other than ( and ) are true anyway + match[i] = true + } + } + + for i := 0; i < len(s); i++ { + if match[i] { + sb.WriteByte(s[i]) + } + } + + if sb.Len() == 0 { + return "" + } + + return sb.String() +} From a07347b21d4516f84018a0ad6fa085d4803fc7bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 15 Mar 2022 15:24:34 -0700 Subject: [PATCH 414/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ff8e3d2..9c2b438 100644 --- a/README.md +++ b/README.md @@ -306,6 +306,7 @@ Please give this repo a ⭐ if it inspires you. |[61](https://leetcode.com/problems/rotate-list/)| Rotate List| |[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| |[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| +|[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| ## Hard |LC #|Description| From f419889d44ceb7d087949b49ba83dfe5a7d9e7d5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Mar 2022 10:56:22 -0700 Subject: [PATCH 415/969] Create L1351.go --- Easy/L1351.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Easy/L1351.go diff --git a/Easy/L1351.go b/Easy/L1351.go new file mode 100644 index 0000000..0d94cc0 --- /dev/null +++ b/Easy/L1351.go @@ -0,0 +1,26 @@ +package Easy + +func countNegatives(grid [][]int) int { + res := 0 + + for _, r := range grid { + res += binarySearch(r) + } + + return res +} + +func binarySearch(row []int) int { + l, r := 0, len(row) + + for l < r { + m := l + (r-l)/2 + if row[m] < 0 { + r = m + } else { + l = m + 1 + } + } + + return len(row) - l +} From c03437c111c10b84d349e9a05a42cb2e8dfc2038 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Mar 2022 10:56:57 -0700 Subject: [PATCH 416/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9c2b438..f6a10ba 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ Please give this repo a ⭐ if it inspires you. |[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| |[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| |[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| +|[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| ## Medium |LC #|Description| From fe29e2a1b0b7e1e4ac5d60942587128ff8f65157 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Mar 2022 14:47:06 -0700 Subject: [PATCH 417/969] Create L856.go --- Medium/L856.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L856.go diff --git a/Medium/L856.go b/Medium/L856.go new file mode 100644 index 0000000..b72f515 --- /dev/null +++ b/Medium/L856.go @@ -0,0 +1,26 @@ +package Medium + +func scoreOfParentheses(s string) int { + var st []int + cur := 0 + + for i := 0; i < len(s); i++ { + if s[i] == '(' { + st = append(st, cur) + cur = 0 + } else { + cur = st[len(st)-1] + max(cur*2, 1) + st = st[:len(st)-1] + } + } + + return cur +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 0dfd56a803e41603d1a939345b8e34bbc56ea4a5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Mar 2022 14:47:37 -0700 Subject: [PATCH 418/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f6a10ba..f4acd2c 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,7 @@ Please give this repo a ⭐ if it inspires you. |[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| |[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| |[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| +|[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| ## Hard |LC #|Description| From 638da2195a0eb146456e6613c6af9125ba783ba4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:29:33 -0700 Subject: [PATCH 419/969] Create L316.go --- Medium/L316.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Medium/L316.go diff --git a/Medium/L316.go b/Medium/L316.go new file mode 100644 index 0000000..b59ddf7 --- /dev/null +++ b/Medium/L316.go @@ -0,0 +1,44 @@ +package Medium + +func removeDuplicateLetters(s string) string { + lastIndex := make([]int, 26) + for i := 0; i < len(s); i++ { + // track the lastIndex of character presence + lastIndex[s[i]-'a'] = i + } + + var st []int + seen := make([]bool, 26) + + for i := 0; i < len(s); i++ { + curr := s[i] - 'a' + if seen[curr] { + // if seen continue as we need to pick one char only + continue + } + for len(st) > 0 && st[len(st)-1] > int(curr) && i < lastIndex[st[len(st)-1]] { + // mark unseen + seen[st[len(st)-1]] = false + // pop out + st = st[:len(st)-1] + } + st = append(st, int(curr)) + seen[curr] = true + } + + var sb strings.Builder + for len(st) > 0 { + sb.WriteByte(byte(st[len(st)-1] + 'a')) + st = st[:len(st)-1] + } + + return reverse(sb.String()) +} + +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} From bfe55bcef0b8fcdc68c9cbde85907603f405db2e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:30:08 -0700 Subject: [PATCH 420/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f4acd2c..438a2e9 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,7 @@ Please give this repo a ⭐ if it inspires you. |[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| |[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| |[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| +|[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| ## Hard |LC #|Description| From 76585af66dee7296c5e290a629be65226f79e102 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:46:02 -0700 Subject: [PATCH 421/969] Create L905.go --- Easy/L905.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Easy/L905.go diff --git a/Easy/L905.go b/Easy/L905.go new file mode 100644 index 0000000..2f93232 --- /dev/null +++ b/Easy/L905.go @@ -0,0 +1,25 @@ +package Easy + +func sortArrayByParity(nums []int) []int { + ret := make([]int, len(nums)) + if len(nums) == 0 { + return ret + } + + i, j := 0, len(nums)-1 + for _, n := range nums { + if isOdd(n) { + ret[j] = n + j-- + } else { + ret[i] = n + i++ + } + } + + return ret +} + +func isOdd(n int) bool { + return n%2 > 0 +} From 087e91b8f22854644a394392a1d9277afebd09e1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:46:22 -0700 Subject: [PATCH 422/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 438a2e9..0181496 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Please give this repo a ⭐ if it inspires you. |[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| |[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| |[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| +|[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| ## Medium |LC #|Description| From 0f567166d15ed39e619950ad76d524b986f14b6d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:54:21 -0700 Subject: [PATCH 423/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0181496..21dc8d1 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Please give this repo a ⭐ if it inspires you. |[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| |[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| |[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| +|[344](https://leetcode.com/problems/reverse-string/)| Reverse String| ## Medium |LC #|Description| From 03a9c9d13b845b4b2718ead357926169df6b629c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 11:54:44 -0700 Subject: [PATCH 424/969] Create L344.go --- Easy/L344.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L344.go diff --git a/Easy/L344.go b/Easy/L344.go new file mode 100644 index 0000000..4281dd7 --- /dev/null +++ b/Easy/L344.go @@ -0,0 +1,13 @@ +package Easy + +func reverseString(s []byte) { + if len(s) == 0 { + return + } + + st, end := 0, len(s)-1 + for st <= end { + s[st], s[end] = s[end], s[st] + st, end = st+1, end-1 + } +} From 2d14fc4b8d24a8e073d2f39190c228a0a2039ead Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 12:09:23 -0700 Subject: [PATCH 425/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 21dc8d1..cdbeec8 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ Please give this repo a ⭐ if it inspires you. |[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| |[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| |[344](https://leetcode.com/problems/reverse-string/)| Reverse String| +|[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| ## Medium |LC #|Description| From 7e84b6dc232ba89ebb99eccf71978c5ff9ced786 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 12:10:17 -0700 Subject: [PATCH 426/969] Create L1460.go --- Easy/L1460.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L1460.go diff --git a/Easy/L1460.go b/Easy/L1460.go new file mode 100644 index 0000000..1c1efc9 --- /dev/null +++ b/Easy/L1460.go @@ -0,0 +1,22 @@ +package Easy + +func canBeEqual(target []int, arr []int) bool { + m, n := len(arr), len(target) + if m != n { + return false + } + + mp := make(map[int]int) + for i := 0; i < m; i++ { + mp[target[i]]++ + mp[arr[i]]-- + } + + for _, v := range mp { + if v != 0 { + return false + } + } + + return true +} From dc51455fd836ce3f4e5bdb24217937b193485f21 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 13:24:26 -0700 Subject: [PATCH 427/969] Create L977.go --- Easy/L977.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Easy/L977.go diff --git a/Easy/L977.go b/Easy/L977.go new file mode 100644 index 0000000..f7d4d0c --- /dev/null +++ b/Easy/L977.go @@ -0,0 +1,29 @@ +package Easy + +func sortedSquares(nums []int) []int { + ret := make([]int, len(nums)) + if len(nums) == 0 { + return ret + } + + i, j := 0, len(nums)-1 + for idx := len(nums) - 1; idx >= 0; idx-- { + if abs(nums[i]) > abs(nums[j]) { + ret[idx] = nums[i] * nums[i] + i++ + } else { + ret[idx] = nums[j] * nums[j] + j-- + } + } + + return ret +} + +func abs(n int) int { + if n < 0 { + return -n + } + + return n +} From 6d30d3117001f5c2efcd2b84bfbdbf89f3d74ad3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Mar 2022 13:24:46 -0700 Subject: [PATCH 428/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cdbeec8..08b9979 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Please give this repo a ⭐ if it inspires you. |[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| |[344](https://leetcode.com/problems/reverse-string/)| Reverse String| |[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| +|[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| ## Medium |LC #|Description| From 9732788ea6dd5a749055a4f68c43360c6a5760ac Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 07:18:26 -0700 Subject: [PATCH 429/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 08b9979..0640d6d 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,7 @@ Please give this repo a ⭐ if it inspires you. |[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| |[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| |[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| +|[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| ## Hard |LC #|Description| From 5fc2696f0252f97d4b7f2489d844b7563fb164ee Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 07:18:51 -0700 Subject: [PATCH 430/969] Create L763.go --- Medium/L763.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L763.go diff --git a/Medium/L763.go b/Medium/L763.go new file mode 100644 index 0000000..b584c4f --- /dev/null +++ b/Medium/L763.go @@ -0,0 +1,29 @@ +package Medium + +func partitionLabels(s string) []int { + var res []int + if len(s) == 0 { + return res + } + + mp := make(map[byte]int) + right, size := 0, 0 + + for i := 0; i < len(s); i++ { + mp[s[i]] = i + } + + for left := 0; left < len(s); left++ { + size++ + if mp[s[left]] > right { + right = mp[s[left]] + } + + if left == right { + res = append(res, size) + size = 0 + } + } + + return res +} From 3ad62fdcd6fbd87c3c083c77c56ee1b8a094643b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 12:32:21 -0700 Subject: [PATCH 431/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0640d6d..1c07006 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ Please give this repo a ⭐ if it inspires you. |[344](https://leetcode.com/problems/reverse-string/)| Reverse String| |[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| |[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| +|[1007](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| ## Medium |LC #|Description| From 079f24901a45dc1ebd3a7a01a5d9d84111a92f09 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 12:32:51 -0700 Subject: [PATCH 432/969] Create L1007.go --- Easy/L1007.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L1007.go diff --git a/Easy/L1007.go b/Easy/L1007.go new file mode 100644 index 0000000..67730f4 --- /dev/null +++ b/Easy/L1007.go @@ -0,0 +1,28 @@ +package Easy + +func intersection(nums1 []int, nums2 []int) []int { + var res []int + if len(nums1) == 0 || len(nums2) == 0 { + return res + } + + sort.Ints(nums1) + sort.Ints(nums2) + + for i, j := 0, 0; i < len(nums1) && j < len(nums2); { + if nums1[i] == nums2[j] { + size := len(res) + i, j = i+1, j+1 + if size != 0 && res[size-1] == nums1[i-1] { + continue + } + res = append(res, nums1[i-1]) + } else if nums1[i] < nums2[j] { + i++ + } else { + j++ + } + } + + return res +} From c5f83479b329d3a9478f12428c8dbb86fb631172 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 12:34:12 -0700 Subject: [PATCH 433/969] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c07006..dcc1daf 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ Please give this repo a ⭐ if it inspires you. |[344](https://leetcode.com/problems/reverse-string/)| Reverse String| |[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| |[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| -|[1007](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| +|[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| ## Medium |LC #|Description| From 0fce6063eee710b7f01688c7c57a89bf2eace556 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 21 Mar 2022 12:34:55 -0700 Subject: [PATCH 434/969] Rename L1007.go to L349.go --- Easy/{L1007.go => L349.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Easy/{L1007.go => L349.go} (100%) diff --git a/Easy/L1007.go b/Easy/L349.go similarity index 100% rename from Easy/L1007.go rename to Easy/L349.go From 49ad7bd4e2ddc0ec23e3072754d76b5c9d2cf794 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Mar 2022 15:56:14 -0700 Subject: [PATCH 435/969] Create L1663.go --- Medium/L1663.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L1663.go diff --git a/Medium/L1663.go b/Medium/L1663.go new file mode 100644 index 0000000..67842a2 --- /dev/null +++ b/Medium/L1663.go @@ -0,0 +1,20 @@ +package Medium + +func getSmallestString(n int, k int) string { + chars := make([]byte, n) + for i := 0; i < n; i++ { + chars[i] = 'a' + } + k -= n + + for k > 0 { + if chars[n-1] < 'z' { + chars[n-1]++ + k-- + } else { + n-- + } + } + + return string(chars) +} From 3e09da4244529fe959a3671bce21d7c64d40b413 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Mar 2022 15:56:57 -0700 Subject: [PATCH 436/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dcc1daf..b215418 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ Please give this repo a ⭐ if it inspires you. |[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| |[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| |[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| +|[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| ## Hard |LC #|Description| From 94cc4ce4ad568af3af8e15560e0573be4ef935ce Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Mar 2022 21:28:50 -0700 Subject: [PATCH 437/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b215418..467424d 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ Please give this repo a ⭐ if it inspires you. |[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| |[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| |[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| +|[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| ## Hard |LC #|Description| From ffbcdf0be0e9d1ce49ba0404860a519cf94ede99 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Mar 2022 21:30:05 -0700 Subject: [PATCH 438/969] Create L991.go --- Medium/L991.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Medium/L991.go diff --git a/Medium/L991.go b/Medium/L991.go new file mode 100644 index 0000000..f5bfd7e --- /dev/null +++ b/Medium/L991.go @@ -0,0 +1,23 @@ +package Medium + +func brokenCalc(startValue int, target int) int { + // res for counting number of operation + res := 0 + + for target > startValue { + if isOdd(target) { + // if target is odd we will make it even + target++ + } else { + // if target is even divide by 2 + target /= 2 + } + res++ + } + + return res + startValue - target +} + +func isOdd(n int) bool { + return n%2 > 0 +} From 33a9c43f5235b1fd88179c225ffaa69f5beb5b91 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Mar 2022 21:53:43 -0700 Subject: [PATCH 439/969] Update L637.go --- Easy/L637.go | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Easy/L637.go b/Easy/L637.go index f38f7e3..ca3deb2 100644 --- a/Easy/L637.go +++ b/Easy/L637.go @@ -1,27 +1,30 @@ package Easy func averageOfLevels(root *TreeNode) []float64 { - if root == nil { - return make([]float64, 0) - } - avgs := make([]float64, 0, 10) - queue := make([]*TreeNode, 1, 10) - queue[0] = root - for len(queue) != 0 { - n := len(queue) - sum := 0 - for i := 0; i < n; i++ { - cur := queue[0] - sum += cur.Val - queue = queue[1:] - if cur.Left != nil { - queue = append(queue, cur.Left) - } - if cur.Right != nil { - queue = append(queue, cur.Right) - } - } - avgs = append(avgs, float64(sum)/float64(n)) - } - return avgs + var ( + res []float64 + q []*TreeNode + ) + if root == nil { + return res + } + + q = append(q, root) + for len(q) != 0 { + n := len(q) + sum := 0 + for i := 0; i < n; i++ { + cur := q[0] + sum += cur.Val + q = q[1:] + if cur.Left != nil { + q = append(q, cur.Left) + } + if cur.Right != nil { + q = append(q, cur.Right) + } + } + res = append(res, float64(sum)/float64(n)) + } + return res } From 748c487346b56c12fc557508d815576d1acd8648 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Mar 2022 09:33:06 -0700 Subject: [PATCH 440/969] Create L1002.go --- Easy/L1002.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L1002.go diff --git a/Easy/L1002.go b/Easy/L1002.go new file mode 100644 index 0000000..459406b --- /dev/null +++ b/Easy/L1002.go @@ -0,0 +1,28 @@ +package Easy + +func commonChars(words []string) []string { + var res []string + count := make([]int, 26) + for i := 0; i < 26; i++ { + count[i] = math.MaxInt32 + } + for _, w := range words { + cnt := make([]int, 26) + for i := range w { + cnt[w[i]-'a']++ + } + for i := 0; i < 26; i++ { + if cnt[i] < count[i] { + count[i] = cnt[i] + } + } + } + + for ch := 'a'; ch <= 'z'; ch++ { + for i := count[ch-'a']; i > 0; i-- { + res = append(res, string(ch)) + } + } + + return res +} From 8f5bcc91169773f30e7c0a810f7961bdffe2628a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Mar 2022 09:33:39 -0700 Subject: [PATCH 441/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 467424d..90df96e 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ Please give this repo a ⭐ if it inspires you. |[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| |[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| |[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| +|[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| ## Medium |LC #|Description| From c342027d282929b22f7cc3f2229b86ee8da178ca Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Mar 2022 07:18:15 -0700 Subject: [PATCH 442/969] Create L881.go --- Medium/L881.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L881.go diff --git a/Medium/L881.go b/Medium/L881.go new file mode 100644 index 0000000..a52756c --- /dev/null +++ b/Medium/L881.go @@ -0,0 +1,21 @@ +package Medium + +func numRescueBoats(people []int, limit int) int { + boatCnt, left, right := 0, 0, len(people)-1 + if len(people) == 0 { + return boatCnt + } + + sort.Ints(people) + + for left <= right { + sum := people[left] + people[right] + if sum <= limit { + left++ + } + boatCnt++ + right-- + } + + return boatCnt +} From cc0a4db024f18d21172143a98f115084df220293 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Mar 2022 07:18:57 -0700 Subject: [PATCH 443/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 90df96e..fa566aa 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,7 @@ Please give this repo a ⭐ if it inspires you. |[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| |[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| |[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| +|[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| ## Hard |LC #|Description| From 6745a9ef8a4041a22c41ef5d28faf0b93972b152 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Mar 2022 21:58:21 -0700 Subject: [PATCH 444/969] Create L1029.go --- Medium/L1029.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L1029.go diff --git a/Medium/L1029.go b/Medium/L1029.go new file mode 100644 index 0000000..9f03ac2 --- /dev/null +++ b/Medium/L1029.go @@ -0,0 +1,18 @@ +package Medium + +func twoCitySchedCost(costs [][]int) int { + sort.Slice(costs, func(i, j int) bool { + return (costs[i][1]-costs[i][0])-(costs[j][1]-costs[j][0]) > 0 + }) + + res := 0 + for i := 0; i < len(costs); i++ { + if i < len(costs)/2 { + res += costs[i][0] + } else { + res += costs[i][1] + } + } + + return res +} From eee74a290173c65844b4d4bbbea94f5251f5f4fc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Mar 2022 21:58:57 -0700 Subject: [PATCH 445/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fa566aa..fb7c5b2 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,7 @@ Please give this repo a ⭐ if it inspires you. |[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| |[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| |[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| +|[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| ## Hard |LC #|Description| From 5d1660cad149a35de3d39bbe71115d9b5567a922 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Mar 2022 07:46:35 -0700 Subject: [PATCH 446/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fb7c5b2..d5ae92c 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ Please give this repo a ⭐ if it inspires you. |[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| |[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| |[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| +|[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| ## Medium |LC #|Description| From 008c424f00b2c23062bdf4853adebc9fb610a628 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Mar 2022 07:46:59 -0700 Subject: [PATCH 447/969] Create L766.go --- Easy/L766.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L766.go diff --git a/Easy/L766.go b/Easy/L766.go new file mode 100644 index 0000000..1c263de --- /dev/null +++ b/Easy/L766.go @@ -0,0 +1,13 @@ +package Easy + +func isToeplitzMatrix(matrix [][]int) bool { + for i := 0; i < len(matrix)-1; i++ { + for j := 0; j < len(matrix[i])-1; j++ { + if matrix[i][j] != matrix[i+1][j+1] { + return false + } + } + } + + return true +} From 52ff25e88171e2ae692bc11444cfb6689092a50a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Mar 2022 22:32:58 -0700 Subject: [PATCH 448/969] Create L704.go --- Easy/L704.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L704.go diff --git a/Easy/L704.go b/Easy/L704.go new file mode 100644 index 0000000..41088e3 --- /dev/null +++ b/Easy/L704.go @@ -0,0 +1,18 @@ +package Easy + +func search(nums []int, target int) int { + low, high := 0, len(nums)-1 + + for low < high { + mid := low + (high-low)/2 + if target == nums[mid] { + return mid + } else if target > nums[mid] { + low = mid + 1 + } else { + high = mid - 1 + } + } + + return -1 +} From 92a97bcf30dd31bed6f97b0f640253435ba238f1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 25 Mar 2022 22:34:45 -0700 Subject: [PATCH 449/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d5ae92c..82b9b1d 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Please give this repo a ⭐ if it inspires you. |[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| |[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| |[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| +|[704](https://leetcode.com/problems/binary-search/)| Binary Search| ## Medium |LC #|Description| From cc3cf83ccb44fc7a95df6431dbe64836cee77dd1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Mar 2022 19:03:54 -0700 Subject: [PATCH 450/969] Create L81.go --- Medium/L81.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L81.go diff --git a/Medium/L81.go b/Medium/L81.go new file mode 100644 index 0000000..7027999 --- /dev/null +++ b/Medium/L81.go @@ -0,0 +1,29 @@ +package Medium + +func search(nums []int, target int) bool { + left, right := 0, len(nums)-1 + for left <= right { + mid := left + (right-left)/2 + if nums[mid] == target { + return true + } + + if nums[left] < nums[mid] { + if nums[left] <= target && target < nums[mid] { + right = mid - 1 + } else { + left = mid + 1 + } + } else if nums[left] > nums[mid] { + if nums[mid] < target && target <= nums[right] { + left = mid + 1 + } else { + right = mid - 1 + } + } else { + left++ + } + } + + return false +} From 08a90f78feec34863b5f0c1837d35e78e1c57b28 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Mar 2022 19:04:42 -0700 Subject: [PATCH 451/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 82b9b1d..a525904 100644 --- a/README.md +++ b/README.md @@ -323,6 +323,7 @@ Please give this repo a ⭐ if it inspires you. |[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| |[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| |[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| +|[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| ## Hard |LC #|Description| From 83118b56ff6a14c708cb23e0b55fd2361bd9a98a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Mar 2022 19:09:59 -0700 Subject: [PATCH 452/969] Create L1160.go --- Easy/L1160.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L1160.go diff --git a/Easy/L1160.go b/Easy/L1160.go new file mode 100644 index 0000000..98e1960 --- /dev/null +++ b/Easy/L1160.go @@ -0,0 +1,28 @@ +package Easy + +func countCharacters(words []string, chars string) int { + res, cnt := 0, make([]int, 26) + + for i := range chars { + cnt[chars[i]-'a']++ + } + + for _, w := range words { + match, tmp := true, make([]int, 26) + copy(tmp, cnt) + + for i := range w { + tmp[w[i]-'a']-- + if tmp[w[i]-'a'] < 0 { + match = false + break + } + } + + if match { + res += len(w) + } + } + + return res +} From 791a3c17c52214c267dc1b5dfbea2b43567d877a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Mar 2022 19:10:28 -0700 Subject: [PATCH 453/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a525904..c5efba6 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ Please give this repo a ⭐ if it inspires you. |[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| |[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| |[704](https://leetcode.com/problems/binary-search/)| Binary Search| +|[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| ## Medium |LC #|Description| From bb25841e2b88f6e10f434db9b529f2774ad831dc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Mar 2022 07:10:46 -0700 Subject: [PATCH 454/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c5efba6..6dc715b 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Please give this repo a ⭐ if it inspires you. |[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| |[704](https://leetcode.com/problems/binary-search/)| Binary Search| |[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| +|[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| ## Medium |LC #|Description| From 7f462be7c34bc5b5b0cad233075eec998e19c9ca Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Mar 2022 07:11:00 -0700 Subject: [PATCH 455/969] Create L412.go --- Easy/L412.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Easy/L412.go diff --git a/Easy/L412.go b/Easy/L412.go new file mode 100644 index 0000000..e17f06a --- /dev/null +++ b/Easy/L412.go @@ -0,0 +1,28 @@ +func fizzBuzz(n int) []string { + var res []string + if n == 0 { + return res + } + + for i := 1; i <= n; i++ { + if isDiv3(i) && isDiv5(i) { + res = append(res, "FizzBuzz") + } else if isDiv3(i) && !isDiv5(i) { + res = append(res, "Fizz") + } else if !isDiv3(i) && isDiv5(i) { + res = append(res, "Buzz") + } else { + res = append(res, fmt.Sprintf("%d", i)) + } + } + + return res +} + +func isDiv3(n int) bool { + return n%3 == 0 +} + +func isDiv5(n int) bool { + return n%5 == 0 +} From af83565cb52a8161bf1fd240ebf77de3ef10b7a6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Mar 2022 20:36:20 -0700 Subject: [PATCH 456/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6dc715b..59ef1d5 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,7 @@ Please give this repo a ⭐ if it inspires you. |[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| |[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| |[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| +|[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| ## Hard |LC #|Description| From a26f7b4c9ad038e1d98544f992250bf662cf0a3c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Mar 2022 20:36:39 -0700 Subject: [PATCH 457/969] Create L287.go --- Medium/L287.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Medium/L287.go diff --git a/Medium/L287.go b/Medium/L287.go new file mode 100644 index 0000000..4ad154c --- /dev/null +++ b/Medium/L287.go @@ -0,0 +1,16 @@ +package Medium + +func findDuplicate(nums []int) int { + slow, fast := nums[0], nums[nums[0]] + + for slow != fast { + slow, fast = nums[slow], nums[nums[fast]] + } + + fast = 0 + for fast != slow { + fast, slow = nums[fast], nums[slow] + } + + return slow +} From a31ea1ef0516e2b685c4affbf3265b166b523ec5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Mar 2022 21:56:45 -0700 Subject: [PATCH 458/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 59ef1d5..c1f5964 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,7 @@ Please give this repo a ⭐ if it inspires you. |[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| |[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| |[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| +|[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| ## Hard |LC #|Description| From 9a4e4dbf4a2bd5db5b845cce6f09ad4a8c509e86 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Mar 2022 21:58:30 -0700 Subject: [PATCH 459/969] Create L74.go --- Medium/L74.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L74.go diff --git a/Medium/L74.go b/Medium/L74.go new file mode 100644 index 0000000..21c467d --- /dev/null +++ b/Medium/L74.go @@ -0,0 +1,39 @@ +package Medium + +func searchMatrix(matrix [][]int, target int) bool { + if len(matrix) == 0 { + return false + } + + m, n := len(matrix), len(matrix[0]) + for i := 0; i < m; i++ { + if matrix[i][n-1] == target { + return true + } + + if matrix[i][n-1] > target { + return binSearch(matrix[i], target) + } + } + + return false +} + +func binSearch(n []int, tgt int) bool { + st, end := 0, len(n)-1 + + for st <= end { + mid := st + (end-st)/2 + if n[mid] == tgt { + return true + } + + if n[mid] < tgt { + st = mid + 1 + } else { + end = mid - 1 + } + } + + return false +} From 7731554d0191c807c2ffe47a21f34fd76fc8bf56 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 06:45:02 -0700 Subject: [PATCH 460/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c1f5964..2a24cf4 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Please give this repo a ⭐ if it inspires you. |[704](https://leetcode.com/problems/binary-search/)| Binary Search| |[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| |[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| +|[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| ## Medium |LC #|Description| From 7575d753545032306594ac5847418568743d3f43 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 06:45:27 -0700 Subject: [PATCH 461/969] Create L266.go --- Easy/L266.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Easy/L266.go diff --git a/Easy/L266.go b/Easy/L266.go new file mode 100644 index 0000000..fca1027 --- /dev/null +++ b/Easy/L266.go @@ -0,0 +1,17 @@ +package Easy + +var exists struct{} + +func canPermutePalindrome(s string) bool { + mp := make(map[byte]struct{}) + + for i := range s { + if _, ok := mp[s[i]]; !ok { + mp[s[i]] = exists + } else { + delete(mp, s[i]) + } + } + + return len(mp) <= 1 +} From 747266806771332ecb86d85348b88f3a39468486 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 06:53:29 -0700 Subject: [PATCH 462/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2a24cf4..af8fbd1 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Please give this repo a ⭐ if it inspires you. |[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| |[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| |[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| +|[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| ## Medium |LC #|Description| From be603d827b4754d587f791613345b8d53db88f01 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 06:54:11 -0700 Subject: [PATCH 463/969] Create L145.go --- Easy/L145.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L145.go diff --git a/Easy/L145.go b/Easy/L145.go new file mode 100644 index 0000000..30df343 --- /dev/null +++ b/Easy/L145.go @@ -0,0 +1,22 @@ +package Easy + +func postorderTraversal(root *TreeNode) []int { + var res []int + if root == nil { + return res + } + + postOrder(root, &res) + + return res +} + +func postOrder(root *TreeNode, list *[]int) { + if root == nil { + return + } + + postOrder(root.Left, list) + postOrder(root.Right, list) + *list = append(*list, root.Val) +} From e583de4a879044039169b7adebc7d60306e200ef Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 07:01:41 -0700 Subject: [PATCH 464/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index af8fbd1..2f2ed38 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Please give this repo a ⭐ if it inspires you. |[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| |[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| |[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| +|[169](https://leetcode.com/problems/majority-element/)| Majority Element| ## Medium |LC #|Description| From 4ab9f8eeaddb4b3d862b9bb780c311a463afb272 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 07:02:05 -0700 Subject: [PATCH 465/969] Create L169.go --- Easy/L169.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L169.go diff --git a/Easy/L169.go b/Easy/L169.go new file mode 100644 index 0000000..5098eb8 --- /dev/null +++ b/Easy/L169.go @@ -0,0 +1,19 @@ +package Easy + +func majorityElement(nums []int) int { + cnt, candidate := 0, -1 + + for _, n := range nums { + if cnt == 0 { + candidate = n + } + + if candidate == n { + cnt++ + } else { + cnt -= 1 + } + } + + return candidate +} From b0cbcf2f05d96d0139707bc96d7524061a694790 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 21:44:19 -0700 Subject: [PATCH 466/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2f2ed38..b78e13c 100644 --- a/README.md +++ b/README.md @@ -377,3 +377,4 @@ Please give this repo a ⭐ if it inspires you. |[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| |[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| |[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| +|[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| From ddd16ec2c64c851de108c0dbaf5dccb1cb4de0c2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 21:44:37 -0700 Subject: [PATCH 467/969] Create L410.go --- Hard/L410.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Hard/L410.go diff --git a/Hard/L410.go b/Hard/L410.go new file mode 100644 index 0000000..c3bf716 --- /dev/null +++ b/Hard/L410.go @@ -0,0 +1,42 @@ +package Hard + +func splitArray(nums []int, m int) int { + if len(nums) == 0 || m == 0 { + return 0 + } + + max, sum := math.MinInt32, 0 + for _, n := range nums { + if n > max { + max = n + } + sum += n + } + + l, r, mid := max, sum, 0 + + for l < r { + mid = l + (r-l)/2 + if isValid(nums, mid, m) { + r = mid + } else { + l = mid + 1 + } + } + + return l +} + +func isValid(nums []int, cap, m int) bool { + days, sum := 1, 0 + + for _, n := range nums { + sum += n + if sum > cap { + sum = n + days++ + } + } + + return days <= m +} From 6847ba56c23f57f6296f04bae5e5a0f2be7f802d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 21:56:33 -0700 Subject: [PATCH 468/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b78e13c..b5454b1 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ Please give this repo a ⭐ if it inspires you. |[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| |[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| |[169](https://leetcode.com/problems/majority-element/)| Majority Element| +|[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| ## Medium |LC #|Description| From ca130805617759cade64ce8294a7b2da58bb6d72 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 30 Mar 2022 21:56:54 -0700 Subject: [PATCH 469/969] Create L242.go --- Easy/L242.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L242.go diff --git a/Easy/L242.go b/Easy/L242.go new file mode 100644 index 0000000..9434b75 --- /dev/null +++ b/Easy/L242.go @@ -0,0 +1,22 @@ +package Easy + +func isAnagram(s string, t string) bool { + alphabet := make([]int, 26) + for i := range s { + alphabet[s[i]-'a']++ + } + for i := range t { + alphabet[t[i]-'a']-- + if alphabet[t[i]-'a'] < 0 { + return false + } + } + + for _, a := range alphabet { + if a != 0 { + return false + } + } + + return true +} From 152ef0c4e1f8e2b91641749aaee1266f527a1718 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 1 Apr 2022 07:18:05 -0700 Subject: [PATCH 470/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b5454b1..7c64b25 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ Please give this repo a ⭐ if it inspires you. |[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| |[169](https://leetcode.com/problems/majority-element/)| Majority Element| |[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| +|[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| ## Medium |LC #|Description| From 23518b64f708445fc11187d73557253ee984b535 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 1 Apr 2022 07:18:25 -0700 Subject: [PATCH 471/969] Create L217.go --- Easy/L217.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L217.go diff --git a/Easy/L217.go b/Easy/L217.go new file mode 100644 index 0000000..9bd0e9b --- /dev/null +++ b/Easy/L217.go @@ -0,0 +1,15 @@ +package Easy + +var exists struct{} +func containsDuplicate(nums []int) bool { + mp := make(map[int]struct{} + + for _, n := range nums { + if _, ok := mp[n]; ok { + return true + } + mp[n] = exists + } + + return len(mp) != len(nums) +} From 0815877f8c05d9f66bdfb552b41c7d0fa473ffcd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 1 Apr 2022 09:33:50 -0700 Subject: [PATCH 472/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7c64b25..d69f187 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ Please give this repo a ⭐ if it inspires you. |[169](https://leetcode.com/problems/majority-element/)| Majority Element| |[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| |[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| +|[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| ## Medium |LC #|Description| From 8dec00650c1677bafc6adae9916539e58df8863d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 1 Apr 2022 09:34:35 -0700 Subject: [PATCH 473/969] Create L283.go --- Easy/L283.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L283.go diff --git a/Easy/L283.go b/Easy/L283.go new file mode 100644 index 0000000..23384e2 --- /dev/null +++ b/Easy/L283.go @@ -0,0 +1,20 @@ +package Easy + +func moveZeroes(nums []int) { + if len(nums) == 0 { + return + } + + insertPos := 0 + for _, n := range nums { + if n != 0 { + nums[insertPos] = n + insertPos++ + } + } + + for insertPos < len(nums) { + nums[insertPos] = 0 + insertPos++ + } +} From 38fd0352603cab3438f09fd3545f4c9edc58a58a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Apr 2022 06:23:09 -0700 Subject: [PATCH 474/969] Create L11.go --- Medium/L11.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L11.go diff --git a/Medium/L11.go b/Medium/L11.go new file mode 100644 index 0000000..6a7f332 --- /dev/null +++ b/Medium/L11.go @@ -0,0 +1,33 @@ +package Medium + +func maxArea(height []int) int { + l, r, mini, maxi := 0, len(height)-1, math.MaxInt32, math.MinInt32 + + for l <= r { + mini = min(height[l], height[r]) + maxi = max(maxi, mini*(r-l)) + if l <= r && height[l] < height[r] { + l++ + } else { + r-- + } + } + + return maxi +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From fbbac8a67ee6b0966b033c90063c237f33572886 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Apr 2022 06:24:04 -0700 Subject: [PATCH 475/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d69f187..371a22e 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,7 @@ Please give this repo a ⭐ if it inspires you. |[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| |[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| |[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| +|[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| ## Hard |LC #|Description| From 306fe487c03d3694e289aa9dedff6ca0f6b282e0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Apr 2022 15:39:29 -0700 Subject: [PATCH 476/969] Create L1260.go --- Easy/L1260.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Easy/L1260.go diff --git a/Easy/L1260.go b/Easy/L1260.go new file mode 100644 index 0000000..2383d85 --- /dev/null +++ b/Easy/L1260.go @@ -0,0 +1,32 @@ +package Easy + +func shiftGrid(grid [][]int, k int) [][]int { + r, c := len(grid), len(grid[0]) + n := r * c + sh := k % n + + res := make([][]int, len(grid)) + for i := range grid { + res[i] = make([]int, len(grid[i])) + copy(res[i], grid[i]) + } + + reverse(&res, 0, n-sh) + reverse(&res, n-sh, n) + reverse(&res, 0, n) + + return res +} + +func reverse(grid *[][]int, lo, hi int) { + for lo < hi { + hi-- + swap(grid, lo, hi) + lo++ + } +} + +func swap(grid *[][]int, lo, hi int) { + n := len((*grid)[0]) + (*grid)[lo/n][lo%n], (*grid)[hi/n][hi%n] = (*grid)[hi/n][hi%n], (*grid)[lo/n][lo%n] +} From b9a5b67214e01dfc26aafbc999a8143f7546addd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Apr 2022 15:39:57 -0700 Subject: [PATCH 477/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 371a22e..9756176 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ Please give this repo a ⭐ if it inspires you. |[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| |[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| |[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| +|[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| ## Medium |LC #|Description| From 2e9c5971315011a77d3ef526f574b56b0ab50114 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Apr 2022 16:56:47 -0700 Subject: [PATCH 478/969] Create L700.go --- Easy/L700.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L700.go diff --git a/Easy/L700.go b/Easy/L700.go new file mode 100644 index 0000000..fa16bea --- /dev/null +++ b/Easy/L700.go @@ -0,0 +1,13 @@ +package Easy + +func searchBST(root *TreeNode, val int) *TreeNode { + if root == nil || val == root.Val { + return root + } + + if val < root.Val { + return searchBST(root.Left, val) + } + + return searchBST(root.Right, val) +} From fb70b9c8e3e61e6363915d09b476d06c201dbb02 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Apr 2022 16:57:17 -0700 Subject: [PATCH 479/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9756176..44dbf73 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ Please give this repo a ⭐ if it inspires you. |[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| |[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| |[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| +|[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| ## Medium |LC #|Description| From 6dc3caf9213071902d37cff82a89bbfdb9411987 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Apr 2022 08:01:55 -0700 Subject: [PATCH 480/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 44dbf73..1faa5be 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,7 @@ Please give this repo a ⭐ if it inspires you. |[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| |[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| |[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| +|[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| ## Hard |LC #|Description| From 2c0f3cf13c36feb26936102778071b18d1d6c6cb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Apr 2022 08:02:16 -0700 Subject: [PATCH 481/969] Create L538.go --- Medium/L538.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L538.go diff --git a/Medium/L538.go b/Medium/L538.go new file mode 100644 index 0000000..609f36d --- /dev/null +++ b/Medium/L538.go @@ -0,0 +1,18 @@ +package Medium + +func convertBST(root *TreeNode) *TreeNode { + sum := 0 + convert(root, &sum) + return root +} + +func convert(cur *TreeNode, sum *int) { + if cur == nil { + return + } + + convert(cur.Right, sum) + cur.Val += *sum + *sum = cur.Val + convert(cur.Left, sum) +} From 13520f97f1ee45a2f4b100d5e4bd93c617ab5476 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 18 Apr 2022 15:54:44 -0700 Subject: [PATCH 482/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1faa5be..b7e5137 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,7 @@ Please give this repo a ⭐ if it inspires you. |[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| |[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| |[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| +|[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| ## Hard |LC #|Description| From 59d17e9be6393ea4292e40dc4d407bdd3265eaff Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 18 Apr 2022 15:55:02 -0700 Subject: [PATCH 483/969] Create L230.go --- Medium/L230.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L230.go diff --git a/Medium/L230.go b/Medium/L230.go new file mode 100644 index 0000000..37b6e09 --- /dev/null +++ b/Medium/L230.go @@ -0,0 +1,32 @@ +package Medium + +func kthSmallest(root *TreeNode, k int) int { + if root == nil { + return 0 + } + + var st []*TreeNode + st = append(st, root) + + for len(st) > 0 { + node := st[len(st)-1] + st = st[:len(st)-1] + for node != nil { + st = append(st, node) + node = node.Left + } + + if len(st) > 0 { + node = st[len(st)-1] + st = st[:len(st)-1] + + k-- + if k == 0 { + return node.Val + } + st = append(st, node.Right) + } + } + + return 0 +} From 715e32f472e0e078ce21ea0ca3970cfdad1ab41b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Apr 2022 16:44:47 -0700 Subject: [PATCH 484/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b7e5137..638120f 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,7 @@ Please give this repo a ⭐ if it inspires you. |[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| |[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| |[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| +|[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| ## Hard |LC #|Description| From 244496d2aad60dae48d272e4b1733b5e9f094b0b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Apr 2022 16:45:10 -0700 Subject: [PATCH 485/969] Create L284.go --- Medium/L284.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Medium/L284.go diff --git a/Medium/L284.go b/Medium/L284.go new file mode 100644 index 0000000..516bf13 --- /dev/null +++ b/Medium/L284.go @@ -0,0 +1,54 @@ +package Medium + +/* Below is the interface for Iterator, which is already defined for you. + * + * type Iterator struct { + * + * } + * + * func (this *Iterator) hasNext() bool { + * // Returns true if the iteration has more elements. + * } + * + * func (this *Iterator) next() int { + * // Returns the next element in the iteration. + * } + */ + +type PeekingIterator struct { + nextInt int + iter *Iterator +} + +func Constructor(iter *Iterator) *PeekingIterator { + pk := &PeekingIterator{ + nextInt: -1, + iter: iter, + } + + if iter.hasNext() { + pk.nextInt = iter.next() + } + + return pk +} + +func (this *PeekingIterator) hasNext() bool { + return this.nextInt != -1 +} + +func (this *PeekingIterator) next() int { + res := this.nextInt + + if this.iter.hasNext() { + this.nextInt = this.iter.next() + } else { + this.nextInt = -1 + } + + return res +} + +func (this *PeekingIterator) peek() int { + return this.nextInt +} From babcb1a1b5757a325081303db6bcf14c4a6b0b09 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 30 Apr 2022 22:53:09 -0700 Subject: [PATCH 486/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 638120f..1b8b0b3 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ Please give this repo a ⭐ if it inspires you. |[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| |[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| |[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| +|[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| ## Medium |LC #|Description| From fc4a23d8b081218a82f98ded180b222940913740 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 30 Apr 2022 22:53:51 -0700 Subject: [PATCH 487/969] Create L844.go --- Easy/L844.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Easy/L844.go diff --git a/Easy/L844.go b/Easy/L844.go new file mode 100644 index 0000000..b35dc8d --- /dev/null +++ b/Easy/L844.go @@ -0,0 +1,24 @@ +package Easy + +func backspaceCompare(s string, t string) bool { + return eval(s) == eval(t) +} + +func eval(s string) string { + n, cnt := len(s), 0 + var res strings.Builder + + for i := n - 1; i >= 0; i-- { + if s[i] == '#' { + cnt++ + } else { + if cnt > 0 { + cnt-- + } else { + res.WriteByte(s[i]) + } + } + } + + return res.String() +} From 59f6b1ba5928f2cb49c72f9428f6f1309c15e627 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 10 May 2022 07:07:05 -0700 Subject: [PATCH 488/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1b8b0b3..1bb9a89 100644 --- a/README.md +++ b/README.md @@ -341,6 +341,7 @@ Please give this repo a ⭐ if it inspires you. |[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| |[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| |[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| +|[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| ## Hard |LC #|Description| From abe52d8d61285cf160828f09a4e982c6940bc601 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 10 May 2022 07:08:44 -0700 Subject: [PATCH 489/969] Create L216.go --- Medium/L216.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L216.go diff --git a/Medium/L216.go b/Medium/L216.go new file mode 100644 index 0000000..723f4d3 --- /dev/null +++ b/Medium/L216.go @@ -0,0 +1,33 @@ +package Medium + +func combinationSum3(k int, n int) [][]int { + var ans [][]int + var tmp []int + + if k == 0 || n == 0 { + return ans + } + + arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + backtrack(&ans, &tmp, arr, k, n, 0) + + return ans +} + +func backtrack(ans *[][]int, tmp *[]int, arr []int, k, target, start int) { + if len(*tmp) == k && target == 0 { + tmpCopy := make([]int, len(*tmp)) + copy(tmpCopy, *tmp) + *ans = append(*ans, tmpCopy) + } + + if target < 0 { + return + } + + for i := start; i < len(arr); i++ { + *tmp = append(*tmp, arr[i]) + backtrack(ans, tmp, arr, k, target-arr[i], i+1) + *tmp = (*tmp)[:len(*tmp)-1] + } +} From 40385245734f3233a88de3b456f0755cda6fd80e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 May 2022 07:27:58 -0700 Subject: [PATCH 490/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1bb9a89..33123e9 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ Please give this repo a ⭐ if it inspires you. |[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| |[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| |[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| +|[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| ## Hard |LC #|Description| From bb84f440cb66c104ce55ab0e7d64b6bc4a5ae146 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 May 2022 07:28:25 -0700 Subject: [PATCH 491/969] Create L47.go --- Medium/L47.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L47.go diff --git a/Medium/L47.go b/Medium/L47.go new file mode 100644 index 0000000..17909b0 --- /dev/null +++ b/Medium/L47.go @@ -0,0 +1,38 @@ +package Medium + +func permuteUnique(nums []int) [][]int { + var ( + ans [][]int + tmp []int + ) + if len(nums) == 0 { + return ans + } + + sort.Ints(nums) + used := make([]bool, len(nums)) + backTrack(&ans, &tmp, nums, &used) + + return ans +} + +func backTrack(ans *[][]int, tmp *[]int, nums []int, used *[]bool) { + if len(*tmp) == len(nums) { + tmpCopy := make([]int, len(*tmp)) + copy(tmpCopy, *tmp) + *ans = append(*ans, tmpCopy) + return + } + + for i := range nums { + if (*used)[i] || (i > 0 && nums[i] == nums[i-1] && !(*used)[i-1]) { + continue + } + + (*used)[i] = true + *tmp = append(*tmp, nums[i]) + backTrack(ans, tmp, nums, used) + (*used)[i] = false + *tmp = (*tmp)[:len(*tmp)-1] + } +} From e906f119735765dddbc5f64d9d95394e52e7d51a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 May 2022 16:30:07 -0700 Subject: [PATCH 492/969] Create L117.go --- Medium/L117.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L117.go diff --git a/Medium/L117.go b/Medium/L117.go new file mode 100644 index 0000000..96a44fa --- /dev/null +++ b/Medium/L117.go @@ -0,0 +1,34 @@ +package Medium + +func connect(root *Node) *Node { + if root == nil { + return root + } + + var q []*Node + q = append(q, root) + + for len(q) > 0 { + size := len(q) + tmp := make([]*Node, len(q)) + copy(tmp, q) + for i := 0; i < size; i++ { + node := q[0] + q = q[1:] + + if i != size-1 { + node.Next = tmp[i+1] + } + + if node.Left != nil { + q = append(q, node.Left) + } + + if node.Right != nil { + q = append(q, node.Right) + } + } + } + + return root +} From 76f64719526e0a3ca6c0a956fff6a87fae89a1b0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 May 2022 16:30:47 -0700 Subject: [PATCH 493/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 33123e9..8654925 100644 --- a/README.md +++ b/README.md @@ -343,6 +343,7 @@ Please give this repo a ⭐ if it inspires you. |[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| |[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| |[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| +|[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| ## Hard |LC #|Description| From a32b44a60d9b142a997626dec94124c6c8af60a7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 May 2022 17:29:35 -0700 Subject: [PATCH 494/969] Create L743.go --- Medium/L743.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Medium/L743.go diff --git a/Medium/L743.go b/Medium/L743.go new file mode 100644 index 0000000..153b272 --- /dev/null +++ b/Medium/L743.go @@ -0,0 +1,30 @@ +package Medium + +func networkDelayTime(times [][]int, n int, k int) int { + dist := make([]int, n+1) + for i := 1; i < len(dist); i++ { + dist[i] = math.MaxInt32 + } + dist[k] = 0 + + for i := 1; i < n; i++ { + for _, t := range times { + source, dest, weight := t[0], t[1], t[2] + if dist[source] != math.MaxInt32 && dist[source]+weight < dist[dest] { + dist[dest] = dist[source] + weight + } + } + } + + max := math.MinInt32 + for i := range dist { + if dist[i] == math.MaxInt32 { + return -1 + } + if dist[i] > max { + max = dist[i] + } + } + + return max +} From 1708bc46200879191f0feddb4de09d1574e0c081 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 May 2022 17:30:11 -0700 Subject: [PATCH 495/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8654925..e0f36bb 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,7 @@ Please give this repo a ⭐ if it inspires you. |[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| |[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| |[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| +|[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| ## Hard |LC #|Description| From b6dc813b67f4b14dbe16b3dac22d40b4a1f02801 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 15 May 2022 08:22:25 -0700 Subject: [PATCH 496/969] Update L1302.go --- Medium/L1302.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Medium/L1302.go b/Medium/L1302.go index b0e7e99..6090328 100644 --- a/Medium/L1302.go +++ b/Medium/L1302.go @@ -9,34 +9,33 @@ package Medium * } */ func deepestLeavesSum(root *TreeNode) int { - if root == nil { + if root == nil { return 0 } - - sum := 0 + var q []*TreeNode q = append(q, root) - + sum := 0 + for len(q) > 0 { size := len(q) sum = 0 for ; size > 0; size-- { - n := q[0] + node := q[0] q = q[1:] - - if n.Left == nil && n.Right == nil { - sum += n.Val - } - - if n.Left != nil { - q = append(q, n.Left) - } - - if n.Right != nil { - q = append(q, n.Right) + + if node.Left == nil && node.Right == nil { + sum += node.Val + } else { + if node.Left != nil { + q = append(q, node.Left) + } + if node.Right != nil { + q = append(q, node.Right) + } } } } - + return sum } From ca5d2ba2c8d0b75d940bd02b2ab8258afc0621f3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 23 May 2022 16:04:35 -0700 Subject: [PATCH 497/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e0f36bb..e44ae62 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,7 @@ Please give this repo a ⭐ if it inspires you. |[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| |[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| |[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| +|[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| ## Hard |LC #|Description| From 35365a3297daa69d4e2f7f896dfa6c2503d5d276 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 23 May 2022 16:05:40 -0700 Subject: [PATCH 498/969] Update L474.go --- Medium/L474.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Medium/L474.go b/Medium/L474.go index a44653d..08302d5 100644 --- a/Medium/L474.go +++ b/Medium/L474.go @@ -34,3 +34,58 @@ func max(a, b int) int { return b } + + +// Recursion + Memoization +func findMaxForm(strs []string, m int, n int) int { + if len(strs) == 0 { + return 0 + } + + return solve(strs, make(map[string]int), m, n, 0) +} + +func solve(strs []string, mp map[string]int, m, n, index int) int { + // If we are done with the our remaining zeros and ones return 0 as we cant get any furtherr strings. + if m == 0 && n == 0 { + return 0 + } + + if index >= len(strs) { + return 0 + } + + key := fmt.Sprintf("%d:%d:%d", m, n, index) + if _, ok := mp[key]; ok { + return mp[key] + } + + // For the current index count the required number of zeros and ones . + totalCnt, curr := 0, strs[index] + ones, zeroes := 0, 0 + + for i := range curr { + if curr[i] == '0' { + zeroes++ + } else { + ones++ + } + } + + takenStrCnt := 0 + // if we have the required number of zeros and ones we take the current string and update the + // remaining zeros and ones and go to the next index + if ones <= n && zeroes <= m { + takenStrCnt += 1 + solve(strs, mp, m-zeroes, n-ones, index+1) + } + + skippedStrNum := solve(strs, mp, m, n, index+1) + totalCnt = skippedStrNum + // For every position we also the option to skip the current string + if takenStrCnt > skippedStrNum { + totalCnt = takenStrCnt + } + + mp[key] = totalCnt + return totalCnt +} From 9265dfaec8a879d319f04d5f8578613aa4b0b28d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Jun 2022 07:27:30 -0700 Subject: [PATCH 499/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e44ae62..b680ada 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ Please give this repo a ⭐ if it inspires you. |[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| |[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| |[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| +|[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| ## Medium |LC #|Description| From 3f4a2d945ddfce9b393865b44e23b91c0febc37b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Jun 2022 07:27:54 -0700 Subject: [PATCH 500/969] Create L88.go --- Easy/L88.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L88.go diff --git a/Easy/L88.go b/Easy/L88.go new file mode 100644 index 0000000..99d94d5 --- /dev/null +++ b/Easy/L88.go @@ -0,0 +1,19 @@ +package Easy + +func merge(nums1 []int, m int, nums2 []int, n int) { + i, j, k := m-1, n-1, m+n-1 + + for i >= 0 && j >= 0 { + if nums1[i] > nums2[j] { + nums1[k] = nums1[i] + k, i = k-1, i-1 + } else { + nums1[k] = nums2[j] + k, j = k-1, j-1 + } + } + for j >= 0 { + nums1[k] = nums2[j] + k, j = k-1, j-1 + } +} From 7114e4cd8a2fbfa9aaeec2f673ac7278fe920966 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 9 Jun 2022 12:24:01 -0700 Subject: [PATCH 501/969] Create L167.go --- Medium/L167.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L167.go diff --git a/Medium/L167.go b/Medium/L167.go new file mode 100644 index 0000000..bd35d6d --- /dev/null +++ b/Medium/L167.go @@ -0,0 +1,37 @@ +package Medium + +func twoSum(numbers []int, target int) []int { + ret := make([]int, 2) + + if len(numbers) == 0 { + return ret + } + + for i := range numbers { + idx := binSearch(numbers, i+1, len(numbers)-1, target-numbers[i]) + if idx != -1 { + ret[0], ret[1] = i+1, idx+1 + break + } + } + + return ret +} + +func binSearch(arr []int, st, end, tgt int) int { + for st <= end { + mid := st + (end-st)/2 + + if arr[mid] == tgt { + return mid + } + + if arr[mid] > tgt { + end = mid - 1 + } else { + st = mid + 1 + } + } + + return -1 +} From 02dc09af6fefd0e37970c101cdfb46d41402297c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 9 Jun 2022 12:24:51 -0700 Subject: [PATCH 502/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b680ada..9df2792 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,7 @@ Please give this repo a ⭐ if it inspires you. |[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| |[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| |[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| +|[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| ## Hard |LC #|Description| From ee47c759d5877fab6f4f619af4145083143945b1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Jun 2022 17:09:53 -0700 Subject: [PATCH 503/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9df2792..23e5830 100644 --- a/README.md +++ b/README.md @@ -348,6 +348,7 @@ Please give this repo a ⭐ if it inspires you. |[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| |[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| |[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| +|[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| ## Hard |LC #|Description| From 29568e35137af64abd7a9684940961dbcc9d4224 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Jun 2022 17:10:13 -0700 Subject: [PATCH 504/969] Create L3.go --- Medium/L3.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L3.go diff --git a/Medium/L3.go b/Medium/L3.go new file mode 100644 index 0000000..6ccbbe4 --- /dev/null +++ b/Medium/L3.go @@ -0,0 +1,27 @@ +package Medium + +func lengthOfLongestSubstring(s string) int { + if len(s) == 0 { + return 0 + } + + mp, max := make(map[byte]int), 0 + + for i, j := 0, 0; i < len(s); i++ { + if _, ok := mp[s[i]]; ok { + j = maximum(j, mp[s[i]]+1) + } + mp[s[i]] = i + max = maximum(max, i-j+1) + } + + return max +} + +func maximum(i, j int) int { + if i > j { + return i + } + + return j +} From aa81e03e079e3724dbe63b3251b257be8e018146 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Jun 2022 21:12:00 -0700 Subject: [PATCH 505/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 23e5830..37b5834 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,7 @@ Please give this repo a ⭐ if it inspires you. |[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| |[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| |[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| +|[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| ## Hard |LC #|Description| From 3552b9849b4874c8b7642d62834ff1955d8e21ca Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 11 Jun 2022 22:00:31 -0700 Subject: [PATCH 506/969] Create L1658.go --- Medium/L1658.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L1658.go diff --git a/Medium/L1658.go b/Medium/L1658.go new file mode 100644 index 0000000..67b04c7 --- /dev/null +++ b/Medium/L1658.go @@ -0,0 +1,37 @@ +package Medium + +func minOperations(nums []int, x int) int { + tgt := -x + for _, n := range nums { + tgt += n + } + + if tgt == 0 { + return len(nums) + } + + mp, sum, res := make(map[int]int), 0, math.MinInt32 + mp[0] = -1 + for i := 0; i < len(nums); i++ { + sum += nums[i] + if _, ok := mp[sum-tgt]; ok { + res = maximum(res, i-mp[sum-tgt]) + } + + mp[sum] = i + } + + if res == math.MinInt32 { + return -1 + } + + return len(nums) - res +} + +func maximum(i, j int) int { + if i > j { + return i + } + + return j +} From a680afdc4c12d5a440e1d1f0464c12ea1bdfc87d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 15:48:29 -0700 Subject: [PATCH 507/969] Create L215.go --- Medium/L215.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Medium/L215.go diff --git a/Medium/L215.go b/Medium/L215.go new file mode 100644 index 0000000..6fb4d5b --- /dev/null +++ b/Medium/L215.go @@ -0,0 +1,28 @@ +package Medium + +func findKthLargest(nums []int, k int) int { + return quickSelect(nums, 0, len(nums)-1, k) +} + +func quickSelect(nums []int, low, high, k int) int { + pivot := low + + j := low + for ; j < high; j++ { + if nums[j] <= nums[high] { + nums[pivot], nums[j] = nums[j], nums[pivot] + pivot++ + } + } + + nums[pivot], nums[j] = nums[j], nums[pivot] + cnt := high - pivot + 1 + + if cnt == k { + return nums[pivot] + } else if cnt > k { + return quickSelect(nums, pivot+1, high, k) + } + + return quickSelect(nums, low, pivot-1, k-cnt) +} From 1d8f4b619e5ec375ccf98ac7cd9d12076f5e197a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 15:49:07 -0700 Subject: [PATCH 508/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 37b5834..9b1e6ae 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,7 @@ Please give this repo a ⭐ if it inspires you. |[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| |[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| |[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| +|[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| ## Hard |LC #|Description| From 760618c9be3f84cd88f8ec87ae396a9460d95899 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 16:33:43 -0700 Subject: [PATCH 509/969] Create L859.go --- Easy/L859.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Easy/L859.go diff --git a/Easy/L859.go b/Easy/L859.go new file mode 100644 index 0000000..d88a61d --- /dev/null +++ b/Easy/L859.go @@ -0,0 +1,26 @@ +package Easy + +func buddyStrings(s string, goal string) bool { + if len(s) != len(goal) { + return false + } + + var exists struct{} + uniqueS := make(map[byte]struct{}) + for i := range s { + uniqueS[s[i]] = exists + } + if len(uniqueS) < len(s) && s == goal { + return true + } + + var diff []byte + for i := 0; i < len(s) && len(diff) <= 4; i++ { + if s[i] != goal[i] { + diff = append(diff, s[i]) + diff = append(diff, goal[i]) + } + } + + return len(diff) == 4 && diff[0] == diff[3] && diff[1] == diff[2] +} From 0206fe39b869e2d162c9b0173d700bc9beebf284 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 16:34:14 -0700 Subject: [PATCH 510/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9b1e6ae..2822073 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ Please give this repo a ⭐ if it inspires you. |[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| |[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| |[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| +|[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| ## Medium |LC #|Description| From d8cbe7eb126e88b23fe20377e436c1e1b11b5a17 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 17:09:09 -0700 Subject: [PATCH 511/969] Create L1570.go --- Medium/L1570.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L1570.go diff --git a/Medium/L1570.go b/Medium/L1570.go new file mode 100644 index 0000000..a131db4 --- /dev/null +++ b/Medium/L1570.go @@ -0,0 +1,33 @@ +package Medium + +type SparseVector struct { + mp map[int]int +} + +func Constructor(nums []int) SparseVector { + mp := make(map[int]int) + + for i := range nums { + if nums[i] != 0 { + mp[i] = nums[i] + } + } + + return SparseVector{mp: mp} +} + +// Return the dotProduct of two sparse vectors +func (this *SparseVector) dotProduct(vec SparseVector) int { + if len(vec.mp) < len(this.mp) { + return vec.dotProduct(*this) + } + + sum := 0 + for k, _ := range this.mp { + if _, ok := vec.mp[k]; ok { + sum += this.mp[k] * vec.mp[k] + } + } + + return sum +} From b431a0e6d3c2e6f634f15e98871407f7e14b5b81 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Jun 2022 17:09:57 -0700 Subject: [PATCH 512/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2822073..1fc5ef9 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,7 @@ Please give this repo a ⭐ if it inspires you. |[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| |[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| |[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| +|[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| ## Hard |LC #|Description| From 0ce82c286455c005a142bd5a10f3702d77eecd8b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Jul 2022 16:27:45 -0700 Subject: [PATCH 513/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1fc5ef9..97c5e0f 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,7 @@ Please give this repo a ⭐ if it inspires you. |[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| |[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| |[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| +|[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| ## Hard |LC #|Description| From 5269c5db0857eab1680273d02ec866e418b0f3aa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Jul 2022 16:28:29 -0700 Subject: [PATCH 514/969] Create L199.go --- Medium/L199.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L199.go diff --git a/Medium/L199.go b/Medium/L199.go new file mode 100644 index 0000000..31db945 --- /dev/null +++ b/Medium/L199.go @@ -0,0 +1,35 @@ +package Medium + +func rightSideView(root *TreeNode) []int { + var res []int + + if root == nil { + return res + } + + var q []*TreeNode + q = append(q, root) + + for len(q) > 0 { + size := len(q) + + for size > 0 { + size-- + cur := q[0] + q = q[1:] + + if size == 0 { + res = append(res, cur.Val) + } + + if cur.Left != nil { + q = append(q, cur.Left) + } + if cur.Right != nil { + q = append(q, cur.Right) + } + } + } + + return res +} From cce2060ee6ccbcfdabdeb27855b1c9dec496ee48 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Jul 2022 18:39:27 -0700 Subject: [PATCH 515/969] Create L629.go --- Hard/L629.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Hard/L629.go diff --git a/Hard/L629.go b/Hard/L629.go new file mode 100644 index 0000000..7bbfa18 --- /dev/null +++ b/Hard/L629.go @@ -0,0 +1,40 @@ +package Hard + +func kInversePairs(n int, k int) int { + memo := make([][]int, n+1) + for i := 0; i < n+1; i++ { + memo[i] = make([]int, k+1) + for j := 0; j < k+1; j++ { + memo[i][j] = -1 + } + } + + return solve(&memo, 1, n, k) +} + +func solve(memo *[][]int, num, n, k int) int { + mod := 1000000007 + if num == n+1 { + if k == 0 { + return 1 + } + + return 0 + } + + if (*memo)[num][k] != -1 { + return (*memo)[num][k] + } + + cnt, i := 0, 0 + if k-num+1 > 0 { + i = k - num + 1 + } + for ; i <= k; i++ { + val := solve(memo, num+1, n, i) + cnt = (cnt + val) % mod + } + + (*memo)[num][k] = cnt + return (*memo)[num][k] +} From 5331bcca99cc08e63b3f1fff2c9c75bb27fc03b4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 16 Jul 2022 18:40:01 -0700 Subject: [PATCH 516/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 97c5e0f..ee30c91 100644 --- a/README.md +++ b/README.md @@ -401,3 +401,4 @@ Please give this repo a ⭐ if it inspires you. |[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| |[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| |[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| +|[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| From 31bd5da76659a9fa2d4d201dc23a8746191cb5bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 18 Jul 2022 14:46:43 -0700 Subject: [PATCH 517/969] Create L1074.go --- Hard/L1074.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Hard/L1074.go diff --git a/Hard/L1074.go b/Hard/L1074.go new file mode 100644 index 0000000..b9f9ddf --- /dev/null +++ b/Hard/L1074.go @@ -0,0 +1,32 @@ +package Hard + +func numSubmatrixSumTarget(matrix [][]int, target int) int { + res, m, n := 0, len(matrix), len(matrix[0]) + + for i := 0; i < m; i++ { + for j := 1; j < n; j++ { + matrix[i][j] += matrix[i][j-1] + } + } + + for i := 0; i < n; i++ { + for j := i; j < n; j++ { + mp := make(map[int]int) + mp[0] = 1 + cur := 0 + for k := 0; k < m; k++ { + if i > 0 { + cur += matrix[k][j] - matrix[k][i - 1] + } else { + cur += matrix[k][j] + } + if v, ok := mp[cur - target]; ok { + res += v + } + mp[cur]++ + } + } + } + + return res +} From 7fe15fcf7b5f2fa92ab39ea06b3c5b2fcaecfdfa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 18 Jul 2022 14:47:18 -0700 Subject: [PATCH 518/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ee30c91..c41c0a6 100644 --- a/README.md +++ b/README.md @@ -402,3 +402,4 @@ Please give this repo a ⭐ if it inspires you. |[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| |[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| |[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| +|[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| From 5398c849ec1102baffc7094ef1b8b7d1b9330eca Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 21 Jul 2022 15:24:34 -0700 Subject: [PATCH 519/969] Create L92.go --- Medium/L92.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/L92.go diff --git a/Medium/L92.go b/Medium/L92.go new file mode 100644 index 0000000..e76daf9 --- /dev/null +++ b/Medium/L92.go @@ -0,0 +1,55 @@ +package Medium + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reverseBetween(head *ListNode, left int, right int) *ListNode { + if head == nil || (left == right) { + return head + } + + var start, end, startPrev, endNext *ListNode + curr := head + for i := 1; i < left; i++ { + startPrev = curr + curr = curr.Next + } + + start = curr + curr = head + for i := 1; i < right; i++ { + curr = curr.Next + } + end = curr + endNext = curr.Next + reverse(start, end, left, right) + if startPrev == nil { + if endNext == nil { + return end + } + + start.Next = endNext + return end + } + + startPrev.Next = end + start.Next = endNext + + return head +} + +func reverse(start, end *ListNode, m, n int) { + var prev, tmp *ListNode + curr := start + + for i := m; i <= n; i++ { + tmp = curr + curr = curr.Next + tmp.Next = prev + prev = tmp + } +} From fac399bf2cbbf0654ca8bc57310152f93a9e2dfb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 21 Jul 2022 15:25:14 -0700 Subject: [PATCH 520/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c41c0a6..b41a53b 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,7 @@ Please give this repo a ⭐ if it inspires you. |[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| |[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| |[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| +|[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| ## Hard |LC #|Description| From 1ee7bf584cc891301db57873bebbfebe6df6603d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 30 Jul 2022 17:55:38 -0700 Subject: [PATCH 521/969] Create L307.go --- Medium/L307.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Medium/L307.go diff --git a/Medium/L307.go b/Medium/L307.go new file mode 100644 index 0000000..7e8b1e5 --- /dev/null +++ b/Medium/L307.go @@ -0,0 +1,75 @@ +package Medium + +type NumArray struct { + root *SegmentTree +} + +type SegmentTree struct { + start, end, sum int + left, right *SegmentTree +} + +func newSegmentTree(start, end int) *SegmentTree { + return &SegmentTree{ + start: start, + end: end, + } +} + +func Constructor(nums []int) NumArray { + return NumArray{root: buildTree(nums, 0, len(nums)-1)} +} + +func buildTree(nums []int, start, end int) *SegmentTree { + if start > end { + return nil + } + + ret := newSegmentTree(start, end) + if start == end { + ret.sum = nums[start] + } else { + mid := start + (end-start)/2 + ret.left = buildTree(nums, start, mid) + ret.right = buildTree(nums, mid+1, end) + ret.sum = ret.left.sum + ret.right.sum + } + + return ret +} + +func (this *NumArray) Update(index int, val int) { + update(this.root, index, val) +} + +func update(root *SegmentTree, pos, val int) { + if root.start == root.end { + root.sum = val + } else { + mid := root.start + (root.end-root.start)/2 + if pos <= mid { + update(root.left, pos, val) + } else { + update(root.right, pos, val) + } + root.sum = root.left.sum + root.right.sum + } +} + +func (this *NumArray) SumRange(left int, right int) int { + return sumRange(this.root, left, right) +} + +func sumRange(root *SegmentTree, start, end int) int { + if root.end == end && root.start == start { + return root.sum + } + + mid := root.start + (root.end-root.start)/2 + if end <= mid { + return sumRange(root.left, start, end) + } else if start >= mid+1 { + return sumRange(root.right, start, end) + } + return sumRange(root.right, mid+1, end) + sumRange(root.left, start, mid) +} From efeb25ca31ff9d77356cf0234019fbb61fc708cc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 30 Jul 2022 17:56:09 -0700 Subject: [PATCH 522/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b41a53b..aea494b 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,7 @@ Please give this repo a ⭐ if it inspires you. |[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| |[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| |[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| +|[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| ## Hard |LC #|Description| From 57cc89c1859e9761127e6f9732b8450da78933f4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Jul 2022 09:54:36 -0700 Subject: [PATCH 523/969] Create L240.go --- Medium/L240.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L240.go diff --git a/Medium/L240.go b/Medium/L240.go new file mode 100644 index 0000000..24a95c8 --- /dev/null +++ b/Medium/L240.go @@ -0,0 +1,36 @@ +package Medium + +func searchMatrix(matrix [][]int, target int) bool { + if len(matrix) == 0 || len(matrix[0]) == 0 { + return false + } + + found := false + for _, m := range matrix { + if target >= m[0] && target <= m[len(m)-1] { + found = binSearch(m, 0, len(m)-1, target) + if found { + return found + } + } + } + + return found +} + +func binSearch(arr []int, low, high, tgt int) bool { + for low <= high { + mid := low + (high-low)/2 + if arr[mid] == tgt { + return true + } + + if tgt < arr[mid] { + high = mid - 1 + } else { + low = mid + 1 + } + } + + return false +} From 94fb6ceea63bb1ca587822f8f4f1257699c15bfc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Jul 2022 09:55:11 -0700 Subject: [PATCH 524/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aea494b..eef7416 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,7 @@ Please give this repo a ⭐ if it inspires you. |[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| |[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| |[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| +|[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| ## Hard |LC #|Description| From b1263f6f62d6e17cc92bc5ea5f2856b9aa0fce03 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Sep 2022 17:06:11 -0700 Subject: [PATCH 525/969] Create L1457.go --- Medium/L1457.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L1457.go diff --git a/Medium/L1457.go b/Medium/L1457.go new file mode 100644 index 0000000..b6d59ea --- /dev/null +++ b/Medium/L1457.go @@ -0,0 +1,39 @@ +func pseudoPalindromicPaths(root *TreeNode) int { + res, mp := 0, make([]int, 10) + findPesudoPalindromUtil(&res, &mp, root) + + return res +} + +func findPesudoPalindromUtil(res *int, mp *[]int, root *TreeNode) { + if root == nil { + return + } + + (*mp)[root.Val] = (*mp)[root.Val] + 1 + if root.Left == nil && root.Right == nil { + if isPalindrome(mp) { + *res++ + } + } + + findPesudoPalindromUtil(res, mp, root.Left) + findPesudoPalindromUtil(res, mp, root.Right) + + (*mp)[root.Val] = (*mp)[root.Val] - 1 +} + +func isPalindrome(mp *[]int) bool { + miss := 0 + + for i := 0; i <= 9; i++ { + if (*mp)[i]%2 != 0 { + miss++ + } + if miss > 1 { + return false + } + } + + return true +} From 6b001ec6c8698385eb7945cef68b6b744b920a13 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Sep 2022 17:06:50 -0700 Subject: [PATCH 526/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eef7416..ab7123c 100644 --- a/README.md +++ b/README.md @@ -357,6 +357,7 @@ Please give this repo a ⭐ if it inspires you. |[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| |[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| |[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| +|[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| ## Hard |LC #|Description| From 7e8d30baa0c504eedea9ddbac44674cabc7ff974 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 16 Sep 2022 16:38:56 -0700 Subject: [PATCH 527/969] Create L1770.go --- Medium/L1770.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L1770.go diff --git a/Medium/L1770.go b/Medium/L1770.go new file mode 100644 index 0000000..f24e7bd --- /dev/null +++ b/Medium/L1770.go @@ -0,0 +1,37 @@ +package Medium + +func maximumScore(nums []int, multipliers []int) int { + m := len(multipliers) + memo := make([][]int, m) + for i := 0; i < m; i++ { + memo[i] = make([]int, m) + } + + solveMaxScore(nums, multipliers, 0, 0, &memo) + + return memo[0][0] +} + +func solveMaxScore(nums, multipliers []int, l, i int, memo *[][]int) int { + if i == len(multipliers) { + return 0 + } + + if (*memo)[l][i] != 0 { + return (*memo)[l][i] + } + + pickLeft := solveMaxScore(nums, multipliers, l+1, i+1, memo) + (nums[l] * multipliers[i]) + pickRight := solveMaxScore(nums, multipliers, l, i+1, memo) + (nums[len(nums)-(i-l)-1] * multipliers[i]) + + (*memo)[l][i] = max(pickLeft, pickRight) + return (*memo)[l][i] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From baeb72c405abec9c0c40b579f38e81589022b4d8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 16 Sep 2022 16:39:32 -0700 Subject: [PATCH 528/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ab7123c..0fa6300 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,7 @@ Please give this repo a ⭐ if it inspires you. |[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| |[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| |[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| +|[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| ## Hard |LC #|Description| From 69f257dd83a57de376ce4884df966b44e066efb7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Sep 2022 17:07:07 -0700 Subject: [PATCH 529/969] Create L985.go --- Medium/L985.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L985.go diff --git a/Medium/L985.go b/Medium/L985.go new file mode 100644 index 0000000..a2e70ad --- /dev/null +++ b/Medium/L985.go @@ -0,0 +1,35 @@ +package Medium + +func sumEvenAfterQueries(nums []int, queries [][]int) []int { + sum := 0 + + for _, n := range nums { + if n%2 == 0 { + sum += n + } + } + + var ret []int + for _, q := range queries { + x, y := q[1], q[0] + if abs(nums[x])%2 == 0 { + sum -= nums[x] + } + if (nums[x]+y)%2 == 0 { + sum += nums[x] + y + } + + nums[x] += y + ret = append(ret, sum) + } + + return ret +} + +func abs(a int) int { + if a < 0 { + return -1 * a + } + + return a +} From 8b87836a381523952b479b1875e14932b8e53ce0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Sep 2022 17:07:41 -0700 Subject: [PATCH 530/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0fa6300..fb37af7 100644 --- a/README.md +++ b/README.md @@ -359,6 +359,7 @@ Please give this repo a ⭐ if it inspires you. |[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| |[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| |[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| +|[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| ## Hard |LC #|Description| From 7d4df118cc56b549e03e5a70aa8b1400a853b902 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 22 Sep 2022 13:01:59 -0700 Subject: [PATCH 531/969] Create L557.go --- Easy/L557.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Easy/L557.go diff --git a/Easy/L557.go b/Easy/L557.go new file mode 100644 index 0000000..94582f1 --- /dev/null +++ b/Easy/L557.go @@ -0,0 +1,23 @@ +package Easy + +func reverseWords(s string) string { + var sb strings.Builder + + strs := strings.Split(s, " ") + for i := 0; i < len(strs); i++ { + sb.WriteString(reverse(strs[i])) + if i != len(strs)-1 { + sb.WriteString(" ") + } + } + + return sb.String() +} + +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} From 33844535e297cd4a93da08acc10673cc6ec3fe98 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 22 Sep 2022 13:03:42 -0700 Subject: [PATCH 532/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fb37af7..dd73723 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ Please give this repo a ⭐ if it inspires you. |[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| |[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| |[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| +|[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| ## Medium |LC #|Description| From 7debead1e574a5503ab4cd110e5a623fb93ea081 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 23 Sep 2022 16:35:02 -0700 Subject: [PATCH 533/969] Create L1680.go --- Medium/L1680.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Medium/L1680.go diff --git a/Medium/L1680.go b/Medium/L1680.go new file mode 100644 index 0000000..4ac8e77 --- /dev/null +++ b/Medium/L1680.go @@ -0,0 +1,15 @@ +package Medium + +func concatenatedBinary(n int) int { + mod, res := 1000000007, 0 + + for i := 1; i <= n; i++ { + tmp := i + for tmp > 0 { + tmp, res = tmp/2, res*2 + } + res = (res + i) % mod + } + + return res +} From 4675984aa816d8e0183c9e1fde56612a77f542d3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 23 Sep 2022 16:35:41 -0700 Subject: [PATCH 534/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dd73723..038fe5e 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,7 @@ Please give this repo a ⭐ if it inspires you. |[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| |[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| |[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| +|[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| ## Hard |LC #|Description| From 83fdd1b687e777d481c6d7a7bad19c58c615e432 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Sep 2022 17:09:30 -0700 Subject: [PATCH 535/969] Update L622.go --- Medium/L622.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Medium/L622.go b/Medium/L622.go index a147500..36973ef 100644 --- a/Medium/L622.go +++ b/Medium/L622.go @@ -1,14 +1,16 @@ package Medium type MyCircularQueue struct { - Q []int - Capacity int + maxSize, head, tail int + data []int } func Constructor(k int) MyCircularQueue { return MyCircularQueue{ - Q: make([]int, 0, k), - Capacity: k, + maxSize: k, + head: 0, + tail: -1, + data: make([]int, k), } } @@ -17,7 +19,9 @@ func (this *MyCircularQueue) EnQueue(value int) bool { return false } - this.Q = append(this.Q, value) + this.tail = (this.tail + 1) % this.maxSize + this.data[this.tail] = value + return true } @@ -26,7 +30,12 @@ func (this *MyCircularQueue) DeQueue() bool { return false } - this.Q = this.Q[1:] + if this.head == this.tail { + this.head, this.tail = 0, -1 + } else { + this.head = (this.head + 1) % this.maxSize + } + return true } @@ -35,7 +44,7 @@ func (this *MyCircularQueue) Front() int { return -1 } - return this.Q[0] + return this.data[this.head] } func (this *MyCircularQueue) Rear() int { @@ -43,15 +52,15 @@ func (this *MyCircularQueue) Rear() int { return -1 } - return this.Q[len(this.Q)-1] + return this.data[this.tail] } func (this *MyCircularQueue) IsEmpty() bool { - return len(this.Q) == 0 + return this.tail == -1 } func (this *MyCircularQueue) IsFull() bool { - return len(this.Q) == this.Capacity + return !this.IsEmpty() && (this.tail+1)%this.maxSize == this.head } /** From fdc7b36417259a3e2b639dbcc4210c07e38caef3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Sep 2022 15:08:20 -0700 Subject: [PATCH 536/969] Create L990.go --- Medium/L990.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Medium/L990.go diff --git a/Medium/L990.go b/Medium/L990.go new file mode 100644 index 0000000..dc82a8e --- /dev/null +++ b/Medium/L990.go @@ -0,0 +1,30 @@ +package Medium + +func equationsPossible(equations []string) bool { + root := make([]int, 26) + + for i := 0; i < 26; i++ { + root[i] = i + } + for _, e := range equations { + if e[1] == '=' { + root[find(int(e[0]-'a'), &root)] = find(int(e[3]-'a'), &root) + } + } + + for _, e := range equations { + if e[1] == '!' && find(int(e[0]-'a'), &root) == find(int(e[3]-'a'), &root) { + return false + } + } + + return true +} + +func find(x int, root *[]int) int { + if x != (*root)[x] { + (*root)[x] = find((*root)[x], root) + } + + return (*root)[x] +} From 29d351407edf883cf1449beb40b39459e3b892b3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Sep 2022 15:08:52 -0700 Subject: [PATCH 537/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 038fe5e..870835b 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,7 @@ Please give this repo a ⭐ if it inspires you. |[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| |[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| |[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| +|[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| ## Hard |LC #|Description| From deae14a985b871f2a61c67dae9859098d87f2151 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 29 Sep 2022 15:22:19 -0700 Subject: [PATCH 538/969] Create L658.go --- Medium/L658.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L658.go diff --git a/Medium/L658.go b/Medium/L658.go new file mode 100644 index 0000000..3211c0e --- /dev/null +++ b/Medium/L658.go @@ -0,0 +1,26 @@ +package Medium + +func findClosestElements(arr []int, k int, x int) []int { + lo, hi := 0, len(arr)-1 + for hi-lo >= k { + if abs(arr[lo]-x) > abs(arr[hi]-x) { + lo++ + } else { + hi-- + } + } + var res []int + for i := lo; i <= hi; i++ { + res = append(res, arr[i]) + } + + return res +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From 566fdda7dfabd741490b53e5ae8c74536472c1bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 29 Sep 2022 15:23:03 -0700 Subject: [PATCH 539/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 870835b..19f5f9e 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,7 @@ Please give this repo a ⭐ if it inspires you. |[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| |[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| |[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| +|[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| ## Hard |LC #|Description| From 679896ccbc8f30185a46f0f655d7ae18df25f849 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 3 Oct 2022 16:58:14 -0700 Subject: [PATCH 540/969] Create L1578.go --- Medium/L1578.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L1578.go diff --git a/Medium/L1578.go b/Medium/L1578.go new file mode 100644 index 0000000..2889466 --- /dev/null +++ b/Medium/L1578.go @@ -0,0 +1,32 @@ +package Medium + +func minCost(colors string, neededTime []int) int { + n, res, prev := len(colors), 0, neededTime[0] + + for i := 1; i < n; i++ { + if colors[i] == colors[i-1] { + res += min(neededTime[i], prev) + prev = max(neededTime[i], prev) + } else { + prev = neededTime[i] + } + } + + return res +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 7b1e951f065b69234b3aae03e0bfdd94c5ae0c95 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 3 Oct 2022 16:59:24 -0700 Subject: [PATCH 541/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 19f5f9e..a93e220 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,7 @@ Please give this repo a ⭐ if it inspires you. |[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| |[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| |[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| +|[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| ## Hard |LC #|Description| From f03de1e63ab99e40299842a08b27283d0581f4bf Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 8 Oct 2022 08:03:03 -0700 Subject: [PATCH 542/969] Create L16.go --- Medium/L16.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L16.go diff --git a/Medium/L16.go b/Medium/L16.go new file mode 100644 index 0000000..4468f12 --- /dev/null +++ b/Medium/L16.go @@ -0,0 +1,36 @@ +package Medium + +func threeSumClosest(nums []int, target int) int { + if len(nums) < 3 { + return 0 + } + sort.Ints(nums) + res := nums[0] + nums[1] + nums[2] + for i := 0; i < len(nums)-2; i++ { + l, r := i+1, len(nums)-1 + for l < r { + sum := nums[i] + nums[l] + nums[r] + if sum == target { + return sum + } + if abs(sum-target) < abs(res-target) { + res = sum + } + if sum < target { + l += 1 + } else if sum > target { + r -= 1 + } + } + } + + return res +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From a10b39dbddbb7c65b932bb0c5deb81c5241bfcc1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 8 Oct 2022 08:03:44 -0700 Subject: [PATCH 543/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a93e220..d12f627 100644 --- a/README.md +++ b/README.md @@ -365,6 +365,7 @@ Please give this repo a ⭐ if it inspires you. |[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| |[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| |[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| +|[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| ## Hard |LC #|Description| From 95d6768827ca5bdd3413754f99fa896504e198e3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Oct 2022 16:16:31 -0700 Subject: [PATCH 544/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d12f627..af9f0ba 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,7 @@ Please give this repo a ⭐ if it inspires you. |[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| |[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| |[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| +|[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| ## Hard |LC #|Description| From 53f579493aee471cb0c040f4c5135ab08e469128 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Oct 2022 16:17:00 -0700 Subject: [PATCH 545/969] Create L334.go --- Medium/L334.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L334.go diff --git a/Medium/L334.go b/Medium/L334.go new file mode 100644 index 0000000..0ca1216 --- /dev/null +++ b/Medium/L334.go @@ -0,0 +1,19 @@ +package Medium + +func increasingTriplet(nums []int) bool { + left, mid := math.MaxInt32, math.MaxInt32 + + for _, n := range nums { + if n > mid { + return true + } + + if n < mid && n > left { + mid = n + } else if n < left { + left = n + } + } + + return false +} From 4809bcfc1e1bf44f7e36efd6a3c525327001b495 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 12 Oct 2022 17:02:51 -0700 Subject: [PATCH 546/969] Create L976.go --- Easy/L976.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L976.go diff --git a/Easy/L976.go b/Easy/L976.go new file mode 100644 index 0000000..31ddd1d --- /dev/null +++ b/Easy/L976.go @@ -0,0 +1,15 @@ +package Easy + +func largestPerimeter(nums []int) int { + sort.Ints(nums) + + for i := len(nums) - 1; i >= 0; i-- { + perimeter := nums[i] + nums[i-1] + nums[i-2] + + if nums[i] < nums[i-1]+nums[i-2] { + return perimeter + } + } + + return 0 +} From 10a29c4776ba8e0e7d902d9737ef653b80d8ffba Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 12 Oct 2022 17:03:25 -0700 Subject: [PATCH 547/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index af9f0ba..bcb6964 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ Please give this repo a ⭐ if it inspires you. |[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| |[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| |[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| +|[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| ## Medium |LC #|Description| From c75a415e87bd160640f78b32a40020a36ca5f42d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Oct 2022 18:24:58 -0700 Subject: [PATCH 548/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bcb6964..03140de 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,7 @@ Please give this repo a ⭐ if it inspires you. |[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| |[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| |[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| +|[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| ## Hard |LC #|Description| From 91a1172a378bd5be441d19d854ecad24d3663421 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Oct 2022 18:25:19 -0700 Subject: [PATCH 549/969] Create L2095.go --- Medium/L2095.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Medium/L2095.go diff --git a/Medium/L2095.go b/Medium/L2095.go new file mode 100644 index 0000000..79578be --- /dev/null +++ b/Medium/L2095.go @@ -0,0 +1,15 @@ +package Medium + +func deleteMiddle(head *ListNode) *ListNode { + if head == nil || head.Next == nil { + return nil + } + + slow, fast := head, head.Next.Next + for fast != nil && fast.Next != nil { + fast, slow = fast.Next.Next, slow.Next + } + + slow.Next = slow.Next.Next + return head +} From 4beccd0bff1c47655c0affcffd48ca2f9dd6bb4b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 15 Oct 2022 08:32:59 -0700 Subject: [PATCH 550/969] Create L1531.go --- Hard/L1531.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Hard/L1531.go diff --git a/Hard/L1531.go b/Hard/L1531.go new file mode 100644 index 0000000..d0b27d0 --- /dev/null +++ b/Hard/L1531.go @@ -0,0 +1,66 @@ +package Hard + +func getLengthOfOptimalCompression(s string, k int) int { + m, n := len(s), k+1 + dp := make([][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([]int, n) + for j := 0; j < n; j++ { + dp[i][j] = -1 + } + } + + return solve(s, 0, k, &dp) +} + +func solve(s string, currIdx, rest int, dp *[][]int) int { + if currIdx == len(s) || len(s)-currIdx <= rest { + return 0 + } + + if (*dp)[currIdx][rest] != -1 { + return (*dp)[currIdx][rest] + } + + free := make([]int, 26) + most, res := 0, math.MaxInt32 + + for i := currIdx; i < len(s); i++ { + idx := s[i] - 'a' + free[idx]++ + + if most < free[idx] { + most = free[idx] + } + if rest >= i-currIdx+1-most { + res = min(res, getLen(most)+1+solve(s, i+1, rest-(i-currIdx+1-most), dp)) + } + } + + (*dp)[currIdx][rest] = res + return res +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func getLen(most int) int { + if most == 1 { + return 0 + } + + if most < 10 { + return 1 + } + + if most < 99 { + return 2 + } + + return 3 +} From 31ca3c23db8ce373bf735175fdd984075ea49bde Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 15 Oct 2022 08:33:44 -0700 Subject: [PATCH 551/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03140de..a1c851e 100644 --- a/README.md +++ b/README.md @@ -418,3 +418,4 @@ Please give this repo a ⭐ if it inspires you. |[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| |[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| |[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| +|[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| From cfcf4dda06d53ef54e212c5eff8fb198901d8d7b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 15 Oct 2022 18:18:02 -0700 Subject: [PATCH 552/969] Create L1335.go --- Hard/L1335.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Hard/L1335.go diff --git a/Hard/L1335.go b/Hard/L1335.go new file mode 100644 index 0000000..43bf7d2 --- /dev/null +++ b/Hard/L1335.go @@ -0,0 +1,50 @@ +package Hard + +func minDifficulty(jobDifficulty []int, d int) int { + if d > len(jobDifficulty) { + return -1 + } + + m, n := d-1, len(jobDifficulty) + dp := make([][]int, m) + for i := 0; i < m; i++ { + dp[i] = make([]int, n) + for j := 0; j < n; j++ { + dp[i][j] = -1 + } + } + + return solve(jobDifficulty, d-1, 0, &dp) +} + +func solve(jobDifficulty []int, d, pos int, dp *[][]int) int { + if d == 0 { + max := jobDifficulty[pos] + for i := pos; i < len(jobDifficulty); i++ { + if jobDifficulty[i] > max { + max = jobDifficulty[i] + } + } + + return max + } + + day := len(*dp) - d + if (*dp)[day][pos] != -1 { + return (*dp)[day][pos] + } + + max, min := math.MinInt32, math.MaxInt32 + for i := pos; i < len(jobDifficulty)-d; i++ { + if jobDifficulty[i] > max { + max = jobDifficulty[i] + } + compute := solve(jobDifficulty, d-1, i+1, dp) + if max+compute < min { + min = max + compute + } + } + + (*dp)[day][pos] = min + return min +} From d91977fd4fc0b5630c7559c6680c8b1f4f714334 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 15 Oct 2022 18:18:42 -0700 Subject: [PATCH 553/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a1c851e..e362870 100644 --- a/README.md +++ b/README.md @@ -419,3 +419,4 @@ Please give this repo a ⭐ if it inspires you. |[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| |[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| |[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| +|[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| From d4c0d640f64e3993471f8271548c87317cb3030f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Oct 2022 17:02:36 -0700 Subject: [PATCH 554/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e362870..e639e17 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ Please give this repo a ⭐ if it inspires you. |[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| |[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| |[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| +|[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| ## Medium |LC #|Description| From 70ca24850598eb35345480ffcfa099c13956c302 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Oct 2022 17:02:57 -0700 Subject: [PATCH 555/969] Create L219.go --- Easy/L219.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Easy/L219.go diff --git a/Easy/L219.go b/Easy/L219.go new file mode 100644 index 0000000..d6d04d6 --- /dev/null +++ b/Easy/L219.go @@ -0,0 +1,16 @@ +package Easy + +func containsNearbyDuplicate(nums []int, k int) bool { + mp := make(map[int]int) + + for i := range nums { + if v, ok := mp[nums[i]]; ok { + if i-v <= k { + return true + } + } + mp[nums[i]] = i + } + + return false +} From a9b53de6bba2d26ba08c895892b869c874c26de2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 23 Oct 2022 19:53:27 -0700 Subject: [PATCH 556/969] Create L76.go --- Hard/L76.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Hard/L76.go diff --git a/Hard/L76.go b/Hard/L76.go new file mode 100644 index 0000000..969c9b2 --- /dev/null +++ b/Hard/L76.go @@ -0,0 +1,45 @@ +package Hard + +func minWindow(s string, t string) string { + if len(t) > len(s) { + return "" + } + + mp := make(map[byte]int) + for i := range t { + mp[t[i]]++ + } + + begin, end, counter, length, head := 0, 0, len(mp), math.MaxInt32, 0 + + for end < len(s) { + if v, ok := mp[s[end]]; ok { + mp[s[end]] = v - 1 + if mp[s[end]] == 0 { + counter-- + } + } + end++ + + for counter == 0 { + if v, ok := mp[s[begin]]; ok { + mp[s[begin]] = v + 1 + if mp[s[begin]] > 0 { + counter++ + } + } + + if end-begin < length { + length = end - begin + head = begin + } + begin++ + } + } + + if length == math.MaxInt32 { + return "" + } + + return s[head : head+length] +} From fe02844fe93405d896183450972d88a478868350 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 23 Oct 2022 19:54:04 -0700 Subject: [PATCH 557/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e639e17..21b833a 100644 --- a/README.md +++ b/README.md @@ -421,3 +421,4 @@ Please give this repo a ⭐ if it inspires you. |[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| |[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| |[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| +|[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| From 0812e6021c4e57dc85d6fcf38222bf1dae08504a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Oct 2022 16:44:10 -0700 Subject: [PATCH 558/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 21b833a..a780ba5 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,7 @@ Please give this repo a ⭐ if it inspires you. |[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| |[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| |[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| +|[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| ## Hard |LC #|Description| From e0f4fdf522c87bafe20f64aeafb29f9d74ea3c55 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Oct 2022 16:44:42 -0700 Subject: [PATCH 559/969] Create L1662.go --- Easy/L1662.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Easy/L1662.go diff --git a/Easy/L1662.go b/Easy/L1662.go new file mode 100644 index 0000000..0c46622 --- /dev/null +++ b/Easy/L1662.go @@ -0,0 +1,27 @@ +package Easy + +func arrayStringsAreEqual(word1 []string, word2 []string) bool { + idx1, idx2, arrIdx1, arrIdx2 := 0, 0, 0, 0 + + for arrIdx1 < len(word1) && arrIdx2 < len(word2) { + if word1[arrIdx1][idx1] != word2[arrIdx2][idx2] { + return false + } + + if idx1 == len(word1[arrIdx1])-1 { + idx1 = 0 + arrIdx1++ + } else { + idx1++ + } + + if idx2 == len(word2[arrIdx2])-1 { + idx2 = 0 + arrIdx2++ + } else { + idx2++ + } + } + + return arrIdx1 == len(word1) && arrIdx2 == len(word2) +} From e922b66cccd9c746dfd8a47d3bbf312a8af0972c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Oct 2022 16:01:27 -0700 Subject: [PATCH 560/969] Create L523.go --- Medium/L523.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L523.go diff --git a/Medium/L523.go b/Medium/L523.go new file mode 100644 index 0000000..c0b42f3 --- /dev/null +++ b/Medium/L523.go @@ -0,0 +1,21 @@ +package Medium + +func checkSubarraySum(nums []int, k int) bool { + mp := make(map[int]int) + mp[0] = -1 + runningSum := 0 + for i := 0; i < len(nums); i++ { + runningSum += nums[i] + if k != 0 { + runningSum %= k + } + if v, ok := mp[runningSum]; ok { + if i-v > 1 { + return true + } + } else { + mp[runningSum] = i + } + } + return false +} From 35a0a8f70a4e9c932462d45d4c378db6d50e3a63 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Oct 2022 16:03:05 -0700 Subject: [PATCH 561/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a780ba5..4c367c5 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,7 @@ Please give this repo a ⭐ if it inspires you. |[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| |[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| |[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| +|[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| ## Hard |LC #|Description| From 482f5a2c7d17b57ca96b44d990106d71dea56a2f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Oct 2022 16:32:14 -0700 Subject: [PATCH 562/969] Create L835.go --- Medium/L835.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L835.go diff --git a/Medium/L835.go b/Medium/L835.go new file mode 100644 index 0000000..773c45a --- /dev/null +++ b/Medium/L835.go @@ -0,0 +1,39 @@ +package Medium + +func largestOverlap(img1 [][]int, img2 [][]int) int { + ans := 0 + + for row := -len(img1); row < len(img1); row++ { + for col := -len(img1[0]); col < len(img1[0]); col++ { + max := overlap(img1, img2, row, col) + if max > ans { + ans = max + } + } + } + + return ans +} + +func overlap(img1, img2 [][]int, rowOffset, colOffset int) int { + ans := 0 + for row := 0; row < len(img1); row++ { + for col := 0; col < len(img1[0]); col++ { + if isValid(row, col, rowOffset, colOffset, img1) { + if img1[row][col] == 1 && img2[row+rowOffset][col+colOffset] == 1 { + ans++ + } + } + } + } + + return ans +} + +func isValid(row, col, rowOffset, colOffset int, img [][]int) bool { + return row+rowOffset >= 0 && + row+rowOffset < len(img) && + col+colOffset >= 0 && + col+colOffset < len(img) +} +Console From 49cfb12c029c448294e8bbdee6d4481e4058ba7e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Oct 2022 16:32:58 -0700 Subject: [PATCH 563/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4c367c5..b45cdb0 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,7 @@ Please give this repo a ⭐ if it inspires you. |[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| |[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| |[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| +|[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| ## Hard |LC #|Description| From bb103f70c115e2f1853ebd9d3a7dd1e6216b1b67 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 29 Oct 2022 16:29:22 -0700 Subject: [PATCH 564/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b45cdb0..5bf6039 100644 --- a/README.md +++ b/README.md @@ -425,3 +425,4 @@ Please give this repo a ⭐ if it inspires you. |[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| |[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| |[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| +|[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| From a4db0b15ad6716d07815737200f7940362767cef Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 29 Oct 2022 16:29:43 -0700 Subject: [PATCH 565/969] Create L2136.go --- Hard/L2136.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Hard/L2136.go diff --git a/Hard/L2136.go b/Hard/L2136.go new file mode 100644 index 0000000..d3b6e85 --- /dev/null +++ b/Hard/L2136.go @@ -0,0 +1,24 @@ +package Hard + +func earliestFullBloom(plantTime []int, growTime []int) int { + n := len(plantTime) + pairs := make([][]int, n) + for i := 0; i < n; i++ { + pairs[i] = make([]int, 2) + pairs[i][0], pairs[i][1] = plantTime[i], growTime[i] + } + + sort.Slice(pairs, func(i, j int) bool { + return pairs[j][1] > pairs[i][1] + }) + + plantingDays, totalDays := 0, 0 + for _, p := range pairs { + if plantingDays+p[0]+p[1] > totalDays { + totalDays = plantingDays + p[0] + p[1] + } + plantingDays += p[0] + } + + return totalDays +} From ae44f8e8a2ee4db665ae0fcfd37fb8e3784ff636 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Nov 2022 15:39:30 -0700 Subject: [PATCH 566/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5bf6039..1e65891 100644 --- a/README.md +++ b/README.md @@ -373,6 +373,7 @@ Please give this repo a ⭐ if it inspires you. |[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| |[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| |[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| +|[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| ## Hard |LC #|Description| From 0cfe18b1c80d75fedaa0d66bff5b68834bacd67c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 2 Nov 2022 15:39:50 -0700 Subject: [PATCH 567/969] Create L433.go --- Medium/L433.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L433.go diff --git a/Medium/L433.go b/Medium/L433.go new file mode 100644 index 0000000..cc1db99 --- /dev/null +++ b/Medium/L433.go @@ -0,0 +1,45 @@ +package Medium + +var exists struct{} + +func minMutation(start string, end string, bank []string) int { + return dfs(start, end, bank, make(map[string]struct{})) +} + +func dfs(start, end string, bank []string, visited map[string]struct{}) int { + if start == end { + return 0 + } + + min := math.MaxInt32 + for _, s := range bank { + diff := 0 + for i := 0; i < len(start); i++ { + if start[i] != s[i] { + diff++ + } + if diff > 1 { + break + } + } + + if _, ok := visited[s]; !ok { + if diff == 1 { + visited[s] = exists + h := dfs(s, end, bank, visited) + if h >= 0 { + if 1+h < min { + min = 1 + h + } + } + delete(visited, s) + } + } + } + + if min == math.MaxInt32 { + return -1 + } + + return min +} From e31a464c1f58d517ed20686de696e59a278a61de Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Nov 2022 07:33:33 -0800 Subject: [PATCH 568/969] Create L1544.go --- Easy/L1544.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Easy/L1544.go diff --git a/Easy/L1544.go b/Easy/L1544.go new file mode 100644 index 0000000..3cf241b --- /dev/null +++ b/Easy/L1544.go @@ -0,0 +1,32 @@ +package Easy + +func makeGood(s string) string { + var st []byte + + for i := range s { + if len(st) > 0 && areEqual(st[len(st)-1], s[i]) { + st = st[:len(st)-1] + continue + } + st = append(st, s[i]) + } + + return string(st) +} + +func areEqual(a, b byte) bool { + if (isUpperCase(a) && isLowerCase(b)) || + (isLowerCase(a) && isUpperCase(b)) { + return byte(unicode.ToLower(rune(a))) == byte(unicode.ToLower(rune(b))) + } + + return false +} + +func isUpperCase(ch byte) bool { + return ch >= 'A' && ch <= 'Z' +} + +func isLowerCase(ch byte) bool { + return ch >= 'a' && ch <= 'z' +} From e93816a410bfa9cbd47e4a72b9b7c60a65532989 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Nov 2022 07:34:17 -0800 Subject: [PATCH 569/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1e65891..88cd556 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ Please give this repo a ⭐ if it inspires you. |[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| |[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| |[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| +|[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| ## Medium |LC #|Description| From 31939a31452c280710a3a0640ea71c52b8d09972 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Nov 2022 07:43:39 -0800 Subject: [PATCH 570/969] Create L1323.go --- Easy/L1323.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Easy/L1323.go diff --git a/Easy/L1323.go b/Easy/L1323.go new file mode 100644 index 0000000..9ac2c5a --- /dev/null +++ b/Easy/L1323.go @@ -0,0 +1,16 @@ +package Easy + +func maximum69Number(num int) int { + str := fmt.Sprintf("%d", num) + arr := []byte(str) + + for i := range arr { + if arr[i] == '6' { + arr[i] = '9' + ret, _ := strconv.Atoi(string(arr)) + return ret + } + } + + return num +} From d2e13ca0adf83d9a777d13e610bf55766cd3d44d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 9 Nov 2022 07:44:09 -0800 Subject: [PATCH 571/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 88cd556..d513a3a 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ Please give this repo a ⭐ if it inspires you. |[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| |[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| |[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| +|[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| ## Medium |LC #|Description| From 2bc4fe5624a64838596b7cdcb3d6fda8ec05c504 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Nov 2022 07:08:32 -0800 Subject: [PATCH 572/969] Create L2325.go --- Easy/L2325.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Easy/L2325.go diff --git a/Easy/L2325.go b/Easy/L2325.go new file mode 100644 index 0000000..85cb498 --- /dev/null +++ b/Easy/L2325.go @@ -0,0 +1,27 @@ +package Easy + +func decodeMessage(key string, message string) string { + var sb strings.Builder + hash, j := make(map[byte]byte), 0 + + for i := range key { + if key[i] == ' ' { + continue + } + if _, ok := hash[key[i]]; !ok { + hash[key[i]] = byte('a' + j) + j++ + } + } + + for i := range message { + if message[i] == ' ' { + sb.WriteByte(' ') + continue + } + by := hash[message[i]] + sb.WriteByte(by) + } + + return sb.String() +} From 076c2ef47e07f5e13f8e865a9e30d542139b50d0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 12 Nov 2022 07:09:06 -0800 Subject: [PATCH 573/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d513a3a..2cd4b1b 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ Please give this repo a ⭐ if it inspires you. |[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| |[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| |[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| +|[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| ## Medium |LC #|Description| From 5abf3a0c551590fff711232990aa4fa9c438a2ef Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Nov 2022 15:55:04 -0800 Subject: [PATCH 574/969] Create L1926.go --- Medium/L1926.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Medium/L1926.go diff --git a/Medium/L1926.go b/Medium/L1926.go new file mode 100644 index 0000000..6ac739c --- /dev/null +++ b/Medium/L1926.go @@ -0,0 +1,60 @@ +package Medium + +var ( + exists struct{} + dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} +) + +func nearestExit(maze [][]byte, entrance []int) int { + vis := make(map[string]struct{}) + + var q [][]int + q = append(q, entrance) + vis[key(entrance[0], entrance[1])] = exists + + cnt := 0 + for len(q) > 0 { + cnt++ + size := len(q) + for i := 0; i < size; i++ { + curr := q[0] + q = q[1:] + + for _, d := range dirs { + x, y := curr[0]+d[0], curr[1]+d[1] + + if !isValid(maze, x, y) { + continue + } + if _, ok := vis[key(x, y)]; ok { + continue + } + + if isExit(maze, x, y) { + return cnt + } + + vis[key(x, y)] = exists + q = append(q, []int{x, y}) + } + } + } + + return -1 +} + +func isValid(maze [][]byte, x, y int) bool { + m, n := len(maze), len(maze[0]) + + return x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' +} + +func isExit(maze [][]byte, x, y int) bool { + m, n := len(maze), len(maze[0]) + + return x == 0 || y == 0 || x == m-1 || y == n-1 +} + +func key(x, y int) string { + return fmt.Sprintf("%dx%d", x, y) +} From 0fd1622c8a81bb9c5e2e8fb5d6f6d4617a51cffd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 22 Nov 2022 15:55:43 -0800 Subject: [PATCH 575/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2cd4b1b..9901e4d 100644 --- a/README.md +++ b/README.md @@ -377,6 +377,7 @@ Please give this repo a ⭐ if it inspires you. |[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| |[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| |[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| +|[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| ## Hard |LC #|Description| From 3e94d49d6954ee610f02c1cd1eef4a3ff0d3870f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Nov 2022 16:19:47 -0800 Subject: [PATCH 576/969] Create L2193.go --- Hard/L2193.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Hard/L2193.go diff --git a/Hard/L2193.go b/Hard/L2193.go new file mode 100644 index 0000000..4190668 --- /dev/null +++ b/Hard/L2193.go @@ -0,0 +1,42 @@ +package Hard + +func minMovesToMakePalindrome(s string) int { + l, r, k, cnt := 0, len(s)-1, len(s)-1, 0 + arr := []byte(s) + + for l < r { + if arr[l] == arr[r] { + l, r = l+1, r-1 + } else { + k = findKthIndexMatchingwithLthIndex(arr, l, r) + + if k == l { + if l+1 < len(s) { + arr[l], arr[l+1] = arr[l+1], arr[l] + } + cnt++ + } else { + for k < r { + if k+1 < len(s) { + arr[k], arr[k+1] = arr[k+1], arr[k] + } + cnt++ + k++ + } + l, r = l+1, r-1 + } + } + } + + return cnt +} + +func findKthIndexMatchingwithLthIndex(arr []byte, l, k int) int { + for k > l { + if arr[k] == arr[l] { + return k + } + k-- + } + return k +} From 42f84b5275ab85f88f66f826c0ca495d7562e767 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 23 Nov 2022 16:20:30 -0800 Subject: [PATCH 577/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9901e4d..9c4c888 100644 --- a/README.md +++ b/README.md @@ -431,3 +431,4 @@ Please give this repo a ⭐ if it inspires you. |[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| |[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| |[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| +|[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| From cf947290c0f228b3ee79cfd20aeee5fe77383054 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Nov 2022 15:20:22 -0800 Subject: [PATCH 578/969] Create L1235.go --- Hard/L1235.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Hard/L1235.go diff --git a/Hard/L1235.go b/Hard/L1235.go new file mode 100644 index 0000000..e4f271e --- /dev/null +++ b/Hard/L1235.go @@ -0,0 +1,57 @@ +package Hard + +func jobScheduling(startTime []int, endTime []int, profit []int) int { + memo := make(map[int]int) + jobs := make([][]int, len(startTime)) + for i := 0; i < len(startTime); i++ { + jobs[i] = []int{startTime[i], endTime[i], profit[i]} + } + sort.Slice(jobs, func(i, j int) bool { + return jobs[i][0] < jobs[j][0] + }) + + return dfs(0, jobs, memo) +} + +func dfs(cur int, jobs [][]int, mp map[int]int) int { + if cur == len(jobs) { + return 0 + } + + if v, ok := mp[cur]; ok { + return v + } + + next := findNext(cur, jobs) + inclProf := jobs[cur][2] + if next != -1 { + inclProf += dfs(next, jobs, mp) + } + exclProf := dfs(cur+1, jobs, mp) + + max := inclProf + if exclProf > inclProf { + max = exclProf + } + + mp[cur] = max + return max +} + +func findNext(cur int, jobs [][]int) int { + lo, hi := cur+1, len(jobs)-1 + for lo <= hi { + mid := lo + (hi-lo)/2 + if jobs[mid][0] >= jobs[cur][1] { + if jobs[mid-1][0] >= jobs[cur][1] { + hi = mid - 1 + } else { + return mid + } + } else { + lo = mid + 1 + } + } + + return -1 +} From 7ba427921e10a20869ec7f7b023fee6a046e8e9c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 26 Nov 2022 15:21:10 -0800 Subject: [PATCH 579/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9c4c888..5202793 100644 --- a/README.md +++ b/README.md @@ -432,3 +432,4 @@ Please give this repo a ⭐ if it inspires you. |[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| |[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| |[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| +|[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| From 401c94303e6c29233973b654f1a360b585909d73 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Nov 2022 15:57:01 -0800 Subject: [PATCH 580/969] Create L446.go --- Hard/L446.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Hard/L446.go diff --git a/Hard/L446.go b/Hard/L446.go new file mode 100644 index 0000000..1f11f0c --- /dev/null +++ b/Hard/L446.go @@ -0,0 +1,33 @@ +package Hard + +func numberOfArithmeticSlices(nums []int) int { + n, res := len(nums), 0 + + for i := 0; i < n; i++ { + for j := i + 1; j < n; j++ { + mp := make(map[string]int) + res += solve(nums, j + 1, int64(nums[j]) - int64(nums[i]), 2, mp) + } + } + + return res +} + +func solve(nums []int, idx int, diff int64, cnt int, mp map[string]int) int { + res, key := 0, fmt.Sprintf("%d-%d", idx, diff) + if v, ok := mp[key]; ok { + return v + } + + if cnt >= 3 { + res++ + } + + for i := idx; i < len(nums); i++ { + if int64(nums[i]) - int64(nums[idx - 1]) == diff { + res += solve(nums, i + 1, diff, cnt + 1, mp) + } + } + mp[key] = res + return res +} From 15c76c2d8eb63826e348db5643fe8b2486142297 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 27 Nov 2022 15:57:36 -0800 Subject: [PATCH 581/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5202793..4abff3c 100644 --- a/README.md +++ b/README.md @@ -433,3 +433,4 @@ Please give this repo a ⭐ if it inspires you. |[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| |[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| |[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| +|[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| From 549144e47ad42529071210712082cc5fecebcf7b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Nov 2022 16:25:48 -0800 Subject: [PATCH 582/969] Create L2225.go --- Medium/L2225.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L2225.go diff --git a/Medium/L2225.go b/Medium/L2225.go new file mode 100644 index 0000000..fb375d2 --- /dev/null +++ b/Medium/L2225.go @@ -0,0 +1,26 @@ +package Medium + +func findWinners(matches [][]int) [][]int { + var winners, losers []int + table := make(map[int]int) + + for _, m := range matches { + if _, ok := table[m[0]]; !ok { + table[m[0]] = 0 + } + table[m[1]]++ + } + + for k, v := range table { + if v == 0 { + winners = append(winners, k) + } + if v == 1 { + losers = append(losers, k) + } + } + + sort.Ints(winners) + sort.Ints(losers) + return [][]int{winners, losers} +} From 5a174af7784c9490e082f86b647be6e2a648b973 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 28 Nov 2022 16:26:26 -0800 Subject: [PATCH 583/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4abff3c..62691af 100644 --- a/README.md +++ b/README.md @@ -378,6 +378,7 @@ Please give this repo a ⭐ if it inspires you. |[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| |[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| |[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| +|[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| ## Hard |LC #|Description| From d878248eb232c8a3d47efc6616146531debf0a8c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Nov 2022 14:46:37 -0800 Subject: [PATCH 584/969] Create L380.go --- Medium/L380.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L380.go diff --git a/Medium/L380.go b/Medium/L380.go new file mode 100644 index 0000000..75dda73 --- /dev/null +++ b/Medium/L380.go @@ -0,0 +1,46 @@ +package Medium + +type RandomizedSet struct { + nums []int + locs map[int]int +} + +/** Initialize your data structure here. */ +func Constructor() RandomizedSet { + rand.Seed(time.Now().Unix()) + return RandomizedSet{ + nums: make([]int, 0), + locs: make(map[int]int), + } +} + +/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ +func (this *RandomizedSet) Insert(val int) bool { + if _, ok := this.locs[val]; ok { + return false + } + this.locs[val] = len(this.nums) + this.nums = append(this.nums, val) + return true +} + +/** Removes a value from the set. Returns true if the set contained the specified element. */ +func (this *RandomizedSet) Remove(val int) bool { + if _, ok := this.locs[val]; !ok { + return false + } + location := this.locs[val] + if location < len(this.nums)-1 { + lastOne := this.nums[len(this.nums)-1] + this.nums[location] = lastOne + this.locs[lastOne] = location + } + delete(this.locs, val) + this.nums = this.nums[:len(this.nums)-1] + return true +} + +/** Get a random element from the set. */ +func (this *RandomizedSet) GetRandom() int { + return this.nums[rand.Intn(len(this.nums))] +} From d84a887523b5ad492a4ec2d7d2e7842001767387 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 29 Nov 2022 14:47:18 -0800 Subject: [PATCH 585/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 62691af..dc14460 100644 --- a/README.md +++ b/README.md @@ -379,6 +379,7 @@ Please give this repo a ⭐ if it inspires you. |[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| |[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| |[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| +|[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| ## Hard |LC #|Description| From ffe9c077827b1e149f88e4b79ea51c2f943e8ce2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 1 Dec 2022 05:33:49 -0800 Subject: [PATCH 586/969] Create L1207.go --- Medium/L1207.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L1207.go diff --git a/Medium/L1207.go b/Medium/L1207.go new file mode 100644 index 0000000..c551ff0 --- /dev/null +++ b/Medium/L1207.go @@ -0,0 +1,18 @@ +package Medium + +func uniqueOccurrences(arr []int) bool { + var exists struct{} + mp, seen := make(map[int]int), make(map[int]struct{}) + + for _, n := range arr { + mp[n]++ + } + for _, v := range mp { + if _, ok := seen[v]; ok { + return false + } + seen[v] = exists + } + + return true +} From 6f8bf981a986f118194e13c3313bbfac144b220e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 1 Dec 2022 05:34:29 -0800 Subject: [PATCH 587/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dc14460..84b73fc 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,7 @@ Please give this repo a ⭐ if it inspires you. |[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| |[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| |[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| +|[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| ## Hard |LC #|Description| From a0360aa57fc78d580c002fe86eccec650d9cf380 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 10 Dec 2022 17:53:32 -0800 Subject: [PATCH 588/969] Create L45.go --- Medium/L45.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L45.go diff --git a/Medium/L45.go b/Medium/L45.go new file mode 100644 index 0000000..df12cf5 --- /dev/null +++ b/Medium/L45.go @@ -0,0 +1,36 @@ +package Medium + +func jump(nums []int) int { + dp := make([]int, len(nums)) + for i := range dp { + dp[i] = -1 + } + + return solve(nums, &dp, 0) +} + +func solve(nums []int, dp *[]int, idx int) int { + if idx >= len(nums)-1 { + return 0 + } + + if (*dp)[idx] != -1 { + return (*dp)[idx] + } + + minJump := len(nums) + for i := idx + 1; i <= idx+nums[idx]; i++ { + minJump = min(minJump, 1+solve(nums, dp, i)) + } + + (*dp)[idx] = minJump + return minJump +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From d229b420d6ce274eb877c9c1de78c2bb4198e16d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 10 Dec 2022 17:54:07 -0800 Subject: [PATCH 589/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 84b73fc..73e0ae2 100644 --- a/README.md +++ b/README.md @@ -381,6 +381,7 @@ Please give this repo a ⭐ if it inspires you. |[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| |[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| |[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| +|[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| ## Hard |LC #|Description| From 09dd13449fde6250b5f83653086c571c2162450b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 11 Dec 2022 15:16:11 -0800 Subject: [PATCH 590/969] Create L124.go --- Hard/L124.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Hard/L124.go diff --git a/Hard/L124.go b/Hard/L124.go new file mode 100644 index 0000000..43c5ee5 --- /dev/null +++ b/Hard/L124.go @@ -0,0 +1,29 @@ +package Hard + +func maxPathSum(root *TreeNode) int { + maxi := math.MinInt32 + + solve(root, &maxi) + + return maxi +} + +func solve(root *TreeNode, maximum *int) int { + if root == nil { + return 0 + } + + left, right := max(solve(root.Left, maximum), 0), max(solve(root.Right, maximum), 0) + + *maximum = max(*maximum, root.Val+right+left) + + return root.Val + max(left, right) +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 600bbce1c9ae76bdbab7b93c6284c5d6bb819da0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 11 Dec 2022 15:16:55 -0800 Subject: [PATCH 591/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 73e0ae2..3530f56 100644 --- a/README.md +++ b/README.md @@ -438,3 +438,4 @@ Please give this repo a ⭐ if it inspires you. |[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| |[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| |[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| +|[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| From d6bde32744e5641d0a390d2237d6478895565120 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 07:25:59 -0800 Subject: [PATCH 592/969] Create L1014.go --- Medium/L1014.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L1014.go diff --git a/Medium/L1014.go b/Medium/L1014.go new file mode 100644 index 0000000..5791d4d --- /dev/null +++ b/Medium/L1014.go @@ -0,0 +1,41 @@ +package Medium + +func maxScoreSightseeingPair(values []int) int { + mp := make(map[string]int) + + return solve(0, 0, values, mp) +} + +func solve(i, taken int, values []int, mp map[string]int) int { + if taken >= 2 { + return 0 + } + + if i >= len(values) { + return math.MinInt32 + } + + key := fmt.Sprintf("%d-%d", i, taken) + if v, ok := mp[key]; ok { + return v + } + + pick, notPick := values[i]+solve(i+1, taken+1, values, mp), solve(i+1, taken, values, mp) + + if taken == 1 { + pick -= i + } else { + pick += i + } + + mp[key] = max(pick, notPick) + return mp[key] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 6a63abd7e453927effb19c4e3fa049473b71eb70 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 07:26:40 -0800 Subject: [PATCH 593/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3530f56..5cc6101 100644 --- a/README.md +++ b/README.md @@ -382,6 +382,7 @@ Please give this repo a ⭐ if it inspires you. |[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| |[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| |[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| +|[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| ## Hard |LC #|Description| From 35e8378ae1bdc8f56cfbe6320545f30bcbfd3834 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 08:04:37 -0800 Subject: [PATCH 594/969] Create L121.go --- Easy/L121.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Easy/L121.go diff --git a/Easy/L121.go b/Easy/L121.go new file mode 100644 index 0000000..5323746 --- /dev/null +++ b/Easy/L121.go @@ -0,0 +1,34 @@ +package Easy + +func maxProfit(prices []int) int { + mp := make(map[string]int) + + return solve(prices, 0, 1, true, mp) +} + +func solve(prices []int, i, k int, buy bool, mp map[string]int) int { + if i >= len(prices) || k <= 0 { + return 0 + } + + key := fmt.Sprintf("%d-%t", i, buy) + if v, ok := mp[key]; ok { + return v + } + + if buy { + mp[key] = max(-prices[i]+solve(prices, i+1, k, !buy, mp), solve(prices, i+1, k, buy, mp)) + return mp[key] + } + + mp[key] = max(prices[i]+solve(prices, i+1, k-1, !buy, mp), solve(prices, i+1, k, buy, mp)) + return mp[key] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From a69824a382a5b4074aad80165b663c32345afa4e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 08:05:20 -0800 Subject: [PATCH 595/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5cc6101..6be271c 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ Please give this repo a ⭐ if it inspires you. |[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| |[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| |[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| +|[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| ## Medium |LC #|Description| From 10a57630dc6c506baf486cad656900299b73e62a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 17:08:52 -0800 Subject: [PATCH 596/969] Create L931.go --- Medium/L931.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L931.go diff --git a/Medium/L931.go b/Medium/L931.go new file mode 100644 index 0000000..e326189 --- /dev/null +++ b/Medium/L931.go @@ -0,0 +1,36 @@ +package Medium + +func minFallingPathSum(matrix [][]int) int { + rows, cols := len(matrix), len(matrix[0]) + mp, ans := make(map[string]int), math.MaxInt32 + + for c := 0; c < cols; c++ { + ans = min(ans, solve(matrix, rows-1, c, mp)) + } + + return ans +} + +func solve(matrix [][]int, r, c int, mp map[string]int) int { + if r == 0 && c < len(matrix[0]) && c >= 0 { + return matrix[r][c] + } + if c >= len(matrix[0]) || c < 0 { + return math.MaxInt32 + } + key := fmt.Sprintf("%d-%d", r, c) + if v, ok := mp[key]; ok { + return v + } + + mp[key] = matrix[r][c] + min(min(solve(matrix, r-1, c+1, mp), solve(matrix, r-1, c, mp)), solve(matrix, r-1, c-1, mp)) + return mp[key] +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 0f710e4b84540c5f44963005c913924fbf73969e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 17:09:33 -0800 Subject: [PATCH 597/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6be271c..967a11c 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ Please give this repo a ⭐ if it inspires you. |[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| |[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| |[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| +|[931](https://leetcode.com/problems/minimum-falling-path-sum/)| Minimum Falling Path Sum| ## Medium |LC #|Description| From a181ea99dbd8af3c0063fc2733fefb6fcc9cc1ac Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 12 Dec 2022 17:09:50 -0800 Subject: [PATCH 598/969] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 967a11c..6988700 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,11 @@ Please give this repo a ⭐ if it inspires you. |[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| |[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| |[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| -|[931](https://leetcode.com/problems/minimum-falling-path-sum/)| Minimum Falling Path Sum| ## Medium |LC #|Description| |:-:|:-| +|[931](https://leetcode.com/problems/minimum-falling-path-sum/)| Minimum Falling Path Sum| |[946](https://leetcode.com/problems/validate-stack-sequences/)| Validate Stack Sequences| |[29](https://leetcode.com/problems/divide-two-integers/)| Divide Two Integers| |[820](https://leetcode.com/problems/short-encoding-of-words/)| Short Encoding of Words| From e171f15b8843bf76b33554381ff193e4a1238c7e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 18 Dec 2022 18:24:06 -0800 Subject: [PATCH 599/969] Create L1971.go --- Easy/L1971.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Easy/L1971.go diff --git a/Easy/L1971.go b/Easy/L1971.go new file mode 100644 index 0000000..fe543ad --- /dev/null +++ b/Easy/L1971.go @@ -0,0 +1,34 @@ +package Easy + +var exists struct{} + +func validPath(n int, edges [][]int, source int, destination int) bool { + graph, vis := make([][]int, n), make(map[int]struct{}) + for i := 0; i < n; i++ { + graph[i] = make([]int, 0) + } + + for _, e := range edges { + graph[e[0]] = append(graph[e[0]], e[1]) + graph[e[1]] = append(graph[e[1]], e[0]) + } + + return dfs(graph, vis, source, destination) +} + +func dfs(graph [][]int, vis map[int]struct{}, source, dest int) bool { + if source == dest { + return true + } + if _, ok := vis[source]; ok { + return false + } + vis[source] = exists + + found := false + for _, neigh := range graph[source] { + found = found || dfs(graph, vis, neigh, dest) + } + + return found +} From 36bfcd37053c37ba49e1e6193d8c8243cbf421bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 18 Dec 2022 18:24:37 -0800 Subject: [PATCH 600/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6988700..9605e35 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ Please give this repo a ⭐ if it inspires you. |[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| |[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| |[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| +|[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| ## Medium |LC #|Description| From 4ed82ae406ff59a06c717a8aca7b3d63de476c96 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Dec 2022 07:54:13 -0800 Subject: [PATCH 601/969] Create L886.go --- Medium/L886.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Medium/L886.go diff --git a/Medium/L886.go b/Medium/L886.go new file mode 100644 index 0000000..ac15e98 --- /dev/null +++ b/Medium/L886.go @@ -0,0 +1,49 @@ +package Medium + +var exists struct{} + +func possibleBipartition(n int, dislikes [][]int) bool { + if n == 0 || len(dislikes) == 0 { + return true + } + + graph := make([][]int, n+1) + for i := range graph { + graph[i] = make([]int, 0) + } + + for _, d := range dislikes { + graph[d[0]] = append(graph[d[0]], d[1]) + graph[d[1]] = append(graph[d[1]], d[0]) + } + + color := make([]int, n+1) + var q []int + for i := 0; i <= n; i++ { + if color[i] != 0 { + continue + } + q = append(q, i) + color[i] = 1 + st := make(map[int]struct{}) + + for len(q) > 0 { + node := q[0] + q = q[1:] + if _, ok := st[node]; ok { + continue + } + st[node] = exists + + for _, nei := range graph[node] { + if color[nei] != 0 && color[nei] != -color[node] { + return false + } + color[nei] = -color[node] + q = append(q, nei) + } + } + } + + return true +} From 0cef2b13cc928ffff534dd7c7d5c69d4ff179748 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 21 Dec 2022 07:55:08 -0800 Subject: [PATCH 602/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9605e35..fd3be1b 100644 --- a/README.md +++ b/README.md @@ -386,6 +386,7 @@ Please give this repo a ⭐ if it inspires you. |[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| |[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| |[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| +|[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| ## Hard |LC #|Description| From 70351ea417ef579fa957cb1a71b89e7e9704984b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Dec 2022 07:30:22 -0800 Subject: [PATCH 603/969] Create L600.go --- Hard/L600.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Hard/L600.go diff --git a/Hard/L600.go b/Hard/L600.go new file mode 100644 index 0000000..c9b6a1c --- /dev/null +++ b/Hard/L600.go @@ -0,0 +1,26 @@ +package Hard + +func findIntegers(n int) int { + f := make([]int, 32) + f[0], f[1] = 1, 2 + + for i := 2; i < 32; i++ { + f[i] = f[i-1] + f[i-2] + } + + ans, k, preBit := 0, 30, 0 + for k >= 0 { + if (n & (1 << k)) > 0 { + ans += f[k] + if preBit > 0 { + return ans + } + preBit = 1 + } else { + preBit = 0 + } + k-- + } + + return ans + 1 +} From b0d0c2e3ae6165dd8659e9d5d427634648091ff2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Dec 2022 07:31:03 -0800 Subject: [PATCH 604/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fd3be1b..27600d6 100644 --- a/README.md +++ b/README.md @@ -444,3 +444,4 @@ Please give this repo a ⭐ if it inspires you. |[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| |[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| |[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| +|[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| From 1e8312b6e3f03315f37c8cc66b62d0bd55edb096 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Dec 2022 07:38:28 -0800 Subject: [PATCH 605/969] Create L2389.go --- Easy/L2389.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Easy/L2389.go diff --git a/Easy/L2389.go b/Easy/L2389.go new file mode 100644 index 0000000..2a52d03 --- /dev/null +++ b/Easy/L2389.go @@ -0,0 +1,26 @@ +package Easy + +func answerQueries(nums []int, queries []int) []int { + n, m, sum := len(nums), len(queries), 0 + + sort.Ints(nums) + var preSum []int + + for i := 0; i < n; i++ { + sum += nums[i] + preSum = append(preSum, sum) + } + + ans := make([]int, m) + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if preSum[j] <= queries[i] { + ans[i] = j + 1 + } else { + break + } + } + } + + return ans +} From 694f69f179c2f11e1c55a8b731a1a144fad0308e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Dec 2022 07:39:08 -0800 Subject: [PATCH 606/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 27600d6..86acabc 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Please give this repo a ⭐ if it inspires you. |[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| |[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| |[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| +|[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| ## Medium |LC #|Description| From d6fefe76ef28638d9b5c373de8d995b06e86336f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 25 Dec 2022 07:52:20 -0800 Subject: [PATCH 607/969] Update L233.go --- Hard/L233.go | 55 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/Hard/L233.go b/Hard/L233.go index 2f883ec..35e01f6 100644 --- a/Hard/L233.go +++ b/Hard/L233.go @@ -1,26 +1,49 @@ package Hard -func findIntegers(n int) int { - f := make([]int, 32) - f[0], f[1] = 1, 2 +func countDigitOne(n int) int { + str := fmt.Sprintf("%d", n) + dp := make([][][]int, 10) - for i := 2; i < 32; i++ { - f[i] = f[i-1] + f[i-2] + for i := 0; i < 10; i++ { + dp[i] = make([][]int, 2) + for j := 0; j < 2; j++ { + dp[i][j] = make([]int, 10) + for k := 0; k < 10; k++ { + dp[i][j][k] = -1 + } + } } - ans, k, preBit := 0, 30, 0 - for k >= 0 { - if (n & (1 << k)) > 0 { - ans += f[k] - if preBit > 0 { - return ans - } - preBit = 1 + return solve(str, 0, 1, 0, &dp) +} + +func solve(s string, idx, tight, count int, dp *[][][]int) int { + if idx == len(s) { + return count + } + + if (*dp)[idx][tight][count] != -1 { + return (*dp)[idx][tight][count] + } + + ans, bound := 0, 9 + if tight == 1 { + bound = int(s[idx] - '0') + } + + for i := 0; i <= bound; i++ { + add := 0 + if i == 1 { + add = 1 + } + + if i == bound && tight == 1 { + ans += solve(s, idx+1, tight, count+add, dp) } else { - preBit = 0 + ans += solve(s, idx+1, 0, count+add, dp) } - k-- } - return ans + 1 + (*dp)[idx][tight][count] = ans + return ans } From 12499a7496880dc538d7b8b422c84c2a28e1dc77 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Dec 2022 15:58:10 -0800 Subject: [PATCH 608/969] Create L72.go --- Hard/L72.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Hard/L72.go diff --git a/Hard/L72.go b/Hard/L72.go new file mode 100644 index 0000000..95e25e0 --- /dev/null +++ b/Hard/L72.go @@ -0,0 +1,42 @@ +package Hard + +func minDistance(word1 string, word2 string) int { + m, n, mp := len(word1), len(word2), make(map[string]int) + return solve(word1, word2, m, n, mp) +} + +func solve(w1, w2 string, m, n int, mp map[string]int) int { + key := fmt.Sprintf("%d-%d", m, n) + if m == 0 { + mp[key] = n + return mp[key] + } + + if n == 0 { + mp[key] = m + return mp[key] + } + + if _, ok := mp[key]; ok { + return mp[key] + } + + if w1[m-1] == w2[n-1] { + return solve(w1, w2, m-1, n-1, mp) + } + + insertChar := solve(w1, w2, m-1, n, mp) + deleteChar := solve(w1, w2, m, n-1, mp) + replaceChar := solve(w1, w2, m - 1, n - 1, mp) + + mp[key] = 1 + min(insertChar, min(replaceChar, deleteChar)) + return mp[key] +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 6357a292372afa0e112c7a27206b693ab417d240 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Dec 2022 15:58:48 -0800 Subject: [PATCH 609/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 86acabc..0087544 100644 --- a/README.md +++ b/README.md @@ -446,3 +446,4 @@ Please give this repo a ⭐ if it inspires you. |[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| |[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| |[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| +|[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| From 4fde4f6d59d4d962a5207c618fe286d618c60a6d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Dec 2022 16:39:06 -0800 Subject: [PATCH 610/969] Create L2279.go --- Medium/L2279.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L2279.go diff --git a/Medium/L2279.go b/Medium/L2279.go new file mode 100644 index 0000000..27929f2 --- /dev/null +++ b/Medium/L2279.go @@ -0,0 +1,20 @@ +package Medium + +func maximumBags(capacity []int, rocks []int, additionalRocks int) int { + var diff []int + for i := 0; i < len(rocks); i++ { + diff = append(diff, capacity[i]-rocks[i]) + } + + sort.Ints(diff) + ans, cnt := 0, 0 + for _, entry := range diff { + cnt += entry + if cnt > additionalRocks { + break + } + ans++ + } + + return ans +} From 9e8deb4de1b930ee8c6126fc01afa2859bc7c437 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 26 Dec 2022 16:39:55 -0800 Subject: [PATCH 611/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0087544..2fbc947 100644 --- a/README.md +++ b/README.md @@ -388,6 +388,7 @@ Please give this repo a ⭐ if it inspires you. |[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| |[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| |[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| +|[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| ## Hard |LC #|Description| From 97b190c2b9154636ea9038787861884934c42d48 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 11:33:39 -0800 Subject: [PATCH 612/969] Create L343.go --- Medium/L343.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L343.go diff --git a/Medium/L343.go b/Medium/L343.go new file mode 100644 index 0000000..ff1c07e --- /dev/null +++ b/Medium/L343.go @@ -0,0 +1,34 @@ +package Medium + +func integerBreak(n int) int { + mp := make(map[string]int) + + return solve(n, n - 1, mp) +} + +func solve(n, idx int, mp map[string]int) int { + if n == 0 || idx == 0 { + return 1 + } + + key := fmt.Sprintf("%d-%d", n, idx) + if v, ok := mp[key]; ok { + return v + } + + if idx > n { + mp[key] = solve(n, idx - 1, mp) + } else { + mp[key] = max(idx * solve(n - idx, idx, mp), solve(n, idx - 1, mp)) + } + + return mp[key] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 82fd8bc73af19d379d532043bc8d05ea68034386 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 11:34:17 -0800 Subject: [PATCH 613/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2fbc947..23ef882 100644 --- a/README.md +++ b/README.md @@ -389,6 +389,7 @@ Please give this repo a ⭐ if it inspires you. |[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| |[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| |[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| +|[343](https://leetcode.com/problems/integer-break/)| Integer Break| ## Hard |LC #|Description| From 9e00d5d06b21c890a12a6d1e822466e2dc2d811a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 14:58:12 -0800 Subject: [PATCH 614/969] Create L256.go --- Medium/L256.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L256.go diff --git a/Medium/L256.go b/Medium/L256.go new file mode 100644 index 0000000..363e0cf --- /dev/null +++ b/Medium/L256.go @@ -0,0 +1,46 @@ +package Medium + +func minCost(costs [][]int) int { + mp := make(map[string]int) + + mini := math.MaxInt32 + for i := 0; i < len(costs[0]); i++ { + mini = min(mini, solve(costs, mp, 0, i)) + } + + return mini +} + +func solve(costs [][]int, mp map[string]int, house, color int) int { + if house >= len(costs) { + return 0 + } + + if color >= len(costs[0]) { + return 0 + } + + key := fmt.Sprintf("%d-%d", house, color) + if v, ok := mp[key]; ok { + return v + } + + mini := math.MaxInt32 + for i := 0; i < len(costs[0]); i++ { + if i == color { + continue + } + mini = min(mini, costs[house][color]+solve(costs, mp, house+1, i)) + } + + mp[key] = mini + return mp[key] +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From df92e2d56d6f7d075937494f3a1cd52c25abc338 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 14:58:49 -0800 Subject: [PATCH 615/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 23ef882..ff50d3a 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,7 @@ Please give this repo a ⭐ if it inspires you. |[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| |[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| |[343](https://leetcode.com/problems/integer-break/)| Integer Break| +|[256](https://leetcode.com/problems/paint-house/)| Paint House| ## Hard |LC #|Description| From 0eee6692d54c70501558bd463afe0391269975e3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 20:34:38 -0800 Subject: [PATCH 616/969] Create L487.go --- Medium/L487.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L487.go diff --git a/Medium/L487.go b/Medium/L487.go new file mode 100644 index 0000000..63cb40a --- /dev/null +++ b/Medium/L487.go @@ -0,0 +1,38 @@ +package Medium + +func findMaxConsecutiveOnes(nums []int) int { + mp := make(map[string]int) + + return solve(0, 1, 0, nums, mp) +} + +func solve(idx, k, cnt int, nums []int, mp map[string]int) int { + if idx >= len(nums) { + return cnt + } + + if nums[idx] == 0 && k == 0 { + return cnt + } + + key := fmt.Sprintf("%d-%d", idx, k) + if v, ok := mp[key]; ok { + return v + } + + if nums[idx] == 0 { + mp[key] = max(solve(idx+1, k-1, cnt+1, nums, mp), solve(idx+1, k, 0, nums, mp)) + } else { + mp[key] = solve(idx + 1, k, cnt + 1, nums, mp) + } + + return mp[key] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From a0d734e2fa68837f2a2bf02580eccd2220cc8832 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 27 Dec 2022 20:35:14 -0800 Subject: [PATCH 617/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ff50d3a..f2ab3fd 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,7 @@ Please give this repo a ⭐ if it inspires you. |[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| |[343](https://leetcode.com/problems/integer-break/)| Integer Break| |[256](https://leetcode.com/problems/paint-house/)| Paint House| +|[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| ## Hard |LC #|Description| From b380c65cc43b220a7e111357509996df9e248059 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 28 Dec 2022 07:20:51 -0800 Subject: [PATCH 618/969] Create L1746.go --- Medium/L1746.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L1746.go diff --git a/Medium/L1746.go b/Medium/L1746.go new file mode 100644 index 0000000..9c2a079 --- /dev/null +++ b/Medium/L1746.go @@ -0,0 +1,39 @@ +package Medium + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func maxSumAfterOperation(nums []int) int { + mp, finalRes := make(map[string]int), nums[0] + + solve(0, &finalRes, true, nums, mp) + return finalRes +} + +func solve(idx int, finalRes *int, useSquare bool, nums []int, mp map[string]int) int { + if idx >= len(nums) { + return 0 + } + + key := fmt.Sprintf("%d-%v", idx, useSquare) + if v, ok := mp[key]; ok { + return v + } + + ans := 0 + if useSquare { + ans = max(nums[idx]*nums[idx], (nums[idx]*nums[idx])+solve(idx+1, finalRes, false, nums, mp)) + } + + ans = max(ans, max(nums[idx]+solve(idx+1, finalRes, useSquare, nums, mp), nums[idx])) + + mp[key] = ans + *finalRes = max(*finalRes, ans) + + return mp[key] +} From 8218427ae897bced71e6c3f61c0bf1b91106647f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 28 Dec 2022 07:21:26 -0800 Subject: [PATCH 619/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2ab3fd..f0ffc72 100644 --- a/README.md +++ b/README.md @@ -392,6 +392,7 @@ Please give this repo a ⭐ if it inspires you. |[343](https://leetcode.com/problems/integer-break/)| Integer Break| |[256](https://leetcode.com/problems/paint-house/)| Paint House| |[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| +|[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| ## Hard |LC #|Description| From 66445b92ae6d55af08caf04fd6cd43dcb223a850 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 28 Dec 2022 08:09:54 -0800 Subject: [PATCH 620/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f0ffc72..7533a2e 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,7 @@ Please give this repo a ⭐ if it inspires you. |[256](https://leetcode.com/problems/paint-house/)| Paint House| |[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| |[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| +|[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| ## Hard |LC #|Description| From 0fd6b80ddde35c8e67994f5f07ae8ecc87187b6f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 28 Dec 2022 08:10:23 -0800 Subject: [PATCH 621/969] Create L1230.go --- Medium/L1230.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L1230.go diff --git a/Medium/L1230.go b/Medium/L1230.go new file mode 100644 index 0000000..9827c44 --- /dev/null +++ b/Medium/L1230.go @@ -0,0 +1,31 @@ +package Medium + +func probabilityOfHeads(prob []float64, target int) float64 { + mp := make(map[string]float64) + + return solve(len(prob), target, prob, mp) +} + +func solve(idx, tgt int, prob []float64, mp map[string]float64) float64 { + if idx == 0 && tgt == 0 { + return 1 + } + if idx == 0 { + return 0 + } + if idx < tgt { + return 0 + } + + key := fmt.Sprintf("%d-%d", idx, tgt) + if v, ok := mp[key]; ok { + return v + } + + if tgt == 1 { + mp[key] += solve(idx-1, tgt-1, prob, mp) * prob[idx-1] + } + mp[key] += solve(idx-1, tgt, prob, mp) * (1 - prob[idx-1]) + + return mp[key] +} From f52e23c5a810bf506b6aaca092dd4d39236c5ba5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 29 Dec 2022 12:08:47 -0800 Subject: [PATCH 622/969] Create L1340.go --- Hard/L1340.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Hard/L1340.go diff --git a/Hard/L1340.go b/Hard/L1340.go new file mode 100644 index 0000000..042cb21 --- /dev/null +++ b/Hard/L1340.go @@ -0,0 +1,44 @@ +package Hard + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func maxJumps(arr []int, d int) int { + n, mp, res := len(arr), make(map[int]int), 1 + + for i := 0; i < n; i++ { + res = max(res, solve(arr, mp, n, d, i)) + } + + return res +} + +func solve(arr []int, mp map[int]int, n, d, idx int) int { + if v, ok := mp[idx]; ok { + return v + } + + res := 1 + for j := idx + 1; j <= min(idx + d, n - 1) && arr[j] < arr[idx]; j++ { + res = max(res, 1 + solve(arr, mp, n, d, j)) + } + for j := idx - 1; j >= max(idx - d, 0) && arr[j] < arr[idx]; j-- { + res = max(res, 1 + solve(arr, mp, n, d, j)) + } + + mp[idx] = res + return mp[idx] +} From bb2b9327392fa3e1db24958ecae6f2e8d60b9611 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 29 Dec 2022 12:09:24 -0800 Subject: [PATCH 623/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7533a2e..f884ad4 100644 --- a/README.md +++ b/README.md @@ -453,3 +453,4 @@ Please give this repo a ⭐ if it inspires you. |[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| |[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| |[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| +|[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| From ffe071843169f1141250b5d6d66a02cf7e2336e9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:26:34 -0800 Subject: [PATCH 624/969] Create L403.go --- Hard/L403.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Hard/L403.go diff --git a/Hard/L403.go b/Hard/L403.go new file mode 100644 index 0000000..7ca864d --- /dev/null +++ b/Hard/L403.go @@ -0,0 +1,45 @@ +package Hard + +var exists struct{} + +func canCross(stones []int) bool { + st, mp, n := make(map[int]struct{}), make(map[string]bool), len(stones) + + for _, s := range stones { + st[s] = exists + } + + return solve(st, mp, stones, n, 0, 1) +} + +func solve(st map[int]struct{}, mp map[string]bool, stones []int, n, pos, prevStep int) bool { + if _, ok := st[pos]; !ok { + return false + } + if pos > stones[n-1] { + return false + } + if pos == stones[n-1] { + return true + } + + key := fmt.Sprintf("%d-%d", pos, prevStep) + if v, ok := mp[key]; ok { + return v + } + + if pos == 0 { + mp[key] = solve(st, mp, stones, n, pos+1, 1) + } else { + if prevStep-1 > 0 { + mp[key] = solve(st, mp, stones, n, pos+(prevStep-1), prevStep-1) + if mp[key] { + return mp[key] + } + } + mp[key] = solve(st, mp, stones, n, pos+prevStep, prevStep) || + solve(st, mp, stones, n, pos+(prevStep+1), prevStep+1) + } + + return mp[key] +} From b6aa2437570dc5ce36eb5a0b995b7774ccff9c4f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:27:08 -0800 Subject: [PATCH 625/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f884ad4..1d59481 100644 --- a/README.md +++ b/README.md @@ -454,3 +454,4 @@ Please give this repo a ⭐ if it inspires you. |[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| |[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| |[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| +|[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| From 86511f917d0d930a587a146fa4c3103071bb936b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:38:44 -0800 Subject: [PATCH 626/969] Create L2498.go --- Medium/L2498.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L2498.go diff --git a/Medium/L2498.go b/Medium/L2498.go new file mode 100644 index 0000000..7b7e624 --- /dev/null +++ b/Medium/L2498.go @@ -0,0 +1,18 @@ +package Medium + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func maxJump(stones []int) int { + res := stones[1] - stones[0] + for i := 2; i < len(stones); i++ { + res = max(res, stones[i] - stones[i - 2]) + } + + return res +} From 4961b6a27a071a96944e6fe52315e4028ac8fa5f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:39:22 -0800 Subject: [PATCH 627/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1d59481..26d19d0 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,7 @@ Please give this repo a ⭐ if it inspires you. |[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| |[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| |[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| +|[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| ## Hard |LC #|Description| From 9e19bd80c4875806d46dd5c457087241f54ec138 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:58:06 -0800 Subject: [PATCH 628/969] Create L2073.go --- Easy/L2073.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L2073.go diff --git a/Easy/L2073.go b/Easy/L2073.go new file mode 100644 index 0000000..96f7441 --- /dev/null +++ b/Easy/L2073.go @@ -0,0 +1,18 @@ +package Easy + +func timeRequiredToBuy(tickets []int, k int) int { + total := 0 + for i := 0; i < len(tickets); i++ { + if tickets[i] <= tickets[k] { + total += tickets[i] + } else { + total += tickets[k] + } + + if i > k && tickets[i] >= tickets[k] { + total-- + } + } + + return total +} From cb2b4d39b46ca1ed099366250646b7e54355ece2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 30 Dec 2022 07:58:55 -0800 Subject: [PATCH 629/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 26d19d0..6aece1f 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Please give this repo a ⭐ if it inspires you. |[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| |[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| |[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| +|[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| ## Medium |LC #|Description| From cc718783129c1e13ed9174404d22c4689f1a9677 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 2 Jan 2023 06:27:07 -0800 Subject: [PATCH 630/969] Create L188.go --- Hard/L188.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Hard/L188.go diff --git a/Hard/L188.go b/Hard/L188.go new file mode 100644 index 0000000..19d78a4 --- /dev/null +++ b/Hard/L188.go @@ -0,0 +1,34 @@ +package Hard + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func maxProfit(k int, prices []int) int { + mp := make(map[string]int) + + return solve(prices, mp, 0, k, true) +} + +func solve(prices []int, mp map[string]int, idx, k int, buy bool) int { + if idx >= len(prices) || k <= 0 { + return 0 + } + + key := fmt.Sprintf("%d-%d-%d", idx, buy, k) + if v, ok := mp[key]; ok { + return v + } + + if buy { + mp[key] = max(-prices[idx]+solve(prices, mp, idx+1, k, !buy), solve(prices, mp, idx+1, k, buy)) + } else { + mp[key] = max(prices[idx]+solve(prices, mp, idx+1, k-1, !buy), solve(prices, mp, idx+1, k, buy)) + } + + return mp[key] +} From a26ca9c96a478d9daabe294ca2417f015d28cd8e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 2 Jan 2023 06:27:44 -0800 Subject: [PATCH 631/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6aece1f..e641490 100644 --- a/README.md +++ b/README.md @@ -457,3 +457,4 @@ Please give this repo a ⭐ if it inspires you. |[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| |[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| |[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| +|[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| From 82c7441e843fd66b1417469b364b4769fb169d60 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Jan 2023 06:06:32 -0800 Subject: [PATCH 632/969] Create L2244.go --- Medium/L2244.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Medium/L2244.go diff --git a/Medium/L2244.go b/Medium/L2244.go new file mode 100644 index 0000000..a78e7a7 --- /dev/null +++ b/Medium/L2244.go @@ -0,0 +1,23 @@ +package Medium + +func minimumRounds(tasks []int) int { + cnt, mp := 0, make(map[int]int) + + for _, t := range tasks { + mp[t]++ + } + + for _, v := range mp { + if v < 2 { + return -1 + } + + if v%3 == 0 { + cnt += v / 3 + } else { + cnt += (v / 3) + 1 + } + } + + return cnt +} From 856a29d58fa877dee635b2216ef93d3633ac0e72 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Jan 2023 06:07:15 -0800 Subject: [PATCH 633/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e641490..d2090ad 100644 --- a/README.md +++ b/README.md @@ -396,6 +396,7 @@ Please give this repo a ⭐ if it inspires you. |[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| |[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| |[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| +|[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| ## Hard |LC #|Description| From 185edaa0d0898fce72650432d3384e7b4112fc87 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Jan 2023 06:50:38 -0800 Subject: [PATCH 634/969] Create L697.go --- Easy/L697.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Easy/L697.go diff --git a/Easy/L697.go b/Easy/L697.go new file mode 100644 index 0000000..17ee9bc --- /dev/null +++ b/Easy/L697.go @@ -0,0 +1,36 @@ +package Easy + +func findShortestSubArray(nums []int) int { + if len(nums) == 0 { + return 0 + } + + mp := make(map[int][]int) + for i := range nums { + if _, ok := mp[nums[i]]; !ok { + mp[nums[i]] = append(mp[nums[i]], []int{1, i, i}...) + } else { + mp[nums[i]][0]++ + mp[nums[i]][2] = i + } + } + + deg, res := math.MinInt32, math.MaxInt32 + for _, v := range mp { + if v[0] > deg { + deg, res = v[0], v[2]-v[1]+1 + } else if v[0] == deg { + res = min(res, v[2]-v[1]+1) + } + } + + return res +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From e51a6321086f25dfbf0f0df55c0251bb7f5e7710 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Jan 2023 06:51:10 -0800 Subject: [PATCH 635/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d2090ad..e71b1ae 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Please give this repo a ⭐ if it inspires you. |[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| |[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| |[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| +|[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| ## Medium |LC #|Description| From 3900fa005d8d958f0f3741a2b05fd3efd9ffb172 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 06:28:16 -0800 Subject: [PATCH 636/969] Create L1749.go --- Medium/L1749.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L1749.go diff --git a/Medium/L1749.go b/Medium/L1749.go new file mode 100644 index 0000000..ddb734d --- /dev/null +++ b/Medium/L1749.go @@ -0,0 +1,39 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func maxAbsoluteSum(nums []int) int { + posAns, curPosSum, negAns, curNegSum := 0, 0, 0, 0 + + for i := range nums { + if curPosSum+nums[i] > 0 { + curPosSum += nums[i] + } else { + curPosSum = 0 + } + posAns = max(posAns, curPosSum) + + if curNegSum+nums[i] < 0 { + curNegSum += nums[i] + } else { + curNegSum = 0 + } + negAns = min(negAns, curNegSum) + } + + return max(posAns, -negAns) +} From 77a3e1524e7749f2e08494d8c0a79f0e5768da57 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 06:28:54 -0800 Subject: [PATCH 637/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e71b1ae..5cce99d 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,7 @@ Please give this repo a ⭐ if it inspires you. |[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| |[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| |[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| +|[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| ## Hard |LC #|Description| From 97e0e477018e9d805265b7e7e28a050e6664926c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 07:36:19 -0800 Subject: [PATCH 638/969] Create L2189.go --- Medium/L2189.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Medium/L2189.go diff --git a/Medium/L2189.go b/Medium/L2189.go new file mode 100644 index 0000000..68ae8af --- /dev/null +++ b/Medium/L2189.go @@ -0,0 +1,26 @@ +package Medium + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} + +func minSteps(s string, t string) int { + cnt, arr := 0, make([]int, 26) + + for _, c := range s { + arr[c-'a']++ + } + for _, c := range t { + arr[c-'a']-- + } + + for _, v := range arr { + cnt += abs(v) + } + + return cnt +} From 7ef8ef0a88b164f8bb6912bfbf32d66fc55804cb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 07:36:57 -0800 Subject: [PATCH 639/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5cce99d..bc522e5 100644 --- a/README.md +++ b/README.md @@ -399,6 +399,7 @@ Please give this repo a ⭐ if it inspires you. |[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| |[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| |[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| +|[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| ## Hard |LC #|Description| From c1317bf7ea32c10c50abef9da0b38adbf92e5d80 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 17:05:47 -0800 Subject: [PATCH 640/969] Create L149.go --- Hard/L149.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Hard/L149.go diff --git a/Hard/L149.go b/Hard/L149.go new file mode 100644 index 0000000..89d257b --- /dev/null +++ b/Hard/L149.go @@ -0,0 +1,47 @@ +package Hard + +func maxPoints(points [][]int) int { + if len(points) == 0 { + return 0 + } + + res := 0 + for i := 0; i < len(points); i++ { + mp, dup, maxi := make(map[string]int), 0, 0 + for j := i + 1; j < len(points); j++ { + deltaX := points[j][0] - points[i][0] + deltaY := points[j][1] - points[i][1] + + if deltaX == 0 && deltaY == 0 { + dup++ + continue + } + + gcd := greatestCommonDiv(deltaX, deltaY) + dX, dY := deltaX / gcd, deltaY / gcd + + key := fmt.Sprintf("%d-%d", dX, dY) + mp[key]++ + maxi = max(maxi, mp[key]) + } + res = max(res, maxi + dup + 1) + } + + return res +} + +func greatestCommonDiv(a, b int) int { + if b == 0 { + return a + } + + return greatestCommonDiv(b, a % b) +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From d4faa4bdf656b8c274a309fca695f1aaab5fafac Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 7 Jan 2023 17:06:26 -0800 Subject: [PATCH 641/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bc522e5..7eaf241 100644 --- a/README.md +++ b/README.md @@ -462,3 +462,4 @@ Please give this repo a ⭐ if it inspires you. |[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| |[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| |[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| +|[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| From 8747ca1c0c50f70591da6e37737db9263dc5c8af Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 8 Jan 2023 07:10:59 -0800 Subject: [PATCH 642/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7eaf241..7c043cd 100644 --- a/README.md +++ b/README.md @@ -463,3 +463,4 @@ Please give this repo a ⭐ if it inspires you. |[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| |[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| |[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| +|[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| From 4e8312b9b63e6c6d945e224048b481033bdf4608 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 8 Jan 2023 07:11:28 -0800 Subject: [PATCH 643/969] Create L140.go --- Hard/L140.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Hard/L140.go diff --git a/Hard/L140.go b/Hard/L140.go new file mode 100644 index 0000000..b36776e --- /dev/null +++ b/Hard/L140.go @@ -0,0 +1,42 @@ +package Hard + +var exists struct{} + +func wordBreak(s string, wordDict []string) []string { + mp, st := make(map[int][]string), make(map[string]struct{}) + + for _, w := range wordDict { + st[w] = exists + } + + return solve(s, 0, st, mp) +} + +func solve(s string, start int, st map[string]struct{}, mp map[int][]string) []string { + if v, ok := mp[start]; ok { + return v + } + + var res []string + if start == len(s) { + res = append(res, "") + return res + } + + curr := s[start:] + for w, _ := range st { + if strings.HasPrefix(curr, w) { + subLst := solve(s, start+len(w), st, mp) + for _, sub := range subLst { + if len(sub) == 0 { + res = append(res, w+"") + } else { + res = append(res, w+" "+sub) + } + } + } + } + + mp[start] = res + return mp[start] +} From 023d218d9f4c6f61cf0a1a6cbee77ab3960dba31 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 9 Jan 2023 05:20:21 -0800 Subject: [PATCH 644/969] Update L145.go --- Easy/L145.go | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/Easy/L145.go b/Easy/L145.go index 30df343..12a14e6 100644 --- a/Easy/L145.go +++ b/Easy/L145.go @@ -1,22 +1,50 @@ package Easy -func postorderTraversal(root *TreeNode) []int { - var res []int - if root == nil { - return res +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +// Iterative Solution +func preorderTraversal(root *TreeNode) []int { + var ( + res []int + st []*TreeNode + ) + st = append(st, root) + + for len(st) > 0 { + node := st[len(st) - 1] + st = st[:len(st) - 1] + if node != nil { + res = append(res, node.Val) + st = append(st, node.Right) + st = append(st, node.Left) + } } + + return res +} + - postOrder(root, &res) +// Recursive Solution +func preorderTraversal(root *TreeNode) []int { + var res []int + solve(root, &res) return res } -func postOrder(root *TreeNode, list *[]int) { +func solve(root *TreeNode, res *[]int) { if root == nil { return } - postOrder(root.Left, list) - postOrder(root.Right, list) - *list = append(*list, root.Val) + *res = append(*res, root.Val) + solve(root.Left, res) + solve(root.Right, res) } From 94c9683bc727a3682e4f82a0cff1ef971b23c18c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 9 Jan 2023 06:32:23 -0800 Subject: [PATCH 645/969] Create L2414.go --- Medium/L2414.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L2414.go diff --git a/Medium/L2414.go b/Medium/L2414.go new file mode 100644 index 0000000..b73b949 --- /dev/null +++ b/Medium/L2414.go @@ -0,0 +1,25 @@ +package Medium + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func longestContinuousSubstring(s string) int { + res, cnt := 0, 1 + + for i := 1; i < len(s); i++ { + if s[i-1] + 1 == s[i] { + cnt++ + } else { + res = max(res, cnt) + cnt = 1 + } + } + + res = max(res, cnt) + return res +} From 847b4837552a0caa3b047ab55d507615fe089f34 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 9 Jan 2023 06:33:02 -0800 Subject: [PATCH 646/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7c043cd..b9a9a7f 100644 --- a/README.md +++ b/README.md @@ -400,6 +400,7 @@ Please give this repo a ⭐ if it inspires you. |[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| |[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| |[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| +|[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| ## Hard |LC #|Description| From 8fa34c7c09add09ea04b1286b50e923c70450d65 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 Jan 2023 07:07:15 -0800 Subject: [PATCH 647/969] Create L1519.go --- Medium/L1519.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Medium/L1519.go diff --git a/Medium/L1519.go b/Medium/L1519.go new file mode 100644 index 0000000..6550eb2 --- /dev/null +++ b/Medium/L1519.go @@ -0,0 +1,30 @@ +package Medium + +func countSubTrees(n int, edges [][]int, labels string) []int { + res, mp := make([]int, n), make(map[int][]int) + + for _, e := range edges { + mp[e[0]] = append(mp[e[0]], e[1]) + mp[e[1]] = append(mp[e[1]], e[0]) + } + + solve(mp, labels, 0, -1, &res) + return res +} + +func solve(graph map[int][]int, labels string, node, parent int, res *[]int) []int { + nodeCnt := make([]int, 26) + nodeCnt[labels[node]-'a']++ + + for _, nei := range graph[node] { + if nei != parent { + childCnt := solve(graph, labels, nei, node, res) + for i := 0; i < 26; i++ { + nodeCnt[i] += childCnt[i] + } + } + } + + (*res)[node] = nodeCnt[labels[node]-'a'] + return nodeCnt +} From d1658d81c4f48ca3a204a8f87064e9c4adb708ea Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 12 Jan 2023 07:08:53 -0800 Subject: [PATCH 648/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b9a9a7f..a11ef79 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,7 @@ Please give this repo a ⭐ if it inspires you. |[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| |[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| |[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| +|[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| ## Hard |LC #|Description| From 6ddf4f6f66f4617a8841ad1104b7af46cec94fbe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Jan 2023 07:38:13 -0800 Subject: [PATCH 649/969] Create L1289.go --- Hard/L1289.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Hard/L1289.go diff --git a/Hard/L1289.go b/Hard/L1289.go new file mode 100644 index 0000000..c446aa1 --- /dev/null +++ b/Hard/L1289.go @@ -0,0 +1,50 @@ +package Hard + +func minFallingPathSum(grid [][]int) int { + n, mp, res := len(grid), make([][]int, len(grid)), math.MaxInt32 + if n == 1 { + return grid[0][0] + } + + for i := 0; i < n; i++ { + mp[i] = make([]int, n) + for j := 0; j < n; j++ { + mp[i][j] = -1 + } + } + + for i := 0; i < n; i++ { + res = min(res, solve(0, i, grid, &mp)) + } + + return res +} + +func solve(r, c int, grid [][]int, mp *[][]int) int { + if r == len(grid) { + return 0 + } + + if (*mp)[r][c] != -1 { + return (*mp)[r][c] + } + + res := math.MaxInt32 + for k := 0; k < len(grid[0]); k++ { + if c == k { + continue + } + res = min(res, grid[r][c]+solve(r+1, k, grid, mp)) + } + + (*mp)[r][c] = res + return (*mp)[r][c] +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From f4d0c1923a10acd310746652cb67870575ff91c7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Jan 2023 07:39:00 -0800 Subject: [PATCH 650/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a11ef79..d901f53 100644 --- a/README.md +++ b/README.md @@ -466,3 +466,4 @@ Please give this repo a ⭐ if it inspires you. |[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| |[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| |[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| +|[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| From 1d91e6331625aba20024d742bc0cbb702d6de7da Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Jan 2023 11:53:01 -0800 Subject: [PATCH 651/969] Create L2246.go --- Hard/L2246.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Hard/L2246.go diff --git a/Hard/L2246.go b/Hard/L2246.go new file mode 100644 index 0000000..09fa6c2 --- /dev/null +++ b/Hard/L2246.go @@ -0,0 +1,47 @@ +package Hard + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func longestPath(parent []int, s string) int { + longstPathVal, mp := math.MinInt32, make(map[int][]int) + + for i := range parent { + mp[parent[i]] = append(mp[parent[i]], i) + } + + solve(0, parent, mp, s, &longstPathVal) + if longstPathVal == math.MinInt32 { + return 1 + } + + return longstPathVal +} + +func solve(node int, parent []int, mp map[int][]int, s string, longstPathVal *int) int { + if _, ok := mp[node]; !ok { + return 1 + } + maxi, secondMaxi := 0, 0 + + for _, nei := range mp[node] { + longestPathFromNei := solve(nei, parent, mp, s, longstPathVal) + if s[node] == s[nei] { + continue + } + if longestPathFromNei > maxi { + secondMaxi = maxi + maxi = longestPathFromNei + } else if longestPathFromNei > secondMaxi { + secondMaxi = longestPathFromNei + } + } + + *longstPathVal = max(*longstPathVal, maxi+secondMaxi+1) + return maxi + 1 +} From 99b1c44152c4ddc9fcdaf1f362a1158a271bb041 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 13 Jan 2023 11:53:38 -0800 Subject: [PATCH 652/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d901f53..1ec106e 100644 --- a/README.md +++ b/README.md @@ -467,3 +467,4 @@ Please give this repo a ⭐ if it inspires you. |[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| |[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| |[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| +|[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| From 25473fff1ac49b3083aeec5d5e379592efac59c8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 07:05:24 -0800 Subject: [PATCH 653/969] Create L1061.go --- Medium/L1061.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Medium/L1061.go diff --git a/Medium/L1061.go b/Medium/L1061.go new file mode 100644 index 0000000..0d6a5e6 --- /dev/null +++ b/Medium/L1061.go @@ -0,0 +1,51 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func smallestEquivalentString(s1 string, s2 string, baseStr string) string { + var sb strings.Builder + parent := make([]int, 26) + for i := 0; i < 26; i++ { + parent[i] = -1 + } + + for i := 0; i < len(s1); i++ { + union(int(s1[i]-'a'), int(s2[i]-'a'), &parent) + } + for i := 0; i < len(baseStr); i++ { + sb.WriteByte(byte(find(int(baseStr[i]-'a'), &parent) + 'a')) + } + + return sb.String() +} + +func union(x, y int, parent *[]int) { + x, y = find(x, parent), find(y, parent) + + if x != y { + (*parent)[max(x, y)] = min(x, y) + } +} + +func find(x int, parent *[]int) int { + if (*parent)[x] == -1 { + return x + } + + (*parent)[x] = find((*parent)[x], parent) + return (*parent)[x] +} From a056030445e0338bff433dcfdb0318ae5e9505f0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 07:06:03 -0800 Subject: [PATCH 654/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1ec106e..16467f5 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,7 @@ Please give this repo a ⭐ if it inspires you. |[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| |[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| |[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| +|[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| ## Hard |LC #|Description| From 5d86aecd86399a2a82be5dd1949d54a47a26cdd5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 07:20:29 -0800 Subject: [PATCH 655/969] Create L1314.go --- Medium/L1314.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Medium/L1314.go diff --git a/Medium/L1314.go b/Medium/L1314.go new file mode 100644 index 0000000..3089f40 --- /dev/null +++ b/Medium/L1314.go @@ -0,0 +1,48 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func matrixBlockSum(mat [][]int, k int) [][]int { + m, n, total := len(mat), len(mat[0]), make([][]int, len(mat)+1) + for i := 0; i <= m; i++ { + total[i] = make([]int, n+1) + } + + for r := 1; r <= m; r++ { + for c := 1; c <= n; c++ { + total[r][c] = mat[r-1][c-1] + total[r-1][c] + total[r][c-1] - total[r-1][c-1] + } + } + + res := make([][]int, m) + for i := 0; i < m; i++ { + res[i] = make([]int, n) + } + + for r := 0; r < m; r++ { + for c := 0; c < n; c++ { + r1, c1 := max(0, r-k), max(0, c-k) + r2, c2 := min(m-1, r+k), min(n-1, c+k) + + r1, c1 = r1+1, c1+1 + r2, c2 = r2+1, c2+1 + res[r][c] = total[r2][c2] - total[r2][c1-1] - total[r1-1][c2] + total[r1-1][c1-1] + } + } + + return res +} From 98905937579dc8ef89d55d68701ded2caad2c606 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 07:21:25 -0800 Subject: [PATCH 656/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 16467f5..87bee24 100644 --- a/README.md +++ b/README.md @@ -403,6 +403,7 @@ Please give this repo a ⭐ if it inspires you. |[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| |[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| |[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| +|[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| ## Hard |LC #|Description| From 30a6237f8aca9ad5cd5809eaadb7b33441ac731b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 17:11:13 -0800 Subject: [PATCH 657/969] Create L2421.go --- Hard/L2421.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Hard/L2421.go diff --git a/Hard/L2421.go b/Hard/L2421.go new file mode 100644 index 0000000..0541db6 --- /dev/null +++ b/Hard/L2421.go @@ -0,0 +1,87 @@ +package Hard + +type UnionFind struct { + parent []int +} + +func newUnionFind(n int) *UnionFind { + par := make([]int, n) + for i := 0; i < n; i++ { + par[i] = -1 + } + return &UnionFind{ + parent: par, + } +} + +func (u *UnionFind) find(x int) int { + if u.parent[x] >= 0 { + u.parent[x] = u.find(u.parent[x]) + return u.parent[x] + } + + return x +} + +func (u *UnionFind) union(x, y int) bool { + parentX, parentY := u.find(x), u.find(y) + + if parentY == parentX { + return false + } + + if u.parent[parentX] <= u.parent[parentY] { + u.parent[parentX] += u.parent[parentY] + u.parent[parentY] = parentX + } else { + u.parent[parentY] += u.parent[parentX] + u.parent[parentX] = parentY + } + + return true +} + +func numberOfGoodPaths(vals []int, edges [][]int) int { + adjList, mp, n := make(map[int][]int), make(map[int][]int), len(vals) + + for i := range vals { + mp[vals[i]] = append(mp[vals[i]], i) + } + + for _, e := range edges { + u, v := e[0], e[1] + + adjList[u] = append(adjList[u], v) + adjList[v] = append(adjList[v], u) + } + + var keys []int + for k, _ := range mp { + keys = append(keys, k) + } + sort.Ints(keys) + uf, res := newUnionFind(n), n + + for _, k := range keys { + for _, curr := range mp[k] { + for _, adj := range adjList[curr] { + if vals[adj] <= vals[curr] { + uf.union(curr, adj) + } + } + } + + group := make(map[int]int) + for _, curr := range mp[k] { + group[uf.find(curr)]++ + } + + for _, v := range group { + if v >= 2 { + res += v * (v - 1) / 2 + } + } + } + + return res +} From 48670dd5ff9e4c55ac7914aabad945c0fb44c310 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 14 Jan 2023 17:11:51 -0800 Subject: [PATCH 658/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 87bee24..8be7776 100644 --- a/README.md +++ b/README.md @@ -470,3 +470,4 @@ Please give this repo a ⭐ if it inspires you. |[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| |[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| |[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| +|[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| From 6ec69b2eaf3eff154f324e1909ed4dbd3c62958e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 16 Jan 2023 12:05:06 -0800 Subject: [PATCH 659/969] Create L57.go --- Medium/L57.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Medium/L57.go diff --git a/Medium/L57.go b/Medium/L57.go new file mode 100644 index 0000000..0ec39e7 --- /dev/null +++ b/Medium/L57.go @@ -0,0 +1,38 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func insert(intervals [][]int, newInterval []int) [][]int { + var res [][]int + start, end := newInterval[0], newInterval[1] + + for _, itvl := range intervals { + currStart, currEnd := itvl[0], itvl[1] + if currEnd < start { + res = append(res, []int{currStart, currEnd}) + } else if currStart > end { + res = append(res, []int{start, end}) + start, end = currStart, currEnd + } else { + start = min(start, currStart) + end = max(end, currEnd) + } + } + res = append(res, []int{start, end}) + + return res +} From 972b179adb324a74fc6883808446f5ce33cbf20b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 16 Jan 2023 12:05:39 -0800 Subject: [PATCH 660/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8be7776..1267b09 100644 --- a/README.md +++ b/README.md @@ -404,6 +404,7 @@ Please give this repo a ⭐ if it inspires you. |[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| |[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| |[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| +|[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| ## Hard |LC #|Description| From d83212712380361b1005980a8d222427279ca107 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 19 Jan 2023 16:34:23 -0800 Subject: [PATCH 661/969] Create L974.go --- Medium/L974.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L974.go diff --git a/Medium/L974.go b/Medium/L974.go new file mode 100644 index 0000000..70c0a0b --- /dev/null +++ b/Medium/L974.go @@ -0,0 +1,18 @@ +package Medium + +func subarraysDivByK(nums []int, k int) int { + mp := make(map[int]int) + mp[0] = 1 + cnt, sum := 0, 0 + + for _, n := range nums { + sum = (sum + n) % k + if sum < 0 { + sum += k + } + cnt += mp[sum] + mp[sum]++ + } + + return cnt +} From 52def8e9b655dd4638270755bfe6ad254179cf0d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 19 Jan 2023 16:35:03 -0800 Subject: [PATCH 662/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1267b09..801b61f 100644 --- a/README.md +++ b/README.md @@ -405,6 +405,7 @@ Please give this repo a ⭐ if it inspires you. |[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| |[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| |[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| +|[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| ## Hard |LC #|Description| From 839d3c907d0c68fe52fde668a3e79311ac930f20 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 20 Jan 2023 07:52:48 -0800 Subject: [PATCH 663/969] Create L491.go --- Medium/L491.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L491.go diff --git a/Medium/L491.go b/Medium/L491.go new file mode 100644 index 0000000..ac03c35 --- /dev/null +++ b/Medium/L491.go @@ -0,0 +1,32 @@ +package Medium + +func findSubsequences(nums []int) [][]int { + var ( + tmp []int + res [][]int + ) + solve(0, nums, &tmp, &res) + return res +} + +func solve(idx int, nums []int, tmp *[]int, res *[][]int) { + if idx > len(nums)-1 { + if len(*tmp) > 1 { + tmpCopy := make([]int, len(*tmp)) + copy(tmpCopy, *tmp) + *res = append(*res, tmpCopy) + } + return + } + + if len(*tmp) == 0 || nums[idx] >= (*tmp)[len(*tmp)-1] { + *tmp = append(*tmp, nums[idx]) + solve(idx+1, nums, tmp, res) + *tmp = (*tmp)[:len(*tmp)-1] + } + + if idx > 0 && len(*tmp) > 0 && nums[idx] == (*tmp)[len(*tmp)-1] { + return + } + solve(idx+1, nums, tmp, res) +} From 563ae8006322dbf2bb623f29877988d55752053e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 20 Jan 2023 07:53:25 -0800 Subject: [PATCH 664/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 801b61f..08d54cc 100644 --- a/README.md +++ b/README.md @@ -406,6 +406,7 @@ Please give this repo a ⭐ if it inspires you. |[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| |[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| |[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| +|[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| ## Hard |LC #|Description| From 2e282be07195f5de27f6a7ad524906d8d8fa5fc1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 25 Jan 2023 07:19:45 -0800 Subject: [PATCH 665/969] Create L2359.go --- Medium/L2359.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Medium/L2359.go diff --git a/Medium/L2359.go b/Medium/L2359.go new file mode 100644 index 0000000..6749d8c --- /dev/null +++ b/Medium/L2359.go @@ -0,0 +1,50 @@ +package Medium + +var exists struct{} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func closestMeetingNode(edges []int, node1 int, node2 int) int { + n, ans, minDist := len(edges), -1, math.MaxInt32 + dist1, dist2 := make([]int, n), make([]int, n) + vis1, vis2 := make(map[int]struct{}), make(map[int]struct{}) + + solve(edges, node1, &dist1, vis1) + solve(edges, node2, &dist2, vis2) + + for i := 0; i < n; i++ { + if hasVisited(i, vis1) && hasVisited(i, vis2) { + if minDist > max(dist1[i], dist2[i]) { + minDist = max(dist1[i], dist2[i]) + ans = i + } + } + } + + return ans +} + +func hasVisited(node int, vis map[int]struct{}) bool { + if _, ok := vis[node]; ok { + return true + } + + return false +} + +func solve(edges []int, node int, dist *[]int, vis map[int]struct{}) { + vis[node] = exists + neigh := edges[node] + if neigh != -1 { + if !hasVisited(neigh, vis) { + (*dist)[neigh] = (*dist)[node] + 1 + solve(edges, neigh, dist, vis) + } + } +} From f7eb9be1629cadb4709a9ef66692bf98f798e7dc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 25 Jan 2023 07:20:24 -0800 Subject: [PATCH 666/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 08d54cc..ca19bdb 100644 --- a/README.md +++ b/README.md @@ -407,6 +407,7 @@ Please give this repo a ⭐ if it inspires you. |[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| |[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| |[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| +|[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| ## Hard |LC #|Description| From 8791c2d69a513acb62f29a3d652266cb325ee8d9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 25 Jan 2023 07:26:14 -0800 Subject: [PATCH 667/969] Create L909.go --- Medium/L909.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Medium/L909.go diff --git a/Medium/L909.go b/Medium/L909.go new file mode 100644 index 0000000..dc11c3b --- /dev/null +++ b/Medium/L909.go @@ -0,0 +1,52 @@ +package Medium + +func snakesAndLadders(board [][]int) int { + var q []int + n, move := len(board), 0 + visited := make([]bool, n*(n+1)) + q = append(q, 1) + + for ; len(q) > 0; move++ { + size := len(q) + for i := 0; i < size; i++ { + num := q[0] + q = q[1:] + + if visited[num] { + continue + } + visited[num] = true + if num == n*n { + return move + } + + for idx := 1; idx <= 6 && num+idx <= (n*n); idx++ { + next := num + idx + val := getBoardValue(next, board) + if val > 0 { + next = val + } + if !visited[next] { + q = append(q, next) + } + } + } + } + + return -1 +} + +func getBoardValue(num int, board [][]int) int { + n := len(board) + r := (num - 1) / n + + var x, y int + x = n - 1 - r + if r%2 == 0 { + y = num - 1 - (r * n) + } else { + y = n + (r * n) - num + } + + return board[x][y] +} From 822ce717ec440a7b56aaf38b6b2e75dc50eba45e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 25 Jan 2023 07:26:54 -0800 Subject: [PATCH 668/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ca19bdb..bcd00d3 100644 --- a/README.md +++ b/README.md @@ -408,6 +408,7 @@ Please give this repo a ⭐ if it inspires you. |[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| |[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| |[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| +|[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| ## Hard |LC #|Description| From 800dfdf51e0be3fca2808eaf5db742c17f73335a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 26 Jan 2023 06:43:43 -0800 Subject: [PATCH 669/969] Create L787.go --- Medium/L787.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Medium/L787.go diff --git a/Medium/L787.go b/Medium/L787.go new file mode 100644 index 0000000..6329b5c --- /dev/null +++ b/Medium/L787.go @@ -0,0 +1,36 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func findCheapestPrice(n int, flights [][]int, src int, dst int, k int) int { + cost := make([]int, n) + for i := 0; i < n; i++ { + cost[i] = math.MaxInt32 + } + cost[src] = 0 + + for i := 0; i <= k; i++ { + tmp := make([]int, len(cost)) + copy(tmp, cost) + for _, f := range flights { + curr, nxt, price := f[0], f[1], f[2] + if cost[curr] == math.MaxInt32 { + continue + } + tmp[nxt] = min(tmp[nxt], cost[curr]+price) + } + cost = tmp + } + + if cost[dst] == math.MaxInt32 { + return -1 + } + + return cost[dst] +} From b22948ab7528971e9814fbcf55c4b12339b4430a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 26 Jan 2023 06:44:24 -0800 Subject: [PATCH 670/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bcd00d3..4bf63af 100644 --- a/README.md +++ b/README.md @@ -409,6 +409,7 @@ Please give this repo a ⭐ if it inspires you. |[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| |[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| |[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| +|[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| ## Hard |LC #|Description| From bdcebc9026e572b58491d506ce59cf3d67e106c2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 27 Jan 2023 15:53:29 -0800 Subject: [PATCH 671/969] Create L472.go --- Hard/L472.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Hard/L472.go diff --git a/Hard/L472.go b/Hard/L472.go new file mode 100644 index 0000000..17c1330 --- /dev/null +++ b/Hard/L472.go @@ -0,0 +1,46 @@ +package Hard + +var exists struct{} + +func findAllConcatenatedWordsInADict(words []string) []string { + var res []string + preWords := make(map[string]struct{}) + + sort.Slice(words, func(i, j int) bool { + return len(words[i]) < len(words[j]) + }) + + for i := 0; i < len(words); i++ { + if canConcat(0, words[i], preWords, make(map[int]bool)) { + res = append(res, words[i]) + } + preWords[words[i]] = exists + } + + return res +} + +func canConcat(idx int, s string, st map[string]struct{}, mp map[int]bool) bool { + if idx == len(s) { + return true + } + if len(st) == 0 { + return false + } + + if v, ok := mp[idx]; ok { + return v + } + + for i := idx + 1; i <= len(s); i++ { + if _, ok := st[s[idx:i]]; ok { + if canConcat(i, s, st, mp) { + mp[idx] = true + return mp[idx] + } + } + } + + mp[idx] = false + return mp[idx] +} From edc045bc20074cc72637f5859d68a6ac9dc767f8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 27 Jan 2023 15:54:06 -0800 Subject: [PATCH 672/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4bf63af..e4f5e25 100644 --- a/README.md +++ b/README.md @@ -477,3 +477,4 @@ Please give this repo a ⭐ if it inspires you. |[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| |[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| |[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| +|[472](https://leetcode.com/problems/concatenated-words/description/)| Concatenated Words| From 5f8b4f2f9048613f162bdd89ea65fd78a8109b01 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Feb 2023 14:51:47 -0800 Subject: [PATCH 673/969] Create L1071.go --- Easy/L1071.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L1071.go diff --git a/Easy/L1071.go b/Easy/L1071.go new file mode 100644 index 0000000..511bf6a --- /dev/null +++ b/Easy/L1071.go @@ -0,0 +1,19 @@ +package Easy + +func gcdOfStrings(str1 string, str2 string) string { + s1, s2 := fmt.Sprintf("%s%s", str1, str2), fmt.Sprintf("%s%s", str2, str1) + if s1 != s2 { + return "" + } + + gcdVal := gcd(len(str1), len(str2)) + return str2[:gcdVal] +} + +func gcd(p, q int) int { + if q == 0 { + return p + } + + return gcd(q, p%q) +} From d7556d439d744fc7638b7b5f6dcdf0c50f37aed0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 1 Feb 2023 14:52:31 -0800 Subject: [PATCH 674/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e4f5e25..c1bc975 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ Please give this repo a ⭐ if it inspires you. |[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| |[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| |[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| +|[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| ## Medium |LC #|Description| From 5da41ee929cad9b422d4f03baf97a1c293dc3b45 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 Feb 2023 06:58:03 -0800 Subject: [PATCH 675/969] Create L6.go --- Medium/L6.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L6.go diff --git a/Medium/L6.go b/Medium/L6.go new file mode 100644 index 0000000..865c478 --- /dev/null +++ b/Medium/L6.go @@ -0,0 +1,24 @@ +package Medium + +func convert(s string, numRows int) string { + if numRows <= 1 { + return s + } + + res, idx, dir := make([]string, numRows), 0, -1 + + for i := 0; i < len(s); i++ { + res[idx] += fmt.Sprintf("%c", s[i]) + if idx == 0 || idx == numRows-1 { + dir = -dir + } + idx += dir + } + + var sb strings.Builder + for i := 0; i < len(res); i++ { + sb.WriteString(res[i]) + } + + return sb.String() +} From 2e171130ca82b062448bbb5bbe6f7d151292a801 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 Feb 2023 06:59:11 -0800 Subject: [PATCH 676/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c1bc975..176c9b1 100644 --- a/README.md +++ b/README.md @@ -411,6 +411,7 @@ Please give this repo a ⭐ if it inspires you. |[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| |[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| |[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| +|[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| ## Hard |LC #|Description| From 2c60ee8ad26cef054610bf0a3a2a41b979b64e9b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Feb 2023 15:59:03 -0800 Subject: [PATCH 677/969] Create L1470.go --- Easy/L1470.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L1470.go diff --git a/Easy/L1470.go b/Easy/L1470.go new file mode 100644 index 0000000..77d42dd --- /dev/null +++ b/Easy/L1470.go @@ -0,0 +1,13 @@ +package Easy + +func shuffle(nums []int, n int) []int { + res := make([]int, 2*n) + for i, j, idx := 0, n, 0; idx < len(res); i, j = i+1, j+1 { + res[idx] = nums[i] + idx++ + res[idx] = nums[j] + idx++ + } + + return res +} From 8ee04ff0c25c3d1d5f328132196f34db6ec47b76 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Feb 2023 15:59:38 -0800 Subject: [PATCH 678/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 176c9b1..fdb22ac 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Please give this repo a ⭐ if it inspires you. |[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| |[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| |[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| +|[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| ## Medium |LC #|Description| From 2153aa1a9c1980bc1361115daf6e7ca93789a3e0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Feb 2023 15:54:09 -0800 Subject: [PATCH 679/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fdb22ac..10b8077 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,7 @@ Please give this repo a ⭐ if it inspires you. |[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| |[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| |[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| +|[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| ## Hard |LC #|Description| From c49b282432b238eb0e9605e2046d5c630969d4a9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Feb 2023 15:54:37 -0800 Subject: [PATCH 680/969] Create L904.go --- Medium/L904.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L904.go diff --git a/Medium/L904.go b/Medium/L904.go new file mode 100644 index 0000000..51d7fc8 --- /dev/null +++ b/Medium/L904.go @@ -0,0 +1,32 @@ +package Medium + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func totalFruit(fruits []int) int { + mp := make(map[int]int) + + i, j, res := 0, 0, 0 + + for j < len(fruits) { + mp[fruits[j]]++ + + if len(mp) <= 2 { + res = max(res, j - i + 1) + } else { + mp[fruits[i]]-- + if mp[fruits[i]] == 0 { + delete(mp, fruits[i]) + } + i++ + } + j++ + } + + return res +} From 247bad723c678c942c4f843d84c0360ea727b345 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Feb 2023 12:43:35 -0800 Subject: [PATCH 681/969] Create L1162.go --- Medium/L1162.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Medium/L1162.go diff --git a/Medium/L1162.go b/Medium/L1162.go new file mode 100644 index 0000000..20688c5 --- /dev/null +++ b/Medium/L1162.go @@ -0,0 +1,61 @@ +package Medium + +var dirs = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + +func maxDistance(grid [][]int) int { + if len(grid) == 0 { + return 0 + } + + m, n := len(grid), len(grid[0]) + vis := make([][]bool, m) + for i := 0; i < m; i++ { + vis[i] = make([]bool, n) + } + var q [][]int + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == 1 { + q = append(q, []int{i, j}) + vis[i][j] = true + } + } + } + + if len(q) == 0 || len(q) == m*n { + return -1 + } + + lvl := -1 + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + c := q[0] + q = q[1:] + + for _, d := range dirs { + newX, newY := c[0]+d[0], c[1]+d[1] + + if isValid(grid, newX, newY) { + if !vis[newX][newY] { + q = append(q, []int{newX, newY}) + vis[newX][newY] = true + } + } + } + } + lvl++ + } + + if lvl <= 0 { + return -1 + } + + return lvl +} + +func isValid(grid [][]int, x, y int) bool { + m, n := len(grid), len(grid[0]) + + return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 +} From aad99a9accc123dd726830f8bc9b9500fd7aec03 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 10 Feb 2023 12:44:10 -0800 Subject: [PATCH 682/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 10b8077..7f2cf54 100644 --- a/README.md +++ b/README.md @@ -414,6 +414,7 @@ Please give this repo a ⭐ if it inspires you. |[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| |[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| |[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| +|[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| ## Hard |LC #|Description| From e63cf8c0fa3694ee821961e4eab6bc5d63bca596 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 06:36:33 -0800 Subject: [PATCH 683/969] Create L1523.go --- Easy/L1523.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L1523.go diff --git a/Easy/L1523.go b/Easy/L1523.go new file mode 100644 index 0000000..c7ad8ca --- /dev/null +++ b/Easy/L1523.go @@ -0,0 +1,15 @@ +package Easy + +func countOdds(low int, high int) int { + cnt := (high - low) / 2 + + if isOdd(low) || isOdd(high) { + cnt++ + } + + return cnt +} + +func isOdd(n int) bool { + return n%2 != 0 +} From 688f211ca5c94ec983e3c60222f5ee5b53930113 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 06:37:20 -0800 Subject: [PATCH 684/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7f2cf54..139359e 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ Please give this repo a ⭐ if it inspires you. |[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| |[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| |[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| +|[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| ## Medium |LC #|Description| From 5fbb81cab50a7148394ce442fcdd02f8acf9cd15 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 06:53:21 -0800 Subject: [PATCH 685/969] Create L2477.go --- Medium/L2477.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L2477.go diff --git a/Medium/L2477.go b/Medium/L2477.go new file mode 100644 index 0000000..3fdcc63 --- /dev/null +++ b/Medium/L2477.go @@ -0,0 +1,34 @@ +package Medium + +func minimumFuelCost(roads [][]int, seats int) int64 { + res, mp := int64(0), make(map[int][]int) + + for _, r := range roads { + mp[r[0]] = append(mp[r[0]], r[1]) + mp[r[1]] = append(mp[r[1]], r[0]) + } + + solve(0, -1, int64(seats), mp, &res) + return res +} + +func solve(node, parent int, seats int64, mp map[int][]int, res *int64) int64 { + rep := int64(1) + + if _, ok := mp[node]; !ok { + return rep + } + + for _, n := range mp[node] { + if n != parent { + rep += solve(n, node, seats, mp, res) + } + } + + if node != 0 { + // math.ceil for int64 + *res += (rep + seats - 1) / seats + } + + return rep +} From 669a748015e85ed51acb75196286958f0df113b3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 06:54:13 -0800 Subject: [PATCH 686/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 139359e..d37c50c 100644 --- a/README.md +++ b/README.md @@ -416,6 +416,7 @@ Please give this repo a ⭐ if it inspires you. |[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| |[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| |[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| +|[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| ## Hard |LC #|Description| From d2b60edd52d279b84df4d01db0bad2381c5bef6c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 07:14:25 -0800 Subject: [PATCH 687/969] Create L1129.go --- Medium/L1129.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Medium/L1129.go diff --git a/Medium/L1129.go b/Medium/L1129.go new file mode 100644 index 0000000..6d0d837 --- /dev/null +++ b/Medium/L1129.go @@ -0,0 +1,57 @@ +package Medium + +var exists struct{} + +//1=red, 2=blue, 0=root-edge (special case) +func shortestAlternatingPaths(n int, redEdges [][]int, blueEdges [][]int) []int { + reds, blues := make([][]int, n), make([][]int, n) + + for _, e := range redEdges { + reds[e[0]] = append(reds[e[0]], e[1]) + } + + for _, e := range blueEdges { + blues[e[0]] = append(blues[e[0]], e[1]) + } + + var q [][]int + res, moves, seen := make([]int, n), 0, make(map[string]struct{}) + for i := 0; i < n; i++ { + res[i] = -1 + } + q = append(q, []int{0, 0}) + + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + curr := q[0] + q = q[1:] + key := fmt.Sprintf("%d-%d", curr[0], curr[1]) + if _, ok := seen[key]; ok { + continue + } + seen[key] = exists + + if res[curr[0]] == -1 { + res[curr[0]] = moves + } + if curr[1] == 2 || curr[1] == 0 { + if len(reds[curr[0]]) > 0 { + for _, neigh := range reds[curr[0]] { + q = append(q, []int{neigh, 1}) + } + } + } + if curr[1] == 1 || curr[1] == 0 { + if len(blues[curr[0]]) > 0 { + for _, neigh := range blues[curr[0]] { + q = append(q, []int{neigh, 2}) + } + } + } + } + moves++ + } + + return res +} From 13a3599d8b0969ee88f9a10c642a77aa6a3b37e7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 Feb 2023 07:15:07 -0800 Subject: [PATCH 688/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d37c50c..f448271 100644 --- a/README.md +++ b/README.md @@ -417,6 +417,7 @@ Please give this repo a ⭐ if it inspires you. |[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| |[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| |[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| +|[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| ## Hard |LC #|Description| From fb9b624bf4911e51b7682755082164450110d932 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Feb 2023 06:29:48 -0800 Subject: [PATCH 689/969] Create L989.go --- Easy/L989.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Easy/L989.go diff --git a/Easy/L989.go b/Easy/L989.go new file mode 100644 index 0000000..6cd9113 --- /dev/null +++ b/Easy/L989.go @@ -0,0 +1,17 @@ +package Easy + +func addToArrayForm(num []int, k int) []int { + var res []int + + for i := len(num) - 1; i >= 0; i-- { + res = append([]int{(num[i] + k) % 10}, res...) + k = (num[i] + k) / 10 + } + + for k > 0 { + res = append([]int{k % 10}, res...) + k /= 10 + } + + return res +} From 1e597ecf7bda1c19e31eedca9e9c89d6ce5c0120 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Feb 2023 06:30:29 -0800 Subject: [PATCH 690/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f448271..bc75098 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ Please give this repo a ⭐ if it inspires you. |[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| |[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| |[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| +|[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| ## Medium |LC #|Description| From 4c13a39af1d2702546b0915697513885289896df Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 18 Feb 2023 08:04:23 -0800 Subject: [PATCH 691/969] Create L783.go --- Medium/L783.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L783.go diff --git a/Medium/L783.go b/Medium/L783.go new file mode 100644 index 0000000..1579724 --- /dev/null +++ b/Medium/L783.go @@ -0,0 +1,31 @@ +package Medium + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func minDiffInBST(root *TreeNode) int { + var prev *TreeNode + minimum := math.MaxInt32 + + solve := func(root *TreeNode, prev **TreeNode) {} + solve = func(root *TreeNode, prev **TreeNode) { + if root == nil { + return + } + + solve(root.Left, prev) + if prev != nil && *prev != nil { + minimum = min(minimum, root.Val-(*prev).Val) + } + *prev = root + solve(root.Right, prev) + } + + solve(root, &prev) + return minimum +} From edd236e35ea2f238e9b9b16b32f914e7214a2557 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 18 Feb 2023 08:05:01 -0800 Subject: [PATCH 692/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bc75098..246926e 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Please give this repo a ⭐ if it inspires you. |[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| |[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| |[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| +|[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| ## Medium |LC #|Description| From 110aa6ce321fd514a19a1f6cb7e7be18911ad34a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 18 Feb 2023 08:05:47 -0800 Subject: [PATCH 693/969] Update and rename Medium/L783.go to Easy/L783.go --- {Medium => Easy}/L783.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {Medium => Easy}/L783.go (96%) diff --git a/Medium/L783.go b/Easy/L783.go similarity index 96% rename from Medium/L783.go rename to Easy/L783.go index 1579724..a48fe7a 100644 --- a/Medium/L783.go +++ b/Easy/L783.go @@ -1,4 +1,4 @@ -package Medium +package Easy func min(a, b int) int { if a < b { From 5b181faa245403d7fa22d7af9bee783264684c8e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Feb 2023 16:18:10 -0800 Subject: [PATCH 694/969] Create L103.go --- Medium/L103.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L103.go diff --git a/Medium/L103.go b/Medium/L103.go new file mode 100644 index 0000000..5df5925 --- /dev/null +++ b/Medium/L103.go @@ -0,0 +1,46 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func zigzagLevelOrder(root *TreeNode) [][]int { + var res [][]int + if root == nil { + return res + } + + var q []*TreeNode + q = append(q, root) + normalOrder := false + + for len(q) > 0 { + size := len(q) + var lvlVals []int + normalOrder = !normalOrder + for i := 0; i < size; i++ { + cur := q[0] + q = q[1:] + + if normalOrder { + lvlVals = append(lvlVals, cur.Val) + } else { + lvlVals = append([]int{cur.Val}, lvlVals...) + } + + if cur.Left != nil { + q = append(q, cur.Left) + } + if cur.Right != nil { + q = append(q, cur.Right) + } + } + res = append(res, lvlVals) + } + + return res +} From 2ff728bd9e1f9089fd6227a1610ca1a11f8376aa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 20 Feb 2023 16:18:48 -0800 Subject: [PATCH 695/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 246926e..b4f003d 100644 --- a/README.md +++ b/README.md @@ -420,6 +420,7 @@ Please give this repo a ⭐ if it inspires you. |[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| |[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| |[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| +|[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| ## Hard |LC #|Description| From 0e0fcc0585619ce2c35aaf2a5c05ddbb8b545873 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:39:07 -0800 Subject: [PATCH 696/969] Update L509.go --- Easy/L509.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Easy/L509.go b/Easy/L509.go index dc20ce7..b4ab958 100644 --- a/Easy/L509.go +++ b/Easy/L509.go @@ -17,3 +17,25 @@ func fibo(mp map[int]int, n int) int { return mp[n] } + +// With anonymous function +func fib(n int) int { + mp := map[int]int{ + 0: 0, + 1: 1, + 2: 1, + } + + var solve func(n int) int + solve = func(n int) int { + if v, ok := mp[n]; ok { + return v + } + + mp[n] = solve(n-1) + solve(n-2) + + return mp[n] + } + + return solve(n) +} From 6013224b6e331d17d7b02a46d735c919c0966e50 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:48:24 -0800 Subject: [PATCH 697/969] Create L1137.go --- Easy/L1137.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Easy/L1137.go diff --git a/Easy/L1137.go b/Easy/L1137.go new file mode 100644 index 0000000..62455c2 --- /dev/null +++ b/Easy/L1137.go @@ -0,0 +1,25 @@ +package Easy + +func tribonacci(n int) int { + mp := map[int]int{ + 0: 0, + 1: 1, + 2: 1, + } + + var solve func(int) int + solve = func(n int) int { + if n < 0 { + return 0 + } + + if v, ok := mp[n]; ok { + return v + } + + mp[n] = solve(n - 1) + solve(n - 2) + solve(n - 3) + return mp[n] + } + + return solve(n) +} From 4e9b4b1904bb6ef0b2b1768615513ea5735a5db8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:49:02 -0800 Subject: [PATCH 698/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b4f003d..10f82c3 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ Please give this repo a ⭐ if it inspires you. |[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| |[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| |[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| +|[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| ## Medium |LC #|Description| From 7d27a64307676ec9ce1d2ed2aa67bfa32a90f487 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:52:35 -0800 Subject: [PATCH 699/969] Update L70.go --- Easy/L70.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Easy/L70.go b/Easy/L70.go index a487033..b3759d7 100644 --- a/Easy/L70.go +++ b/Easy/L70.go @@ -16,3 +16,28 @@ func dp(mp map[int]int, n int) int { return mp[n] } + +// With anonymous function +func climbStairs(n int) int { + mp := map[int]int{ + 0: 0, + 1: 1, + 2: 2, + } + + var solve func(int) int + solve = func(n int) int { + if n < 0 { + return 0 + } + + if v, ok := mp[n]; ok { + return v + } + + mp[n] = solve(n-1) + solve(n-2) + return mp[n] + } + + return solve(n) +} From 5990fb14f0c5e6d8e39106b7890b7c9456fc2503 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:57:49 -0800 Subject: [PATCH 700/969] Create L746.go --- Easy/L746.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Easy/L746.go diff --git a/Easy/L746.go b/Easy/L746.go new file mode 100644 index 0000000..7ee5a88 --- /dev/null +++ b/Easy/L746.go @@ -0,0 +1,31 @@ +package Easy + +func minCostClimbingStairs(cost []int) int { + mp := make(map[int]int) + + var solve func(int) int + solve = func(n int) int { + if n >= len(cost) { + return 0 + } + if v, ok := mp[n]; ok { + return v + } + + oneStep := cost[n] + solve(n + 1) + twoStep := cost[n] + solve(n + 2) + + mp[n] = min(oneStep, twoStep) + return mp[n] + } + + return min(solve(0), solve(1)) +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From eca01189081347bd30c1ced129d51cce79614ece Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Feb 2023 06:58:25 -0800 Subject: [PATCH 701/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 10f82c3..5988971 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ Please give this repo a ⭐ if it inspires you. |[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| |[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| |[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| +|[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| ## Medium |LC #|Description| From 064cc7d04e27d91920e7d4e5c680b235d497bfc9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 06:59:05 -0800 Subject: [PATCH 702/969] Create L1011.go --- Medium/L1011.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Medium/L1011.go diff --git a/Medium/L1011.go b/Medium/L1011.go new file mode 100644 index 0000000..054e35d --- /dev/null +++ b/Medium/L1011.go @@ -0,0 +1,54 @@ +package Medium + +func shipWithinDays(weights []int, days int) int { + if len(weights) == 0 { + return 0 + } + + maxi, sum := math.MinInt32, 0 + for _, w := range weights { + maxi = max(maxi, w) + sum += w + } + + if days == 1 { + return sum + } + + isFeasible := func(maxWeight int) bool { + cnt, maximum, day := 0, maxWeight, 0 + for _, w := range weights { + if w <= maximum { + cnt++ + } else { + cnt = 0 + maximum = maxWeight + day++ + } + maximum -= w + } + + return day+1 <= days + } + + low, high := maxi, sum + for low < high { + mid := low + (high-low)/2 + + if isFeasible(mid) { + high = mid + } else { + low = mid + 1 + } + } + + return low +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From c1cda82e775ac08f95dbf8b77d722c5379b81548 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 06:59:42 -0800 Subject: [PATCH 703/969] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5988971..b52b4eb 100644 --- a/README.md +++ b/README.md @@ -423,6 +423,7 @@ Please give this repo a ⭐ if it inspires you. |[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| |[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| |[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| +|[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| ## Hard |LC #|Description| From 5fb656fad56f28b784c9ab81e8b4585644f47705 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 07:02:38 -0800 Subject: [PATCH 704/969] Split up easy into its own README --- Easy.md | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Easy.md diff --git a/Easy.md b/Easy.md new file mode 100644 index 0000000..57ad938 --- /dev/null +++ b/Easy.md @@ -0,0 +1,138 @@ +## Easy +|LC #|Description| +|:-:|:-| +|[1480](https://leetcode.com/problems/running-sum-of-1d-array/)| Running Sum of 1d Array| +|[1431](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)| Kids With the Greatest Number of Candies| +|[1](https://leetcode.com/problems/two-sum/)| Two Sum| +|[575](https://leetcode.com/problems/distribute-candies/)| Distribute Candies| +|[1365](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)| How Many Numbers Are Smaller Than the Current Number| +|[1773](https://leetcode.com/problems/count-items-matching-a-rule/)| Count Items Matching a Rule| +|[645](https://leetcode.com/problems/set-mismatch/)| Set Mismatch| +|[268](https://leetcode.com/problems/missing-number/)| Missing Number| +|[160](https://leetcode.com/problems/intersection-of-two-linked-lists/)| Intersection of Two Linked Lists| +|[637](https://leetcode.com/problems/average-of-levels-in-binary-tree/)| Average of Levels in Binary Tree| +|[559](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)| Maximum Depth of N-ary Tree| +|[690](https://leetcode.com/problems/employee-importance/)| Employee Importance| +|[993](https://leetcode.com/problems/cousins-in-binary-tree/)| Cousins in Binary Tree| +|[1748](https://leetcode.com/problems/sum-of-unique-elements/)| Sum of Unique Elements| +|[232](https://leetcode.com/problems/implement-queue-using-stacks/)| Implement Queue using Stacks| +|[1332](https://leetcode.com/problems/remove-palindromic-subsequences/)| Remove Palindromic Subsequences| +|[1678](https://leetcode.com/problems/goal-parser-interpretation/)| Goal Parser Interpretation| +|[1022](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)| Sum of Root To Leaf Binary Numbers| +|[404](https://leetcode.com/problems/sum-of-left-leaves/)| Sum of Left Leaves| +|[112](https://leetcode.com/problems/path-sum/)| Path Sum| +|[535](https://leetcode.com/problems/encode-and-decode-tinyurl/)| Encode and Decode TinyURL| +|[1560](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/)| Most Visited Sector in a Circular Track| +|[598](https://leetcode.com/problems/range-addition-ii/)| Range Addition II| +|[762](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)| Prime Number of Set Bits in Binary Representation| +|[500](https://leetcode.com/problems/keyboard-row/)| Keyboard Row| +|[1656](https://leetcode.com/problems/design-an-ordered-stream/)| Design an Ordered Stream| +|[234](https://leetcode.com/problems/palindrome-linked-list/)| Palindrome Linked List| +|[1614](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/)| Maximum Nesting Depth of the Parentheses| +|[1668](https://leetcode.com/problems/maximum-repeating-substring/)| Maximum Repeating Substring| +|[1704](https://leetcode.com/problems/determine-if-string-halves-are-alike/)| Determine if String Halves Are Alike| +|[953](https://leetcode.com/problems/verifying-an-alien-dictionary/)| Verifying an Alien Dictionary| +|[509](https://leetcode.com/problems/fibonacci-number/)| Fibonacci Number| +|[589](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)| N-ary Tree Preorder Traversal| +|[696](https://leetcode.com/problems/count-binary-substrings/)| Count Binary Substrings| +|[326](https://leetcode.com/problems/power-of-three/)| Power of Three| +|[703](https://leetcode.com/problems/kth-largest-element-in-a-stream/)| Kth Largest Element in a Stream| +|[204](https://leetcode.com/problems/count-primes/)| Count Primes| +|[453](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| Minimum Moves to Equal Array Elements| +|[709](https://leetcode.com/problems/to-lower-case/)| To Lower Case| +|[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| +|[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| +|[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| +|[415](https://leetcode.com/problems/add-strings/)| Add Strings| +|[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| +|[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| +|[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| +|[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| +|[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| +|[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| +|[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| +|[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| +|[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| +|[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| +|[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| +|[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| +|[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| +|[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| +|[155](https://leetcode.com/problems/min-stack/)| Min Stack| +|[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| +|[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| +|[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| +|[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| +|[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| +|[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| +|[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| +|[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| +|[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| +|[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| +|[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| +|[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| +|[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| +|[231](https://leetcode.com/problems/power-of-two/)| Power of Two| +|[476](https://leetcode.com/problems/number-complement/)| Number Complement| +|[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| +|[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| +|[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| +|[67](https://leetcode.com/problems/add-binary/)| Add Binary| +|[290](https://leetcode.com/problems/word-pattern/)| Word Pattern| +|[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| +|[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| +|[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| +|[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| +|[258](https://leetcode.com/problems/add-digits/)| Add Digits| +|[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| +|[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| +|[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| +|[136](https://leetcode.com/problems/single-number/)| Single Number| +|[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| +|[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| +|[338](https://leetcode.com/problems/counting-bits/)| Counting Bits| +|[392](https://leetcode.com/problems/is-subsequence/)| Is Subsequence| +|[21](https://leetcode.com/problems/merge-two-sorted-lists/)| Merge Two Sorted Lists| +|[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| +|[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| +|[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| +|[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| +|[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| +|[344](https://leetcode.com/problems/reverse-string/)| Reverse String| +|[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| +|[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| +|[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| +|[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| +|[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| +|[704](https://leetcode.com/problems/binary-search/)| Binary Search| +|[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| +|[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| +|[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| +|[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| +|[169](https://leetcode.com/problems/majority-element/)| Majority Element| +|[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| +|[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| +|[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| +|[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| +|[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| +|[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| +|[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| +|[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| +|[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| +|[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| +|[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| +|[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| +|[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| +|[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| +|[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| +|[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| +|[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| +|[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| +|[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| +|[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| +|[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| +|[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| +|[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| +|[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| +|[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| +|[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| From ef7972126626870a2efe034a73b9218b2aa3cff9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 07:03:10 -0800 Subject: [PATCH 705/969] Split up medium into its own README --- Medium.md | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 Medium.md diff --git a/Medium.md b/Medium.md new file mode 100644 index 0000000..5695f7b --- /dev/null +++ b/Medium.md @@ -0,0 +1,282 @@ +## Medium +|LC #|Description| +|:-:|:-| +|[931](https://leetcode.com/problems/minimum-falling-path-sum/)| Minimum Falling Path Sum| +|[946](https://leetcode.com/problems/validate-stack-sequences/)| Validate Stack Sequences| +|[29](https://leetcode.com/problems/divide-two-integers/)| Divide Two Integers| +|[820](https://leetcode.com/problems/short-encoding-of-words/)| Short Encoding of Words| +|[1381](https://leetcode.com/problems/design-a-stack-with-increment-operation/)| Design a Stack With Increment Operation| +|[623](https://leetcode.com/problems/add-one-row-to-tree/)| Add One Row to Tree| +|[12](https://leetcode.com/problems/integer-to-roman/)| Integer to Roman| +|[1315](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)| Sum of Nodes with Even-Valued Grandparent| +|[1302](https://leetcode.com/problems/deepest-leaves-sum/)| Deepest Leaves Sum| +|[322](https://leetcode.com/problems/coin-change/)| Coin Change| +|[1305](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)| All Elements in Two Binary Search Trees| +|[1461](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/)| Check If a String Contains All Binary Codes of Size K| +|[1721](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)| Swapping Nodes in a Linked List| +|[714](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)| Best Time to Buy and Sell Stock with Transaction Fee| +|[478](https://leetcode.com/problems/generate-random-point-in-a-circle/)| Generate Random Point in a Circle| +|[208](https://leetcode.com/problems/implement-trie-prefix-tree/)| Implement Trie (Prefix Tree)| +|[376](https://leetcode.com/problems/wiggle-subsequence/)| Wiggle Subsequence| +|[38](https://leetcode.com/problems/count-and-say/)| Count and Say| +|[146](https://leetcode.com/problems/lru-cache/)| LRU Cache| +|[1583](https://leetcode.com/problems/count-unhappy-friends/)| Count Unhappy Friends| +|[729](https://leetcode.com/problems/my-calendar-i/)| My Calendar I| +|[841](https://leetcode.com/problems/keys-and-rooms/)| Keys And Rooms| +|[113](https://leetcode.com/problems/path-sum-ii/)| Path Sum II| +|[1398](https://leetcode.com/problems/design-underground-system/)| Design Underground System| +|[1797](https://leetcode.com/problems/design-authentication-manager/)| Design Authentication Manager| +|[1798](https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/)| Maximum Number of Consecutive Values You Can Make| +|[966](https://leetcode.com/problems/vowel-spellchecker/)| Vowel Spellchecker| +|[1689](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)| Partitioning Into Minimum Number Of Deci-Binary Numbers| +|[1764](https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/)| Form Array by Concatenating Subarrays of Another Array| +|[923](https://leetcode.com/problems/3sum-with-multiplicity/)| 3Sum With Multiplicity| +|[417](https://leetcode.com/problems/pacific-atlantic-water-flow/)| Pacific Atlantic Water Flow| +|[916](https://leetcode.com/problems/word-subsets/)| Word Subsets| +|[5](https://leetcode.com/problems/longest-palindromic-substring/)| Longest Palindromic Substring| +|[647](https://leetcode.com/problems/palindromic-substrings/)| Palindromic Substrings| +|[971](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/)| Flip Binary Tree To Match Preorder Traversal| +|[1726](https://leetcode.com/problems/tuple-with-same-product/)| Tuple with Same Product| +|[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| +|[1488](https://leetcode.com/problems/avoid-flood-in-the-city/)| Avoid Flood in The City| +|[947](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/)| Most Stones Removed with Same Row or Column| +|[622](https://leetcode.com/problems/design-circular-queue/)| Design Circular Queue| +|[1670](https://leetcode.com/problems/design-front-middle-back-queue/)| Design Front Middle Back Queue| +|[494](https://leetcode.com/problems/target-sum/)| Target Sum| +|[17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| Letter Combinations of a Phone Number| +|[638](https://leetcode.com/problems/shopping-offers/)| Shopping Offers| +|[667](https://leetcode.com/problems/beautiful-arrangement-ii/)| Beautiful Arrangement II| +|[650](https://leetcode.com/problems/2-keys-keyboard/)| 2 Keys Keyboard| +|[801](https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/)| Minimum Swaps To Make Sequences Increasing| +|[341](https://leetcode.com/problems/flatten-nested-list-iterator/)| Flatten Nested List Iterator| +|[86](https://leetcode.com/problems/partition-list/)| Partition List| +|[91](https://leetcode.com/problems/decode-ways/)| Decode Ways| +|[62](https://leetcode.com/problems/unique-paths/)| Unique Paths| +|[1209](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)| Remove All Adjacent Duplicates in String II| +|[279](https://leetcode.com/problems/perfect-squares/)| Perfect Squares| +|[377](https://leetcode.com/problems/combination-sum-iv/)| Combination Sum IV| +|[120](https://leetcode.com/problems/triangle/)| Triangle| +|[554](https://leetcode.com/problems/brick-wall/)| Brick Wall| +|[48](https://leetcode.com/problems/rotate-image/)| Rotate Image| +|[935](https://leetcode.com/problems/knight-dialer/)| Knight Dialer| +|[740](https://leetcode.com/problems/delete-and-earn/)| Delete and Earn| +|[63](https://leetcode.com/problems/unique-paths-ii/)| Unique Paths II| +|[34](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| Find First and Last Position of Element in Sorted Array| +|[970](https://leetcode.com/problems/powerful-integers/)| Powerful Integers| +|[665](https://leetcode.com/problems/non-decreasing-array/)| Non-decreasing Array| +|[1472](https://leetcode.com/problems/design-browser-history/)| Design Browser History| +|[109](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| Convert Sorted List to Binary Search Tree| +|[583](https://leetcode.com/problems/delete-operation-for-two-strings/)| Delete Operation for Two Strings| +|[1423](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)| Maximum Points You Can Obtain from Cards| +|[304](https://leetcode.com/problems/range-sum-query-2d-immutable/)| Range Sum Query 2D - Immutable| +|[816](https://leetcode.com/problems/ambiguous-coordinates/)| Ambiguous Coordinates| +|[114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| Flatten Binary Tree to Linked List| +|[1048](https://leetcode.com/problems/longest-string-chain/)| Longest String Chain| +|[213](https://leetcode.com/problems/house-robber-ii/)| House Robber II| +|[462](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)| Minimum Moves to Equal Array Elements II| +|[890](https://leetcode.com/problems/find-and-replace-pattern/)| Find and Replace Pattern| +|[337](https://leetcode.com/problems/house-robber-iii/)| House Robber III| +|[921](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/)| Minimum Add to Make Parentheses Valid| +|[1871](https://leetcode.com/problems/jump-game-vii/)| Jump Game VII| +|[150](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| Evaluate Reverse Polish Notation| +|[318](https://leetcode.com/problems/maximum-product-of-word-lengths/)| Maximum Product of Word Lengths| +|[1695](https://leetcode.com/problems/maximum-erasure-value/)| Maximum Erasure Value| +|[1268](https://leetcode.com/problems/search-suggestions-system/)| Search Suggestions System| +|[695](https://leetcode.com/problems/max-area-of-island/)| Max Area of Island| +|[97](https://leetcode.com/problems/interleaving-string/)| Interleaving String| +|[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| +|[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| +|[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| +|[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| +|[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| +|[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| +|[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| +|[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| +|[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| +|[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| +|[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| +|[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| +|[89](https://leetcode.com/problems/gray-code/)| Gray Code| +|[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| +|[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| +|[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| +|[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| +|[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| +|[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| +|[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| +|[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| +|[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| +|[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| +|[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| +|[877](https://leetcode.com/problems/stone-game/)| Stone Game| +|[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| +|[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| +|[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| +|[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| +|[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| +|[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| +|[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| +|[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| +|[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| +|[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| +|[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| +|[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| +|[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| +|[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| +|[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| +|[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| +|[55](https://leetcode.com/problems/jump-game/)| Jump Game| +|[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| +|[79](https://leetcode.com/problems/word-search/)| Word Search| +|[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| +|[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| +|[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| +|[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| +|[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| +|[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| +|[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| +|[15](https://leetcode.com/problems/3sum/)| 3Sum| +|[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| +|[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| +|[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| +|[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| +|[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| +|[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| +|[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| +|[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| +|[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| +|[368](https://leetcode.com/problems/largest-divisible-subset/)| Largest Divisible Subset| +|[540](https://leetcode.com/problems/single-element-in-a-sorted-array/)| Single Element in a Sorted Array| +|[106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| Construct Binary Tree from Inorder and Postorder Traversal| +|[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| +|[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| +|[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| +|[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| +|[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| +|[198](https://leetcode.com/problems/house-robber/)| House Robber| +|[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| +|[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| +|[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| +|[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| +|[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| +|[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| +|[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| +|[394](https://leetcode.com/problems/decode-string/)| Decode String| +|[143](https://leetcode.com/problems/reorder-list/)| Reorder List| +|[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| +|[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| +|[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| +|[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| +|[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| +|[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| +|[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| +|[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| +|[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| +|[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| +|[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| +|[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| +|[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| +|[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| +|[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| +|[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| +|[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| +|[134](https://leetcode.com/problems/gas-station/)| Gas Station| +|[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| +|[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| +|[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| +|[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| +|[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| +|[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| +|[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| +|[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| +|[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| +|[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| +|[78](https://leetcode.com/problems/subsets/)| Subsets| +|[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| +|[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| +|[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| +|[148](https://leetcode.com/problems/sort-list/)| Sort List| +|[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| +|[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| +|[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| +|[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| +|[61](https://leetcode.com/problems/rotate-list/)| Rotate List| +|[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| +|[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| +|[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| +|[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| +|[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| +|[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| +|[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| +|[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| +|[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| +|[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| +|[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| +|[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| +|[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| +|[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| +|[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| +|[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| +|[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| +|[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| +|[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| +|[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| +|[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| +|[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| +|[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| +|[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| +|[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| +|[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| +|[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| +|[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| +|[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| +|[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| +|[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| +|[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| +|[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| +|[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| +|[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| +|[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| +|[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| +|[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| +|[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| +|[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| +|[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| +|[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| +|[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| +|[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| +|[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| +|[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| +|[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| +|[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| +|[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| +|[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| +|[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| +|[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| +|[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| +|[343](https://leetcode.com/problems/integer-break/)| Integer Break| +|[256](https://leetcode.com/problems/paint-house/)| Paint House| +|[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| +|[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| +|[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| +|[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| +|[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| +|[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| +|[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| +|[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| +|[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| +|[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| +|[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| +|[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| +|[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| +|[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| +|[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| +|[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| +|[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| +|[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| +|[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| +|[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| +|[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| +|[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| +|[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| +|[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| From b4dce1757d877a5aa53e5099b98d7d494f2f2887 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 07:03:31 -0800 Subject: [PATCH 706/969] Create Hard.md --- Hard.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Hard.md diff --git a/Hard.md b/Hard.md new file mode 100644 index 0000000..7ff986a --- /dev/null +++ b/Hard.md @@ -0,0 +1,67 @@ +## Hard +|LC #|Description| +|:-:|:-| +|[354](https://leetcode.com/problems/russian-doll-envelopes/)| Russian Doll Envelopes| +|[936](https://leetcode.com/problems/stamping-the-sequence/)| Stamping The Sequence| +|[32](https://leetcode.com/problems/longest-valid-parentheses/)| Longest Valid Parentheses| +|[1559](https://leetcode.com/problems/detect-cycles-in-2d-grid/)| Detect Cycles in 2D Grid| +|[329](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)| Longest Increasing Path in a Matrix| +|[1192](https://leetcode.com/problems/critical-connections-in-a-network/)| Critical Connections in a Network| +|[980](https://leetcode.com/problems/unique-paths-iii/)| Unique Paths III| +|[745](https://leetcode.com/problems/prefix-and-suffix-search/)| Prefix and Suffix Search| +|[630](https://leetcode.com/problems/course-schedule-iii/)| Course Schedule III| +|[65](https://leetcode.com/problems/valid-number/)| Valid Number| +|[968](https://leetcode.com/problems/binary-tree-cameras/)| Binary Tree Cameras| +|[1473](https://leetcode.com/problems/paint-house-iii/)| Paint House III| +|[51](https://leetcode.com/problems/n-queens/)| N-Queens| +|[52](https://leetcode.com/problems/n-queens-ii/)| N-Queens II| +|[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| +|[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| +|[135](https://leetcode.com/problems/candy/)| Candy| +|[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| +|[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| +|[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| +|[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| +|[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| +|[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| +|[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| +|[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| +|[330](https://leetcode.com/problems/patching-array//)| Patching Array| +|[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| +|[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| +|[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| +|[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| +|[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| +|[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| +|[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| +|[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| +|[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| +|[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| +|[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| +|[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| +|[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| +|[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| +|[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| +|[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| +|[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| +|[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| +|[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| +|[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| +|[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| +|[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| +|[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| +|[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| +|[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| +|[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| +|[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| +|[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| +|[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| +|[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| +|[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| +|[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| +|[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| +|[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| +|[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| +|[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| +|[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| +|[472](https://leetcode.com/problems/concatenated-words/description/)| Concatenated Words| From 43150a69ed877da953accb0a2418673f19c78a4f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Feb 2023 07:06:14 -0800 Subject: [PATCH 707/969] Update README.md --- README.md | 490 +----------------------------------------------------- 1 file changed, 3 insertions(+), 487 deletions(-) diff --git a/README.md b/README.md index b52b4eb..5c4bcf9 100644 --- a/README.md +++ b/README.md @@ -3,492 +3,8 @@ Leetcode Solutions in Go Please give this repo a ⭐ if it inspires you. -## Easy -|LC #|Description| -|:-:|:-| -|[1480](https://leetcode.com/problems/running-sum-of-1d-array/)| Running Sum of 1d Array| -|[1431](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)| Kids With the Greatest Number of Candies| -|[1](https://leetcode.com/problems/two-sum/)| Two Sum| -|[575](https://leetcode.com/problems/distribute-candies/)| Distribute Candies| -|[1365](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)| How Many Numbers Are Smaller Than the Current Number| -|[1773](https://leetcode.com/problems/count-items-matching-a-rule/)| Count Items Matching a Rule| -|[645](https://leetcode.com/problems/set-mismatch/)| Set Mismatch| -|[268](https://leetcode.com/problems/missing-number/)| Missing Number| -|[160](https://leetcode.com/problems/intersection-of-two-linked-lists/)| Intersection of Two Linked Lists| -|[637](https://leetcode.com/problems/average-of-levels-in-binary-tree/)| Average of Levels in Binary Tree| -|[559](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)| Maximum Depth of N-ary Tree| -|[690](https://leetcode.com/problems/employee-importance/)| Employee Importance| -|[993](https://leetcode.com/problems/cousins-in-binary-tree/)| Cousins in Binary Tree| -|[1748](https://leetcode.com/problems/sum-of-unique-elements/)| Sum of Unique Elements| -|[232](https://leetcode.com/problems/implement-queue-using-stacks/)| Implement Queue using Stacks| -|[1332](https://leetcode.com/problems/remove-palindromic-subsequences/)| Remove Palindromic Subsequences| -|[1678](https://leetcode.com/problems/goal-parser-interpretation/)| Goal Parser Interpretation| -|[1022](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)| Sum of Root To Leaf Binary Numbers| -|[404](https://leetcode.com/problems/sum-of-left-leaves/)| Sum of Left Leaves| -|[112](https://leetcode.com/problems/path-sum/)| Path Sum| -|[535](https://leetcode.com/problems/encode-and-decode-tinyurl/)| Encode and Decode TinyURL| -|[1560](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/)| Most Visited Sector in a Circular Track| -|[598](https://leetcode.com/problems/range-addition-ii/)| Range Addition II| -|[762](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)| Prime Number of Set Bits in Binary Representation| -|[500](https://leetcode.com/problems/keyboard-row/)| Keyboard Row| -|[1656](https://leetcode.com/problems/design-an-ordered-stream/)| Design an Ordered Stream| -|[234](https://leetcode.com/problems/palindrome-linked-list/)| Palindrome Linked List| -|[1614](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/)| Maximum Nesting Depth of the Parentheses| -|[1668](https://leetcode.com/problems/maximum-repeating-substring/)| Maximum Repeating Substring| -|[1704](https://leetcode.com/problems/determine-if-string-halves-are-alike/)| Determine if String Halves Are Alike| -|[953](https://leetcode.com/problems/verifying-an-alien-dictionary/)| Verifying an Alien Dictionary| -|[509](https://leetcode.com/problems/fibonacci-number/)| Fibonacci Number| -|[589](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)| N-ary Tree Preorder Traversal| -|[696](https://leetcode.com/problems/count-binary-substrings/)| Count Binary Substrings| -|[326](https://leetcode.com/problems/power-of-three/)| Power of Three| -|[703](https://leetcode.com/problems/kth-largest-element-in-a-stream/)| Kth Largest Element in a Stream| -|[204](https://leetcode.com/problems/count-primes/)| Count Primes| -|[453](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| Minimum Moves to Equal Array Elements| -|[709](https://leetcode.com/problems/to-lower-case/)| To Lower Case| -|[1710](https://leetcode.com/problems/maximum-units-on-a-truck/)| Maximum Units on a Truck| -|[1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)| Remove All Adjacent Duplicates In String| -|[108](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| Convert Sorted Array to Binary Search Tree| -|[415](https://leetcode.com/problems/add-strings/)| Add Strings| -|[653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| Two Sum IV - Input is a BST| -|[1629](https://leetcode.com/problems/slowest-key/)| Slowest Key| -|[206](https://leetcode.com/problems/reverse-linked-list/)| Reverse Linked List| -|[1189](https://leetcode.com/problems/maximum-number-of-balloons/)| Maximum Number of Balloons| -|[350](https://leetcode.com/problems/intersection-of-two-arrays-ii/)| Intersection of Two Arrays II| -|[1275](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)| Find Winner on a Tic Tac Toe Game| -|[485](https://leetcode.com/problems/max-consecutive-ones/)| Max Consecutive Ones| -|[929](https://leetcode.com/problems/unique-email-addresses/)| Unique Email Addresses| -|[922](https://leetcode.com/problems/sort-array-by-parity-ii/)| Sort Array By Parity II| -|[463](https://leetcode.com/problems/island-perimeter/)| Island Perimeter| -|[70](https://leetcode.com/problems/climbing-stairs/)| Climbing Stairs| -|[543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter of Binary Tree| -|[374](https://leetcode.com/problems/guess-number-higher-or-lower/)| Guess Number Higher or Lower| -|[496](https://leetcode.com/problems/next-greater-element-i/)| Next Greater Element I| -|[155](https://leetcode.com/problems/min-stack/)| Min Stack| -|[226](https://leetcode.com/problems/invert-binary-tree/)| Invert Binary Tree| -|[441](https://leetcode.com/problems/arranging-coins/)| Arranging Coins| -|[1413](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)| Minimum Value to Get Positive Step by Step Sum| -|[203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements| -|[448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Find All Numbers Disappeared in an Array| -|[461](https://leetcode.com/problems/hamming-distance/)| Hamming Distance| -|[53](https://leetcode.com/problems/maximum-subarray/)| Maximum Subarray| -|[35](https://leetcode.com/problems/search-insert-position/)| Search Insert Position| -|[1217](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/)| Minimum Cost to Move Chips to The Same Position| -|[563](https://leetcode.com/problems/binary-tree-tilt/)| Binary Tree Tilt| -|[1446](https://leetcode.com/problems/consecutive-characters/)| Consecutive Characters| -|[938](https://leetcode.com/problems/range-sum-of-bst/)| Range Sum of BST| -|[1200](https://leetcode.com/problems/minimum-absolute-difference/)| Minimum Absolute Difference| -|[231](https://leetcode.com/problems/power-of-two/)| Power of Two| -|[476](https://leetcode.com/problems/number-complement/)| Number Complement| -|[876](https://leetcode.com/problems/middle-of-the-linked-list/)| Middle of the Linked List| -|[997](https://leetcode.com/problems/find-the-town-judge/)| Find the Town Judge| -|[1009](https://leetcode.com/problems/complement-of-base-10-integer/)| Complement of Base 10 Integer| -|[67](https://leetcode.com/problems/add-binary/)| Add Binary| -|[290](https://leetcode.com/problems/word-pattern/)| Word Pattern| -|[605](https://leetcode.com/problems/can-place-flowers/)| Can Place Flowers| -|[520](https://leetcode.com/problems/detect-capital/)| Detect Capital| -|[941](https://leetcode.com/problems/valid-mountain-array/)| Valid Mountain Array| -|[389](https://leetcode.com/problems/find-the-difference/)| Find the Difference| -|[258](https://leetcode.com/problems/add-digits/)| Add Digits| -|[1108](https://leetcode.com/problems/defanging-an-ip-address/)| Defanging an IP Address| -|[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)| Check if the Sentence Is Pangram| -|[104](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| Maximum Depth of Binary Tree| -|[136](https://leetcode.com/problems/single-number/)| Single Number| -|[171](https://leetcode.com/problems/excel-sheet-column-number/)| Excel Sheet Column Number| -|[228](https://leetcode.com/problems/summary-ranges/)| Summary Ranges| -|[338](https://leetcode.com/problems/counting-bits/)| Counting Bits| -|[392](https://leetcode.com/problems/is-subsequence/)| Is Subsequence| -|[21](https://leetcode.com/problems/merge-two-sorted-lists/)| Merge Two Sorted Lists| -|[141](https://leetcode.com/problems/linked-list-cycle/)| Linked List Cycle| -|[617](https://leetcode.com/problems/merge-two-binary-trees/)| Merge Two Binary Trees| -|[20](https://leetcode.com/problems/valid-parentheses/)| Valid Parentheses| -|[1351](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)| Count Negative Numbers in a Sorted Matrix| -|[905](https://leetcode.com/problems/sort-array-by-parity/)| Sort Array By Parity| -|[344](https://leetcode.com/problems/reverse-string/)| Reverse String| -|[1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)| Make Two Arrays Equal by Reversing Sub-arrays| -|[977](https://leetcode.com/problems/squares-of-a-sorted-array/)| Squares of a Sorted Array| -|[349](https://leetcode.com/problems/intersection-of-two-arrays/)| Intersection of Two Arrays| -|[1002](https://leetcode.com/problems/find-common-characters/)| Find Common Characters| -|[766](https://leetcode.com/problems/toeplitz-matrix/)| Toeplitz Matrix| -|[704](https://leetcode.com/problems/binary-search/)| Binary Search| -|[1160](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)| Find Words That Can Be Formed by Characters| -|[412](https://leetcode.com/problems/fizz-buzz/)| Fizz Buzz| -|[266](https://leetcode.com/problems/palindrome-permutation/)| Palindrome Permutation| -|[145](https://leetcode.com/problems/binary-tree-postorder-traversal/)| Binary Tree Postorder Traversal| -|[169](https://leetcode.com/problems/majority-element/)| Majority Element| -|[242](https://leetcode.com/problems/valid-anagram/)| Valid Anagram| -|[217](https://leetcode.com/problems/contains-duplicate/)| Contains Duplicate| -|[283](https://leetcode.com/problems/move-zeroes/)| Move Zeroes| -|[1260](https://leetcode.com/problems/shift-2d-grid/)| Shift 2D Grid| -|[700](https://leetcode.com/problems/search-in-a-binary-search-tree/)| Search in a Binary Search Tree| -|[844](https://leetcode.com/problems/backspace-string-compare/)| Backspace String Compare| -|[88](https://leetcode.com/problems/merge-sorted-array/)| Merge Sorted Array| -|[859](https://leetcode.com/problems/buddy-strings/)| Buddy Strings| -|[557](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| Reverse Words in a String III| -|[976](https://leetcode.com/problems/largest-perimeter-triangle/)| Largest Perimeter Triangle| -|[219](https://leetcode.com/problems/contains-duplicate-ii/)| Contains Duplicate II| -|[1544](https://leetcode.com/problems/make-the-string-great/)| Make The String Great| -|[1323](https://leetcode.com/problems/maximum-69-number/)| Maximum 69 Number| -|[2325](https://leetcode.com/problems/decode-the-message/)| Decode the Message| -|[121](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| Best Time to Buy and Sell Stock| -|[1971](https://leetcode.com/problems/find-if-path-exists-in-graph/)| Find if Path Exists in Graph| -|[2389](https://leetcode.com/problems/longest-subsequence-with-limited-sum/)| Longest Subsequence With Limited Sum| -|[2073](https://leetcode.com/problems/time-needed-to-buy-tickets/)| Time Needed to Buy Tickets| -|[697](https://leetcode.com/problems/degree-of-an-array/)| Degree of an Array| -|[1071](https://leetcode.com/problems/greatest-common-divisor-of-strings/)| Greatest Common Divisor of Strings| -|[1470](https://leetcode.com/problems/shuffle-the-array/)| Shuffle the Array| -|[1523](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)| Count Odd Numbers in an Interval Range| -|[989](https://leetcode.com/problems/add-to-array-form-of-integer/)| Add to Array-Form of Integer| -|[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| -|[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| -|[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| +## [Easy Problems](Easy.md) -## Medium -|LC #|Description| -|:-:|:-| -|[931](https://leetcode.com/problems/minimum-falling-path-sum/)| Minimum Falling Path Sum| -|[946](https://leetcode.com/problems/validate-stack-sequences/)| Validate Stack Sequences| -|[29](https://leetcode.com/problems/divide-two-integers/)| Divide Two Integers| -|[820](https://leetcode.com/problems/short-encoding-of-words/)| Short Encoding of Words| -|[1381](https://leetcode.com/problems/design-a-stack-with-increment-operation/)| Design a Stack With Increment Operation| -|[623](https://leetcode.com/problems/add-one-row-to-tree/)| Add One Row to Tree| -|[12](https://leetcode.com/problems/integer-to-roman/)| Integer to Roman| -|[1315](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)| Sum of Nodes with Even-Valued Grandparent| -|[1302](https://leetcode.com/problems/deepest-leaves-sum/)| Deepest Leaves Sum| -|[322](https://leetcode.com/problems/coin-change/)| Coin Change| -|[1305](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)| All Elements in Two Binary Search Trees| -|[1461](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/)| Check If a String Contains All Binary Codes of Size K| -|[1721](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)| Swapping Nodes in a Linked List| -|[714](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)| Best Time to Buy and Sell Stock with Transaction Fee| -|[478](https://leetcode.com/problems/generate-random-point-in-a-circle/)| Generate Random Point in a Circle| -|[208](https://leetcode.com/problems/implement-trie-prefix-tree/)| Implement Trie (Prefix Tree)| -|[376](https://leetcode.com/problems/wiggle-subsequence/)| Wiggle Subsequence| -|[38](https://leetcode.com/problems/count-and-say/)| Count and Say| -|[146](https://leetcode.com/problems/lru-cache/)| LRU Cache| -|[1583](https://leetcode.com/problems/count-unhappy-friends/)| Count Unhappy Friends| -|[729](https://leetcode.com/problems/my-calendar-i/)| My Calendar I| -|[841](https://leetcode.com/problems/keys-and-rooms/)| Keys And Rooms| -|[113](https://leetcode.com/problems/path-sum-ii/)| Path Sum II| -|[1398](https://leetcode.com/problems/design-underground-system/)| Design Underground System| -|[1797](https://leetcode.com/problems/design-authentication-manager/)| Design Authentication Manager| -|[1798](https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/)| Maximum Number of Consecutive Values You Can Make| -|[966](https://leetcode.com/problems/vowel-spellchecker/)| Vowel Spellchecker| -|[1689](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)| Partitioning Into Minimum Number Of Deci-Binary Numbers| -|[1764](https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/)| Form Array by Concatenating Subarrays of Another Array| -|[923](https://leetcode.com/problems/3sum-with-multiplicity/)| 3Sum With Multiplicity| -|[417](https://leetcode.com/problems/pacific-atlantic-water-flow/)| Pacific Atlantic Water Flow| -|[916](https://leetcode.com/problems/word-subsets/)| Word Subsets| -|[5](https://leetcode.com/problems/longest-palindromic-substring/)| Longest Palindromic Substring| -|[647](https://leetcode.com/problems/palindromic-substrings/)| Palindromic Substrings| -|[971](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/)| Flip Binary Tree To Match Preorder Traversal| -|[1726](https://leetcode.com/problems/tuple-with-same-product/)| Tuple with Same Product| -|[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| -|[1488](https://leetcode.com/problems/avoid-flood-in-the-city/)| Avoid Flood in The City| -|[947](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/)| Most Stones Removed with Same Row or Column| -|[622](https://leetcode.com/problems/design-circular-queue/)| Design Circular Queue| -|[1670](https://leetcode.com/problems/design-front-middle-back-queue/)| Design Front Middle Back Queue| -|[494](https://leetcode.com/problems/target-sum/)| Target Sum| -|[17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| Letter Combinations of a Phone Number| -|[638](https://leetcode.com/problems/shopping-offers/)| Shopping Offers| -|[667](https://leetcode.com/problems/beautiful-arrangement-ii/)| Beautiful Arrangement II| -|[650](https://leetcode.com/problems/2-keys-keyboard/)| 2 Keys Keyboard| -|[801](https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/)| Minimum Swaps To Make Sequences Increasing| -|[341](https://leetcode.com/problems/flatten-nested-list-iterator/)| Flatten Nested List Iterator| -|[86](https://leetcode.com/problems/partition-list/)| Partition List| -|[91](https://leetcode.com/problems/decode-ways/)| Decode Ways| -|[62](https://leetcode.com/problems/unique-paths/)| Unique Paths| -|[1209](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)| Remove All Adjacent Duplicates in String II| -|[279](https://leetcode.com/problems/perfect-squares/)| Perfect Squares| -|[377](https://leetcode.com/problems/combination-sum-iv/)| Combination Sum IV| -|[120](https://leetcode.com/problems/triangle/)| Triangle| -|[554](https://leetcode.com/problems/brick-wall/)| Brick Wall| -|[48](https://leetcode.com/problems/rotate-image/)| Rotate Image| -|[935](https://leetcode.com/problems/knight-dialer/)| Knight Dialer| -|[740](https://leetcode.com/problems/delete-and-earn/)| Delete and Earn| -|[63](https://leetcode.com/problems/unique-paths-ii/)| Unique Paths II| -|[34](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| Find First and Last Position of Element in Sorted Array| -|[970](https://leetcode.com/problems/powerful-integers/)| Powerful Integers| -|[665](https://leetcode.com/problems/non-decreasing-array/)| Non-decreasing Array| -|[1472](https://leetcode.com/problems/design-browser-history/)| Design Browser History| -|[109](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| Convert Sorted List to Binary Search Tree| -|[583](https://leetcode.com/problems/delete-operation-for-two-strings/)| Delete Operation for Two Strings| -|[1423](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)| Maximum Points You Can Obtain from Cards| -|[304](https://leetcode.com/problems/range-sum-query-2d-immutable/)| Range Sum Query 2D - Immutable| -|[816](https://leetcode.com/problems/ambiguous-coordinates/)| Ambiguous Coordinates| -|[114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| Flatten Binary Tree to Linked List| -|[1048](https://leetcode.com/problems/longest-string-chain/)| Longest String Chain| -|[213](https://leetcode.com/problems/house-robber-ii/)| House Robber II| -|[462](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)| Minimum Moves to Equal Array Elements II| -|[890](https://leetcode.com/problems/find-and-replace-pattern/)| Find and Replace Pattern| -|[337](https://leetcode.com/problems/house-robber-iii/)| House Robber III| -|[921](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/)| Minimum Add to Make Parentheses Valid| -|[1871](https://leetcode.com/problems/jump-game-vii/)| Jump Game VII| -|[150](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| Evaluate Reverse Polish Notation| -|[318](https://leetcode.com/problems/maximum-product-of-word-lengths/)| Maximum Product of Word Lengths| -|[1695](https://leetcode.com/problems/maximum-erasure-value/)| Maximum Erasure Value| -|[1268](https://leetcode.com/problems/search-suggestions-system/)| Search Suggestions System| -|[695](https://leetcode.com/problems/max-area-of-island/)| Max Area of Island| -|[97](https://leetcode.com/problems/interleaving-string/)| Interleaving String| -|[1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/)| Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts| -|[128](https://leetcode.com/problems/longest-consecutive-sequence/)| Longest Consecutive Sequence| -|[105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| Construct Binary Tree from Preorder and Inorder Traversal| -|[1696](https://leetcode.com/problems/jump-game-vi/)| Jump Game VI| -|[698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| Partition to K Equal Sum Subsets| -|[22](https://leetcode.com/problems/generate-parentheses/)| Generate Parentheses| -|[795](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)| Number of Subarrays with Bounded Maximum| -|[118](https://leetcode.com/problems/pascals-triangle/)| Pascal's Triangle| -|[576](https://leetcode.com/problems/out-of-boundary-paths/)| Out of Boundary Paths| -|[684](https://leetcode.com/problems/redundant-connection/)| Redundant Connection| -|[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)| Max Consecutive Ones III| -|[236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| Lowest Common Ancestor of a Binary Tree| -|[89](https://leetcode.com/problems/gray-code/)| Gray Code| -|[378](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)| Kth Smallest Element in a Sorted Matrix| -|[718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)| Maximum Length of Repeated Subarray| -|[300](https://leetcode.com/problems/longest-increasing-subsequence/)| Longest Increasing Subsequence| -|[162](https://leetcode.com/problems/find-peak-element/)| Find Peak Element| -|[791](https://leetcode.com/problems/custom-sort-string/)| Custom Sort String| -|[611](https://leetcode.com/problems/valid-triangle-number/)| Valid Triangle Number| -|[235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| Lowest Common Ancestor of a Binary Search Tree| -|[838](https://leetcode.com/problems/push-dominoes/)| Push Dominoes| -|[915](https://leetcode.com/problems/partition-array-into-disjoint-intervals/)| Partition Array into Disjoint Intervals| -|[814](https://leetcode.com/problems/binary-tree-pruning/)| Binary Tree Pruning| -|[90](https://leetcode.com/problems/subsets-ii/)| Subsets II| -|[877](https://leetcode.com/problems/stone-game/)| Stone Game| -|[429](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)| N-ary Tree Level Order Traversal| -|[926](https://leetcode.com/problems/flip-string-to-monotone-increasing/)| Flip String to Monotone Increasing| -|[954](https://leetcode.com/problems/array-of-doubled-pairs/)| Array of Doubled Pairs| -|[49](https://leetcode.com/problems/group-anagrams/)| Group Anagrams| -|[73](https://leetcode.com/problems/set-matrix-zeroes/)| Set Matrix Zeroes| -|[1448](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)| Count Good Nodes in Binary Tree| -|[36](https://leetcode.com/problems/valid-sudoku/)| Valid Sudoku| -|[153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| Find Minimum in Rotated Sorted Array| -|[565](https://leetcode.com/problems/array-nesting/)| Array Nesting| -|[848](https://leetcode.com/problems/shifting-letters/)| Shifting Letters| -|[978](https://leetcode.com/problems/longest-turbulent-subarray/)| Longest Turbulent Subarray| -|[54](https://leetcode.com/problems/spiral-matrix/)| Spiral Matrix| -|[1239](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)| Maximum Length of a Concatenated String with Unique Characters| -|[725](https://leetcode.com/problems/split-linked-list-in-parts/)| Split Linked List in Parts| -|[473](https://leetcode.com/problems/matchsticks-to-square/)| Matchsticks to Square| -|[1143](https://leetcode.com/problems/longest-common-subsequence/)| Longest Common Subsequence| -|[55](https://leetcode.com/problems/jump-game/)| Jump Game| -|[442](https://leetcode.com/problems/find-all-duplicates-in-an-array/)| Find All Duplicates in an Array| -|[79](https://leetcode.com/problems/word-search/)| Word Search| -|[201](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| Bitwise AND of Numbers Range| -|[1008](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| Construct Binary Search Tree from Preorder Traversal| -|[309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)| Best Time to Buy and Sell Stock with Cooldown| -|[437](https://leetcode.com/problems/path-sum-iii/)| Path Sum III| -|[4511](https://leetcode.com/problems/sort-characters-by-frequency/)| Sort Characters By Frequency| -|[222](https://leetcode.com/problems/count-complete-tree-nodes/)| Count Complete Tree Nodes| -|[75](https://leetcode.com/problems/sort-colors/)| Sort Colors| -|[15](https://leetcode.com/problems/3sum/)| 3Sum| -|[994](https://leetcode.com/problems/rotting-oranges/)| Rotting Oranges| -|[430](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)| Flatten a Multilevel Doubly Linked List| -|[130](https://leetcode.com/problems/surrounded-regions/)| Surrounded Regions| -|[129](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| Sum Root to Leaf Numbers| -|[260](https://leetcode.com/problems/single-number-iii/)| Single Number III| -|[43](https://leetcode.com/problems/multiply-strings/)| Multiply Strings| -|[96](https://leetcode.com/problems/unique-binary-search-trees/)| Unique Binary Search Trees| -|[122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| Best Time to Buy and Sell Stock II| -|[739](https://leetcode.com/problems/daily-temperatures/)| Daily Temperatures| -|[368](https://leetcode.com/problems/largest-divisible-subset/)| Largest Divisible Subset| -|[540](https://leetcode.com/problems/single-element-in-a-sorted-array/)| Single Element in a Sorted Array| -|[106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| Construct Binary Tree from Inorder and Postorder Traversal| -|[450](https://leetcode.com/problems/delete-node-in-a-bst/)| Delete Node in a BST| -|[986](https://leetcode.com/problems/interval-list-intersections/)| Interval List Intersections| -|[238](https://leetcode.com/problems/product-of-array-except-self/)| Product of Array Except Self| -|[797](https://leetcode.com/problems/all-paths-from-source-to-target/)| All Paths From Source to Target| -|[721](https://leetcode.com/problems/accounts-merge/)| Accounts Merge| -|[198](https://leetcode.com/problems/house-robber/)| House Robber| -|[328](https://leetcode.com/problems/odd-even-linked-list/)| Odd Even Linked List| -|[152](https://leetcode.com/problems/maximum-product-subarray/)| Maximum Product Subarray| -|[1306](https://leetcode.com/problems/jump-game-iii/)| Jump Game III| -|[790](https://leetcode.com/problems/domino-and-tromino-tiling/)| Domino and Tromino Tiling| -|[416](https://leetcode.com/problems/partition-equal-subset-sum/)| Partition Equal Subset Sum| -|[310](https://leetcode.com/problems/minimum-height-trees/)| Minimum Height Trees| -|[221](https://leetcode.com/problems/maximal-square/)| Maximal Square| -|[394](https://leetcode.com/problems/decode-string/)| Decode String| -|[143](https://leetcode.com/problems/reorder-list/)| Reorder List| -|[210](https://leetcode.com/problems/course-schedule-ii/)| Course Schedule II| -|[56](https://leetcode.com/problems/merge-intervals/)| Merge Intervals| -|[227](https://leetcode.com/problems/basic-calculator-ii/)| Basic Calculator II| -|[973](https://leetcode.com/problems/k-closest-points-to-origin/)| K Closest Points to Origin| -|[116](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| Populating Next Right Pointers in Each Node| -|[1015](https://leetcode.com/problems/smallest-integer-divisible-by-k/)| Smallest Integer Divisible by K| -|[1026](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)| Maximum Difference Between Node and Ancestor| -|[1010](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)| Pairs of Songs With Total Durations Divisible by 60| -|[131](https://leetcode.com/problems/palindrome-partitioning/)| Palindrome Partitioning| -|[382](https://leetcode.com/problems/linked-list-random-node/)| Linked List Random Node| -|[1041](https://leetcode.com/problems/robot-bounded-in-circle/)| Robot Bounded In Circle| -|[701](https://leetcode.com/problems/insert-into-a-binary-search-tree/)| Insert into a Binary Search Tree| -|[452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)| Minimum Number of Arrows to Burst Balloons| -|[8](https://leetcode.com/problems/string-to-integer-atoi/)| String to Integer (atoi)| -|[849](https://leetcode.com/problems/maximize-distance-to-closest-person/)| Maximize Distance to Closest Person| -|[142](https://leetcode.com/problems/linked-list-cycle-ii/)| Linked List Cycle II| -|[875](https://leetcode.com/problems/koko-eating-bananas/)| Koko Eating Bananas| -|[134](https://leetcode.com/problems/gas-station/)| Gas Station| -|[1291](https://leetcode.com/problems/sequential-digits/)| Sequential Digits| -|[421](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)| Maximum XOR of Two Numbers in an Array| -|[211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add and Search Words Data Structure| -|[438](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| Find All Anagrams in a String| -|[454](https://leetcode.com/problems/4sum-ii/)| 4Sum II| -|[525](https://leetcode.com/problems/contiguous-array/)| Contiguous Array| -|[80](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| Remove Duplicates from Sorted Array II| -|[532](https://leetcode.com/problems/k-diff-pairs-in-an-array/)| K-diff Pairs in an Array| -|[560](https://leetcode.com/problems/subarray-sum-equals-k/)| Subarray Sum Equals K| -|[567](https://leetcode.com/problems/permutation-in-string/)| Permutation in String| -|[78](https://leetcode.com/problems/subsets/)| Subsets| -|[39](https://leetcode.com/problems/combination-sum/)| Combination Sum| -|[402](https://leetcode.com/problems/remove-k-digits/)| Remove K Digits| -|[133](https://leetcode.com/problems/clone-graph/)| Clone Graph| -|[148](https://leetcode.com/problems/sort-list/)| Sort List| -|[662](https://leetcode.com/problems/maximum-width-of-binary-tree/)| Maximum Width of Binary Tree| -|[413](https://leetcode.com/problems/arithmetic-slices/)| Arithmetic Slices| -|[799](https://leetcode.com/problems/champagne-tower/)| Champagne Tower| -|[82](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| Remove Duplicates from Sorted List II| -|[61](https://leetcode.com/problems/rotate-list/)| Rotate List| -|[138](https://leetcode.com/problems/copy-list-with-random-pointer/)| Copy List with Random Pointer| -|[71](https://leetcode.com/problems/simplify-path/)| Simplify Path| -|[1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| Minimum Remove to Make Valid Parentheses| -|[856](https://leetcode.com/problems/score-of-parentheses/)| Score of Parentheses| -|[316](https://leetcode.com/problems/remove-duplicate-letters/)| Remove Duplicate Letters| -|[763](https://leetcode.com/problems/partition-labels/)| Partition Labels| -|[1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)| Smallest String With A Given Numeric Value| -|[991](https://leetcode.com/problems/broken-calculator/)| Broken Calculator| -|[881](https://leetcode.com/problems/boats-to-save-people/)| Boats to Save People| -|[1024](https://leetcode.com/problems/two-city-scheduling/)| Two City Scheduling| -|[81](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| Search in Rotated Sorted Array II| -|[287](https://leetcode.com/problems/find-the-duplicate-number/)| Find the Duplicate Number| -|[74](https://leetcode.com/problems/search-a-2d-matrix/)| Search a 2D Matrix| -|[11](https://leetcode.com/problems/container-with-most-water/)| Container With Most Water| -|[538](https://leetcode.com/problems/convert-bst-to-greater-tree/)| Convert BST to Greater Tree| -|[230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| Kth Smallest Element in a BST| -|[284](https://leetcode.com/problems/peeking-iterator/)| Peeking Iterator| -|[216](https://leetcode.com/problems/combination-sum-iii/)| Combination Sum III| -|[47](https://leetcode.com/problems/permutations-ii/)| Permutations II| -|[117](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| Populating Next Right Pointers in Each Node II| -|[743](https://leetcode.com/problems/network-delay-time/)| Network Delay Time| -|[474](https://leetcode.com/problems/ones-and-zeroes/)| Ones and Zeroes| -|[167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| Two Sum II - Input Array Is Sorted| -|[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| Longest Substring Without Repeating Characters| -|[1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)| Minimum Operations to Reduce X to Zero| -|[215](https://leetcode.com/problems/kth-largest-element-in-an-array/)| Kth Largest Element in an Array| -|[1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/)| Dot Product of Two Sparse Vectors| -|[199](https://leetcode.com/problems/binary-tree-right-side-view/)| Binary Tree Right Side View| -|[92](https://leetcode.com/problems/reverse-linked-list-ii/)| Reverse Linked List II| -|[307](https://leetcode.com/problems/range-sum-query-mutable/)| Range Sum Query - Mutable| -|[240](https://leetcode.com/problems/search-a-2d-matrix-ii/)| Search a 2D Matrix II| -|[1457](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)| Pseudo-Palindromic Paths in a Binary Tree| -|[1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/)| Maximum Score from Performing Multiplication Operations| -|[985](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)| Sum of Even Numbers After Queries| -|[1680](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)| Concatenation of Consecutive Binary Numbers| -|[990](https://leetcode.com/problems/satisfiability-of-equality-equations/)| Satisfiability of Equality Equations| -|[658](https://leetcode.com/problems/find-k-closest-elements/description/)| Find K Closest Elements| -|[1578](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/)| Minimum Time to Make Rope Colorful| -|[16](https://leetcode.com/problems/3sum-closest/description/)| 3Sum Closest| -|[334](https://leetcode.com/problems/increasing-triplet-subsequence/)| Increasing Triplet Subsequence| -|[2095](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)| Delete the Middle Node of a Linked List| -|[1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)| Check If Two String Arrays are Equivalent| -|[523](https://leetcode.com/problems/continuous-subarray-sum/)| Continuous Subarray Sum| -|[835](https://leetcode.com/problems/image-overlap/)| Image Overlap| -|[433](https://leetcode.com/problems/minimum-genetic-mutation/)| Minimum Genetic Mutation| -|[1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)| Nearest Exit from Entrance in Maze| -|[2225](https://leetcode.com/problems/find-players-with-zero-or-one-losses/)| Find Players With Zero or One Losses| -|[380](https://leetcode.com/problems/insert-delete-getrandom-o1/)| Insert Delete GetRandom O(1)| -|[1207](https://leetcode.com/problems/unique-number-of-occurrences/)| Unique Number of Occurrences| -|[45](https://leetcode.com/problems/jump-game-ii/)| Jump Game II| -|[1014](https://leetcode.com/problems/best-sightseeing-pair/)| Best Sightseeing Pair| -|[886](https://leetcode.com/problems/possible-bipartition/)| Possible Bipartition| -|[2279](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/)| Maximum Bags With Full Capacity of Rocks| -|[343](https://leetcode.com/problems/integer-break/)| Integer Break| -|[256](https://leetcode.com/problems/paint-house/)| Paint House| -|[487](https://leetcode.com/problems/max-consecutive-ones-ii/)| Max Consecutive Ones II| -|[1746](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)| Maximum Subarray Sum After One Operation| -|[1230](https://leetcode.com/problems/toss-strange-coins/)| Toss Strange Coins| -|[2498](https://leetcode.com/problems/frog-jump-ii/)| Frog Jump II| -|[2244](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/)| Minimum Rounds to Complete All Tasks| -|[1749](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)| Maximum Absolute Sum of Any Subarray| -|[2186](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/)| Minimum Number of Steps to Make Two Strings Anagram II| -|[2414](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/)| Length of the Longest Alphabetical Continuous Substring| -|[1519](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/)| Number of Nodes in the Sub-Tree With the Same Label| -|[1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/)| Lexicographically Smallest Equivalent String| -|[1314](https://leetcode.com/problems/matrix-block-sum/)| Matrix Block Sum| -|[57](https://leetcode.com/problems/insert-interval/)| Insert Interval| -|[974](https://leetcode.com/problems/subarray-sums-divisible-by-k/)| Subarray Sums Divisible by K| -|[491](https://leetcode.com/problems/non-decreasing-subsequences/)| Non-decreasing Subsequences| -|[2359](https://leetcode.com/problems/find-closest-node-to-given-two-nodes/)| Find Closest Node to Given Two Nodes| -|[909](https://leetcode.com/problems/snakes-and-ladders/)| Snakes and Ladders| -|[787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| Cheapest Flights Within K Stops| -|[6](https://leetcode.com/problems/zigzag-conversion/)| Zigzag Conversion| -|[904](https://leetcode.com/problems/fruit-into-baskets/)| Fruit Into Baskets| -|[1162](https://leetcode.com/problems/as-far-from-land-as-possible/)| As Far from Land as Possible| -|[2477](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/)| Minimum Fuel Cost to Report to the Capital| -|[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| -|[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| -|[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| +## [Medium Problems](Medium.md) -## Hard -|LC #|Description| -|:-:|:-| -|[354](https://leetcode.com/problems/russian-doll-envelopes/)| Russian Doll Envelopes| -|[936](https://leetcode.com/problems/stamping-the-sequence/)| Stamping The Sequence| -|[32](https://leetcode.com/problems/longest-valid-parentheses/)| Longest Valid Parentheses| -|[1559](https://leetcode.com/problems/detect-cycles-in-2d-grid/)| Detect Cycles in 2D Grid| -|[329](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)| Longest Increasing Path in a Matrix| -|[1192](https://leetcode.com/problems/critical-connections-in-a-network/)| Critical Connections in a Network| -|[980](https://leetcode.com/problems/unique-paths-iii/)| Unique Paths III| -|[745](https://leetcode.com/problems/prefix-and-suffix-search/)| Prefix and Suffix Search| -|[630](https://leetcode.com/problems/course-schedule-iii/)| Course Schedule III| -|[65](https://leetcode.com/problems/valid-number/)| Valid Number| -|[968](https://leetcode.com/problems/binary-tree-cameras/)| Binary Tree Cameras| -|[1473](https://leetcode.com/problems/paint-house-iii/)| Paint House III| -|[51](https://leetcode.com/problems/n-queens/)| N-Queens| -|[52](https://leetcode.com/problems/n-queens-ii/)| N-Queens II| -|[778](https://leetcode.com/problems/swim-in-rising-water/)| Swim in Rising Water| -|[315](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| Count of Smaller Numbers After Self| -|[135](https://leetcode.com/problems/candy/)| Candy| -|[927](https://leetcode.com/problems/three-equal-parts/)| Three Equal Parts| -|[25](https://leetcode.com/problems/reverse-nodes-in-k-group/)| Reverse Nodes in k-Group| -|[233](https://leetcode.com/problems/number-of-digit-one/)| Number of Digit One| -|[42](https://leetcode.com/problems/trapping-rain-water/)| Trapping Rain Water| -|[132](https://leetcode.com/problems/palindrome-partitioning-ii/)| Palindrome Partitioning II| -|[546](https://leetcode.com/problems/remove-boxes/)| Remove Boxes| -|[37](https://leetcode.com/problems/sudoku-solver/)| Sudoku Solver| -|[850](https://leetcode.com/problems/rectangle-area-ii/)| Rectangle Area II| -|[330](https://leetcode.com/problems/patching-array//)| Patching Array| -|[899](https://leetcode.com/problems/orderly-queue/)| Orderly Queue| -|[224](https://leetcode.com/problems/basic-calculator/)| Basic Calculator| -|[782](https://leetcode.com/problems/transform-to-chessboard/)| Transform to Chessboard| -|[174](https://leetcode.com/problems/dungeon-game/)| Dungeon Game| -|[123](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| Best Time to Buy and Sell Stock III| -|[154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| Find Minimum in Rotated Sorted Array II| -|[668](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)| Kth Smallest Number in Multiplication Table| -|[952](https://leetcode.com/problems/largest-component-size-by-common-factor/)| Largest Component Size by Common Factor| -|[85](https://leetcode.com/problems/maximal-rectangle/)| Maximal Rectangle| -|[1032](https://leetcode.com/problems/stream-of-characters/)| Stream of Characters| -|[878](https://leetcode.com/problems/nth-magical-number/)| Nth Magical Number| -|[902](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/)| Numbers At Most N Given Digit Set| -|[1463](https://leetcode.com/problems/cherry-pickup-ii/)| Cherry Pickup II| -|[1345](https://leetcode.com/problems/jump-game-iv/)| Jump Game IV| -|[1510](https://leetcode.com/problems/stone-game-iv/)| Stone Game IV| -|[127](https://leetcode.com/problems/word-ladder/)| Word Ladder| -|[410](https://leetcode.com/problems/split-array-largest-sum/)| Split Array Largest Sum| -|[629](https://leetcode.com/problems/k-inverse-pairs-array/)| K Inverse Pairs Array| -|[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)| Number of Submatrices That Sum to Target| -|[1531](https://leetcode.com/problems/string-compression-ii/)| String Compression II| -|[1335](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)| Minimum Difficulty of a Job Schedule| -|[76](https://leetcode.com/problems/minimum-window-substring/)| Minimum Window Substring| -|[2136](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)| Earliest Possible Day of Full Bloom| -|[2193](https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/)| Minimum Number of Moves to Make Palindrome| -|[1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)| Maximum Profit in Job Scheduling| -|[446](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)| Arithmetic Slices II - Subsequence| -|[124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| Binary Tree Maximum Path Sum| -|[600](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)| Non-negative Integers without Consecutive Ones| -|[72](https://leetcode.com/problems/edit-distance/)| Edit Distance| -|[1340](https://leetcode.com/problems/jump-game-v/)| Jump Game V| -|[403](https://leetcode.com/problems/frog-jump/)| Frog Jump| -|[188](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| Best Time to Buy and Sell Stock IV| -|[149](https://leetcode.com/problems/max-points-on-a-line/)| Max Points on a Line| -|[140](https://leetcode.com/problems/word-break-ii/)| Word Break II| -|[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| -|[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| -|[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| -|[472](https://leetcode.com/problems/concatenated-words/description/)| Concatenated Words| +## [Hard Problems](Hard.md) From 0841823e47adbb3621fb472eb6f5001930ccc86c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Feb 2023 07:15:53 -0800 Subject: [PATCH 708/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 5695f7b..a3049f2 100644 --- a/Medium.md +++ b/Medium.md @@ -280,3 +280,4 @@ |[1129](https://leetcode.com/problems/shortest-path-with-alternating-colors/)| Shortest Path with Alternating Colors| |[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| |[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| +|[427](https://leetcode.com/problems/construct-quad-tree/)| Construct Quad Tree| From 11f03395a19e5ea6b09decdcd2d188674abfcddf Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Feb 2023 07:16:17 -0800 Subject: [PATCH 709/969] Create L427.go --- Medium/L427.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L427.go diff --git a/Medium/L427.go b/Medium/L427.go new file mode 100644 index 0000000..1baa4bc --- /dev/null +++ b/Medium/L427.go @@ -0,0 +1,40 @@ +package Medium + +func construct(grid [][]int) *Node { + var solve func(x1, x2, y1, y2 int) *Node + solve = func(x1, x2, y1, y2 int) *Node { + if x1 == x2 { + val := false + if grid[x1][y1] == 1 { + val = true + } + return &Node{ + Val: val, + IsLeaf: true, + } + } + + rowMid, colMid := (x1+x2)/2, (y1+y2)/2 + topLeft := solve(x1, rowMid, y1, colMid) + topRight := solve(x1, rowMid, colMid+1, y2) + bottomLeft := solve(rowMid+1, x2, y1, colMid) + bottomRight := solve(rowMid+1, x2, colMid+1, y2) + + if topLeft.IsLeaf && topRight.IsLeaf && bottomLeft.IsLeaf && bottomRight.IsLeaf && + topRight.Val == topLeft.Val && topRight.Val == bottomLeft.Val && topRight.Val == bottomRight.Val { + return &Node{ + Val: topLeft.Val, + IsLeaf: true, + } + } else { + return &Node{ + TopLeft: topLeft, + TopRight: topRight, + BottomLeft: bottomLeft, + BottomRight: bottomRight, + } + } + } + + return solve(0, len(grid)-1, 0, len(grid[0])-1) +} From b81e39aba9f09c42e1cb6de7e9d2e1cfc531f0b5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Feb 2023 07:34:44 -0800 Subject: [PATCH 710/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a3049f2..f084a73 100644 --- a/Medium.md +++ b/Medium.md @@ -281,3 +281,4 @@ |[103](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| Binary Tree Zigzag Level Order Traversal| |[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| |[427](https://leetcode.com/problems/construct-quad-tree/)| Construct Quad Tree| +|[652](https://leetcode.com/problems/find-duplicate-subtrees/)| Find Duplicate Subtrees| From 21403d45c482f2e994aeb5215c405983091a6155 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Feb 2023 07:35:07 -0800 Subject: [PATCH 711/969] Create L652.go --- Medium/L652.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L652.go diff --git a/Medium/L652.go b/Medium/L652.go new file mode 100644 index 0000000..0bb4735 --- /dev/null +++ b/Medium/L652.go @@ -0,0 +1,35 @@ +package Medium + +func findDuplicateSubtrees(root *TreeNode) []*TreeNode { + curID := 1 + serialToID, idToCount := make(map[string]int), make(map[int]int) + var ( + res []*TreeNode + solve func(root *TreeNode) int + ) + solve = func(root *TreeNode) int { + if root == nil { + return 0 + } + + leftID, rightID := solve(root.Left), solve(root.Right) + curSerial := fmt.Sprintf("%d-%d-%d", leftID, root.Val, rightID) + serialID := curID + if v, ok := serialToID[curSerial]; ok { + serialID = v + } + if serialID == curID { + curID++ + } + serialToID[curSerial] = serialID + idToCount[serialID]++ + + if idToCount[serialID] == 2 { + res = append(res, root) + } + return serialID + } + + solve(root) + return res +} From e7c9b732bf0ec062f445c7cd2c4c6b7bd61940ed Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 2 Mar 2023 07:13:39 -0800 Subject: [PATCH 712/969] Create L443.go --- Medium/L443.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L443.go diff --git a/Medium/L443.go b/Medium/L443.go new file mode 100644 index 0000000..7be4e6a --- /dev/null +++ b/Medium/L443.go @@ -0,0 +1,32 @@ +package Medium + +func compress(chars []byte) int { + index := 0 + + updateChars := func(freq int) { + arr := []byte(fmt.Sprintf("%d", freq)) + for i := range arr { + chars[index] = arr[i] + index++ + } + } + + for i := 0; i < len(chars); { + j := i + for j < len(chars) && chars[i] == chars[j] { + j++ + } + freq := j - i + chars[index] = chars[i] + index++ + if freq > 1 && freq < 10 { + chars[index] = byte(freq + '0') + index++ + } else if freq >= 10 { + updateChars(freq) + } + i = j + } + + return index +} From 0a71149a64730b0744f2c624f8a2b194878033ee Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 2 Mar 2023 07:14:31 -0800 Subject: [PATCH 713/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index f084a73..dcaa17e 100644 --- a/Medium.md +++ b/Medium.md @@ -282,3 +282,4 @@ |[1011](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)| Capacity To Ship Packages Within D Days| |[427](https://leetcode.com/problems/construct-quad-tree/)| Construct Quad Tree| |[652](https://leetcode.com/problems/find-duplicate-subtrees/)| Find Duplicate Subtrees| +|[443](https://leetcode.com/problems/string-compression/)| String Compression| From 3b69c36a8c338aed462e78753ac7c1aca8cc94ba Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Mar 2023 15:45:47 -0800 Subject: [PATCH 714/969] Create L1539.go --- Easy/L1539.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Easy/L1539.go diff --git a/Easy/L1539.go b/Easy/L1539.go new file mode 100644 index 0000000..db486c5 --- /dev/null +++ b/Easy/L1539.go @@ -0,0 +1,17 @@ +package Easy + +func findKthPositive(arr []int, k int) int { + left, right := 0, len(arr)-1 + + for left <= right { + mid := left + (right-left)/2 + + if arr[mid]-mid <= k { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return left + k +} From 7c4b20f3df7c1721e2624bf9f434415acd0819b4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 Mar 2023 15:46:33 -0800 Subject: [PATCH 715/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 57ad938..fb7711a 100644 --- a/Easy.md +++ b/Easy.md @@ -136,3 +136,4 @@ |[783](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| Minimum Distance Between BST Nodes| |[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| |[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| +|[1539](https://leetcode.com/problems/kth-missing-positive-number/)| Kth Missing Positive Number| From 88ac0ae5a8bd666b010e929bc74387dc9f6f0533 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Mar 2023 06:41:13 -0800 Subject: [PATCH 716/969] Create L2187.go --- Medium/L2187.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Medium/L2187.go diff --git a/Medium/L2187.go b/Medium/L2187.go new file mode 100644 index 0000000..9b18c29 --- /dev/null +++ b/Medium/L2187.go @@ -0,0 +1,47 @@ +package Medium + +func minimumTime(time []int, totalTrips int) int64 { + var ( + lowTime int64 + highTime int64 + ) + minTripTime := func() int64 { + mini := math.MaxInt32 + + for _, t := range time { + mini = min(mini, t) + } + + return int64(mini) + } + + numTripsForGivenTime := func(givenTime int64) int64 { + var totTrips int64 + for _, t := range time { + totTrips += givenTime / int64(t) + } + + return totTrips + } + lowTime, highTime = 1, minTripTime()*int64(totalTrips) + + for lowTime < highTime { + mid := lowTime + (highTime-lowTime)/2 + + if numTripsForGivenTime(mid) >= int64(totalTrips) { + highTime = mid + } else { + lowTime = mid + 1 + } + } + + return lowTime +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 36555cf6bf0a7478b0ba17838eb4ffddeb97e170 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Mar 2023 06:42:02 -0800 Subject: [PATCH 717/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index dcaa17e..de70b0a 100644 --- a/Medium.md +++ b/Medium.md @@ -283,3 +283,4 @@ |[427](https://leetcode.com/problems/construct-quad-tree/)| Construct Quad Tree| |[652](https://leetcode.com/problems/find-duplicate-subtrees/)| Find Duplicate Subtrees| |[443](https://leetcode.com/problems/string-compression/)| String Compression| +|[2187](https://leetcode.com/problems/minimum-time-to-complete-trips/)| Minimum Time to Complete Trips| From f1de959348d5bad4ac1bfe70b47b00c45c321b3e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Mar 2023 06:52:02 -0800 Subject: [PATCH 718/969] Create L2444.go --- Hard/L2444.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Hard/L2444.go diff --git a/Hard/L2444.go b/Hard/L2444.go new file mode 100644 index 0000000..d5ca6b3 --- /dev/null +++ b/Hard/L2444.go @@ -0,0 +1,35 @@ +package Hard + +func countSubarrays(nums []int, minK int, maxK int) int64 { + var ( + res, start int64 + minStart, maxStart int64 + ) + minFound, maxFound := false, false + + for i := range nums { + n := nums[i] + if n < minK || n > maxK { + minFound, maxFound, start = false, false, int64(i+1) + } + if n == minK { + minFound, minStart = true, int64(i) + } + if n == maxK { + maxFound, maxStart = true, int64(i) + } + if minFound && maxFound { + res += min(minStart, maxStart) - start + 1 + } + } + + return res +} + +func min(a, b int64) int64 { + if a < b { + return a + } + + return b +} From 4a641c442aa39528feef789c18e252a905a6190b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 7 Mar 2023 06:53:04 -0800 Subject: [PATCH 719/969] Update Hard.md --- Hard.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Hard.md b/Hard.md index 7ff986a..4faba8c 100644 --- a/Hard.md +++ b/Hard.md @@ -64,4 +64,5 @@ |[1289](https://leetcode.com/problems/minimum-falling-path-sum-ii/)| Minimum Falling Path Sum II| |[2246](https://leetcode.com/problems/longest-path-with-different-adjacent-characters/)| Longest Path With Different Adjacent Characters| |[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| -|[472](https://leetcode.com/problems/concatenated-words/description/)| Concatenated Words| +|[472](https://leetcode.com/problems/concatenated-words/)| Concatenated Words| +|[2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)| Count Subarrays With Fixed Bounds| From ba2965883f8e9c3a36598d3ea01cfd74b6d14f91 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 Mar 2023 20:27:42 -0700 Subject: [PATCH 720/969] Create L23.go --- Hard/L23.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Hard/L23.go diff --git a/Hard/L23.go b/Hard/L23.go new file mode 100644 index 0000000..d3a69b6 --- /dev/null +++ b/Hard/L23.go @@ -0,0 +1,50 @@ +package Hard + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func mergeKLists(lists []*ListNode) *ListNode { + var ( + merge func(l1, l2 *ListNode) *ListNode + partition func(start, end int) *ListNode + ) + + merge = func(l1, l2 *ListNode) *ListNode { + if l1 == nil { + return l2 + } + + if l2 == nil { + return l1 + } + + if l1.Val < l2.Val { + l1.Next = merge(l1.Next, l2) + return l1 + } + + l2.Next = merge(l1, l2.Next) + return l2 + } + + partition = func(start, end int) *ListNode { + if start == end { + return lists[start] + } + + if start >= end { + return nil + } + + q := (start + end) / 2 + l1, l2 := partition(start, q), partition(q + 1, end) + + return merge(l1, l2) + } + + return partition(0, len(lists) - 1) +} From 080e5af9c6de44bb8d5ac17c8bd7632cbf353b36 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 Mar 2023 20:30:58 -0700 Subject: [PATCH 721/969] Create L101.go --- Easy/L101.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Easy/L101.go diff --git a/Easy/L101.go b/Easy/L101.go new file mode 100644 index 0000000..5c89435 --- /dev/null +++ b/Easy/L101.go @@ -0,0 +1,29 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func isSymmetric(root *TreeNode) bool { + if root == nil { + return true + } + + var solve func(left, right *TreeNode) bool + solve = func(left, right *TreeNode) bool { + if left == nil || right == nil { + return left == right + } + if left.Val != right.Val { + return false + } + + return solve(left.Left, right.Right) && solve(left.Right, right.Left) + } + + return solve(root.Left, root.Right) +} From 894c9c72cfa640704152bbf4e4edcb0bcdf31c65 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 Mar 2023 20:31:37 -0700 Subject: [PATCH 722/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index fb7711a..ac9834c 100644 --- a/Easy.md +++ b/Easy.md @@ -137,3 +137,4 @@ |[1137](https://leetcode.com/problems/n-th-tribonacci-number/)| N-th Tribonacci Number| |[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| |[1539](https://leetcode.com/problems/kth-missing-positive-number/)| Kth Missing Positive Number| +|[101](https://leetcode.com/problems/symmetric-tree/)| Symmetric Tree| From 01b7785c3a62f38e8c4887c9ccdeb76bf25431a6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 Mar 2023 20:32:25 -0700 Subject: [PATCH 723/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 4faba8c..29f66f7 100644 --- a/Hard.md +++ b/Hard.md @@ -66,3 +66,4 @@ |[2421](https://leetcode.com/problems/number-of-good-paths/)| Number of Good Paths| |[472](https://leetcode.com/problems/concatenated-words/)| Concatenated Words| |[2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)| Count Subarrays With Fixed Bounds| +|[23](https://leetcode.com/problems/merge-k-sorted-lists/)| Merge k Sorted Lists| From b0760b2882f3819e60d4048a4f7b59e4257be194 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Mar 2023 10:21:58 -0700 Subject: [PATCH 724/969] Create L958.go --- Medium/L958.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L958.go diff --git a/Medium/L958.go b/Medium/L958.go new file mode 100644 index 0000000..ff58d6b --- /dev/null +++ b/Medium/L958.go @@ -0,0 +1,25 @@ +package Medium + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isCompleteTree(self, root: Optional[TreeNode]) -> bool: + end, q = False, deque() + q.append(root) + + while q: + curr = q.popleft() + + if not curr: + end = True + else: + if end: + return False + q.append(curr.left) + q.append(curr.right) + + return True From 9e2f8338bc993a9cacb3559807e6210400ca0ec3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 15 Mar 2023 10:22:35 -0700 Subject: [PATCH 725/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index de70b0a..3b79884 100644 --- a/Medium.md +++ b/Medium.md @@ -284,3 +284,4 @@ |[652](https://leetcode.com/problems/find-duplicate-subtrees/)| Find Duplicate Subtrees| |[443](https://leetcode.com/problems/string-compression/)| String Compression| |[2187](https://leetcode.com/problems/minimum-time-to-complete-trips/)| Minimum Time to Complete Trips| +|[958](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)| Check Completeness of a Binary Tree| From a4b133d3fc6ca44ad7a805453678b3c19557b29b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Mar 2023 07:00:25 -0700 Subject: [PATCH 726/969] Create L2492.go --- Medium/L2492.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Medium/L2492.go diff --git a/Medium/L2492.go b/Medium/L2492.go new file mode 100644 index 0000000..7a2cce9 --- /dev/null +++ b/Medium/L2492.go @@ -0,0 +1,47 @@ +package Medium + +type pair struct { + x, y int +} + +func newPair(x, y int) pair { + return pair{ + x: x, + y: y, + } +} + +func minScore(n int, roads [][]int) int { + adj, vis := make([][]pair, n+1), make([]bool, n+1) + for i := 0; i < n+1; i++ { + adj[i] = make([]pair, 0) + } + + for i := range roads { + adj[roads[i][0]] = append(adj[roads[i][0]], newPair(roads[i][1], roads[i][2])) + adj[roads[i][1]] = append(adj[roads[i][1]], newPair(roads[i][0], roads[i][2])) + } + + var q []int + q = append(q, 1) + vis[1] = true + for len(q) > 0 { + node := q[0] + q = q[1:] + for _, p := range adj[node] { + if !vis[p.x] { + q = append(q, p.x) + vis[p.x] = true + } + } + } + + minPath := math.MaxInt32 + for i := range roads { + if vis[roads[i][0]] && vis[roads[i][1]] && roads[i][2] < minPath { + minPath = roads[i][2] + } + } + + return minPath +} From 00049a5a69fdfd1f524c5c67f66476346f425cf7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 22 Mar 2023 07:01:05 -0700 Subject: [PATCH 727/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 3b79884..a5af843 100644 --- a/Medium.md +++ b/Medium.md @@ -285,3 +285,4 @@ |[443](https://leetcode.com/problems/string-compression/)| String Compression| |[2187](https://leetcode.com/problems/minimum-time-to-complete-trips/)| Minimum Time to Complete Trips| |[958](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)| Check Completeness of a Binary Tree| +|[2492](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/)| Minimum Score of a Path Between Two Cities| From 63c964ee6e74c0d25c02aec29f21071411bb6355 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 24 Mar 2023 16:13:28 -0700 Subject: [PATCH 728/969] Create L1466.go --- Medium/L1466.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L1466.go diff --git a/Medium/L1466.go b/Medium/L1466.go new file mode 100644 index 0000000..207b564 --- /dev/null +++ b/Medium/L1466.go @@ -0,0 +1,41 @@ +package Medium + +type pair struct { + neighbor, edgeWeight int +} + +func newPair(x, y int) pair { + return pair{ + neighbor: x, + edgeWeight: y, + } +} + +func minReorder(n int, connections [][]int) int { + mp, ans := make(map[int][]pair), 0 + var solve func(node, parent int) + + for _, c := range connections { + if _, ok := mp[c[0]]; !ok { + mp[c[0]] = make([]pair, 0) + } + mp[c[0]] = append(mp[c[0]], newPair(c[1], 1)) + + if _, ok := mp[c[1]]; !ok { + mp[c[1]] = make([]pair, 0) + } + mp[c[1]] = append(mp[c[1]], newPair(c[0], 0)) + } + + solve = func(node, parent int) { + for _, nei := range mp[node] { + if nei.neighbor != parent { + ans += nei.edgeWeight + solve(nei.neighbor, node) + } + } + } + + solve(0, -1) + return ans +} From d42508d96833ae0ddb447f38b852e6adaf405828 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 24 Mar 2023 16:25:44 -0700 Subject: [PATCH 729/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a5af843..12f185e 100644 --- a/Medium.md +++ b/Medium.md @@ -286,3 +286,4 @@ |[2187](https://leetcode.com/problems/minimum-time-to-complete-trips/)| Minimum Time to Complete Trips| |[958](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)| Check Completeness of a Binary Tree| |[2492](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/)| Minimum Score of a Path Between Two Cities| +|[1466](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)| Reorder Routes to Make All Paths Lead to the City Zero| From 3a56c991ba40a96bd5956d74be4e78fdedef5615 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Mar 2023 06:49:21 -0700 Subject: [PATCH 730/969] Create L2360.go --- Hard/L2360.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Hard/L2360.go diff --git a/Hard/L2360.go b/Hard/L2360.go new file mode 100644 index 0000000..13b279b --- /dev/null +++ b/Hard/L2360.go @@ -0,0 +1,42 @@ +package Hard + +func longestCycle(edges []int) int { + var exists struct{} + res, st := -1, make(map[int]struct{}) + + for _, e := range edges { + if e != -1 { + if _, contains := st[e]; !contains { + mp, pos := make(map[int]int), 0 + mp[e], st[e] = pos, exists + + for edges[e] != -1 { + e = edges[e] + + if _, contains = mp[e]; contains { + res = max(res, pos-mp[e]+1) + break + } + + if _, contains = st[e]; contains { + break + } + + pos++ + mp[e] = pos + st[e] = exists + } + } + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 4350650fb1dd1fff97278d1847a67899ff939c69 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Mar 2023 06:50:16 -0700 Subject: [PATCH 731/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 29f66f7..82aa426 100644 --- a/Hard.md +++ b/Hard.md @@ -67,3 +67,4 @@ |[472](https://leetcode.com/problems/concatenated-words/)| Concatenated Words| |[2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)| Count Subarrays With Fixed Bounds| |[23](https://leetcode.com/problems/merge-k-sorted-lists/)| Merge k Sorted Lists| +|[2360](https://leetcode.com/problems/longest-cycle-in-a-graph/)| Longest Cycle in a Graph| From 835b39f455e0800f965d8d9d3cba245d6eff86d0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 26 Mar 2023 07:21:50 -0700 Subject: [PATCH 732/969] Create L2316.go --- Medium/L2316.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Medium/L2316.go diff --git a/Medium/L2316.go b/Medium/L2316.go new file mode 100644 index 0000000..f1b3816 --- /dev/null +++ b/Medium/L2316.go @@ -0,0 +1,39 @@ +package Medium + +class Solution { +public: + void dfs(vectoradj[],int src,vector&vis,int &counter){ + if(vis[src]) return; + vis[src]=true; + counter++; + for(auto ele:adj[src]){ + if(!vis[ele]){ + dfs(adj,ele,vis,counter); + } + } + + } + long long countPairs(int n, vector>& edges) { + vectoradj[n]; + for(auto ele:edges){ + adj[ele[0]].push_back(ele[1]); + adj[ele[1]].push_back(ele[0]); + } + long long res = 0; + vectorvis(n,false); + vectortemp; + for(int i = 0;i Date: Sun, 26 Mar 2023 07:22:40 -0700 Subject: [PATCH 733/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 12f185e..25156b9 100644 --- a/Medium.md +++ b/Medium.md @@ -287,3 +287,4 @@ |[958](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)| Check Completeness of a Binary Tree| |[2492](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/)| Minimum Score of a Path Between Two Cities| |[1466](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)| Reorder Routes to Make All Paths Lead to the City Zero| +|[2316](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/)| Count Unreachable Pairs of Nodes in an Undirected Graph| From 2d13d18f9a8303d67ce139883476fd4dbec0e378 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 29 Mar 2023 16:37:00 -0700 Subject: [PATCH 734/969] Create L1402.go --- Hard/L1402.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Hard/L1402.go diff --git a/Hard/L1402.go b/Hard/L1402.go new file mode 100644 index 0000000..65fd78d --- /dev/null +++ b/Hard/L1402.go @@ -0,0 +1,34 @@ +package Hard + +func maxSatisfaction(satisfaction []int) int { + sort.Ints(satisfaction) + mp := make(map[string]int) + var solve func(idx, time int) int + + solve = func(idx, time int) int { + if idx == len(satisfaction) { + return 0 + } + + key := fmt.Sprintf("%d-%d", idx, time) + if v, ok := mp[key]; ok { + return v + } + + includeDish := (satisfaction[idx] * (time + 1)) + solve(idx+1, time+1) + excludeDish := solve(idx+1, time) + + mp[key] = max(includeDish, excludeDish) + return mp[key] + } + + return solve(0, 0) +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From d1e163b565dd2b08a491b5898a287b0e448fb7d3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 29 Mar 2023 16:37:58 -0700 Subject: [PATCH 735/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 82aa426..dd81b3f 100644 --- a/Hard.md +++ b/Hard.md @@ -68,3 +68,4 @@ |[2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)| Count Subarrays With Fixed Bounds| |[23](https://leetcode.com/problems/merge-k-sorted-lists/)| Merge k Sorted Lists| |[2360](https://leetcode.com/problems/longest-cycle-in-a-graph/)| Longest Cycle in a Graph| +|[1402](https://leetcode.com/problems/reducing-dishes/)| Reducing Dishes| From 48fa938cee378bcc53aad47061e1ae88e3e2452c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Mar 2023 15:52:19 -0700 Subject: [PATCH 736/969] Create L87.go --- Hard/L87.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Hard/L87.go diff --git a/Hard/L87.go b/Hard/L87.go new file mode 100644 index 0000000..301a4eb --- /dev/null +++ b/Hard/L87.go @@ -0,0 +1,45 @@ +package Hard + +func isScramble(s1 string, s2 string) bool { + var solve func(a, b string) bool + mp := make(map[string]bool) + + if s1 == s2 { + return true + } + if len(s1) != len(s2) { + return false + } + if len(s1) == 0 { + return true + } + + solve = func(a, b string) bool { + if a == b { + return true + } + if len(a) <= 1 { + return false + } + + key := fmt.Sprintf("%s-%s", a, b) + if v, ok := mp[key]; ok { + return v + } + + n, check := len(a), false + for i := 1; i < n; i++ { + swap := solve(a[:i], b[n-i:]) && solve(a[i:], b[0:n-i]) + unSwap := solve(a[0:i], b[0:i]) && solve(a[i:], b[i:]) + + if swap || unSwap { + check = true + break + } + } + mp[key] = check + return mp[key] + } + + return solve(s1, s2) +} From d6fc6b7ea0e4292bdab179883fd067e6387bd3c2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Mar 2023 15:52:54 -0700 Subject: [PATCH 737/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index dd81b3f..e04e597 100644 --- a/Hard.md +++ b/Hard.md @@ -69,3 +69,4 @@ |[23](https://leetcode.com/problems/merge-k-sorted-lists/)| Merge k Sorted Lists| |[2360](https://leetcode.com/problems/longest-cycle-in-a-graph/)| Longest Cycle in a Graph| |[1402](https://leetcode.com/problems/reducing-dishes/)| Reducing Dishes| +|[87](https://leetcode.com/problems/scramble-string/)| Scramble String| From 9cc42ef578365109e4dd52a7d3a741110d17de86 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 4 Apr 2023 15:37:36 -0700 Subject: [PATCH 738/969] Create L2405.go --- Medium/L2405.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L2405.go diff --git a/Medium/L2405.go b/Medium/L2405.go new file mode 100644 index 0000000..55d64d9 --- /dev/null +++ b/Medium/L2405.go @@ -0,0 +1,18 @@ +package Medium + +func partitionString(s string) int { + var exists struct{} + idx, cnt := 0, 0 + mp := make(map[byte]struct{}) + + for i := range s { + if _, ok := mp[s[i]]; ok { + cnt++ + mp = make(map[byte]struct{}) + } + mp[s[i]] = exists + idx++ + } + + return cnt + 1 +} From 243b0754ff29e1a64f7e362fcd6033ca1c339c7f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 4 Apr 2023 15:38:26 -0700 Subject: [PATCH 739/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 25156b9..b512b35 100644 --- a/Medium.md +++ b/Medium.md @@ -288,3 +288,4 @@ |[2492](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/)| Minimum Score of a Path Between Two Cities| |[1466](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)| Reorder Routes to Make All Paths Lead to the City Zero| |[2316](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/)| Count Unreachable Pairs of Nodes in an Undirected Graph| +|[2405](https://leetcode.com/problems/optimal-partition-of-string/)| Optimal Partition of String| From 4f61cd1b97a6af43ce3ee70a12dca4145bb2712d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 4 Apr 2023 15:44:53 -0700 Subject: [PATCH 740/969] Create L2300.go --- Medium/L2300.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/L2300.go diff --git a/Medium/L2300.go b/Medium/L2300.go new file mode 100644 index 0000000..c23e2e8 --- /dev/null +++ b/Medium/L2300.go @@ -0,0 +1,22 @@ +package Medium + +func successfulPairs(spells []int, potions []int, success int64) []int { + res := make([]int, len(spells)) + sort.Ints(potions) + + for i := 0; i < len(spells); i++ { + low, high := 0, len(potions) - 1 + for low <= high { + mid := low + (high - low) / 2 + + if int64(spells[i] * potions[mid]) >= success { + high = mid - 1 + } else { + low = mid + 1 + } + } + res[i] = len(potions) - 1 - high + } + + return res +} From 01bb5761eec49a9b97cc923510a32323c29361d5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 4 Apr 2023 15:45:29 -0700 Subject: [PATCH 741/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index b512b35..8ca228c 100644 --- a/Medium.md +++ b/Medium.md @@ -289,3 +289,4 @@ |[1466](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)| Reorder Routes to Make All Paths Lead to the City Zero| |[2316](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/)| Count Unreachable Pairs of Nodes in an Undirected Graph| |[2405](https://leetcode.com/problems/optimal-partition-of-string/)| Optimal Partition of String| +|[2300](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/)| Successful Pairs of Spells and Potions| From c915621a01821b3db3871288ea3b117919a2aa3a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 5 Apr 2023 07:05:35 -0700 Subject: [PATCH 742/969] Create L2439.go --- Medium/L2439.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L2439.go diff --git a/Medium/L2439.go b/Medium/L2439.go new file mode 100644 index 0000000..c9963a4 --- /dev/null +++ b/Medium/L2439.go @@ -0,0 +1,33 @@ +package Medium + +func minimizeArrayValue(nums []int) int { + low, high, res := 0, math.MaxInt32, 0 + + isPossible := func(maximum int) bool { + excess := 0 + for _, n := range nums { + if n > maximum { + candidate := n - maximum + if candidate > excess { + return false + } + excess -= candidate + } else { + excess += maximum - n + } + } + + return true + } + + for low <= high { + mid := low + (high-low)/2 + if isPossible(mid) { + high, res = mid-1, mid + } else { + low = mid + 1 + } + } + + return res +} From 545438ff731f1257014a5240dd205628e0a5160e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 5 Apr 2023 07:06:20 -0700 Subject: [PATCH 743/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 8ca228c..db2bcfa 100644 --- a/Medium.md +++ b/Medium.md @@ -290,3 +290,4 @@ |[2316](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/)| Count Unreachable Pairs of Nodes in an Undirected Graph| |[2405](https://leetcode.com/problems/optimal-partition-of-string/)| Optimal Partition of String| |[2300](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/)| Successful Pairs of Spells and Potions| +|[2439](https://leetcode.com/problems/minimize-maximum-of-array/)| Minimize Maximum of Array| From 14e04649b797320acd6cd6c89ff53a056bf494d6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 8 Apr 2023 18:10:56 -0700 Subject: [PATCH 744/969] Create L1857.go --- Hard/L1857.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Hard/L1857.go diff --git a/Hard/L1857.go b/Hard/L1857.go new file mode 100644 index 0000000..cdbdf73 --- /dev/null +++ b/Hard/L1857.go @@ -0,0 +1,76 @@ +package Hard + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func largestPathValue(colors string, edges [][]int) int { + var graph [][]int + n, inDegree := len(colors), make([]int, len(colors)) + + for i := 0; i < n; i++ { + graph = append(graph, make([]int, 0)) + } + for _, e := range edges { + u, v := e[0], e[1] + inDegree[v]++ + graph[u] = append(graph[u], v) + } + + mp := make([][]int, n) + for i := 0; i < n; i++ { + mp[i] = make([]int, 26) + } + + var q []int + for i := 0; i < n; i++ { + if inDegree[i] == 0 { + q = append(q, i) + mp[i][colors[i]-'a'] = 1 + } + } + + getMax := func(nums []int) int { + maxi := math.MinInt32 + + for _, n := range nums { + maxi = max(maxi, n) + } + + return maxi + } + + var res, seen int + for len(q) > 0 { + node := q[0] + q = q[1:] + seen++ + + res = max(res, getMax(mp[node])) + + for _, nei := range graph[node] { + for i := 0; i < 26; i++ { + val := mp[node][i] + if int(colors[nei]-'a') == i { + val++ + } + mp[nei][i] = max(mp[nei][i], val) + } + inDegree[nei]-- + + if inDegree[nei] == 0 { + q = append(q, nei) + } + } + } + + if seen == n { + return res + } + + return -1 +} From 370c250a3c44094a0a21e0d67c12dd4feb39486c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 8 Apr 2023 18:11:37 -0700 Subject: [PATCH 745/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index e04e597..f572b79 100644 --- a/Hard.md +++ b/Hard.md @@ -70,3 +70,4 @@ |[2360](https://leetcode.com/problems/longest-cycle-in-a-graph/)| Longest Cycle in a Graph| |[1402](https://leetcode.com/problems/reducing-dishes/)| Reducing Dishes| |[87](https://leetcode.com/problems/scramble-string/)| Scramble String| +|[1857](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/)| Largest Color Value in a Directed Graph| From bd7a11c77771c802d981095ed37452c71270b3e3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Apr 2023 14:34:23 -0700 Subject: [PATCH 746/969] Create L516.go --- Medium/L516.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L516.go diff --git a/Medium/L516.go b/Medium/L516.go new file mode 100644 index 0000000..4a9e163 --- /dev/null +++ b/Medium/L516.go @@ -0,0 +1,37 @@ +func longestPalindromeSubseq(s string) int { + mp := make(map[string]int) + var solve func(left, right int) int + + solve = func(left, right int) int { + if left == right { + return 1 + } + + if left > right { + return 0 + } + + key := fmt.Sprintf("%d-%d", left, right) + if v, ok := mp[key]; ok { + return v + } + + if s[left] == s[right] { + mp[key] = 2 + solve(left+1, right-1) + } else { + mp[key] = max(solve(left+1, right), solve(left, right-1)) + } + + return mp[key] + } + + return solve(0, len(s) - 1) +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From d4d8788e2f5f9f884e4ac4ce8a49a24fa6dd0640 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Apr 2023 14:35:13 -0700 Subject: [PATCH 747/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index db2bcfa..ad7adfb 100644 --- a/Medium.md +++ b/Medium.md @@ -291,3 +291,4 @@ |[2405](https://leetcode.com/problems/optimal-partition-of-string/)| Optimal Partition of String| |[2300](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/)| Successful Pairs of Spells and Potions| |[2439](https://leetcode.com/problems/minimize-maximum-of-array/)| Minimize Maximum of Array| +|[516](https://leetcode.com/problems/longest-palindromic-subsequence/)| Longest Palindromic Subsequence| From 4b53947cfc0bbeb64eedad1f2fa8c3c15013a488 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 18 Apr 2023 15:55:58 -0700 Subject: [PATCH 748/969] Create L1768.go --- Easy/L1768.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L1768.go diff --git a/Easy/L1768.go b/Easy/L1768.go new file mode 100644 index 0000000..ce7213b --- /dev/null +++ b/Easy/L1768.go @@ -0,0 +1,22 @@ +package Easy + +func mergeAlternately(word1 string, word2 string) string { + i, j, n, m := 0, 0, len(word1), len(word2) + res := make([]byte, n + m) + + for k := 0; k < n + m; k++ { + if i < n && j < m { + res[k] = word1[i] + k, i = k + 1, i + 1 + } else if i < n { + res[k] = word1[i] + i++ + } + if j < m { + res[k] = word2[j] + j++ + } + } + + return string(res) +} From ec89efcacbc092db92370503766aff7a324aae05 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 18 Apr 2023 15:56:39 -0700 Subject: [PATCH 749/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index ac9834c..ddbfc8b 100644 --- a/Easy.md +++ b/Easy.md @@ -138,3 +138,4 @@ |[746](https://leetcode.com/problems/min-cost-climbing-stairs/)| Min Cost Climbing Stairs| |[1539](https://leetcode.com/problems/kth-missing-positive-number/)| Kth Missing Positive Number| |[101](https://leetcode.com/problems/symmetric-tree/)| Symmetric Tree| +|[1768](https://leetcode.com/problems/merge-strings-alternately/)| Merge Strings Alternately| From 5607725fe056ab4f701a5f1a863ae50c67738199 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Apr 2023 15:49:45 -0700 Subject: [PATCH 750/969] Create L879.go --- Hard/L879.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Hard/L879.go diff --git a/Hard/L879.go b/Hard/L879.go new file mode 100644 index 0000000..d403e9e --- /dev/null +++ b/Hard/L879.go @@ -0,0 +1,51 @@ +package Hard + +func profitableSchemes(n int, minProfit int, group []int, profit []int) int { + mod := int64(1000000007) + mp := make(map[string]int64) + var solve func(n, i, minimumProfit int) int64 + + solve = func(n, i, minimumProfit int) int64 { + var ( + ways int64 + zeroProfitKey = fmt.Sprintf("%d-%d-0", n, i) + key = fmt.Sprintf("%d-%d-%d", n, i, minimumProfit) + ) + + if i == len(group) { + if minimumProfit <= 0 { + return 1 + } + + return 0 + } + + if minimumProfit <= 0 { + if v, ok := mp[zeroProfitKey]; ok { + return v + } + } else if minimumProfit > 0 { + if v, ok := mp[key]; ok { + return v + } + } + + ways += solve(n, i+1, minimumProfit) + ways %= mod + + if n >= group[i] { + ways += solve(n-group[i], i+1, minimumProfit-profit[i]) + ways %= mod + } + + if minimumProfit <= 0 { + mp[zeroProfitKey] = ways % mod + } else { + mp[key] = ways % mod + } + + return ways % mod + } + + return int(solve(n, 0, minProfit)) +} From fda241582bf583025e27a5f6230d6a303f8234d8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Apr 2023 15:50:28 -0700 Subject: [PATCH 751/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index f572b79..9b3f8be 100644 --- a/Hard.md +++ b/Hard.md @@ -71,3 +71,4 @@ |[1402](https://leetcode.com/problems/reducing-dishes/)| Reducing Dishes| |[87](https://leetcode.com/problems/scramble-string/)| Scramble String| |[1857](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/)| Largest Color Value in a Directed Graph| +|[879](https://leetcode.com/problems/profitable-schemes/)| Profitable Schemes| From 0cf8b72996b687180d45c9e1f11a69d63ecc8ce5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Apr 2023 16:23:10 -0700 Subject: [PATCH 752/969] Create L319.go --- Medium/L319.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L319.go diff --git a/Medium/L319.go b/Medium/L319.go new file mode 100644 index 0000000..1dcc42e --- /dev/null +++ b/Medium/L319.go @@ -0,0 +1,19 @@ +package Medium + +func bulbSwitch(n int) int { + low, high := 0, n + + for low <= high { + mid := low + (high - low) / 2 + candidate := mid * mid + if candidate == n { + return mid + } else if candidate < n { + low = mid + 1 + } else { + high = mid - 1 + } + } + + return low - 1 +} From cea2b4c0db7cf069cc4e96fa6e488ee5507ca44d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 27 Apr 2023 16:23:49 -0700 Subject: [PATCH 753/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index ad7adfb..a7e94bf 100644 --- a/Medium.md +++ b/Medium.md @@ -292,3 +292,4 @@ |[2300](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/)| Successful Pairs of Spells and Potions| |[2439](https://leetcode.com/problems/minimize-maximum-of-array/)| Minimize Maximum of Array| |[516](https://leetcode.com/problems/longest-palindromic-subsequence/)| Longest Palindromic Subsequence| +|[319](https://leetcode.com/problems/bulb-switcher/)| Bulb Switcher| From 820d75ca202e2ea2021305bfcab559d85103ceea Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Apr 2023 16:36:44 -0700 Subject: [PATCH 754/969] Create L839.go --- Hard/L839.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Hard/L839.go diff --git a/Hard/L839.go b/Hard/L839.go new file mode 100644 index 0000000..de0db9e --- /dev/null +++ b/Hard/L839.go @@ -0,0 +1,51 @@ +package Hard + +func numSimilarGroups(strs []string) int { + n := len(strs) + parent := make([]int, n) + var ( + isSimilar func(s1, s2 string) bool + find func(x int) int + ) + + isSimilar = func(s1, s2 string) bool { + diff := 0 + for i := 0; i < len(s1); i++ { + if s1[i] != s2[i] { + diff++ + } + } + + return diff == 2 || diff == 0 + } + + find = func(x int) int { + if parent[x] != x { + parent[x] = find(parent[x]) + } + return parent[x] + } + + for i := 0; i < n; i++ { + parent[i] = i + } + for i := 0; i < n; i++ { + for j := i + 1; j < n; j++ { + if isSimilar(strs[i], strs[j]) { + p1, p2 := find(i), find(j) + if p1 != p2 { + parent[p2] = p1 + } + } + } + } + + res := 0 + for i := 0; i < n; i++ { + if parent[i] == i { + res++ + } + } + + return res +} From 9ce4eb55a6ab83df59e7715b09f66a74dfd193b3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Apr 2023 16:37:31 -0700 Subject: [PATCH 755/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 9b3f8be..07a494d 100644 --- a/Hard.md +++ b/Hard.md @@ -72,3 +72,4 @@ |[87](https://leetcode.com/problems/scramble-string/)| Scramble String| |[1857](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/)| Largest Color Value in a Directed Graph| |[879](https://leetcode.com/problems/profitable-schemes/)| Profitable Schemes| +|[839](https://leetcode.com/problems/similar-string-groups/)| Similar String Groups| From 4a890cce6e57bfc0a55e342ea6e4ecbf91c4e45f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 1 May 2023 16:25:24 -0700 Subject: [PATCH 756/969] Create L1491.go --- Easy/L1491.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Easy/L1491.go diff --git a/Easy/L1491.go b/Easy/L1491.go new file mode 100644 index 0000000..8aa3f23 --- /dev/null +++ b/Easy/L1491.go @@ -0,0 +1,30 @@ +package Easy + +func average(salary []int) float64 { + var sum float64 + minimum, maximum := math.MaxInt32, math.MinInt32 + + for i := 0; i < len(salary); i++ { + sum += float64(salary[i]) + minimum, maximum = min(minimum, salary[i]), max(maximum, salary[i]) + } + + sum -= float64(minimum + maximum) + return sum / float64(len(salary)-2) +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 32c812efbc28c03ce5e9fa3f25e948ba3a29578f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 1 May 2023 16:26:12 -0700 Subject: [PATCH 757/969] Update Easy.md --- Easy.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Easy.md b/Easy.md index ddbfc8b..855fcb0 100644 --- a/Easy.md +++ b/Easy.md @@ -139,3 +139,5 @@ |[1539](https://leetcode.com/problems/kth-missing-positive-number/)| Kth Missing Positive Number| |[101](https://leetcode.com/problems/symmetric-tree/)| Symmetric Tree| |[1768](https://leetcode.com/problems/merge-strings-alternately/)| Merge Strings Alternately| +|[1491](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)| Average Salary Excluding the Minimum and Maximum Salary| + From d93497019e596cb6b695461ffcbe12eb80a32574 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 May 2023 16:36:41 -0700 Subject: [PATCH 758/969] Create L1822.go --- Easy/L1822.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Easy/L1822.go diff --git a/Easy/L1822.go b/Easy/L1822.go new file mode 100644 index 0000000..170c4db --- /dev/null +++ b/Easy/L1822.go @@ -0,0 +1,14 @@ +package Easy + +func arraySign(nums []int) int { + sign := 1 + for _, n := range nums { + if n < 0 { + sign *= -1 + } else if n == 0 { + return 0 + } + } + + return sign +} From e2c581612fc9de1bb95b7eb207df05272e8f1424 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 May 2023 16:37:26 -0700 Subject: [PATCH 759/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 855fcb0..c42f054 100644 --- a/Easy.md +++ b/Easy.md @@ -140,4 +140,5 @@ |[101](https://leetcode.com/problems/symmetric-tree/)| Symmetric Tree| |[1768](https://leetcode.com/problems/merge-strings-alternately/)| Merge Strings Alternately| |[1491](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)| Average Salary Excluding the Minimum and Maximum Salary| +|[1822](https://leetcode.com/problems/sign-of-the-product-of-an-array/)| Sign of the Product of an Array| From 2cfca3272d92176ef2e2cc88362170e1ba3fbf20 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 May 2023 15:22:09 -0700 Subject: [PATCH 760/969] Create L2215.go --- Easy/L2215.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Easy/L2215.go diff --git a/Easy/L2215.go b/Easy/L2215.go new file mode 100644 index 0000000..3581603 --- /dev/null +++ b/Easy/L2215.go @@ -0,0 +1,35 @@ +package Easy + +var exists struct{} + +func findDifference(nums1 []int, nums2 []int) [][]int { + var res [][]int + st1, st2, i := make(map[int]struct{}), make(map[int]struct{}), 0 + + for i = 0; i < len(nums1); i++ { + st1[nums1[i]] = exists + if i < len(nums2) { + st2[nums2[i]] = exists + } + } + for ; i < len(nums2); i++ { + st2[nums2[i]] = exists + } + + var res1, res2 []int + for k, _ := range st1 { + if _, ok := st2[k]; ok { + continue + } + res1 = append(res1, k) + } + for k, _ := range st2 { + if _, ok := st1[k]; ok { + continue + } + res2 = append(res2, k) + } + + res = append(res, res1, res2) + return res +} From b043b6b53896238b432d7e650ea7d57df2df868e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 May 2023 15:22:50 -0700 Subject: [PATCH 761/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index c42f054..b1dd0b0 100644 --- a/Easy.md +++ b/Easy.md @@ -141,4 +141,5 @@ |[1768](https://leetcode.com/problems/merge-strings-alternately/)| Merge Strings Alternately| |[1491](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)| Average Salary Excluding the Minimum and Maximum Salary| |[1822](https://leetcode.com/problems/sign-of-the-product-of-an-array/)| Sign of the Product of an Array| +|[2215](https://leetcode.com/problems/find-the-difference-of-two-arrays/)| Find the Difference of Two Arrays| From 4153e4c34e653dd77cee71051ca4e94ae649a68d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 4 May 2023 15:57:42 -0700 Subject: [PATCH 762/969] Create L649.go --- Medium/L649.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L649.go diff --git a/Medium/L649.go b/Medium/L649.go new file mode 100644 index 0000000..9831a70 --- /dev/null +++ b/Medium/L649.go @@ -0,0 +1,29 @@ +package Medium + +func predictPartyVictory(senate string) string { + var q1, q2 []int + n := len(senate) + for i := 0; i < n; i++ { + if senate[i] == 'R' { + q1 = append(q1, i) + } else { + q2 = append(q2, i) + } + } + + for len(q1) > 0 && len(q2) > 0 { + rIndex, dIndex := q1[0], q2[0] + q1, q2 = q1[1:], q2[1:] + + if rIndex < dIndex { + q1 = append(q1, rIndex + n) + } else { + q2 = append(q2, dIndex + n) + } + } + if len(q1) > len(q2) { + return "Radiant" + } + + return "Dire" +} From 929387685577ab4411a5b2cea9f2bebe7010637b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 4 May 2023 15:58:20 -0700 Subject: [PATCH 763/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a7e94bf..dbdec53 100644 --- a/Medium.md +++ b/Medium.md @@ -293,3 +293,4 @@ |[2439](https://leetcode.com/problems/minimize-maximum-of-array/)| Minimize Maximum of Array| |[516](https://leetcode.com/problems/longest-palindromic-subsequence/)| Longest Palindromic Subsequence| |[319](https://leetcode.com/problems/bulb-switcher/)| Bulb Switcher| +|[649](https://leetcode.com/problems/dota2-senate/)| Dota2 Senate| From 19e559d36e87cc7a69dbb468398390db4832e166 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 May 2023 16:34:04 -0700 Subject: [PATCH 764/969] Create L1456.go --- Easy/L1456.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Easy/L1456.go diff --git a/Easy/L1456.go b/Easy/L1456.go new file mode 100644 index 0000000..bfe8430 --- /dev/null +++ b/Easy/L1456.go @@ -0,0 +1,32 @@ +package Easy + +var exists struct{} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} +func maxVowels(s string, k int) int { + vowels, ans, cnt := "aeiou", 0, 0 + st := make(map[byte]struct{}) + for i := range vowels { + st[vowels[i]] = exists + } + + for i := range s { + if _, ok := st[s[i]]; ok { + cnt++ + } + if i >= k { + if _, ok := st[s[i - k]]; ok { + cnt-- + } + } + ans = max(ans, cnt) + } + + return ans +} From dfa3abe1dce9e99bde641a2c80f35200d1f523c2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 May 2023 16:34:33 -0700 Subject: [PATCH 765/969] Create L1572.go --- Easy/L1572.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L1572.go diff --git a/Easy/L1572.go b/Easy/L1572.go new file mode 100644 index 0000000..c5d4f63 --- /dev/null +++ b/Easy/L1572.go @@ -0,0 +1,15 @@ +package Easy + +func diagonalSum(mat [][]int) int { + sum, n := 0, len(mat[0]) + + for i := 0; i < n; i++ { + sum += mat[i][i] + + if i != n - 1 - i { + sum += mat[i][n - 1 - i] + } + } + + return sum +} From 5ec4ea829a1d8efb1078a5220c5f1f5fcaa0fa26 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 May 2023 16:35:44 -0700 Subject: [PATCH 766/969] Update Easy.md --- Easy.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Easy.md b/Easy.md index b1dd0b0..6295452 100644 --- a/Easy.md +++ b/Easy.md @@ -142,4 +142,6 @@ |[1491](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)| Average Salary Excluding the Minimum and Maximum Salary| |[1822](https://leetcode.com/problems/sign-of-the-product-of-an-array/)| Sign of the Product of an Array| |[2215](https://leetcode.com/problems/find-the-difference-of-two-arrays/)| Find the Difference of Two Arrays| +|[1572](https://leetcode.com/problems/matrix-diagonal-sum/)| Matrix Diagonal Sum| +|[1456](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)| Maximum Number of Vowels in a Substring of Given Length| From 74d09e00e205028a1f24ae370b9d95d5819e1d73 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 22 May 2023 16:30:57 -0700 Subject: [PATCH 767/969] Create L934.go --- Medium/L934.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Medium/L934.go diff --git a/Medium/L934.go b/Medium/L934.go new file mode 100644 index 0000000..848751f --- /dev/null +++ b/Medium/L934.go @@ -0,0 +1,69 @@ +package Medium + +func shortestBridge(grid [][]int) int { + m, n := len(grid), len(grid[0]) + dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + visited := make([][]bool, m) + for i := 0; i < m; i++ { + visited[i] = make([]bool, n) + } + var ( + found bool + q [][]int + dfs func(x, y int) + isValid func(x, y int) bool + ) + + isValid = func(x, y int) bool { + return x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && grid[x][y] != 0 + } + + dfs = func(x, y int) { + if !isValid(x, y) { + return + } + + visited[x][y] = true + q = append(q, []int{x, y}) + for _, d := range dirs { + dfs(x+d[0], y+d[1]) + } + } + + for i := 0; i < m; i++ { + if found { + break + } + for j := 0; j < n; j++ { + if grid[i][j] == 1 { + dfs(i, j) + found = true + break + } + } + } + + step := 0 + for len(q) > 0 { + size := len(q) + for size > 0 { + cur := q[0] + q = q[1:] + size-- + + for _, d := range dirs { + x, y := cur[0]+d[0], cur[1]+d[1] + if x >= 0 && y >= 0 && x < m && y < n && !visited[x][y] { + if grid[x][y] == 1 { + return step + } + q = append(q, []int{x, y}) + visited[x][y] = true + } + } + } + step++ + } + + return -1 +} From cba448c9ed5f8757d8bad1ac498c91cf40dac522 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 22 May 2023 16:31:45 -0700 Subject: [PATCH 768/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index dbdec53..a1c7aa4 100644 --- a/Medium.md +++ b/Medium.md @@ -294,3 +294,4 @@ |[516](https://leetcode.com/problems/longest-palindromic-subsequence/)| Longest Palindromic Subsequence| |[319](https://leetcode.com/problems/bulb-switcher/)| Bulb Switcher| |[649](https://leetcode.com/problems/dota2-senate/)| Dota2 Senate| +|[934](https://leetcode.com/problems/shortest-bridge/)| Shortest Bridge| From 4c97d02a54d2df2375fc72d33986ed1ecab7655b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 7 Jun 2023 16:28:51 -0700 Subject: [PATCH 769/969] Create L1318.go --- Medium/L1318.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium/L1318.go diff --git a/Medium/L1318.go b/Medium/L1318.go new file mode 100644 index 0000000..316bb49 --- /dev/null +++ b/Medium/L1318.go @@ -0,0 +1,18 @@ +package Medium + +func minFlips(a int, b int, c int) int { + flips := 0 + for a > 0 || b > 0 || c > 0 { + bitA, bitB, bitC := a&1, b&1, c&1 + + if bitC == 0 { + flips += bitA + bitB + } else if bitA == 0 && bitB == 0 { + flips++ + } + + a, b, c = a>>1, b>>1, c>>1 + } + + return flips +} From e16c0a2081dca6303b4acbcda296ad78ef50a15d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 7 Jun 2023 16:29:46 -0700 Subject: [PATCH 770/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a1c7aa4..239b3ea 100644 --- a/Medium.md +++ b/Medium.md @@ -295,3 +295,4 @@ |[319](https://leetcode.com/problems/bulb-switcher/)| Bulb Switcher| |[649](https://leetcode.com/problems/dota2-senate/)| Dota2 Senate| |[934](https://leetcode.com/problems/shortest-bridge/)| Shortest Bridge| +|[1318](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/)| Minimum Flips to Make a OR b Equal to c| From 2b6cd9e4add8c5c8c138881a0945429e6423e257 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 9 Jun 2023 16:30:53 -0700 Subject: [PATCH 771/969] Create L744.go --- Medium/L744.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L744.go diff --git a/Medium/L744.go b/Medium/L744.go new file mode 100644 index 0000000..cf207de --- /dev/null +++ b/Medium/L744.go @@ -0,0 +1,20 @@ +package Easy + +func nextGreatestLetter(letters []byte, target byte) byte { + low, high := 0, len(letters) - 1 + + if target >= letters[len(letters) - 1] { + return letters[0] + } + + for low < high { + mid := low + (high - low) / 2 + if letters[mid] > target { + high = mid + } else { + low = mid + 1 + } + } + + return letters[low % len(letters)] +} From 0ede18417106ee7bb45f168cd67b618df604c64d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 9 Jun 2023 16:31:33 -0700 Subject: [PATCH 772/969] Rename L744.go to L744.go --- {Medium => Easy}/L744.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Medium => Easy}/L744.go (100%) diff --git a/Medium/L744.go b/Easy/L744.go similarity index 100% rename from Medium/L744.go rename to Easy/L744.go From bee18e87b09231d9b2427e37dcb6fb2bec9f8e1c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 9 Jun 2023 16:32:18 -0700 Subject: [PATCH 773/969] Update Easy.md --- Easy.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Easy.md b/Easy.md index 6295452..676a327 100644 --- a/Easy.md +++ b/Easy.md @@ -144,4 +144,4 @@ |[2215](https://leetcode.com/problems/find-the-difference-of-two-arrays/)| Find the Difference of Two Arrays| |[1572](https://leetcode.com/problems/matrix-diagonal-sum/)| Matrix Diagonal Sum| |[1456](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)| Maximum Number of Vowels in a Substring of Given Length| - +|[744](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)| Find Smallest Letter Greater Than Target| From 6692a12c35791247b26c58fc62bc2f13f2e29a1c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 13 Jun 2023 16:50:47 -0700 Subject: [PATCH 774/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 239b3ea..3a5c6ac 100644 --- a/Medium.md +++ b/Medium.md @@ -296,3 +296,4 @@ |[649](https://leetcode.com/problems/dota2-senate/)| Dota2 Senate| |[934](https://leetcode.com/problems/shortest-bridge/)| Shortest Bridge| |[1318](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/)| Minimum Flips to Make a OR b Equal to c| +|[2352](https://leetcode.com/problems/equal-row-and-column-pairs/)| Equal Row and Column Pairs| From 4cace203bd76e9256a95ae0c37d6c09f9777d830 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 13 Jun 2023 16:51:10 -0700 Subject: [PATCH 775/969] Create L2352.go --- Medium/L2352.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Medium/L2352.go diff --git a/Medium/L2352.go b/Medium/L2352.go new file mode 100644 index 0000000..ce79abf --- /dev/null +++ b/Medium/L2352.go @@ -0,0 +1,29 @@ +package Medium + +func equalPairs(grid [][]int) int { + pair, tmp, row := 0, 0, 0 + + for tmp <= len(grid)-1 { + mp := make(map[int]int) + for j := 0; j < len(grid); j++ { + mp[j] = grid[row][j] + } + for i := 0; i < len(grid); i++ { + curr := 0 + for k := 0; k < len(grid); k++ { + if v, ok := mp[k]; ok { + if v != grid[k][i] { + curr = 0 + break + } else { + curr = 1 + } + } + } + pair += curr + } + row, tmp = row+1, tmp+1 + } + + return pair +} From 49770d4b7af2788c0f4fb98e8f121bc12f079c69 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Jun 2023 17:00:02 -0700 Subject: [PATCH 776/969] Create L530.go --- Easy/L530.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Easy/L530.go diff --git a/Easy/L530.go b/Easy/L530.go new file mode 100644 index 0000000..ff78a39 --- /dev/null +++ b/Easy/L530.go @@ -0,0 +1,35 @@ +package Easy + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func getMinimumDifference(root *TreeNode) int { + var ( + solve func(root *TreeNode) int + ) + minimum, prev := math.MaxInt32, -1 + + solve = func(root *TreeNode) int { + if root == nil { + return minimum + } + + solve(root.Left) + + if prev != -1 { + minimum = min(minimum, root.Val-prev) + } + prev = root.Val + + solve(root.Right) + + return minimum + } + + return solve(root) +} From 7e9053d266cb307124767501ebf2a4ac57a73e2a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 14 Jun 2023 17:01:06 -0700 Subject: [PATCH 777/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 676a327..c5631c5 100644 --- a/Easy.md +++ b/Easy.md @@ -145,3 +145,4 @@ |[1572](https://leetcode.com/problems/matrix-diagonal-sum/)| Matrix Diagonal Sum| |[1456](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)| Maximum Number of Vowels in a Substring of Given Length| |[744](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)| Find Smallest Letter Greater Than Target| +|[530](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)| Minimum Absolute Difference in BST| From 5bd4909b195f0d940b27c46e2cb8f823c8e9cb4e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 15 Jun 2023 16:48:56 -0700 Subject: [PATCH 778/969] Create L1161.go --- Medium/L1161.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L1161.go diff --git a/Medium/L1161.go b/Medium/L1161.go new file mode 100644 index 0000000..d8f524c --- /dev/null +++ b/Medium/L1161.go @@ -0,0 +1,32 @@ +package Medium + +func maxLevelSum(root *TreeNode) int { + var q []*TreeNode + maximum, maxLevel, lvl := math.MinInt32, -1, 1 + + q = append(q, root) + for len(q) > 0 { + size := len(q) + sum := 0 + for i := 0; i < size; i++ { + node := q[0] + q = q[1:] + + sum += node.Val + + if node.Left != nil { + q = append(q, node.Left) + } + if node.Right != nil { + q = append(q, node.Right) + } + } + if sum > maximum { + maximum = sum + maxLevel = lvl + } + lvl++ + } + + return maxLevel +} From 780e4f412275a72fda40cbc3a0bc215e538e5111 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 10 Jul 2023 16:13:43 -0700 Subject: [PATCH 779/969] Create L111.go --- Easy/L111.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Easy/L111.go diff --git a/Easy/L111.go b/Easy/L111.go new file mode 100644 index 0000000..6d3f859 --- /dev/null +++ b/Easy/L111.go @@ -0,0 +1,41 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func minDepth(root *TreeNode) int { + if root == nil { + return 0 + } + + var q []*TreeNode + lvl := 1 + q = append(q, root) + + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + node := q[0] + q = q[1:] + + if node.Left == nil && node.Right == nil { + return lvl + } + + if node.Left != nil { + q = append(q, node.Left) + } + if node.Right != nil { + q = append(q, node.Right) + } + } + lvl++ + } + + return lvl +} From 2f3b491da58cf1d4535428a965da6d621e4515bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 10 Jul 2023 16:14:44 -0700 Subject: [PATCH 780/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index c5631c5..77bf533 100644 --- a/Easy.md +++ b/Easy.md @@ -146,3 +146,4 @@ |[1456](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)| Maximum Number of Vowels in a Substring of Given Length| |[744](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)| Find Smallest Letter Greater Than Target| |[530](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)| Minimum Absolute Difference in BST| +|[111](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| Minimum Depth of Binary Tree| From 01669b8816b390912ff99267400084a93cb08155 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jul 2023 07:37:33 -0700 Subject: [PATCH 781/969] Create L863.go --- Medium/L863.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/L863.go diff --git a/Medium/L863.go b/Medium/L863.go new file mode 100644 index 0000000..9c4931f --- /dev/null +++ b/Medium/L863.go @@ -0,0 +1,55 @@ +package Medium + +func distanceK(root *TreeNode, target *TreeNode, k int) []int { + var ( + res []int + find func(root, target *TreeNode) + search func(root *TreeNode, distance int) + ) + mp := make(map[*TreeNode]int) + + find = func(root, target *TreeNode) { + if root == nil { + return + } + + if root == target { + mp[root] = 0 + return + } + + find(root.Left, target) + if val, ok := mp[root.Left]; ok { + mp[root] = val + 1 + return + } + + find(root.Right, target) + if val, ok := mp[root.Right]; ok { + mp[root] = val + 1 + return + } + } + + search = func(root *TreeNode, distance int) { + if root == nil { + return + } + + if val, ok := mp[root]; ok { + distance = val + } + + if distance == k { + res = append(res, root.Val) + } + + search(root.Left, distance+1) + search(root.Right, distance+1) + } + + find(root, target) + search(root, 0) + + return res +} From b06f1796be431a5266ea14bbf58e8104b3d68080 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jul 2023 07:38:46 -0700 Subject: [PATCH 782/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 3a5c6ac..c73f075 100644 --- a/Medium.md +++ b/Medium.md @@ -297,3 +297,4 @@ |[934](https://leetcode.com/problems/shortest-bridge/)| Shortest Bridge| |[1318](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/)| Minimum Flips to Make a OR b Equal to c| |[2352](https://leetcode.com/problems/equal-row-and-column-pairs/)| Equal Row and Column Pairs| +|[863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K in Binary Tree| From 3c0994fca8349a42d6efe8353f6ec2bd3a8f6c12 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jul 2023 14:50:35 -0700 Subject: [PATCH 783/969] Create L2551.go --- Hard/L2551.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Hard/L2551.go diff --git a/Hard/L2551.go b/Hard/L2551.go new file mode 100644 index 0000000..ca91af2 --- /dev/null +++ b/Hard/L2551.go @@ -0,0 +1,17 @@ +package Hard + +func putMarbles(weights []int, k int) int64 { + n, pairs := len(weights), make([]int, len(weights)-1) + + for i := 1; i < n; i++ { + pairs[i-1] = weights[i] + weights[i-1] + } + sort.Ints(pairs) + minScore, maxScore := 0, 0 + for i := 0; i < k-1; i++ { + minScore += pairs[i] + maxScore += pairs[n-i-2] + } + + return int64(maxScore - minScore) +} From 5e84385e11b2dabce51ef28e816bb5c0eb67c3f6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 11 Jul 2023 14:53:25 -0700 Subject: [PATCH 784/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 07a494d..a542679 100644 --- a/Hard.md +++ b/Hard.md @@ -73,3 +73,4 @@ |[1857](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/)| Largest Color Value in a Directed Graph| |[879](https://leetcode.com/problems/profitable-schemes/)| Profitable Schemes| |[839](https://leetcode.com/problems/similar-string-groups/)| Similar String Groups| +|[2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles in Bags| From de1ccb136502ccd6ebaba9cb0d4f2ee87dd92ee7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 13 Jul 2023 16:49:28 -0700 Subject: [PATCH 785/969] Create L207.go --- Medium/L207.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L207.go diff --git a/Medium/L207.go b/Medium/L207.go new file mode 100644 index 0000000..9bf8c34 --- /dev/null +++ b/Medium/L207.go @@ -0,0 +1,46 @@ +package Medium + +func canFinish(numCourses int, prerequisites [][]int) bool { + var exists struct{} + if numCourses == 0 { + return false + } + + inDegree, mp := make([]int, numCourses), make(map[int][]int) + for i := 0; i < numCourses; i++ { + mp[i] = make([]int, 0) + } + + for _, p := range prerequisites { + mp[p[1]] = append(mp[p[1]], p[0]) + inDegree[p[0]]++ + } + + var q []int + for i := 0; i < len(inDegree); i++ { + if inDegree[i] == 0 { + q = append(q, i) + } + } + st := make(map[int]struct{}) + for len(q) > 0 { + size := len(q) + for i := 0; i < size; i++ { + c := q[0] + q = q[1:] + + if _, ok := st[c]; ok { + continue + } + st[c] = exists + for _, n := range mp[c] { + inDegree[n]-- + if inDegree[n] == 0 { + q = append(q, n) + } + } + } + } + + return len(st) == numCourses +} From 99734f1ba758f3a12380c4cd28b7b349ed98cfd3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 13 Jul 2023 16:50:13 -0700 Subject: [PATCH 786/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index c73f075..9ab07a9 100644 --- a/Medium.md +++ b/Medium.md @@ -298,3 +298,4 @@ |[1318](https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/)| Minimum Flips to Make a OR b Equal to c| |[2352](https://leetcode.com/problems/equal-row-and-column-pairs/)| Equal Row and Column Pairs| |[863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K in Binary Tree| +|[207](https://leetcode.com/problems/course-schedule/)| Course Schedule| From 2f2ab92f4766a042e954a35dd5930035b94cd0b6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jul 2023 16:46:04 -0700 Subject: [PATCH 787/969] Create L1218.go --- Medium/L1218.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L1218.go diff --git a/Medium/L1218.go b/Medium/L1218.go new file mode 100644 index 0000000..5dcd61e --- /dev/null +++ b/Medium/L1218.go @@ -0,0 +1,25 @@ +package Medium + +func longestSubsequence(arr []int, difference int) int { + mp, ans := make(map[int]int), 0 + + for i := range arr { + if v, ok := mp[arr[i]-difference]; ok { + ans = max(ans, v+1) + mp[arr[i]] = v + 1 + } else { + ans = max(ans, 1) + mp[arr[i]] = 1 + } + } + + return ans +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 1d94a46e9efd62b570196cf2b11a57ca1009640c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 14 Jul 2023 16:46:48 -0700 Subject: [PATCH 788/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 9ab07a9..16b16fd 100644 --- a/Medium.md +++ b/Medium.md @@ -299,3 +299,4 @@ |[2352](https://leetcode.com/problems/equal-row-and-column-pairs/)| Equal Row and Column Pairs| |[863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K in Binary Tree| |[207](https://leetcode.com/problems/course-schedule/)| Course Schedule| +|[1218](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/)| Longest Arithmetic Subsequence of Given Difference| From d4e646ca2c679772c2d2d0babd19680265eae11d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 20 Jul 2023 12:14:10 -0700 Subject: [PATCH 789/969] Create L735.go --- Medium/L735.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L735.go diff --git a/Medium/L735.go b/Medium/L735.go new file mode 100644 index 0000000..96ff228 --- /dev/null +++ b/Medium/L735.go @@ -0,0 +1,27 @@ +package Medium + +func asteroidCollision(asteroids []int) []int { + var res []int + if len(asteroids) == 0 { + return res + } + + for _, a := range asteroids { + if a > 0 { + res = append(res, a) + } else { + for len(res) > 0 && + res[len(res) - 1] > 0 && + -a > res[len(res) - 1] { + res = res[:len(res) - 1] + } + if len(res) == 0 || res[len(res) - 1] < 0 { + res = append(res, a) + } else if res[len(res) - 1] == -a { + res = res[:len(res) - 1] + } + } + } + + return res +} From 4c2a8b27180954ef6abc2e4b82c68349c0c9d1e5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 20 Jul 2023 12:14:57 -0700 Subject: [PATCH 790/969] Update Medium.md --- Medium.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Medium.md b/Medium.md index 16b16fd..c197940 100644 --- a/Medium.md +++ b/Medium.md @@ -300,3 +300,6 @@ |[863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K in Binary Tree| |[207](https://leetcode.com/problems/course-schedule/)| Course Schedule| |[1218](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/)| Longest Arithmetic Subsequence of Given Difference| +|[735](https://leetcode.com/problems/asteroid-collision/)| Asteroid Collision +| + From f96033cd4c4ba4a3f4e79c0a5f62e3d3a2a78c2a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jul 2023 16:28:20 -0700 Subject: [PATCH 791/969] Create L673.go --- Medium/L673.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Medium/L673.go diff --git a/Medium/L673.go b/Medium/L673.go new file mode 100644 index 0000000..e019848 --- /dev/null +++ b/Medium/L673.go @@ -0,0 +1,62 @@ +package Medium + +type Pair struct { + l, c int +} + +func newPair(a, b int) *Pair { + return &Pair{ + l: a, + c: b, + } +} + +func findNumberOfLIS(nums []int) int { + var ( + solve func(idx int) *Pair + maxL, maxC int + ) + dp := make(map[int]*Pair) + + solve = func(idx int) *Pair { + if idx == len(nums) { + return newPair(0, 0) + } + if v, ok := dp[idx]; ok { + return v + } + + ml, mc := 0, 0 + for i := idx + 1; i < len(nums); i++ { + if nums[i] > nums[idx] { + tmp := solve(i) + if tmp.l > ml { + ml = tmp.l + mc = tmp.c + } else if tmp.l == ml { + mc += tmp.c + } + } + } + + if ml == 0 && mc == 0 { + dp[idx] = newPair(1, 1) + return dp[idx] + } + + dp[idx] = newPair(1+ml, mc) + return dp[idx] + } + + for i := range nums { + tmpPair := solve(i) + if tmpPair.l > maxL { + maxL = tmpPair.l + maxC = tmpPair.c + } else if tmpPair.l == maxL { + maxC += tmpPair.c + } + } + + return maxC +} From 1448cd8c8a6d506dce03383e62dab4d6157cc1aa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 21 Jul 2023 16:29:21 -0700 Subject: [PATCH 792/969] Update Medium.md --- Medium.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Medium.md b/Medium.md index c197940..6890264 100644 --- a/Medium.md +++ b/Medium.md @@ -300,6 +300,6 @@ |[863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)| All Nodes Distance K in Binary Tree| |[207](https://leetcode.com/problems/course-schedule/)| Course Schedule| |[1218](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/)| Longest Arithmetic Subsequence of Given Difference| -|[735](https://leetcode.com/problems/asteroid-collision/)| Asteroid Collision -| +|[735](https://leetcode.com/problems/asteroid-collision/)| Asteroid Collision| +|[673](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)| Number of Longest Increasing Subsequence| From 97cabe40c2319098b8bdebb46b2f2069fa894d2d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 24 Jul 2023 07:32:47 -0700 Subject: [PATCH 793/969] Create L688.go --- Medium/L688.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L688.go diff --git a/Medium/L688.go b/Medium/L688.go new file mode 100644 index 0000000..9ee9ed2 --- /dev/null +++ b/Medium/L688.go @@ -0,0 +1,31 @@ +package Medium + +func knightProbability(n int, k int, row int, column int) float64 { + var ( + solve func(K, r, c int) float64 + dir = [][]int{{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1}} + mp = make(map[string]float64) + ) + + solve = func(K, r, c int) float64 { + if r < 0 || r > n-1 || c < 0 || c > n-1 { + return 0 + } + if K == 0 { + return 1 + } + key := fmt.Sprintf("%d-%d-%d", r, c, K) + if v, ok := mp[key]; ok { + return v + } + var rate float64 + for _, d := range dir { + rate += 0.125 * solve(K - 1, r + d[0], c + d[1]) + } + + mp[key] = rate + return mp[key] + } + + return solve(k, row, column) +} From 093f48abaae0339c6e0d62b7d4d509f3d37ea157 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 24 Jul 2023 07:33:29 -0700 Subject: [PATCH 794/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 6890264..655f6fd 100644 --- a/Medium.md +++ b/Medium.md @@ -302,4 +302,5 @@ |[1218](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/)| Longest Arithmetic Subsequence of Given Difference| |[735](https://leetcode.com/problems/asteroid-collision/)| Asteroid Collision| |[673](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)| Number of Longest Increasing Subsequence| +|[688](https://leetcode.com/problems/knight-probability-in-chessboard/)| Knight Probability in Chessboard| From 6201e155c5395d90455731a6dbe9b25dbce6a7da Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Jul 2023 14:20:11 -0700 Subject: [PATCH 795/969] Create L852.go --- Medium/L852.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Medium/L852.go diff --git a/Medium/L852.go b/Medium/L852.go new file mode 100644 index 0000000..008d975 --- /dev/null +++ b/Medium/L852.go @@ -0,0 +1,16 @@ +package Medium + +func peakIndexInMountainArray(arr []int) int { + low, high := 0, len(arr)-1 + + for low <= high { + mid := low + (high-low)/2 + if arr[mid] < arr[mid+1] { + low = mid + 1 + } else { + high = mid - 1 + } + } + + return low +} From 96829d890f20d9040d74bf33c34ba71ebd4a8681 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 25 Jul 2023 14:21:25 -0700 Subject: [PATCH 796/969] Update Medium.md --- Medium.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Medium.md b/Medium.md index 655f6fd..06c39af 100644 --- a/Medium.md +++ b/Medium.md @@ -303,4 +303,4 @@ |[735](https://leetcode.com/problems/asteroid-collision/)| Asteroid Collision| |[673](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)| Number of Longest Increasing Subsequence| |[688](https://leetcode.com/problems/knight-probability-in-chessboard/)| Knight Probability in Chessboard| - +|[852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| Peak Index in a Mountain Array| From acc25d998832e4e0ca0cb8f7368c17bfa2e0ed1e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Jul 2023 16:45:53 -0700 Subject: [PATCH 797/969] Create L1870.go --- Medium/L1870.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L1870.go diff --git a/Medium/L1870.go b/Medium/L1870.go new file mode 100644 index 0000000..d081ba2 --- /dev/null +++ b/Medium/L1870.go @@ -0,0 +1,34 @@ +package Medium + +func minSpeedOnTime(dist []int, hour float64) int { + var timeTaken func(speed int) float64 + left, right, res := 1, 10000007, -1 + + timeTaken = func(speed int) float64 { + var ans float64 + + for i := 0; i < len(dist); i++ { + if i == len(dist)-1 { + ans += float64(dist[i]) / float64(speed) + } else { + ans += math.Ceil(float64(dist[i]) / float64(speed)) + } + } + + return ans + } + + for left <= right { + mid := right - (right-left)/2 + dur := timeTaken(mid) + + if dur <= hour { + res = mid + right = mid - 1 + } else { + left = mid + 1 + } + } + + return res +} From 4c0f8b5581c1e3fa642f371ea2d7018838b1fcb5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Jul 2023 16:46:39 -0700 Subject: [PATCH 798/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 06c39af..56ddb10 100644 --- a/Medium.md +++ b/Medium.md @@ -304,3 +304,4 @@ |[673](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)| Number of Longest Increasing Subsequence| |[688](https://leetcode.com/problems/knight-probability-in-chessboard/)| Knight Probability in Chessboard| |[852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| Peak Index in a Mountain Array| +|[852](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/)| Minimum Speed to Arrive on Time| From fdfb705bf00091dee050fb45c9a8d6611adaa748 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jul 2023 16:04:41 -0700 Subject: [PATCH 799/969] Create L486.go --- Medium/L486.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L486.go diff --git a/Medium/L486.go b/Medium/L486.go new file mode 100644 index 0000000..b6e8406 --- /dev/null +++ b/Medium/L486.go @@ -0,0 +1,45 @@ +package Medium + +func PredictTheWinner(nums []int) bool { + var solve func(i, j, c int) int + mp := make(map[string]int) + + solve = func(i, j, c int) int { + if j < i { + return 0 + } + + key := fmt.Sprintf("%d-%d-%d", i, j, c) + if v, ok := mp[key]; ok { + return v + } + + res := 0 + if c == 0 { + res = max(solve(i+1, j, 1-c)+nums[i], solve(i, j-1, 1-c)+nums[j]) + } else { + res = min(solve(i+1, j, 1-c)-nums[i], solve(i, j-1, 1-c)-nums[j]) + } + + return res + } + + ans := solve(0, len(nums)-1, 0) + return ans >= 0 +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 6e72e7d000c4f66f960608e47914d33b116fb6dc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jul 2023 16:05:32 -0700 Subject: [PATCH 800/969] Create L2141.go --- Hard/L2141.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Hard/L2141.go diff --git a/Hard/L2141.go b/Hard/L2141.go new file mode 100644 index 0000000..ea4f548 --- /dev/null +++ b/Hard/L2141.go @@ -0,0 +1,47 @@ +package Hard + +func maxRunTime(n int, batteries []int) int64 { + var ( + canFit func(k int64) bool + lower, upper, res, batSum int64 + ) + + for _, b := range batteries { + batSum += int64(b) + } + lower, upper, res = 0, batSum/int64(n), -1 + + canFit = func(k int64) bool { + var ( + currBatSum, target int64 + ) + + target = int64(n) * k + for _, b := range batteries { + if int64(b) < k { + currBatSum += int64(b) + } else { + currBatSum += k + } + + if currBatSum >= target { + return true + } + } + + return currBatSum >= target + } + + for lower <= upper { + var mid int64 + mid = lower + (upper - lower) / 2 + + if canFit(mid) { + res, lower = mid, mid + 1 + } else { + upper = mid - 1 + } + } + + return res +} From 0910d22a4381203eb33796630308a8b95d955507 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jul 2023 16:06:23 -0700 Subject: [PATCH 801/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 56ddb10..8593d67 100644 --- a/Medium.md +++ b/Medium.md @@ -305,3 +305,4 @@ |[688](https://leetcode.com/problems/knight-probability-in-chessboard/)| Knight Probability in Chessboard| |[852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| Peak Index in a Mountain Array| |[852](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/)| Minimum Speed to Arrive on Time| +|[486](https://leetcode.com/problems/predict-the-winner/)| Predict the Winner| From e04ffddb9579053e413b735e84c2a8458998fa80 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 28 Jul 2023 16:07:00 -0700 Subject: [PATCH 802/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index a542679..528aa39 100644 --- a/Hard.md +++ b/Hard.md @@ -74,3 +74,4 @@ |[879](https://leetcode.com/problems/profitable-schemes/)| Profitable Schemes| |[839](https://leetcode.com/problems/similar-string-groups/)| Similar String Groups| |[2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles in Bags| +|[2141](https://leetcode.com/problems/maximum-running-time-of-n-computers/)| Maximum Running Time of N Computers| From 7c86c9eee6acec990a473bc067e1aeb612bb0c7d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 29 Jul 2023 09:53:48 -0700 Subject: [PATCH 803/969] Create L808.go --- Medium/L808.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Medium/L808.go diff --git a/Medium/L808.go b/Medium/L808.go new file mode 100644 index 0000000..99c5811 --- /dev/null +++ b/Medium/L808.go @@ -0,0 +1,34 @@ +package Medium + +func soupServings(n int) float64 { + if n >= 5000 { + return 1.0 + } + + var solve func(a, b int) float64 + mp := make(map[string]float64) + + solve = func(a, b int) float64 { + if a <= 0 && b > 0 { + return 1.0 + } + + if a <= 0 && b <= 0 { + return 0.5 + } + + if a > 0 && b <= 0 { + return 0.0 + } + + key := fmt.Sprintf("%d-%d", a, b) + if v, ok := mp[key]; ok { + return v + } + + mp[key] = 0.25 * (solve(a-100, b) + solve(a-75, b-25) + solve(a-50, b-50) + solve(a-25, b-75)) + return mp[key] + } + + return solve(n, n) +} From f3899667310978eb4f5ef5e22f92294a089eb5b2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 29 Jul 2023 09:54:49 -0700 Subject: [PATCH 804/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 8593d67..04eb376 100644 --- a/Medium.md +++ b/Medium.md @@ -306,3 +306,4 @@ |[852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| Peak Index in a Mountain Array| |[852](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/)| Minimum Speed to Arrive on Time| |[486](https://leetcode.com/problems/predict-the-winner/)| Predict the Winner| +|[808](https://leetcode.com/problems/soup-servings/)| Soup Servings| From f8694d01f8d2d9124b3289d8905c1bb47749fbf5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 30 Jul 2023 16:57:34 -0700 Subject: [PATCH 805/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 528aa39..17d10bb 100644 --- a/Hard.md +++ b/Hard.md @@ -75,3 +75,4 @@ |[839](https://leetcode.com/problems/similar-string-groups/)| Similar String Groups| |[2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles in Bags| |[2141](https://leetcode.com/problems/maximum-running-time-of-n-computers/)| Maximum Running Time of N Computers| +|[664](https://leetcode.com/problems/strange-printer/)| Strange Printer| From 38f0c0f5775772a0875528dcdebe61bbcfd9adfe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 30 Jul 2023 16:58:00 -0700 Subject: [PATCH 806/969] Create L664.go --- Hard/L664.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Hard/L664.go diff --git a/Hard/L664.go b/Hard/L664.go new file mode 100644 index 0000000..a94af1f --- /dev/null +++ b/Hard/L664.go @@ -0,0 +1,39 @@ +package Hard + +func strangePrinter(s string) int { + var ( + solve func(left, right int) int + ) + mp := make(map[string]int) + + solve = func(left, right int) int { + if left == right { + return 1 + } + key := fmt.Sprintf("%d-%d", left, right) + if v, ok := mp[key]; ok { + return v + } + + res := math.MaxInt32 + for k := left; k < right; k++ { + res = min(res, solve(left, k)+solve(k+1, right)) + } + + if s[left] == s[right] { + res-- + } + mp[key] = res + return mp[key] + } + + return solve(0, len(s)-1) +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From cf343b2d98fca8f1790551c9fb0e4ce38e9f0a2e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 31 Jul 2023 16:23:23 -0700 Subject: [PATCH 807/969] Create L712.go --- Medium/L712.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Medium/L712.go diff --git a/Medium/L712.go b/Medium/L712.go new file mode 100644 index 0000000..c85636d --- /dev/null +++ b/Medium/L712.go @@ -0,0 +1,54 @@ +package Medium + +func minimumDeleteSum(s1 string, s2 string) int { + var ( + solve func(i, j int) int + deadEndSum func(str string, i int) int + ) + mp, m, n := make(map[string]int), len(s1), len(s2) + + deadEndSum = func(str string, i int) int { + sum := 0 + for ; i < len(str); i++ { + sum += int(str[i]) + } + + return sum + } + + solve = func(i, j int) int { + sum := 0 + if i == m && j == n { + return 0 + } + if i == m { + return deadEndSum(s2, j) + } else if j == n { + return deadEndSum(s1, i) + } + + key := fmt.Sprintf("%d-%d", i, j) + if v, ok := mp[key]; ok { + return v + } + + if s1[i] == s2[j] { + sum = solve(i+1, j+1) + } else { + sum = min(solve(i+1, j)+int(s1[i]), solve(i, j+1)+int(s2[j])) + } + + mp[key] = sum + return mp[key] + } + + return solve(0, 0) +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From 9733a83203fdaa97ce6351b2fba53e0e27e1bf50 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 31 Jul 2023 16:24:44 -0700 Subject: [PATCH 808/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 04eb376..3baf3d0 100644 --- a/Medium.md +++ b/Medium.md @@ -307,3 +307,4 @@ |[852](https://leetcode.com/problems/minimum-speed-to-arrive-on-time/)| Minimum Speed to Arrive on Time| |[486](https://leetcode.com/problems/predict-the-winner/)| Predict the Winner| |[808](https://leetcode.com/problems/soup-servings/)| Soup Servings| +|[712](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)| Minimum ASCII Delete Sum for Two Strings| From 892ee54c53c4ed3f48d72cf3e2303e6795b36613 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 1 Aug 2023 11:55:34 -0700 Subject: [PATCH 809/969] Create L77.go --- Medium/L77.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L77.go diff --git a/Medium/L77.go b/Medium/L77.go new file mode 100644 index 0000000..5ae99a4 --- /dev/null +++ b/Medium/L77.go @@ -0,0 +1,27 @@ +package Medium + +func combine(n int, k int) [][]int { + var ( + solve func(tmp *[]int, start, k int) + res [][]int + tmp []int + ) + + solve = func(tmp *[]int, start, k int) { + if k == 0 { + tmpCopy := make([]int, len(*tmp)) + copy(tmpCopy, *tmp) + res = append(res, tmpCopy) + return + } + + for i := start; i <= n; i++ { + *tmp = append(*tmp, i) + solve(tmp, i+1, k-1) + *tmp = (*tmp)[:len(*tmp)-1] + } + } + + solve(&tmp, 1, k) + return res +} From 9598d812aac31bd4bb3cf2ed30bb2b887914411b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 1 Aug 2023 11:56:22 -0700 Subject: [PATCH 810/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 3baf3d0..4c5656f 100644 --- a/Medium.md +++ b/Medium.md @@ -308,3 +308,4 @@ |[486](https://leetcode.com/problems/predict-the-winner/)| Predict the Winner| |[808](https://leetcode.com/problems/soup-servings/)| Soup Servings| |[712](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)| Minimum ASCII Delete Sum for Two Strings| +|[77](https://leetcode.com/problems/combinations/)| Combinations| From 02e3f24d92bf291740e2103d57aa4bd204292da8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Aug 2023 19:00:38 -0700 Subject: [PATCH 811/969] Create L46.go --- Medium/L46.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L46.go diff --git a/Medium/L46.go b/Medium/L46.go new file mode 100644 index 0000000..072aaeb --- /dev/null +++ b/Medium/L46.go @@ -0,0 +1,37 @@ +package Medium + +func permute(nums []int) [][]int { + var ( + ans [][]int + tmp []int + solve func(tmp []int) + ) + if len(nums) == 0 { + return ans + } + + mp := make(map[int]bool) + solve = func(tmp []int) { + if len(tmp) == len(nums) { + tmpCopy := make([]int, len(tmp)) + copy(tmpCopy, tmp) + ans = append(ans, tmpCopy) + return + } + + for i := 0; i < len(nums); i++ { + if mp[nums[i]] { + continue + } + tmp = append(tmp, nums[i]) + mp[nums[i]] = true + solve(tmp) + delElem := tmp[len(tmp) - 1] + tmp = tmp[:len(tmp) - 1] + delete(mp, delElem) + } + } + + solve(tmp) + return ans +} From b8b221209a583e9941aeffd0f4608424559dd538 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 3 Aug 2023 19:01:15 -0700 Subject: [PATCH 812/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 4c5656f..74d1d4e 100644 --- a/Medium.md +++ b/Medium.md @@ -309,3 +309,4 @@ |[808](https://leetcode.com/problems/soup-servings/)| Soup Servings| |[712](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)| Minimum ASCII Delete Sum for Two Strings| |[77](https://leetcode.com/problems/combinations/)| Combinations| +|[46](https://leetcode.com/problems/permutations/)| Permutations| From 0546e05e065c6b5ff1ebf00fd4bda0cbb141493c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Aug 2023 14:36:52 -0700 Subject: [PATCH 813/969] Create L33.go --- Medium/L33.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L33.go diff --git a/Medium/L33.go b/Medium/L33.go new file mode 100644 index 0000000..7625c15 --- /dev/null +++ b/Medium/L33.go @@ -0,0 +1,45 @@ +package Medium + +func search(nums []int, target int) int { + if len(nums) == 0 { + return -1 + } + + var rotationCnt func() int + + rotationCnt = func() int { + l, r := 0, len(nums)-1 + + for l < r { + mid := l + (r-l)/2 + if mid < r && nums[mid] > nums[mid+1] { + return mid + 1 + } + if mid > l && nums[mid] < nums[mid-1] { + return mid + } + if nums[mid] < nums[r] { + r = mid - 1 + } else { + l = mid + 1 + } + } + + return l + } + rotCnt := rotationCnt() + low, high := 0, len(nums)-1 + for low <= high { + mid := low + (high-low)/2 + realMid := (mid + rotCnt) % len(nums) + if nums[realMid] == target { + return realMid + } else if nums[realMid] < target { + low = mid + 1 + } else { + high = mid - 1 + } + } + + return -1 +} From 768fc0a3623e84a3c3a9f8ad717ff93d0331a556 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 8 Aug 2023 14:37:52 -0700 Subject: [PATCH 814/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 74d1d4e..06f8ca3 100644 --- a/Medium.md +++ b/Medium.md @@ -310,3 +310,4 @@ |[712](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)| Minimum ASCII Delete Sum for Two Strings| |[77](https://leetcode.com/problems/combinations/)| Combinations| |[46](https://leetcode.com/problems/permutations/)| Permutations| +|[33](https://leetcode.com/problems/search-in-rotated-sorted-array/)| Search in Rotated Sorted Array| From a7caaef6e31ba661cd0b1bce4ba38367f3eeb910 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Aug 2023 16:22:49 -0700 Subject: [PATCH 815/969] Create L239.go --- Hard/L239.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Hard/L239.go diff --git a/Hard/L239.go b/Hard/L239.go new file mode 100644 index 0000000..2770efa --- /dev/null +++ b/Hard/L239.go @@ -0,0 +1,28 @@ +package Hard + +func maxSlidingWindow(nums []int, k int) []int { + if len(nums) == 0 || k <= 0 { + return []int{0} + } + + var ( + res, q []int + ) + n := len(nums) + + for i := 0; i < n; i++ { + for len(q) > 0 && q[0] < (i-k+1) { + q = q[1:] + } + for len(q) > 0 && nums[q[len(q)-1]] < nums[i] { + q = q[:len(q)-1] + } + q = append(q, i) + + if i >= k-1 { + res = append(res, nums[q[0]]) + } + } + + return res +} From 10bc3f4cc454571122c78a59049442b96ed3e840 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 16 Aug 2023 16:23:40 -0700 Subject: [PATCH 816/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 17d10bb..89c3982 100644 --- a/Hard.md +++ b/Hard.md @@ -76,3 +76,4 @@ |[2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles in Bags| |[2141](https://leetcode.com/problems/maximum-running-time-of-n-computers/)| Maximum Running Time of N Computers| |[664](https://leetcode.com/problems/strange-printer/)| Strange Printer| +|[239](https://leetcode.com/problems/sliding-window-maximum/)| Sliding Window Maximum| From 2902ba1c0c8bb7e7e315e9fb59599a13c48435d6 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Aug 2023 16:39:10 -0700 Subject: [PATCH 817/969] Create L542.go --- Medium/L542.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Medium/L542.go diff --git a/Medium/L542.go b/Medium/L542.go new file mode 100644 index 0000000..d6e45c8 --- /dev/null +++ b/Medium/L542.go @@ -0,0 +1,61 @@ +package Medium + +func updateMatrix(mat [][]int) [][]int { + var ( + res, q [][]int + isValid func(x, y int) bool + ) + if len(mat) == 0 { + return res + } + + dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + m, n := len(mat), len(mat[0]) + vis := make([][]bool, m) + for i := 0; i < m; i++ { + vis[i] = make([]bool, n) + } + + isValid = func(x, y int) bool { + return x >= 0 && y >= 0 && x < m && y < n && mat[x][y] == math.MaxInt32 + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if mat[i][j] == 0 { + q = append(q, []int{i, j}) + } else { + mat[i][j] = math.MaxInt32 + } + } + } + + for len(q) > 0 { + c := q[0] + q = q[1:] + + if vis[c[0]][c[1]] { + continue + } + vis[c[0]][c[1]] = true + + for _, d := range dirs { + newX, newY := c[0]+d[0], c[1]+d[1] + if isValid(newX, newY) { + mat[newX][newY] = min(mat[newX][newY], mat[c[0]][c[1]]+1) + q = append(q, []int{newX, newY}) + } + } + } + + return mat +} + + +func min(a, b int) int { + if a < b { + return a + } + + return b +} From f484c2a0d1bf084a1aae77098daf8b2ad5e3cf72 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 17 Aug 2023 16:39:54 -0700 Subject: [PATCH 818/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 06f8ca3..73a9cc5 100644 --- a/Medium.md +++ b/Medium.md @@ -311,3 +311,4 @@ |[77](https://leetcode.com/problems/combinations/)| Combinations| |[46](https://leetcode.com/problems/permutations/)| Permutations| |[33](https://leetcode.com/problems/search-in-rotated-sorted-array/)| Search in Rotated Sorted Array| +|[542](https://leetcode.com/problems/01-matrix/)| 01 Matrix| From c1dffcd2742863a2eee12d492bde043e2131fbaa Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Aug 2023 07:36:12 -0700 Subject: [PATCH 819/969] Create L1615.go --- Medium/L1615.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Medium/L1615.go diff --git a/Medium/L1615.go b/Medium/L1615.go new file mode 100644 index 0000000..ae96757 --- /dev/null +++ b/Medium/L1615.go @@ -0,0 +1,35 @@ +package Medium + +func maximalNetworkRank(n int, roads [][]int) int { + connected, cnt := make([][]bool, n), make([]int, n) + for i := 0; i < n; i++ { + connected[i] = make([]bool, n) + } + + for _, r := range roads { + cnt[r[0]]++ + cnt[r[1]]++ + connected[r[0]][r[1]], connected[r[1]][r[0]] = true, true + } + + res := 0 + for i := 0; i < n; i++ { + for j := i + 1; j < n; j++ { + if connected[i][j] { + res = max(res, cnt[i]+cnt[j]-1) + } else { + res = max(res, cnt[i]+cnt[j]) + } + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From cbfde68ac5e78dfb9dd1acb3327da5541baeab0a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 18 Aug 2023 07:37:12 -0700 Subject: [PATCH 820/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 73a9cc5..ba252a2 100644 --- a/Medium.md +++ b/Medium.md @@ -312,3 +312,4 @@ |[46](https://leetcode.com/problems/permutations/)| Permutations| |[33](https://leetcode.com/problems/search-in-rotated-sorted-array/)| Search in Rotated Sorted Array| |[542](https://leetcode.com/problems/01-matrix/)| 01 Matrix| +|[1615](https://leetcode.com/problems/maximal-network-rank/)| Maximal Network Rank| From ba4a82c17ddf9c33d2608603615c78a8d152470c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Aug 2023 12:51:23 -0700 Subject: [PATCH 821/969] Create L767.go --- Medium/L767.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Medium/L767.go diff --git a/Medium/L767.go b/Medium/L767.go new file mode 100644 index 0000000..bbb2787 --- /dev/null +++ b/Medium/L767.go @@ -0,0 +1,51 @@ +package Medium + +type Pair struct { + Key byte + Value int +} + +type PairList []Pair + +func (p PairList) Len() int { return len(p) } +func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p PairList) Less(i, j int) bool { return p[i].Value > p[j].Value } + +func reorganizeString(s string) string { + freqMap, res := make(map[byte]int), make([]byte, len(s)) + for _, ch := range s { + freqMap[byte(ch)]++ + } + + getSortedChars := func() PairList { + p := make(PairList, len(freqMap)) + i := 0 + for k, v := range freqMap { + p[i] = Pair{ + Key: k, + Value: v, + } + i++ + } + sort.Sort(p) + + return p + } + + sortedChars := getSortedChars() + if freqMap[sortedChars[0].Key] > ((len(s) + 1) / 2) { + return "" + } + + idx := 0 + for _, pair := range sortedChars { + for j := 0; j < freqMap[pair.Key]; j++ { + if idx >= len(s) { + idx = 1 + } + res[idx] = pair.Key + idx += 2 + } + } + return string(res) +} From 05a03a06c0eb776ce8eb6feadade763fe1a5a873 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Aug 2023 12:52:10 -0700 Subject: [PATCH 822/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index ba252a2..ee0ebcc 100644 --- a/Medium.md +++ b/Medium.md @@ -313,3 +313,4 @@ |[33](https://leetcode.com/problems/search-in-rotated-sorted-array/)| Search in Rotated Sorted Array| |[542](https://leetcode.com/problems/01-matrix/)| 01 Matrix| |[1615](https://leetcode.com/problems/maximal-network-rank/)| Maximal Network Rank| +|[767](https://leetcode.com/problems/reorganize-string/)| Reorganize String| From 22511b042b0fb3947f76508384b46e452089a662 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Aug 2023 13:10:30 -0700 Subject: [PATCH 823/969] Create L68.go --- Hard/L68.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Hard/L68.go diff --git a/Hard/L68.go b/Hard/L68.go new file mode 100644 index 0000000..8b362ff --- /dev/null +++ b/Hard/L68.go @@ -0,0 +1,89 @@ +package Hard + +func fullJustify(words []string, maxWidth int) []string { + var res []string + left := 0 + + for left < len(words) { + right := findRight(words, left, maxWidth) + res = append(res, justify(words, left, right, maxWidth)) + left = right + 1 + } + + return res +} + +func findRight(words []string, left, maxWidth int) int { + right := left + sum := len(words[right]) + right++ + + for right < len(words) && (sum + 1 + len(words[right])) <= maxWidth { + sum += 1 + len(words[right]) + right++ + } + + return right - 1 +} + +func justify(words []string, left, right, maxWidth int) string { + if right - left == 0 { + return padResult(words[left], maxWidth) + } + + lastLine := isLastLine(words, right) + numSpaces := right - left + totalSpace := maxWidth - wordsLength(words, left, right) + + space, remainder := " ", 0 + if !lastLine { + space = blank(totalSpace / numSpaces) + remainder = totalSpace % numSpaces + } + + var sb strings.Builder + for i := left; i <= right; i++ { + sb.WriteString(words[i]) + sb.WriteString(space) + + if remainder > 0 { + sb.WriteString(" ") + } + remainder-- + } + + return padResult(strings.Trim(sb.String(), " "), maxWidth) +} + +func isLastLine(words []string, right int) bool { + return right == len(words) - 1 +} + +func wordsLength(words []string, left, right int) int { + wordsLen := 0 + + for i := left; i <= right; i++ { + wordsLen += len(words[i]) + } + + return wordsLen +} + +func padResult(result string, maxWidth int) string { + var sb strings.Builder + + sb.WriteString(result) + sb.WriteString(blank(maxWidth - len(result))) + + return sb.String() +} + +func blank(length int) string { + var sb strings.Builder + + for i := 0; i < length; i++ { + sb.WriteString(" ") + } + + return sb.String() +} From 1f49af8d4921478e89ba67b1be7192e36ff83df7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 24 Aug 2023 13:11:13 -0700 Subject: [PATCH 824/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 89c3982..2bb3e9a 100644 --- a/Hard.md +++ b/Hard.md @@ -77,3 +77,4 @@ |[2141](https://leetcode.com/problems/maximum-running-time-of-n-computers/)| Maximum Running Time of N Computers| |[664](https://leetcode.com/problems/strange-printer/)| Strange Printer| |[239](https://leetcode.com/problems/sliding-window-maximum/)| Sliding Window Maximum| +|[68](https://leetcode.com/problems/text-justification/)| Text Justification| From 092e4baf186cc993c51a27676f7ca97be47dfc3a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Nov 2023 17:07:47 -0800 Subject: [PATCH 825/969] Create L1424.go --- Medium/L1424.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Medium/L1424.go diff --git a/Medium/L1424.go b/Medium/L1424.go new file mode 100644 index 0000000..179bb83 --- /dev/null +++ b/Medium/L1424.go @@ -0,0 +1,46 @@ +package Medium + +func findDiagonalOrder(nums [][]int) []int { + size, mp := 0, make(map[int][]int) + for i := 0; i < len(nums); i++ { + numList := make([]int, len(nums[i])) + copy(numList, nums[i]) + for j := 0; j < len(numList); j++ { + var lst []int + index := i + j + if _, ok := mp[index]; ok { + lst = mp[index] + } + lst = append([]int{numList[j]}, lst...) + mp[index] = lst + size++ + } + } + + maxLen := getMaxLen(mp) + var resultList []int + for i := 0; i <= maxLen; i++ { + diagVal := mp[i] + resultList = append(resultList, diagVal...) + } + + return resultList +} + +func getMaxLen(mp map[int][]int) int { + maxLen := math.MinInt32 + + for k, _ := range mp { + maxLen = max(maxLen, k) + } + + return maxLen +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From a29f5ae79410d89fc1c87f32bbc99995e1d0bf74 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Nov 2023 17:08:42 -0800 Subject: [PATCH 826/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index ee0ebcc..f0ba8b9 100644 --- a/Medium.md +++ b/Medium.md @@ -314,3 +314,4 @@ |[542](https://leetcode.com/problems/01-matrix/)| 01 Matrix| |[1615](https://leetcode.com/problems/maximal-network-rank/)| Maximal Network Rank| |[767](https://leetcode.com/problems/reorganize-string/)| Reorganize String| +|[1424](https://leetcode.com/problems/diagonal-traverse-ii/)| Diagonal Traverse II| From d00f68bfa5b5b05b71386349c68b80d8a9318ad8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Nov 2023 17:14:11 -0800 Subject: [PATCH 827/969] Create L1814.go --- Medium/L1814.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L1814.go diff --git a/Medium/L1814.go b/Medium/L1814.go new file mode 100644 index 0000000..80013d7 --- /dev/null +++ b/Medium/L1814.go @@ -0,0 +1,31 @@ +package Medium + +func countNicePairs(nums []int) int { + mp := make(map[int]int) + for i := 0; i < len(nums); i++ { + rev := revInt(nums[i]) + diff := nums[i] - rev + mp[diff]++ + } + + res, mod := 0, 1000000007 + for _, v := range mp { + if v == 1 { + continue + } + res = (res + v*(v-1)/2) % mod + } + + return res +} + +func revInt(n int) int { + newInt := 0 + for n > 0 { + remainder := n % 10 + newInt *= 10 + newInt += remainder + n /= 10 + } + return newInt +} From 40b5231217b5ad8fe462355e4a045f34aa29540f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 21 Nov 2023 17:14:58 -0800 Subject: [PATCH 828/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index f0ba8b9..dcd2421 100644 --- a/Medium.md +++ b/Medium.md @@ -315,3 +315,4 @@ |[1615](https://leetcode.com/problems/maximal-network-rank/)| Maximal Network Rank| |[767](https://leetcode.com/problems/reorganize-string/)| Reorganize String| |[1424](https://leetcode.com/problems/diagonal-traverse-ii/)| Diagonal Traverse II| +|[1814](https://leetcode.com/problems/count-nice-pairs-in-an-array/)| Count Nice Pairs in an Array| From fee28ec3e9a63dbd967b7f094e3a1825ac1a3c58 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Nov 2023 17:14:35 -0800 Subject: [PATCH 829/969] Create L1561.go --- Medium/L1561.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Medium/L1561.go diff --git a/Medium/L1561.go b/Medium/L1561.go new file mode 100644 index 0000000..a6bcd38 --- /dev/null +++ b/Medium/L1561.go @@ -0,0 +1,12 @@ +package Medium + +func maxCoins(piles []int) int { + sort.Ints(piles) + n, maximum := len(piles), 0 + + for i := n / 3; i < n-1; i += 2 { + maximum += piles[i] + } + + return maximum +} From c517e99cd35b3c2ca2d822c9f05c56f7a80d7d69 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Nov 2023 17:15:28 -0800 Subject: [PATCH 830/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index dcd2421..bd0ec83 100644 --- a/Medium.md +++ b/Medium.md @@ -316,3 +316,4 @@ |[767](https://leetcode.com/problems/reorganize-string/)| Reorganize String| |[1424](https://leetcode.com/problems/diagonal-traverse-ii/)| Diagonal Traverse II| |[1814](https://leetcode.com/problems/count-nice-pairs-in-an-array/)| Count Nice Pairs in an Array| +|[1561](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)| Maximum Number of Coins You Can Get| From 2c0c26e6726a0b1b239c45d4ed7894d717da2911 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Nov 2023 17:24:50 -0800 Subject: [PATCH 831/969] Create L1630.go --- Medium/L1630.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Medium/L1630.go diff --git a/Medium/L1630.go b/Medium/L1630.go new file mode 100644 index 0000000..f5e5aed --- /dev/null +++ b/Medium/L1630.go @@ -0,0 +1,61 @@ +package Medium + +func checkArithmeticSubarrays(nums []int, l []int, r []int) []bool { + var res []bool + for i := 0; i < len(l); i++ { + res = append(res, isArithmetic(nums, l[i], r[i])) + } + + return res +} + +func isArithmetic(nums []int, l, r int) bool { + maximum, minimum := math.MinInt32, math.MaxInt32 + + for i := l; i <= r; i++ { + maximum = max(nums[i], maximum) + minimum = min(nums[i], minimum) + } + + length := r - l + 1 + visited := make([]bool, length) + if (maximum-minimum)%(length-1) != 0 { + return false + } + + diff := (maximum - minimum) / (length - 1) + if diff == 0 { + return true + } + + for i := l; i <= r; i++ { + val := nums[i] + if (val-minimum)%diff != 0 { + return false + } else { + pos := (val - minimum) / diff + if visited[pos] { + return false + } + visited[pos] = true + } + } + + return true +} + +func min(a, b int) int { + if a < b { + return a + } + + return b +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From ce79b07243f9338a645e64a98e41ef55d190636b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 23 Nov 2023 17:25:35 -0800 Subject: [PATCH 832/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index bd0ec83..d4edabf 100644 --- a/Medium.md +++ b/Medium.md @@ -317,3 +317,4 @@ |[1424](https://leetcode.com/problems/diagonal-traverse-ii/)| Diagonal Traverse II| |[1814](https://leetcode.com/problems/count-nice-pairs-in-an-array/)| Count Nice Pairs in an Array| |[1561](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)| Maximum Number of Coins You Can Get| +|[1630](https://leetcode.com/problems/arithmetic-subarrays/)| Arithmetic Subarrays| From 2e452f9988588969e76a3cfd4ab2f431c2302706 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Nov 2023 14:16:07 -0800 Subject: [PATCH 833/969] Create L1685.go --- Medium/L1685.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L1685.go diff --git a/Medium/L1685.go b/Medium/L1685.go new file mode 100644 index 0000000..0e1eabb --- /dev/null +++ b/Medium/L1685.go @@ -0,0 +1,19 @@ +package Medium + +func getSumAbsoluteDifferences(nums []int) []int { + result, prefixSum, suffixSum := make([]int, len(nums)), make([]int, len(nums)), make([]int, len(nums)) + + prefixSum[0], suffixSum[len(nums)-1] = nums[0], nums[len(nums)-1] + + for i := 1; i < len(nums); i++ { + prefixSum[i] = prefixSum[i-1] + nums[i] + suffixSum[len(nums)-i-1] = suffixSum[len(nums)-i] + nums[len(nums)-i-1] + } + + for i := 0; i < len(nums); i++ { + currAbsDiff := ((nums[i] * i) - prefixSum[i]) + (suffixSum[i] - (nums[i] * (len(nums) - i - 1))) + result[i] = currAbsDiff + } + + return result +} From 692e6aaf94dc009a2f2b4d911328ddc7eab25c40 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Nov 2023 14:17:00 -0800 Subject: [PATCH 834/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index d4edabf..a8a5c23 100644 --- a/Medium.md +++ b/Medium.md @@ -318,3 +318,4 @@ |[1814](https://leetcode.com/problems/count-nice-pairs-in-an-array/)| Count Nice Pairs in an Array| |[1561](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)| Maximum Number of Coins You Can Get| |[1630](https://leetcode.com/problems/arithmetic-subarrays/)| Arithmetic Subarrays| +|[1685](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)| Sum of Absolute Differences in a Sorted Array| From 9267a838cc1b44ceb13897442fed5ea39833839b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Nov 2023 14:23:14 -0800 Subject: [PATCH 835/969] Create L1727.go --- Medium/L1727.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Medium/L1727.go diff --git a/Medium/L1727.go b/Medium/L1727.go new file mode 100644 index 0000000..3bce886 --- /dev/null +++ b/Medium/L1727.go @@ -0,0 +1,31 @@ +package Medium + +func largestSubmatrix(matrix [][]int) int { + rn, cn := len(matrix), len(matrix[0]) + + for i := 1; i < rn; i++ { + for j := 0; j < cn; j++ { + if matrix[i][j] == 1 { + matrix[i][j] += matrix[i-1][j] + } + } + } + + res := 0 + for i := 0; i < rn; i++ { + sort.Ints(matrix[i]) + for j := 0; j < cn; j++ { + res = max(res, matrix[i][j] * (cn - j)) + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 5f14203dea2a55da15be7ae724eaeebbf84e0674 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 27 Nov 2023 14:23:49 -0800 Subject: [PATCH 836/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a8a5c23..cbdd600 100644 --- a/Medium.md +++ b/Medium.md @@ -319,3 +319,4 @@ |[1561](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)| Maximum Number of Coins You Can Get| |[1630](https://leetcode.com/problems/arithmetic-subarrays/)| Arithmetic Subarrays| |[1685](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)| Sum of Absolute Differences in a Sorted Array| +|[1727](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)| Largest Submatrix With Rearrangements| From 0c7afcd6669a97d666573ea277c534b83f7b7e99 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Nov 2023 16:24:15 -0800 Subject: [PATCH 837/969] Create L191.go --- Easy/L191.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Easy/L191.go diff --git a/Easy/L191.go b/Easy/L191.go new file mode 100644 index 0000000..0f5c33f --- /dev/null +++ b/Easy/L191.go @@ -0,0 +1,12 @@ +package Easy + +func hammingWeight(num uint32) int { + cnt := 0 + + for num != 0 { + num = num & (num - 1) + cnt++ + } + + return cnt +} From f1acb7a82d075342a0e6327bbaed97be57377abe Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 28 Nov 2023 16:25:12 -0800 Subject: [PATCH 838/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 77bf533..b41e3c3 100644 --- a/Easy.md +++ b/Easy.md @@ -147,3 +147,4 @@ |[744](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)| Find Smallest Letter Greater Than Target| |[530](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)| Minimum Absolute Difference in BST| |[111](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| Minimum Depth of Binary Tree| +|[191](https://leetcode.com/problems/number-of-1-bits/)| Number of 1 Bits| From e28f2a72169b869e69b96fd009c8a64f8f151633 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Nov 2023 15:37:02 -0800 Subject: [PATCH 839/969] Create L1611.go --- Medium/L1611.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L1611.go diff --git a/Medium/L1611.go b/Medium/L1611.go new file mode 100644 index 0000000..577045f --- /dev/null +++ b/Medium/L1611.go @@ -0,0 +1,19 @@ +package Medium + +func minimumOneBitOperations(n int) int { + var solve func(n, res int) int + + solve = func(n, res int) int { + if n == 0 { + return res + } + b := 1 + for (b << 1) <= n { + b = b << 1 + } + + return solve((b >> 1) ^ b ^ n, res + b) + } + + return solve(n, 0) +} From e3f5fd1e702386bbd2c52b3a85ad70f896c4ae9f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Nov 2023 15:37:39 -0800 Subject: [PATCH 840/969] Rename L1611.go to L1611.go --- {Medium => Hard}/L1611.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Medium => Hard}/L1611.go (100%) diff --git a/Medium/L1611.go b/Hard/L1611.go similarity index 100% rename from Medium/L1611.go rename to Hard/L1611.go From c81d535287d5e9d96280184e55b790caaf3625f5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 30 Nov 2023 15:38:53 -0800 Subject: [PATCH 841/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 2bb3e9a..be79baa 100644 --- a/Hard.md +++ b/Hard.md @@ -78,3 +78,4 @@ |[664](https://leetcode.com/problems/strange-printer/)| Strange Printer| |[239](https://leetcode.com/problems/sliding-window-maximum/)| Sliding Window Maximum| |[68](https://leetcode.com/problems/text-justification/)| Text Justification| +|[1611](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/)| Minimum One Bit Operations to Make Integers Zero| From 30abb1583af3e6ecba0705d89eeed48c0779350a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 1 Dec 2023 17:00:48 -0800 Subject: [PATCH 842/969] Update L1160.go --- Easy/L1160.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Easy/L1160.go b/Easy/L1160.go index 98e1960..bca44ba 100644 --- a/Easy/L1160.go +++ b/Easy/L1160.go @@ -1,25 +1,31 @@ package Easy func countCharacters(words []string, chars string) int { - res, cnt := 0, make([]int, 26) + var isMatch func(s string) bool + res := 0 - for i := range chars { - cnt[chars[i]-'a']++ - } + isMatch = func(s string) bool { + mp := make(map[byte]int) - for _, w := range words { - match, tmp := true, make([]int, 26) - copy(tmp, cnt) + for _, c := range chars { + mp[byte(c)]++ + } - for i := range w { - tmp[w[i]-'a']-- - if tmp[w[i]-'a'] < 0 { - match = false - break + for _, c := range s { + if _, ok := mp[byte(c)]; !ok { + return false + } + mp[byte(c)]-- + if mp[byte(c)] < 0 { + return false } } - if match { + return true + } + + for _, w := range words { + if isMatch(w) { res += len(w) } } From ebf6b52bd527c5b6fcf998874fb310135539feb7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 4 Dec 2023 12:48:09 -0800 Subject: [PATCH 843/969] Create L2264.go --- Easy/L2264.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Easy/L2264.go diff --git a/Easy/L2264.go b/Easy/L2264.go new file mode 100644 index 0000000..22e7f5f --- /dev/null +++ b/Easy/L2264.go @@ -0,0 +1,16 @@ +package Easy + +func largestGoodInteger(num string) string { + res := -1 + for i := 0; i+2 < len(num); i++ { + if num[i] == num[i+1] && num[i+1] == num[i+2] { + res = max(res, int(num[i]-'0')) + } + } + + if res == -1 { + return "" + } + + return fmt.Sprintf("%d%d%d", res, res, res) +} From a4e5f1422aa178831f57b62ff13ac0408025b596 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 4 Dec 2023 12:48:46 -0800 Subject: [PATCH 844/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index b41e3c3..0e8353b 100644 --- a/Easy.md +++ b/Easy.md @@ -148,3 +148,4 @@ |[530](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)| Minimum Absolute Difference in BST| |[111](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| Minimum Depth of Binary Tree| |[191](https://leetcode.com/problems/number-of-1-bits/)| Number of 1 Bits| +|[2264](https://leetcode.com/problems/largest-3-same-digit-number-in-string/)| Largest 3-Same-Digit Number in String| From e2f79fb656b331b026d6533ec1097671422e0f6b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 4 Dec 2023 12:58:39 -0800 Subject: [PATCH 845/969] Update L2264.go --- Easy/L2264.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Easy/L2264.go b/Easy/L2264.go index 22e7f5f..213b404 100644 --- a/Easy/L2264.go +++ b/Easy/L2264.go @@ -14,3 +14,11 @@ func largestGoodInteger(num string) string { return fmt.Sprintf("%d%d%d", res, res, res) } + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From f4129bac68033b9d99394075bdd7d40f9495aedb Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 4 Dec 2023 14:16:48 -0800 Subject: [PATCH 846/969] Create L1266.go --- Easy/L1266.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Easy/L1266.go diff --git a/Easy/L1266.go b/Easy/L1266.go new file mode 100644 index 0000000..d1a4122 --- /dev/null +++ b/Easy/L1266.go @@ -0,0 +1,30 @@ +package Easy + +func minTimeToVisitAllPoints(points [][]int) int { + cnt := 0 + + for i := 1; i < len(points); i++ { + x := abs(points[i - 1][0] - points[i][0]) + y := abs(points[i - 1][1] - points[i][1]) + + cnt += max(x, y) + } + + return cnt +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From a40ec5403f7f7e286a0ac7dac44387f621438f81 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 4 Dec 2023 14:17:20 -0800 Subject: [PATCH 847/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 0e8353b..7058068 100644 --- a/Easy.md +++ b/Easy.md @@ -149,3 +149,4 @@ |[111](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| Minimum Depth of Binary Tree| |[191](https://leetcode.com/problems/number-of-1-bits/)| Number of 1 Bits| |[2264](https://leetcode.com/problems/largest-3-same-digit-number-in-string/)| Largest 3-Same-Digit Number in String| +|[1266](https://leetcode.com/problems/minimum-time-visiting-all-points/)| Minimum Time Visiting All Points| From 3bf8af68edce5788d14837e3d4adf877fb6c03f2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Dec 2023 14:59:06 -0800 Subject: [PATCH 848/969] Create L2849.go --- Medium/L2849.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L2849.go diff --git a/Medium/L2849.go b/Medium/L2849.go new file mode 100644 index 0000000..1543919 --- /dev/null +++ b/Medium/L2849.go @@ -0,0 +1,19 @@ +package Medium + +func isReachableAtTime(sx int, sy int, fx int, fy int, t int) bool { + xDist, yDist := abs(sx-fx), abs(sy-fy) + + if xDist == 0 && yDist == 0 { + return t != 1 + } + + return xDist <= t && yDist <= t +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From 536e6d95667fb6c7a624e4506ca21d1070293a00 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 5 Dec 2023 15:01:18 -0800 Subject: [PATCH 849/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index cbdd600..182de4a 100644 --- a/Medium.md +++ b/Medium.md @@ -320,3 +320,4 @@ |[1630](https://leetcode.com/problems/arithmetic-subarrays/)| Arithmetic Subarrays| |[1685](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)| Sum of Absolute Differences in a Sorted Array| |[1727](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)| Largest Submatrix With Rearrangements| +|[2849](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/)| Determine if a Cell Is Reachable at a Given Time| From 216032fb1498d49563cdb7dbbe0712e3555775ec Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Dec 2023 11:52:13 -0800 Subject: [PATCH 850/969] Create L1903.go --- Easy/L1903.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Easy/L1903.go diff --git a/Easy/L1903.go b/Easy/L1903.go new file mode 100644 index 0000000..4d2589f --- /dev/null +++ b/Easy/L1903.go @@ -0,0 +1,11 @@ +package Easy + +func largestOddNumber(num string) string { + for i := len(num) - 1; i >= 0; i-- { + if ((num[i] - '0') % 2) > 0 { + return num[0 : i+1] + } + } + + return "" +} From 97d1cdf04614361a4561a83d2f07b86c8cc79868 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Dec 2023 11:53:03 -0800 Subject: [PATCH 851/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 7058068..ef3902b 100644 --- a/Easy.md +++ b/Easy.md @@ -150,3 +150,4 @@ |[191](https://leetcode.com/problems/number-of-1-bits/)| Number of 1 Bits| |[2264](https://leetcode.com/problems/largest-3-same-digit-number-in-string/)| Largest 3-Same-Digit Number in String| |[1266](https://leetcode.com/problems/minimum-time-visiting-all-points/)| Minimum Time Visiting All Points| +|[1903](https://leetcode.com/problems/largest-odd-number-in-string/)| Largest Odd Number in String| From 4c41fb694a3c79ad217df8cbeaa65df7a45716a3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Dec 2023 11:59:49 -0800 Subject: [PATCH 852/969] Create L1716.go --- Easy/L1716.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L1716.go diff --git a/Easy/L1716.go b/Easy/L1716.go new file mode 100644 index 0000000..f70f12f --- /dev/null +++ b/Easy/L1716.go @@ -0,0 +1,19 @@ +package Easy + +func totalMoney(n int) int { + sum, prev, tot, cnt := 0, 1, 0, 0 + + for i := 1; i <= n; i++ { + if cnt == 7 { + cnt, tot = 0, prev+1 + prev = tot + } else { + tot++ + } + + sum += tot + cnt++ + } + + return sum +} From 2bb882ed36db95b0a6f5f0def039237b8b9500b4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 7 Dec 2023 12:00:30 -0800 Subject: [PATCH 853/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index ef3902b..dd4cab2 100644 --- a/Easy.md +++ b/Easy.md @@ -151,3 +151,4 @@ |[2264](https://leetcode.com/problems/largest-3-same-digit-number-in-string/)| Largest 3-Same-Digit Number in String| |[1266](https://leetcode.com/problems/minimum-time-visiting-all-points/)| Minimum Time Visiting All Points| |[1903](https://leetcode.com/problems/largest-odd-number-in-string/)| Largest Odd Number in String| +|[1716](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)| Calculate Money in Leetcode Bank| From 74746e73666e8795342578fe37260b897698585f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 8 Dec 2023 17:17:52 -0800 Subject: [PATCH 854/969] Create L94.go --- Easy/L94.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Easy/L94.go diff --git a/Easy/L94.go b/Easy/L94.go new file mode 100644 index 0000000..f0fd539 --- /dev/null +++ b/Easy/L94.go @@ -0,0 +1,29 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func inorderTraversal(root *TreeNode) []int { + var ( + res []int + solve func(root *TreeNode) + ) + + solve = func(root *TreeNode) { + if root == nil { + return + } + + solve(root.Left) + res = append(res, root.Val) + solve(root.Right) + } + + solve(root) + return res +} From a4a1fcc019e0fc5bd694971b334375c48b329b1d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 8 Dec 2023 17:18:24 -0800 Subject: [PATCH 855/969] Create L606.go --- Easy/L606.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Easy/L606.go diff --git a/Easy/L606.go b/Easy/L606.go new file mode 100644 index 0000000..7b07fd0 --- /dev/null +++ b/Easy/L606.go @@ -0,0 +1,45 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func tree2str(root *TreeNode) string { + var ( + sb strings.Builder + solve func(root *TreeNode) + ) + if root == nil { + return "" + } + + solve = func(root *TreeNode) { + sb.WriteString(fmt.Sprintf("%d", root.Val)) + if root.Left == nil && root.Right == nil { + return + } + + if root.Left != nil { + sb.WriteString("(") + solve(root.Left) + sb.WriteString(")") + } + + if root.Right != nil { + if root.Left == nil { + sb.WriteString("()") + } + + sb.WriteString("(") + solve(root.Right) + sb.WriteString(")") + } + } + + solve(root) + return sb.String() +} From ef3aabb4ca88258f0ff0e9c075305a52ccfdbdb0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 8 Dec 2023 17:20:01 -0800 Subject: [PATCH 856/969] Update Easy.md --- Easy.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Easy.md b/Easy.md index dd4cab2..0721812 100644 --- a/Easy.md +++ b/Easy.md @@ -152,3 +152,5 @@ |[1266](https://leetcode.com/problems/minimum-time-visiting-all-points/)| Minimum Time Visiting All Points| |[1903](https://leetcode.com/problems/largest-odd-number-in-string/)| Largest Odd Number in String| |[1716](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)| Calculate Money in Leetcode Bank| +|[606](https://leetcode.com/problems/construct-string-from-binary-tree/)| Construct String from Binary Tree| +|[94](https://leetcode.com/problems/binary-tree-inorder-traversal/)| Binary Tree Inorder Traversal| From 5994510df56f3eaf19bc8cece0e9204ade1cbb8a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Dec 2023 10:50:27 -0800 Subject: [PATCH 857/969] Create L1287.go --- Easy/L1287.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Easy/L1287.go diff --git a/Easy/L1287.go b/Easy/L1287.go new file mode 100644 index 0000000..3af0d0a --- /dev/null +++ b/Easy/L1287.go @@ -0,0 +1,13 @@ +package Easy + +func findSpecialInteger(arr []int) int { + n, t := len(arr), len(arr)/4 + + for i := 0; i < n-t; i++ { + if arr[i] == arr[i+t] { + return arr[i] + } + } + + return -1 +} From c652a5095823856e40e705f3c3a596f04df19c82 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 11 Dec 2023 10:51:29 -0800 Subject: [PATCH 858/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 0721812..244fda8 100644 --- a/Easy.md +++ b/Easy.md @@ -154,3 +154,4 @@ |[1716](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)| Calculate Money in Leetcode Bank| |[606](https://leetcode.com/problems/construct-string-from-binary-tree/)| Construct String from Binary Tree| |[94](https://leetcode.com/problems/binary-tree-inorder-traversal/)| Binary Tree Inorder Traversal| +|[1287](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)| Element Appearing More Than 25% In Sorted Array| From 296eb9865e1b4b4fe52690c62a115f9d6994b3f7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 13 Dec 2023 10:38:30 -0800 Subject: [PATCH 859/969] Create L1582.go --- Easy/L1582.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Easy/L1582.go diff --git a/Easy/L1582.go b/Easy/L1582.go new file mode 100644 index 0000000..05bafc4 --- /dev/null +++ b/Easy/L1582.go @@ -0,0 +1,23 @@ +package Easy + +func numSpecial(mat [][]int) int { + m, n, res, col, row := len(mat), len(mat[0]), 0, make([]int, len(mat[0])), make([]int, len(mat)) + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if mat[i][j] == 1 { + col[j], row[i] = col[j]+1, row[i]+1 + } + } + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if mat[i][j] == 1 && row[i] == 1 && col[j] == 1 { + res++ + } + } + } + + return res +} From b431337495f2ec7a16437d1e60e52da89de3ce62 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 13 Dec 2023 10:39:19 -0800 Subject: [PATCH 860/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 244fda8..5f2f503 100644 --- a/Easy.md +++ b/Easy.md @@ -155,3 +155,4 @@ |[606](https://leetcode.com/problems/construct-string-from-binary-tree/)| Construct String from Binary Tree| |[94](https://leetcode.com/problems/binary-tree-inorder-traversal/)| Binary Tree Inorder Traversal| |[1287](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)| Element Appearing More Than 25% In Sorted Array| +|[1582](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)| Special Positions in a Binary Matrix| From fc83c6114b607d7e53ad6c89e762e159e54e9fa0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Dec 2023 15:20:26 -0800 Subject: [PATCH 861/969] Create L2482.go --- Medium/L2482.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L2482.go diff --git a/Medium/L2482.go b/Medium/L2482.go new file mode 100644 index 0000000..3b6db3b --- /dev/null +++ b/Medium/L2482.go @@ -0,0 +1,25 @@ +package Medium + +func onesMinusZeros(grid [][]int) [][]int { + m, n := len(grid), len(grid[0]) + diff, rows, cols := make([][]int, m), make([]int, m), make([]int, n) + + for i := 0; i < m; i++ { + diff[i] = make([]int, n) + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + rows[i] += grid[i][j] + cols[j] += grid[i][j] + } + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + diff[i][j] = rows[i] + cols[j] - (n - rows[i]) - (m - cols[j]) + } + } + + return diff +} From f328ccf4899cbcc5eca94f0abc35f0c7836cac06 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Dec 2023 15:22:15 -0800 Subject: [PATCH 862/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 182de4a..d46aba7 100644 --- a/Medium.md +++ b/Medium.md @@ -321,3 +321,4 @@ |[1685](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)| Sum of Absolute Differences in a Sorted Array| |[1727](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)| Largest Submatrix With Rearrangements| |[2849](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/)| Determine if a Cell Is Reachable at a Given Time| +|[2842](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/)| Difference Between Ones and Zeroes in Row and Column| From 3e5ed0c6c6671b79851eb79f4d5936737c3dd61c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Dec 2023 16:31:13 -0800 Subject: [PATCH 863/969] Create L1436.go --- Easy/L1436.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L1436.go diff --git a/Easy/L1436.go b/Easy/L1436.go new file mode 100644 index 0000000..9c86773 --- /dev/null +++ b/Easy/L1436.go @@ -0,0 +1,18 @@ +package Easy + +func destCity(paths [][]string) string { + inDeg, outDeg := make(map[string]int), make(map[string]int) + + for _, p := range paths { + outDeg[p[0]] += 1 + inDeg[p[1]] += 1 + } + + for k, v := range inDeg { + if v == 1 && outDeg[k] == 0 { + return k + } + } + + return "" +} From 896d4b93476189ef2b82563cb07297114bb1fa2c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 14 Dec 2023 16:31:56 -0800 Subject: [PATCH 864/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 5f2f503..2c85ba2 100644 --- a/Easy.md +++ b/Easy.md @@ -156,3 +156,4 @@ |[94](https://leetcode.com/problems/binary-tree-inorder-traversal/)| Binary Tree Inorder Traversal| |[1287](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)| Element Appearing More Than 25% In Sorted Array| |[1582](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)| Special Positions in a Binary Matrix| +|[1436](https://leetcode.com/problems/destination-city/)| Destination City| From 2827988a30d2f8afbaffe1cb2cccccdf8d544e77 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Dec 2023 15:24:25 -0800 Subject: [PATCH 865/969] Create L2038.go --- Medium/L2038.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Medium/L2038.go diff --git a/Medium/L2038.go b/Medium/L2038.go new file mode 100644 index 0000000..e4dd2cf --- /dev/null +++ b/Medium/L2038.go @@ -0,0 +1,17 @@ +package Medium + +func winnerOfGame(colors string) bool { + a, b := 0, 0 + + for i := 1; i < len(colors)-1; i++ { + if colors[i] == colors[i-1] && colors[i] == colors[i+1] { + if colors[i] == 'A' { + a++ + } else { + b++ + } + } + } + + return a > b +} From 80f62dbeeffe3bd183d6ac68e8231929551f9011 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Dec 2023 15:25:32 -0800 Subject: [PATCH 866/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index d46aba7..eea5e75 100644 --- a/Medium.md +++ b/Medium.md @@ -322,3 +322,4 @@ |[1727](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)| Largest Submatrix With Rearrangements| |[2849](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/)| Determine if a Cell Is Reachable at a Given Time| |[2842](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/)| Difference Between Ones and Zeroes in Row and Column| +|[2038](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)| Remove Colored Pieces if Both Neighbors are the Same Color | From 53d0403ecd68ffd7abb3e7a4a88699f0803b9427 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Dec 2023 15:27:39 -0800 Subject: [PATCH 867/969] Create L1512.go --- Medium/L1512.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Medium/L1512.go diff --git a/Medium/L1512.go b/Medium/L1512.go new file mode 100644 index 0000000..7c9a395 --- /dev/null +++ b/Medium/L1512.go @@ -0,0 +1,15 @@ +package Medium + +func numIdenticalPairs(nums []int) int { + cnt := 0 + + for i := 0; i < len(nums); i++ { + for j := i + 1; j < len(nums); j++ { + if nums[i] == nums[j] { + cnt++ + } + } + } + + return cnt +} From 3e10e72bc9eb1a31ca0eb15fd21383c477ef597f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Dec 2023 15:28:32 -0800 Subject: [PATCH 868/969] Rename L1512.go to L1512.go --- {Medium => Easy}/L1512.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Medium => Easy}/L1512.go (100%) diff --git a/Medium/L1512.go b/Easy/L1512.go similarity index 100% rename from Medium/L1512.go rename to Easy/L1512.go From 5c77492436ba7bdcf4e4e904264832bf3ba10cc9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 15 Dec 2023 15:29:11 -0800 Subject: [PATCH 869/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 2c85ba2..16e1426 100644 --- a/Easy.md +++ b/Easy.md @@ -157,3 +157,4 @@ |[1287](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)| Element Appearing More Than 25% In Sorted Array| |[1582](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)| Special Positions in a Binary Matrix| |[1436](https://leetcode.com/problems/destination-city/)| Destination City| +|[1512](https://leetcode.com/problems/number-of-good-pairs/)| Number of Good Pairs| From b02a8813e1a1c946dc25d229bd1539fc3023239f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Dec 2023 14:20:00 -0800 Subject: [PATCH 870/969] Create L661.go --- Easy/L661.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Easy/L661.go diff --git a/Easy/L661.go b/Easy/L661.go new file mode 100644 index 0000000..c59cab8 --- /dev/null +++ b/Easy/L661.go @@ -0,0 +1,37 @@ +package Easy + +func imageSmoother(img [][]int) [][]int { + if len(img) == 0 { + return [][]int{} + } + + dirs := [][]int{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}} + rows, cols := len(img), len(img[0]) + var isValid func(x, y int) bool + + isValid = func(x, y int) bool { + return x >= 0 && x < rows && y >= 0 && y < cols + } + + res := make([][]int, rows) + for i := 0; i < rows; i++ { + res[i] = make([]int, cols) + } + + for row := 0; row < rows; row++ { + for col := 0; col < cols; col++ { + cnt, sum := 0, 0 + for _, d := range dirs { + newRow, newCol := row+d[0], col+d[1] + if isValid(newRow, newCol) { + cnt++ + sum += img[newRow][newCol] + } + } + + res[row][col] = sum / cnt + } + } + + return res +} From e74800f2e720c8c40fd5e3a7a387b56221437f6b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Dec 2023 14:20:36 -0800 Subject: [PATCH 871/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 16e1426..0fe4474 100644 --- a/Easy.md +++ b/Easy.md @@ -158,3 +158,4 @@ |[1582](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)| Special Positions in a Binary Matrix| |[1436](https://leetcode.com/problems/destination-city/)| Destination City| |[1512](https://leetcode.com/problems/number-of-good-pairs/)| Number of Good Pairs| +|[661](https://leetcode.com/problems/image-smoother/)| Image Smoother| From 1cc8ac78c22525ac856f0776f3ccdf2e8938ecd2 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Dec 2023 15:45:32 -0800 Subject: [PATCH 872/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 0fe4474..b0fdfb4 100644 --- a/Easy.md +++ b/Easy.md @@ -159,3 +159,4 @@ |[1436](https://leetcode.com/problems/destination-city/)| Destination City| |[1512](https://leetcode.com/problems/number-of-good-pairs/)| Number of Good Pairs| |[661](https://leetcode.com/problems/image-smoother/)| Image Smoother| +|[1913](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/)| Maximum Product Difference Between Two Pairs| From 7d4e7afee6beff3759fa02af0aa1e150186fff54 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 19 Dec 2023 15:46:19 -0800 Subject: [PATCH 873/969] Create L1913.go --- Easy/L1913.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Easy/L1913.go diff --git a/Easy/L1913.go b/Easy/L1913.go new file mode 100644 index 0000000..f8b65a0 --- /dev/null +++ b/Easy/L1913.go @@ -0,0 +1,20 @@ +package Easy + +func maxProductDifference(nums []int) int { + largest, secondLargest, smallest, secondSmallest := 0, 0, math.MaxInt32, math.MaxInt32 + for i := 0; i < len(nums); i++ { + if nums[i] >= largest { + secondLargest, largest = largest, nums[i] + } else if nums[i] > secondLargest { + secondLargest = nums[i] + } + + if nums[i] <= smallest { + secondSmallest, smallest = smallest, nums[i] + } else if nums[i] < secondSmallest { + secondSmallest = nums[i] + } + } + + return largest*secondLargest - smallest*secondSmallest +} From 5aa85069e5a629f9975d146d7cc598bc95a1cef1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 20 Dec 2023 14:35:31 -0800 Subject: [PATCH 874/969] Create L2706.go --- Easy/L2706.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Easy/L2706.go diff --git a/Easy/L2706.go b/Easy/L2706.go new file mode 100644 index 0000000..76f10bb --- /dev/null +++ b/Easy/L2706.go @@ -0,0 +1,23 @@ +package Easy + +func buyChoco(prices []int, money int) int { + if len(prices) < 2 { + return money + } + + first, second := math.MaxInt32, math.MaxInt32 + + for _, p := range prices { + if p < first { + second, first = first, p + }else if p < second { + second = p + } + } + + if first + second > money { + return money + } + + return money - (first + second) +} From 0dfa06fe29dd04bcea14145cee67c1411e8d75a8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 20 Dec 2023 14:36:13 -0800 Subject: [PATCH 875/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index b0fdfb4..f86a632 100644 --- a/Easy.md +++ b/Easy.md @@ -160,3 +160,4 @@ |[1512](https://leetcode.com/problems/number-of-good-pairs/)| Number of Good Pairs| |[661](https://leetcode.com/problems/image-smoother/)| Image Smoother| |[1913](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/)| Maximum Product Difference Between Two Pairs| +|[2706](https://leetcode.com/problems/buy-two-chocolates/)| Buy Two Chocolates| From 38aedb6266cdc226fa1b4bbfa15b4452345b48f8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 21 Dec 2023 10:02:50 -0800 Subject: [PATCH 876/969] Create L1637.go --- Medium/L1637.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Medium/L1637.go diff --git a/Medium/L1637.go b/Medium/L1637.go new file mode 100644 index 0000000..dd45e2f --- /dev/null +++ b/Medium/L1637.go @@ -0,0 +1,33 @@ +package Medium + +func maxWidthOfVerticalArea(points [][]int) int { + xCoordMap := make(map[int]bool) + maxDiff, prevX := 0, math.MinInt32 + + for _, p := range points { + xCoordMap[p[0]] = true + } + + var keys []int + for k, _ := range xCoordMap { + keys = append(keys, k) + } + + sort.Ints(keys) + for _, k := range keys { + if prevX != math.MinInt32 { + maxDiff = max(maxDiff, k-prevX) + } + prevX = k + } + + return maxDiff +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 53eea5f0f0c3fd1b04c0f054671a73ebb481e512 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 21 Dec 2023 10:04:01 -0800 Subject: [PATCH 877/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index eea5e75..b3f6762 100644 --- a/Medium.md +++ b/Medium.md @@ -323,3 +323,4 @@ |[2849](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/)| Determine if a Cell Is Reachable at a Given Time| |[2842](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/)| Difference Between Ones and Zeroes in Row and Column| |[2038](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)| Remove Colored Pieces if Both Neighbors are the Same Color | +|[1637](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)| Widest Vertical Area Between Two Points Containing No Points | From c59d6b8a2f5b85968371cf7e37ea620d3e34c84f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 22 Dec 2023 10:57:08 -0800 Subject: [PATCH 878/969] Create L1422.go --- Easy/L1422.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Easy/L1422.go diff --git a/Easy/L1422.go b/Easy/L1422.go new file mode 100644 index 0000000..4aeeecd --- /dev/null +++ b/Easy/L1422.go @@ -0,0 +1,27 @@ +package Easy + +func maxScore(s string) int { + zeroes, ones, maximum := 0, 0, math.MinInt32 + + for i := 0; i < len(s); i++ { + if s[i] == '0' { + zeroes++ + } else { + ones++ + } + + if i != len(s)-1 { + maximum = max(maximum, zeroes-ones) + } + } + + return maximum + ones +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +} From 0d9dcbce097cfc40b177ef0e7d3ec550e4da28f7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 22 Dec 2023 10:57:47 -0800 Subject: [PATCH 879/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index f86a632..2503835 100644 --- a/Easy.md +++ b/Easy.md @@ -161,3 +161,4 @@ |[661](https://leetcode.com/problems/image-smoother/)| Image Smoother| |[1913](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/)| Maximum Product Difference Between Two Pairs| |[2706](https://leetcode.com/problems/buy-two-chocolates/)| Buy Two Chocolates| +|[1422](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)| Maximum Score After Splitting a String| From 2615b813d2590993a3c54649c7b50d178d1a2851 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Dec 2023 07:00:25 -0800 Subject: [PATCH 880/969] Create L1496.go --- Easy/L1496.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Easy/L1496.go diff --git a/Easy/L1496.go b/Easy/L1496.go new file mode 100644 index 0000000..fe3b7c3 --- /dev/null +++ b/Easy/L1496.go @@ -0,0 +1,33 @@ +package Easy + +func isPathCrossing(path string) bool { + var ( + getNextPoint func(b byte) ([]int, string) + key string + ) + visited, curr := make(map[string]bool), []int{0, 0} + visited[fmt.Sprintf("%d#%d", curr[0], curr[1])] = true + + getNextPoint = func(b byte) ([]int, string) { + mp := map[byte][]int{ + 'N': {0, 1}, + 'S': {0, -1}, + 'E': {1, 1}, + 'W': {-1, -1}, + } + + nextPoint := []int{curr[0] + mp[b][0], curr[1] + mp[b][1]} + return nextPoint, fmt.Sprintf("%d#%d", nextPoint[0], nextPoint[1]) + } + + for _, p := range path { + curr, key = getNextPoint(byte(p)) + + if _, ok := visited[key]; ok { + return true + } + visited[key] = true + } + + return false +} From cf2fc2633c3f138d126fff77bc3389560d0443f5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Dec 2023 07:00:57 -0800 Subject: [PATCH 881/969] Create L1758.go --- Easy/L1758.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Easy/L1758.go diff --git a/Easy/L1758.go b/Easy/L1758.go new file mode 100644 index 0000000..76d5330 --- /dev/null +++ b/Easy/L1758.go @@ -0,0 +1,18 @@ +package Easy + +func minOperations(s string) int { + swaps := 0 + for i := 0; i < len(s); i++ { + if i %2 == 0 { + if s[i] != '0' { + swaps++ + } + } else { + if s[i] != '1' { + swaps++ + } + } + } + + return min(swaps, len(s) - swaps) +} From e999d3db00e0e4e76bcab63c51d036c3e5b1bd49 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 25 Dec 2023 07:02:45 -0800 Subject: [PATCH 882/969] Update Easy.md --- Easy.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Easy.md b/Easy.md index 2503835..1e9c6a8 100644 --- a/Easy.md +++ b/Easy.md @@ -162,3 +162,5 @@ |[1913](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/)| Maximum Product Difference Between Two Pairs| |[2706](https://leetcode.com/problems/buy-two-chocolates/)| Buy Two Chocolates| |[1422](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)| Maximum Score After Splitting a String| +|[1758](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/)| Minimum Changes To Make Alternating Binary String| +|[1496](https://leetcode.com/problems/path-crossing/)| Path Crossing| From 191de1aea77e8b6ca17f75eafb4497bc64ecac5a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 26 Dec 2023 10:43:33 -0800 Subject: [PATCH 883/969] Create L1458.go --- Hard/L1458.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Hard/L1458.go diff --git a/Hard/L1458.go b/Hard/L1458.go new file mode 100644 index 0000000..61f3071 --- /dev/null +++ b/Hard/L1458.go @@ -0,0 +1,33 @@ +package Hard + +func maxDotProduct(nums1 []int, nums2 []int) int { + dp := make(map[string]int) + var solve func(n, m int) int + + solve = func(n, m int) int { + key := fmt.Sprintf("%d#%d", n, m) + if v, ok := dp[key]; ok { + return v + } + + p1 := nums1[n] * nums2[m] + ans := p1 + + if (n < len(nums1)-1) && (m < len(nums2)-1) { + p1 += solve(n+1, m+1) + } + + if n < len(nums1)-1 { + ans = max(ans, solve(n+1, m)) + } + + if m < len(nums2)-1 { + ans = max(ans, solve(n, m+1)) + } + + dp[key] = max(ans, p1) + return dp[key] + } + + return solve(0, 0) +} From c8d381e471d3dbf25499ae825ed86c90172e2ba1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 26 Dec 2023 10:44:13 -0800 Subject: [PATCH 884/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index be79baa..daeee9c 100644 --- a/Hard.md +++ b/Hard.md @@ -79,3 +79,4 @@ |[239](https://leetcode.com/problems/sliding-window-maximum/)| Sliding Window Maximum| |[68](https://leetcode.com/problems/text-justification/)| Text Justification| |[1611](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/)| Minimum One Bit Operations to Make Integers Zero| +|[1458](https://leetcode.com/problems/max-dot-product-of-two-subsequences/)| Max Dot Product of Two Subsequences| From bdd3e87df63953e4acfc5f6845864ba4e71137f7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Dec 2023 11:40:29 -0800 Subject: [PATCH 885/969] Create L1624.go --- Easy/L1624.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L1624.go diff --git a/Easy/L1624.go b/Easy/L1624.go new file mode 100644 index 0000000..9216099 --- /dev/null +++ b/Easy/L1624.go @@ -0,0 +1,19 @@ +package Easy + +func maxLengthBetweenEqualCharacters(s string) int { + indexes, dist := make(map[byte]int), -1 + + for i := 0; i < len(s); i++ { + if v, ok := indexes[s[i]-'a']; ok { + dist = max(dist, i-v+1) + } else { + indexes[s[i]-'a'] = i + } + } + + if dist == -1 { + return dist + } + + return dist - 2 +} From c463e16ff6843e7f548b8793359403c5c54c4210 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Dec 2023 11:40:58 -0800 Subject: [PATCH 886/969] Create L1897.go --- Easy/L1897.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Easy/L1897.go diff --git a/Easy/L1897.go b/Easy/L1897.go new file mode 100644 index 0000000..31fa2f9 --- /dev/null +++ b/Easy/L1897.go @@ -0,0 +1,19 @@ +package Easy + +func makeEqual(words []string) bool { + counts := make(map[byte]int) + for _, w := range words { + for i := 0; i < len(w); i++ { + counts[w[i] - 'a']++ + } + } + + n := len(words) + for _, v := range counts { + if v % n != 0 { + return false + } + } + + return true +} From 47074cee48a6fbb3663cbfbf6e6870b6ec984a17 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 31 Dec 2023 11:42:39 -0800 Subject: [PATCH 887/969] Update Easy.md --- Easy.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Easy.md b/Easy.md index 1e9c6a8..9639329 100644 --- a/Easy.md +++ b/Easy.md @@ -164,3 +164,5 @@ |[1422](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)| Maximum Score After Splitting a String| |[1758](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/)| Minimum Changes To Make Alternating Binary String| |[1496](https://leetcode.com/problems/path-crossing/)| Path Crossing| +|[1897](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/)| Redistribute Characters to Make All Strings Equal| +|[1624](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)| Largest Substring Between Two Equal Characters| From a42f047e849cd934ad7bde8804050f45919ffc8c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 Jan 2024 15:44:49 -0800 Subject: [PATCH 888/969] Create L2610.go --- Medium/L2610.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Medium/L2610.go diff --git a/Medium/L2610.go b/Medium/L2610.go new file mode 100644 index 0000000..43ec419 --- /dev/null +++ b/Medium/L2610.go @@ -0,0 +1,17 @@ +package Medium + +func findMatrix(nums []int) [][]int { + var ans [][]int + freq := make([]int, len(nums) + 1) + + for _, n := range nums { + if freq[n] >= len(ans) { + ans = append(ans, []int{n}) + } else { + ans[freq[n]] = append(ans[freq[n]], n) + } + freq[n]++ + } + + return ans +} From 0ac765fe3df5a182fee765e0857c650e7a52cff9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 Jan 2024 15:45:16 -0800 Subject: [PATCH 889/969] Create L455.go --- Easy/L455.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Easy/L455.go diff --git a/Easy/L455.go b/Easy/L455.go new file mode 100644 index 0000000..3ef58ed --- /dev/null +++ b/Easy/L455.go @@ -0,0 +1,15 @@ +package Easy + +func findContentChildren(children []int, cookies []int) int { + sort.Ints(children) + sort.Ints(cookies) + + child := 0 + for cookie := 0; child < len(children) && cookie < len(cookies); cookie++ { + if cookies[cookie] >= children[child] { + child++ + } + } + + return child +} From 97d05e81d595124b57fcdf90be730732277ea900 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 Jan 2024 15:46:00 -0800 Subject: [PATCH 890/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 9639329..1c2c411 100644 --- a/Easy.md +++ b/Easy.md @@ -166,3 +166,4 @@ |[1496](https://leetcode.com/problems/path-crossing/)| Path Crossing| |[1897](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/)| Redistribute Characters to Make All Strings Equal| |[1624](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)| Largest Substring Between Two Equal Characters| +|[455](https://leetcode.com/problems/assign-cookies/)| Assign Cookies| From 956ae9108661433c02e08bdca4f0697e2af632ce Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 2 Jan 2024 15:47:12 -0800 Subject: [PATCH 891/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index b3f6762..0caaef1 100644 --- a/Medium.md +++ b/Medium.md @@ -324,3 +324,4 @@ |[2842](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/)| Difference Between Ones and Zeroes in Row and Column| |[2038](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)| Remove Colored Pieces if Both Neighbors are the Same Color | |[1637](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)| Widest Vertical Area Between Two Points Containing No Points | +|[2610](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/)| Convert an Array Into a 2D Array With Conditions | From bd92ba25dbd757a52b7225c5bdf834213c2e4bdd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 Jan 2024 08:01:20 -0800 Subject: [PATCH 892/969] Create L2125.go --- Medium/L2125.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L2125.go diff --git a/Medium/L2125.go b/Medium/L2125.go new file mode 100644 index 0000000..1f74491 --- /dev/null +++ b/Medium/L2125.go @@ -0,0 +1,20 @@ +package Medium + +func numberOfBeams(bank []string) int { + res, prev := 0, 0 + + for _, s := range bank { + cnt := 0 + for i := 0; i < len(s); i++ { + if s[i] == '1' { + cnt++ + } + } + if cnt > 0 { + res += prev * cnt + prev = cnt + } + } + + return res +} From 14b7847b8fe80b4ba24b89c2578b76813c9a1f71 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 3 Jan 2024 08:02:04 -0800 Subject: [PATCH 893/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 0caaef1..833447c 100644 --- a/Medium.md +++ b/Medium.md @@ -325,3 +325,4 @@ |[2038](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)| Remove Colored Pieces if Both Neighbors are the Same Color | |[1637](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)| Widest Vertical Area Between Two Points Containing No Points | |[2610](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/)| Convert an Array Into a 2D Array With Conditions | +|[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | From 0020c2b9ff27580c3c699936123c01a924318793 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 4 Jan 2024 15:43:19 -0800 Subject: [PATCH 894/969] Create L2870.go --- Medium/L2870.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/L2870.go diff --git a/Medium/L2870.go b/Medium/L2870.go new file mode 100644 index 0000000..e7e62c8 --- /dev/null +++ b/Medium/L2870.go @@ -0,0 +1,22 @@ +package Medium + +func minOperations(nums []int) int { + mp, cnt := make(map[int]int), 0 + + for _, n := range nums { + mp[n]++ + } + + for _, v := range mp { + if v == 1 { + return -1 + } + + cnt += v / 3 + if v%3 != 0 { + cnt++ + } + } + + return cnt +} From 7b79237eb6151002b87d191c31d9a67bafb23ba9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 4 Jan 2024 15:43:59 -0800 Subject: [PATCH 895/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 833447c..efde387 100644 --- a/Medium.md +++ b/Medium.md @@ -326,3 +326,4 @@ |[1637](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)| Widest Vertical Area Between Two Points Containing No Points | |[2610](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/)| Convert an Array Into a 2D Array With Conditions | |[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | +|[2870](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/)| Minimum Number of Operations to Make Array Empty | From 34e3452167f83cf5da6bd0d30496ec626ada90c5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Jan 2024 16:18:05 -0800 Subject: [PATCH 896/969] Create L501.go --- Easy/L501.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Easy/L501.go diff --git a/Easy/L501.go b/Easy/L501.go new file mode 100644 index 0000000..6823db0 --- /dev/null +++ b/Easy/L501.go @@ -0,0 +1,35 @@ +package Easy + +func findMode(root *TreeNode) []int { + var ( + modes []int + solve func(node *TreeNode, count *int) + ) + prev, cnt, maxCnt := root, 0, math.MinInt32 + + solve = func(node *TreeNode, count *int) { + if node == nil { + return + } + solve(node.Left, count) + if prev.Val == node.Val { + *count++ + } else { + *count = 1 + } + + if *count == maxCnt { + modes = append(modes, node.Val) + } else if *count > maxCnt { + modes = nil + modes = append(modes, node.Val) + maxCnt = *count + } + + prev = node + solve(node.Right, count) + } + + solve(root, &cnt) + return modes +} From ea39808c95f358f83649be38802174a8505faacc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 6 Jan 2024 16:18:44 -0800 Subject: [PATCH 897/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 1c2c411..7f8d47e 100644 --- a/Easy.md +++ b/Easy.md @@ -167,3 +167,4 @@ |[1897](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/)| Redistribute Characters to Make All Strings Equal| |[1624](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)| Largest Substring Between Two Equal Characters| |[455](https://leetcode.com/problems/assign-cookies/)| Assign Cookies| +|[501](https://leetcode.com/problems/find-mode-in-binary-search-tree/)| Find Mode in Binary Search Tree| From 9f5fbb9c68155e476e72893590a7d5266c382e0b Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 9 Jan 2024 15:47:59 -0800 Subject: [PATCH 898/969] Create L872.go --- Easy/L872.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Easy/L872.go diff --git a/Easy/L872.go b/Easy/L872.go new file mode 100644 index 0000000..76a6cb8 --- /dev/null +++ b/Easy/L872.go @@ -0,0 +1,32 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool { + var ( + root1Leaves, root2Leaves string + solve func(root *TreeNode, rootLeaves *string) + ) + + solve = func(root *TreeNode, rootLeaves *string) { + if root == nil { + return + } + if root.Left == nil && root.Right == nil { + *rootLeaves = fmt.Sprintf("%s%d#", *rootLeaves, root.Val) + } + solve(root.Left, rootLeaves) + solve(root.Right, rootLeaves) + } + + solve(root1, &root1Leaves) + solve(root2, &root2Leaves) + + return root1Leaves == root2Leaves +} From 9c2a80ea82584700e3cc45754c65864f7369a72a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 9 Jan 2024 15:48:39 -0800 Subject: [PATCH 899/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 7f8d47e..1e58451 100644 --- a/Easy.md +++ b/Easy.md @@ -168,3 +168,4 @@ |[1624](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)| Largest Substring Between Two Equal Characters| |[455](https://leetcode.com/problems/assign-cookies/)| Assign Cookies| |[501](https://leetcode.com/problems/find-mode-in-binary-search-tree/)| Find Mode in Binary Search Tree| +|[872](https://leetcode.com/problems/leaf-similar-trees/)| Leaf-Similar Trees| From 4e2e3f00c2a3067153e416cd298e5796de757af0 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 10 Jan 2024 15:37:44 -0800 Subject: [PATCH 900/969] Create L2385.go --- Medium/L2385.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L2385.go diff --git a/Medium/L2385.go b/Medium/L2385.go new file mode 100644 index 0000000..6a6f53c --- /dev/null +++ b/Medium/L2385.go @@ -0,0 +1,45 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func amountOfTime(root *TreeNode, start int) int { + var ( + amount int + solve func(node *TreeNode, res *int) int + ) + + solve = func(node *TreeNode, res *int) int { + if node == nil { + return 0 + } + + left, right := solve(node.Left, res), solve(node.Right, res) + + if node.Val == start { + *res = max(left, right) + return -1 + } else if left >= 0 && right >= 0 { + return max(left, right) + 1 + } + + *res = max(*res, abs(left-right)) + return min(left, right) - 1 + } + + solve(root, &amount) + return amount +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From 3a7ed65ee84bf10058f98025a7e726b7e6636d22 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 10 Jan 2024 15:39:12 -0800 Subject: [PATCH 901/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index efde387..0117cb0 100644 --- a/Medium.md +++ b/Medium.md @@ -327,3 +327,4 @@ |[2610](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/)| Convert an Array Into a 2D Array With Conditions | |[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | |[2870](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/)| Minimum Number of Operations to Make Array Empty | +|[2385](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/)| Amount of Time for Binary Tree to Be Infected | From 47480e9bc922bff0b2dcb5f8af2f2bb876edcb2d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 13 Jan 2024 08:13:08 -0800 Subject: [PATCH 902/969] Create L1347.go --- Medium/L1347.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L1347.go diff --git a/Medium/L1347.go b/Medium/L1347.go new file mode 100644 index 0000000..22ba933 --- /dev/null +++ b/Medium/L1347.go @@ -0,0 +1,19 @@ +package Medium + +func minSteps(s string, t string) int { + cnt := make([]int, 26) + + for i := 0; i < len(s); i++ { + cnt[s[i]-'a']++ + cnt[t[i]-'a']-- + } + + steps := 0 + for _, count := range cnt { + if count > 0 { + steps += count + } + } + + return steps +} From f94b0fd59a867536052c5b0c3628410f94f23af4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 13 Jan 2024 08:13:45 -0800 Subject: [PATCH 903/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 0117cb0..d32d308 100644 --- a/Medium.md +++ b/Medium.md @@ -328,3 +328,4 @@ |[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | |[2870](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/)| Minimum Number of Operations to Make Array Empty | |[2385](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/)| Amount of Time for Binary Tree to Be Infected | +|[1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/description/)| Minimum Number of Steps to Make Two Strings Anagram | From 321eb7a173255760be5cee4a414fe4bca7b75dac Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 07:05:14 -0800 Subject: [PATCH 904/969] Create L1657.go --- Medium/L1657.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Medium/L1657.go diff --git a/Medium/L1657.go b/Medium/L1657.go new file mode 100644 index 0000000..fc7508d --- /dev/null +++ b/Medium/L1657.go @@ -0,0 +1,45 @@ +package Medium + +func closeStrings(word1 string, word2 string) bool { + if len(word1) != len(word2) { + return false + } + + solve := func(s string) []int{ + arr := make([]int, 26) + + for _, c := range s { + arr[c - 'a']++ + } + + return arr + } + + charCnt1, charCnt2 := solve(word1), solve(word2) + n := len(word1) + freqCnt1, freqCnt2 := make([]int, n + 1), make([]int, n + 1) + + for i := 0; i < 26; i++ { + if charCnt1[i] > 0 { + freqCnt1[charCnt1[i]]++ + if charCnt2[i] == 0 { + return false + } + } + + if charCnt2[i] > 0 { + freqCnt2[charCnt2[i]]++ + if charCnt1[i] == 0 { + return false + } + } + } + + for i := 0; i < n + 1; i++ { + if freqCnt1[i] != freqCnt2[i] { + return false + } + } + + return true +} From 79b6e8e75ca2aa3256f1181ed09f151be987ce8c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 07:05:54 -0800 Subject: [PATCH 905/969] Update Medium.md --- Medium.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Medium.md b/Medium.md index d32d308..b8e0fa3 100644 --- a/Medium.md +++ b/Medium.md @@ -328,4 +328,5 @@ |[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | |[2870](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/)| Minimum Number of Operations to Make Array Empty | |[2385](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/)| Amount of Time for Binary Tree to Be Infected | -|[1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/description/)| Minimum Number of Steps to Make Two Strings Anagram | +|[1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)| Minimum Number of Steps to Make Two Strings Anagram | +|[1657](https://leetcode.com/problems/determine-if-two-strings-are-close/)| Determine if Two Strings Are Close | From 2f68d08e4d9c8a98769a53162c3e0a1f499fb13a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 09:26:59 -0800 Subject: [PATCH 906/969] Create L1793.go --- Hard/L1793.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Hard/L1793.go diff --git a/Hard/L1793.go b/Hard/L1793.go new file mode 100644 index 0000000..cd55da3 --- /dev/null +++ b/Hard/L1793.go @@ -0,0 +1,23 @@ +package Hard + +func maximumScore(nums []int, k int) int { + maxScore, minValue := nums[k], nums[k] + left, right := k, k + + for left >= 0 && right < len(nums) { + minValue = min(minValue, min(nums[left], nums[right])) + maxScore = max(minValue*(right-left+1), maxScore) + + if left == 0 && right < len(nums) { + right++ + } else if right == len(nums)-1 && left >= 0 { + left-- + } else if nums[right+1] > nums[left-1] { + right++ + } else { + left-- + } + } + + return maxScore +} From fc949adba0ae45b13f3dd5e8f7a23c3f9d8634cc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 09:27:37 -0800 Subject: [PATCH 907/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index daeee9c..d00feaa 100644 --- a/Hard.md +++ b/Hard.md @@ -80,3 +80,4 @@ |[68](https://leetcode.com/problems/text-justification/)| Text Justification| |[1611](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/)| Minimum One Bit Operations to Make Integers Zero| |[1458](https://leetcode.com/problems/max-dot-product-of-two-subsequences/)| Max Dot Product of Two Subsequences| +|[1793](https://leetcode.com/problems/maximum-score-of-a-good-subarray/)| Maximum Score of a Good Subarray| From 3d1b41dfc05cea5b31d40fffe0e0271d66d9e529 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 09:34:31 -0800 Subject: [PATCH 908/969] Create L119.go --- Easy/L119.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Easy/L119.go diff --git a/Easy/L119.go b/Easy/L119.go new file mode 100644 index 0000000..323d63e --- /dev/null +++ b/Easy/L119.go @@ -0,0 +1,14 @@ +package Easy + +func getRow(rowIndex int) []int { + res := make([]int, rowIndex+1) + res[0] = 1 + + for i := 1; i < rowIndex+1; i++ { + for j := i; j >= 1; j-- { + res[j] += res[j-1] + } + } + + return res +} From 974ec04806de8bfd5eae8a0d70c4e2103a8c630c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 14 Jan 2024 09:35:27 -0800 Subject: [PATCH 909/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 1e58451..36f60b2 100644 --- a/Easy.md +++ b/Easy.md @@ -169,3 +169,4 @@ |[455](https://leetcode.com/problems/assign-cookies/)| Assign Cookies| |[501](https://leetcode.com/problems/find-mode-in-binary-search-tree/)| Find Mode in Binary Search Tree| |[872](https://leetcode.com/problems/leaf-similar-trees/)| Leaf-Similar Trees| +|[119](https://leetcode.com/problems/pascals-triangle-ii/)| Pascal's Triangle II| From 5f95ee8d86ac62bb2a1e764ca3f67171c191828c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 19 Jan 2024 06:51:49 -0800 Subject: [PATCH 910/969] Update L931.go --- Medium/L931.go | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/Medium/L931.go b/Medium/L931.go index e326189..82b428a 100644 --- a/Medium/L931.go +++ b/Medium/L931.go @@ -1,36 +1,32 @@ package Medium func minFallingPathSum(matrix [][]int) int { - rows, cols := len(matrix), len(matrix[0]) - mp, ans := make(map[string]int), math.MaxInt32 + var ( + solve func(row, col int) int + ) + cols, mp, res := len(matrix[0]), make(map[string]int), math.MaxInt32 - for c := 0; c < cols; c++ { - ans = min(ans, solve(matrix, rows-1, c, mp)) - } + solve = func(row, col int) int { + if col >= cols || col < 0 { + return math.MaxInt32 + } - return ans -} + key := fmt.Sprintf("%d#%d", row, col) + if v, ok := mp[key]; ok { + return v + } -func solve(matrix [][]int, r, c int, mp map[string]int) int { - if r == 0 && c < len(matrix[0]) && c >= 0 { - return matrix[r][c] - } - if c >= len(matrix[0]) || c < 0 { - return math.MaxInt32 - } - key := fmt.Sprintf("%d-%d", r, c) - if v, ok := mp[key]; ok { - return v - } + if row == len(matrix)-1 && col < cols { + return matrix[row][col] + } - mp[key] = matrix[r][c] + min(min(solve(matrix, r-1, c+1, mp), solve(matrix, r-1, c, mp)), solve(matrix, r-1, c-1, mp)) - return mp[key] -} + mp[key] = matrix[row][col] + min(solve(row+1, col-1), min(solve(row+1, col), solve(row+1, col+1))) + return mp[key] + } -func min(a, b int) int { - if a < b { - return a + for i := 0; i < cols; i++ { + res = min(res, solve(0, i)) } - return b + return res } From 1f4df443415d9abeb6308a3ff5f9cc4afc3ed451 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 22 Jan 2024 12:59:43 -0800 Subject: [PATCH 911/969] Create L907.go --- Medium/L907.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L907.go diff --git a/Medium/L907.go b/Medium/L907.go new file mode 100644 index 0000000..9b51891 --- /dev/null +++ b/Medium/L907.go @@ -0,0 +1,32 @@ +package Medium + +func sumSubarrayMins(arr []int) int { + var ( + res int64 + st []int + mod = int64(1000000007) + + ) + st = append(st, -1) + + for i := 0; i < len(arr) + 1; i++ { + currVal := 0 + if i < len(arr) { + currVal = arr[i] + } + + for st[len(st) - 1] != -1 && currVal < arr[st[len(st) - 1]] { + index := st[len(st) - 1] + st = st[:len(st) - 1] + newTop := st[len(st) - 1] + left, right := index - newTop, i - index + add := int64(left * right * arr[index]) % mod + res += add + res %= mod + } + + st = append(st, i) + } + + return int(res) +} From ae606be9fcb7649563270f45bed3b6ae9d4e4322 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 22 Jan 2024 13:00:34 -0800 Subject: [PATCH 912/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index b8e0fa3..6cf8bfb 100644 --- a/Medium.md +++ b/Medium.md @@ -325,6 +325,7 @@ |[2038](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)| Remove Colored Pieces if Both Neighbors are the Same Color | |[1637](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)| Widest Vertical Area Between Two Points Containing No Points | |[2610](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/)| Convert an Array Into a 2D Array With Conditions | +|[907](https://leetcode.com/problems/sum-of-subarray-minimums/)| Sum of Subarray Minimums | |[2125](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/)| Number of Laser Beams in a Bank | |[2870](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/)| Minimum Number of Operations to Make Array Empty | |[2385](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/)| Amount of Time for Binary Tree to Be Infected | From 5b33323d1935304271344c3a8f3bdccbbcd7ffbc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 25 Jan 2024 08:15:04 -0800 Subject: [PATCH 913/969] Update L1143.go --- Medium/L1143.go | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/Medium/L1143.go b/Medium/L1143.go index f49c154..58589cd 100644 --- a/Medium/L1143.go +++ b/Medium/L1143.go @@ -1,40 +1,30 @@ package Medium func longestCommonSubsequence(text1 string, text2 string) int { + var ( + solve func(m, n int) int + ) m, n := len(text1), len(text2) - memo := make([][]int, m+1) - for i := 0; i < m+1; i++ { - memo[i] = make([]int, n+1) - for j := 0; j < n+1; j++ { - memo[i][j] = -1 - } - } - - return lcs(text1, text2, m, n, &memo) -} - -func lcs(s1, s2 string, m, n int, memo *[][]int) int { - if m == 0 || n == 0 { - return 0 - } + mp := make(map[string]int) - if (*memo)[m][n] != -1 { - return (*memo)[m][n] - } + solve = func(m, n int) int { + if m == 0 || n == 0 { + return 0 + } - if s1[m-1] == s2[n-1] { - (*memo)[m][n] = 1 + lcs(s1, s2, m-1, n-1, memo) - } else { - (*memo)[m][n] = max(lcs(s1, s2, m-1, n, memo), lcs(s1, s2, m, n-1, memo)) - } + key := fmt.Sprintf("%d#%d", m, n) + if v, ok := mp[key]; ok { + return v + } - return (*memo)[m][n] -} + if text1[m-1] == text2[n-1] { + mp[key] = 1 + solve(m-1, n-1) + } else { + mp[key] = max(solve(m-1, n), solve(m, n-1)) + } -func max(a, b int) int { - if a > b { - return a + return mp[key] } - return b + return solve(m, n) } From f4cb02224d5bf391b0c2b2973f33ad34963f23a4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 1 Feb 2024 09:57:36 -0800 Subject: [PATCH 914/969] Create L2966.go --- Medium/L2966.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L2966.go diff --git a/Medium/L2966.go b/Medium/L2966.go new file mode 100644 index 0000000..7a28841 --- /dev/null +++ b/Medium/L2966.go @@ -0,0 +1,21 @@ +package Medium + +func divideArray(nums []int, k int) [][]int { + var res [][]int + if len(nums)%3 != 0 { + return res + } + + sort.Ints(nums) + groupIdx := 0 + for i := 0; i < len(nums); i += 3 { + if i+2 < len(nums) && nums[i+2]-nums[i] <= k { + res = append(res, []int{nums[i], nums[i+1], nums[i+2]}) + groupIdx++ + } else { + return [][]int{} + } + } + + return res +} From a06b2a201458735e131618253e0658fb54e5ff33 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 1 Feb 2024 09:58:24 -0800 Subject: [PATCH 915/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 6cf8bfb..88d2b46 100644 --- a/Medium.md +++ b/Medium.md @@ -331,3 +331,4 @@ |[2385](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/)| Amount of Time for Binary Tree to Be Infected | |[1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)| Minimum Number of Steps to Make Two Strings Anagram | |[1657](https://leetcode.com/problems/determine-if-two-strings-are-close/)| Determine if Two Strings Are Close | +|[2966](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/)| Divide Array Into Arrays With Max Difference | From 6dc549fc8443b8111a7df5fa6c61fe2feb44ffa9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Apr 2024 16:55:25 -0700 Subject: [PATCH 916/969] Create L1700.go --- Easy/L1700.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Easy/L1700.go diff --git a/Easy/L1700.go b/Easy/L1700.go new file mode 100644 index 0000000..da13a4d --- /dev/null +++ b/Easy/L1700.go @@ -0,0 +1,29 @@ +package Easy + +func countStudents(students []int, sandwiches []int) int { + ones, zeroes := 0, 0 + + for _, student := range students { + if student == 0 { + zeroes++ + } else { + ones++ + } + } + + for _, sandwich := range sandwiches { + if sandwich == 0 { + if zeroes == 0 { + return ones + } + zeroes-- + } else { + if ones == 0 { + return zeroes + } + ones-- + } + } + + return 0 +} From bc514d228d0faa8bf9a4f6542889df06f06f4031 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Apr 2024 16:56:16 -0700 Subject: [PATCH 917/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 36f60b2..b8fc908 100644 --- a/Easy.md +++ b/Easy.md @@ -170,3 +170,4 @@ |[501](https://leetcode.com/problems/find-mode-in-binary-search-tree/)| Find Mode in Binary Search Tree| |[872](https://leetcode.com/problems/leaf-similar-trees/)| Leaf-Similar Trees| |[119](https://leetcode.com/problems/pascals-triangle-ii/)| Pascal's Triangle II| +|[1700](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)| Number of Students Unable to Eat Lunch| From eefbc467028c7eb97a4fd12acaae5c352087235a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Apr 2024 17:09:34 -0700 Subject: [PATCH 918/969] Create L678.go --- Easy/L678.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L678.go diff --git a/Easy/L678.go b/Easy/L678.go new file mode 100644 index 0000000..237fa12 --- /dev/null +++ b/Easy/L678.go @@ -0,0 +1,22 @@ +package Easy + +func checkValidString(s string) bool { + cMin, cMax := 0, 0 + for i := 0; i < len(s); i++ { + if s[i] == '(' { + cMax, cMin = cMax+1, cMin+1 + } else if s[i] == ')' { + cMax, cMin = cMax-1, cMin-1 + } else if s[i] == '*' { + cMax, cMin = cMax+1, cMin-1 + } + + if cMax < 0 { + return false + } + + cMin = max(cMin, 0) + } + + return cMin == 0 +} From a5ea8ec673ec1804a5ac93a21f769a72dabf144a Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 8 Apr 2024 17:10:13 -0700 Subject: [PATCH 919/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index b8fc908..db1b36e 100644 --- a/Easy.md +++ b/Easy.md @@ -171,3 +171,4 @@ |[872](https://leetcode.com/problems/leaf-similar-trees/)| Leaf-Similar Trees| |[119](https://leetcode.com/problems/pascals-triangle-ii/)| Pascal's Triangle II| |[1700](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)| Number of Students Unable to Eat Lunch| +|[678](https://leetcode.com/problems/valid-parenthesis-string/)| Valid Parenthesis String| From 7bc2a7c2a09973dfd99dc475d7cd69c6efdcc050 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 17 Apr 2024 18:01:35 -0700 Subject: [PATCH 920/969] Create L988.go --- Medium/L988.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Medium/L988.go diff --git a/Medium/L988.go b/Medium/L988.go new file mode 100644 index 0000000..ac64e51 --- /dev/null +++ b/Medium/L988.go @@ -0,0 +1,37 @@ +package Medium + +func smallestFromLeaf(root *TreeNode) string { + var ( + tmp string + isLeaf func(node *TreeNode) bool + solve func(node *TreeNode, s string) string + ) + + isLeaf = func(node *TreeNode) bool { + if node == nil { + return false + } + + return node.Left == nil && node.Right == nil + } + + solve = func(node *TreeNode, res string) string { + if node == nil { + return "|" + } + + res = fmt.Sprintf("%c", byte('a'+node.Val)) + res + if isLeaf(node) { + return res + } + + left, right := solve(node.Left, res), solve(node.Right, res) + if strings.Compare(left, right) < 0 { + return left + } + + return right + } + + return solve(root, tmp) +} From faf1fc9dddaa601d535a0c3cd438f3de0a18ff4f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 17 Apr 2024 18:02:14 -0700 Subject: [PATCH 921/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 88d2b46..f42e98e 100644 --- a/Medium.md +++ b/Medium.md @@ -332,3 +332,4 @@ |[1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)| Minimum Number of Steps to Make Two Strings Anagram | |[1657](https://leetcode.com/problems/determine-if-two-strings-are-close/)| Determine if Two Strings Are Close | |[2966](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/)| Divide Array Into Arrays With Max Difference | +|[988](https://leetcode.com/problems/smallest-string-starting-from-leaf/)| Smallest String Starting From Leaf | From a7098cd2906dc14cbda9ec5e6045e44bfde2b228 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 19 Apr 2024 16:39:37 -0700 Subject: [PATCH 922/969] Create L200.go --- Medium/L200.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Medium/L200.go diff --git a/Medium/L200.go b/Medium/L200.go new file mode 100644 index 0000000..3d1ff24 --- /dev/null +++ b/Medium/L200.go @@ -0,0 +1,48 @@ +package Medium + +func numIslands(grid [][]byte) int { + dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + cnt, m, n, mp := 0, len(grid), len(grid[0]), make(map[string]bool) + var ( + getKey func(i, j int) string + isValid func(x, y int) bool + solve func(i, j int) + ) + + getKey = func(i, j int) string { + return fmt.Sprintf("%d,%d", i, j) + } + + isValid = func(x, y int) bool { + return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' + } + + solve = func(i, j int) { + if !isValid(i, j) { + return + } + + key := getKey(i, j) + if mp[key] { + return + } + mp[key] = true + + for _, d := range dirs { + solve(i+d[0], j+d[1]) + } + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == '1' { + if _, ok := mp[getKey(i, j)]; !ok { + cnt++ + solve(i, j) + } + } + } + } + + return cnt +} From dd610654b6209538d2bccab4b250450312ad0729 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 19 Apr 2024 16:40:16 -0700 Subject: [PATCH 923/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index f42e98e..175cc34 100644 --- a/Medium.md +++ b/Medium.md @@ -333,3 +333,4 @@ |[1657](https://leetcode.com/problems/determine-if-two-strings-are-close/)| Determine if Two Strings Are Close | |[2966](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/)| Divide Array Into Arrays With Max Difference | |[988](https://leetcode.com/problems/smallest-string-starting-from-leaf/)| Smallest String Starting From Leaf | +|[200](https://leetcode.com/problems/number-of-islands/)| Number of Islands | From 9bee5c490f4ae046542a703a9488460885642ad8 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 20 Apr 2024 16:47:24 -0700 Subject: [PATCH 924/969] Create L1992.go --- Medium/L1992.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Medium/L1992.go diff --git a/Medium/L1992.go b/Medium/L1992.go new file mode 100644 index 0000000..adf482c --- /dev/null +++ b/Medium/L1992.go @@ -0,0 +1,41 @@ +package Medium + +func findFarmland(land [][]int) [][]int { + var ( + lst [][]int + solve func(x, y int) + ) + row, col := len(land), len(land[0]) + + solve = func(x, y int) { + endX, endY := x, y + + for endY+1 < len(land[0]) && land[x][endY+1] == 1 { + land[x][endY+1] = 0 + endY++ + } + + for endX+1 < len(land) && land[endX+1][y] == 1 { + land[endX+1][y] = 0 + endX++ + } + + for i := x; i <= endX; i++ { + for j := y; j <= endY; j++ { + land[i][j] = 0 + } + } + + lst = append(lst, []int{x, y, endX, endY}) + } + + for i := 0; i < row; i++ { + for j := 0; j < col; j++ { + if land[i][j] == 1 { + solve(i, j) + } + } + } + + return lst +} From 76a75d3ac2f55a2596e377d1b51c75da342fdcee Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 20 Apr 2024 16:48:04 -0700 Subject: [PATCH 925/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 175cc34..e23c245 100644 --- a/Medium.md +++ b/Medium.md @@ -334,3 +334,4 @@ |[2966](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/)| Divide Array Into Arrays With Max Difference | |[988](https://leetcode.com/problems/smallest-string-starting-from-leaf/)| Smallest String Starting From Leaf | |[200](https://leetcode.com/problems/number-of-islands/)| Number of Islands | +|[1992](https://leetcode.com/problems/find-all-groups-of-farmland/)| Find All Groups of Farmland | From 9db5340d44421dcbcc2f9190861ae1e2675dc8c4 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 21 Apr 2024 17:32:16 -0700 Subject: [PATCH 926/969] Create L752.go --- Medium/L752.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Medium/L752.go diff --git a/Medium/L752.go b/Medium/L752.go new file mode 100644 index 0000000..5efd4a3 --- /dev/null +++ b/Medium/L752.go @@ -0,0 +1,48 @@ +package Medium + +func openLock(deadends []string, target string) int { + var ( + neighbors func(code string) []string + q []string + ) + deadSet := make(map[string]bool) + for _, dead := range deadends { + deadSet[dead] = true + } + if deadSet["0000"] { + return -1 + } + + neighbors = func(code string) []string { + var res []string + for i := 0; i < 4; i++ { + x := int(code[i] - '0') + for diff := -1; diff <= 1; diff += 2 { + y := (x + diff + 10) % 10 + res = append(res, fmt.Sprintf("%s\"\"%d%s", code[:i], y, code[i+1:])) + } + } + + return res + } + + q = append(q, "0000") + for steps := 0; len(q) > 0; steps++ { + for i := len(q); i > 0; i-- { + curr := q[0] + q = q[1:] + if curr == target { + return steps + } + for _, nei := range neighbors(curr) { + if deadSet[nei] { + continue + } + deadSet[nei] = true + q = append(q, nei) + } + } + } + + return -1 +} From 56b250e274103580f7e45e5029dda45e0ac96eef Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 21 Apr 2024 17:32:54 -0700 Subject: [PATCH 927/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index e23c245..8dd5812 100644 --- a/Medium.md +++ b/Medium.md @@ -335,3 +335,4 @@ |[988](https://leetcode.com/problems/smallest-string-starting-from-leaf/)| Smallest String Starting From Leaf | |[200](https://leetcode.com/problems/number-of-islands/)| Number of Islands | |[1992](https://leetcode.com/problems/find-all-groups-of-farmland/)| Find All Groups of Farmland | +|[752](https://leetcode.com/problems/open-the-lock/)| Open the Lock | From 48a516f6692835a437ae776ae66d9185826f9054 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Apr 2024 16:47:02 -0700 Subject: [PATCH 928/969] Create L514.go --- Hard/L514.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Hard/L514.go diff --git a/Hard/L514.go b/Hard/L514.go new file mode 100644 index 0000000..a9ff544 --- /dev/null +++ b/Hard/L514.go @@ -0,0 +1,43 @@ +package Hard + +func findRotateSteps(ring string, key string) int { + var solve func(keyIdx, ringIdx int) int + mp := make(map[string]int) + + solve = func(keyIdx, ringIdx int) int { + if keyIdx == len(key) { + return 0 + } + + mpKey := fmt.Sprintf("%s%d", keyIdx, ringIdx) + if v, ok := mp[mpKey]; ok { + return v + } + + size, stepsReq, tmp, cnt := len(ring), 1000000000, ringIdx, 0 + + for cnt < size { + if key[keyIdx] == ring[tmp] { + stepsReq = min(stepsReq, 1+cnt+solve(keyIdx+1, tmp)) + } + cnt, tmp = cnt+1, tmp+1 + + tmp %= size + } + + cnt, tmp = 0, ringIdx + for cnt < size { + if key[keyIdx] == ring[tmp] { + stepsReq = min(stepsReq, 1+cnt+solve(keyIdx+1, tmp)) + } + cnt, tmp = cnt+1, tmp-1 + tmp += size + tmp %= size + } + + mp[mpKey] = stepsReq + return stepsReq + } + + return solve(0, 0) +} From 5c8cd4f4118c96581abfe0d180271e387a357991 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Apr 2024 16:47:39 -0700 Subject: [PATCH 929/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index d00feaa..893b052 100644 --- a/Hard.md +++ b/Hard.md @@ -81,3 +81,4 @@ |[1611](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/)| Minimum One Bit Operations to Make Integers Zero| |[1458](https://leetcode.com/problems/max-dot-product-of-two-subsequences/)| Max Dot Product of Two Subsequences| |[1793](https://leetcode.com/problems/maximum-score-of-a-good-subarray/)| Maximum Score of a Good Subarray| +|[514](https://leetcode.com/problems/freedom-trail/)| Freedom Trail| From 6d8cbd43fa2eb8cd6078bdb124f7e59b8dd69a58 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Apr 2024 20:17:41 -0700 Subject: [PATCH 930/969] Create L834.go --- Hard/L834.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Hard/L834.go diff --git a/Hard/L834.go b/Hard/L834.go new file mode 100644 index 0000000..288194c --- /dev/null +++ b/Hard/L834.go @@ -0,0 +1,43 @@ +package Hard + +func sumOfDistancesInTree(n int, edges [][]int) []int { + var ( + dfs1 func(node, parent int) + dfs2 func(node, parent, dpVal int) + ) + adjList, sz, val, ans := make([][]int, n), make([]int, n), make([]int, n), make([]int, n) + for i := 0; i < n; i++ { + adjList[i] = make([]int, 0) + } + + for _, edge := range edges { + adjList[edge[0]] = append(adjList[edge[0]], edge[1]) + adjList[edge[1]] = append(adjList[edge[1]], edge[0]) + } + + dfs1 = func(node, parent int) { + for _, c := range adjList[node] { + if c != parent { + dfs1(c, node) + sz[node] += sz[c] + val[node] += val[c] + sz[c] + } + } + sz[node]++ + } + + dfs2 = func(node, parent, dpVal int) { + ans[node] = val[node] + dpVal + (n - sz[node]) + + for _, c := range adjList[node] { + if c != parent { + dfs2(c, node, ans[node]-val[c]-sz[c]) + } + } + } + + dfs1(0, 0) + dfs2(0, 0, 0) + + return ans +} From de4c6038988178a39ca7800830b39f75f794caea Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Apr 2024 20:18:22 -0700 Subject: [PATCH 931/969] Update Hard.md --- Hard.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Hard.md b/Hard.md index 893b052..8d0115c 100644 --- a/Hard.md +++ b/Hard.md @@ -82,3 +82,4 @@ |[1458](https://leetcode.com/problems/max-dot-product-of-two-subsequences/)| Max Dot Product of Two Subsequences| |[1793](https://leetcode.com/problems/maximum-score-of-a-good-subarray/)| Maximum Score of a Good Subarray| |[514](https://leetcode.com/problems/freedom-trail/)| Freedom Trail| +|[834](https://leetcode.com/problems/sum-of-distances-in-tree/)| Sum of Distances in Tree| From 5d4585d0b6f5bca8281117d280c0a1ed24ff44bd Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 29 Apr 2024 16:44:56 -0700 Subject: [PATCH 932/969] Create L2997.go --- Medium/L2997.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Medium/L2997.go diff --git a/Medium/L2997.go b/Medium/L2997.go new file mode 100644 index 0000000..3846a53 --- /dev/null +++ b/Medium/L2997.go @@ -0,0 +1,20 @@ +package Medium + +func minOperations(nums []int, k int) int { + finalXor := 0 + for _, n := range nums { + finalXor ^= n + } + + cnt := 0 + for k > 0 || finalXor > 0 { + if (k % 2) != (finalXor % 2) { + cnt++ + } + + k /= 2 + finalXor /= 2 + } + + return cnt +} From ef1c2bd4227098ad1517d38af1da545db58b59a5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 29 Apr 2024 16:45:32 -0700 Subject: [PATCH 933/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 8dd5812..d1628a1 100644 --- a/Medium.md +++ b/Medium.md @@ -336,3 +336,4 @@ |[200](https://leetcode.com/problems/number-of-islands/)| Number of Islands | |[1992](https://leetcode.com/problems/find-all-groups-of-farmland/)| Find All Groups of Farmland | |[752](https://leetcode.com/problems/open-the-lock/)| Open the Lock | +|[2997](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k/)| Minimum Number of Operations to Make Array XOR Equal to K | From 398df3ac2bb9ac4e3347d7304d0f8141c0cbff11 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Apr 2024 18:58:40 -0700 Subject: [PATCH 934/969] Create L2000.go --- Easy/L2000.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Easy/L2000.go diff --git a/Easy/L2000.go b/Easy/L2000.go new file mode 100644 index 0000000..333db46 --- /dev/null +++ b/Easy/L2000.go @@ -0,0 +1,21 @@ +package Easy + +func reversePrefix(word string, ch byte) string { + for i := 0; i < len(word); i++ { + if word[i] == ch { + return reverse(word[:i+1]) + word[i+1:] + } + } + + return word +} + +func reverse(s string) string { + n := len(s) + runes := make([]rune, n) + for _, ch := range s { + n-- + runes[n] = ch + } + return string(runes) +} From df4d7ebb50dc88327b319de7a0464b6cb69cfa70 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Apr 2024 18:59:13 -0700 Subject: [PATCH 935/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index db1b36e..2f72d2a 100644 --- a/Easy.md +++ b/Easy.md @@ -172,3 +172,4 @@ |[119](https://leetcode.com/problems/pascals-triangle-ii/)| Pascal's Triangle II| |[1700](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)| Number of Students Unable to Eat Lunch| |[678](https://leetcode.com/problems/valid-parenthesis-string/)| Valid Parenthesis String| +|[2000](https://leetcode.com/problems/reverse-prefix-of-word/)| Reverse Prefix of Word| From ab650a30880c42a7976dc5eba2aaeac626df0920 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Apr 2024 19:18:49 -0700 Subject: [PATCH 936/969] Create L1915.go --- Medium/L1915.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Medium/L1915.go diff --git a/Medium/L1915.go b/Medium/L1915.go new file mode 100644 index 0000000..2d9376f --- /dev/null +++ b/Medium/L1915.go @@ -0,0 +1,21 @@ +package Medium + +func wonderfulSubstrings(word string) int64 { + var ( + res int64 + mask int + ) + cnt := make([]int64, 1024) + cnt[0] = 1 + + for i := 0; i < len(word); i++ { + mask ^= 1 << (word[i] - 'a') + res += cnt[mask] + for i := 0; i < 10; i++ { + res += cnt[mask ^ (1 << i)] + } + cnt[mask]++ + } + + return res +} From b2e32ea6a2bb0df8f0f9e3344035fc6c0919b226 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Apr 2024 19:19:36 -0700 Subject: [PATCH 937/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index d1628a1..e9df3ab 100644 --- a/Medium.md +++ b/Medium.md @@ -337,3 +337,4 @@ |[1992](https://leetcode.com/problems/find-all-groups-of-farmland/)| Find All Groups of Farmland | |[752](https://leetcode.com/problems/open-the-lock/)| Open the Lock | |[2997](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k/)| Minimum Number of Operations to Make Array XOR Equal to K | +|[1915](https://leetcode.com/problems/number-of-wonderful-substrings/)| Number of Wonderful Substrings | From 179306de1e479822b8b6e0406cd5bdc025b9a7f7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 May 2024 16:56:30 -0700 Subject: [PATCH 938/969] Create L2441.go --- Easy/L2441.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Easy/L2441.go diff --git a/Easy/L2441.go b/Easy/L2441.go new file mode 100644 index 0000000..5a9abef --- /dev/null +++ b/Easy/L2441.go @@ -0,0 +1,22 @@ +package Easy + +func findMaxK(nums []int) int { + mp, res := make(map[int]bool), -1 + + for _, n := range nums { + if mp[-n] { + res = max(res, abs(n)) + } + mp[n] = true + } + + return res +} + +func abs(a int) int { + if a < 0 { + return -a + } + + return a +} From 53e398cd3953aadfe3ec30f008833c130434ae42 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 May 2024 16:57:16 -0700 Subject: [PATCH 939/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 2f72d2a..4999300 100644 --- a/Easy.md +++ b/Easy.md @@ -173,3 +173,4 @@ |[1700](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)| Number of Students Unable to Eat Lunch| |[678](https://leetcode.com/problems/valid-parenthesis-string/)| Valid Parenthesis String| |[2000](https://leetcode.com/problems/reverse-prefix-of-word/)| Reverse Prefix of Word| +|[2441](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/)| Largest Positive Integer That Exists With Its Negative| From 33802e437aaa887c4476f62eee43e7c1d446e04c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 May 2024 16:57:56 -0700 Subject: [PATCH 940/969] Create L165.go --- Medium/L165.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L165.go diff --git a/Medium/L165.go b/Medium/L165.go new file mode 100644 index 0000000..34e005c --- /dev/null +++ b/Medium/L165.go @@ -0,0 +1,27 @@ +package Medium + +func compareVersion(version1 string, version2 string) int { + tmp1, tmp2, len1, len2 := 0, 0, len(version1), len(version2) + i, j := 0, 0 + + for i < len1 || j < len2 { + tmp1, tmp2 = 0, 0 + for i < len1 && version1[i] != '.' { + tmp1 = tmp1*10 + int(version1[i]-'0') + i++ + } + for j < len2 && version2[j] != '.' { + tmp2 = tmp2*10 + int(version2[j]-'0') + j++ + } + if tmp1 > tmp2 { + return 1 + } else if tmp1 < tmp2 { + return -1 + } else { + i, j = i+1, j+1 + } + } + + return 0 +} From de88c9acc7cd666d753392b996077ee5b9d1758e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 3 May 2024 16:58:45 -0700 Subject: [PATCH 941/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index e9df3ab..f5bc992 100644 --- a/Medium.md +++ b/Medium.md @@ -338,3 +338,4 @@ |[752](https://leetcode.com/problems/open-the-lock/)| Open the Lock | |[2997](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k/)| Minimum Number of Operations to Make Array XOR Equal to K | |[1915](https://leetcode.com/problems/number-of-wonderful-substrings/)| Number of Wonderful Substrings | +|[165](https://leetcode.com/problems/compare-version-numbers/)| Compare Version Numbers | From 3b7645ec89479ce818731f01e843469597caaa3f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 May 2024 16:17:21 -0700 Subject: [PATCH 942/969] Create L1492.go --- Medium/L1492.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L1492.go diff --git a/Medium/L1492.go b/Medium/L1492.go new file mode 100644 index 0000000..ce11cd8 --- /dev/null +++ b/Medium/L1492.go @@ -0,0 +1,25 @@ +package Medium + +func kthFactor(n int, k int) int { + root := math.Sqrt(float64(n)) + + for i := 1; i < int(math.Ceil(root)); i++ { + if n%i == 0 { + k-- + if k == 0 { + return i + } + } + } + + for i := int(root); i > 0; i-- { + if n%i == 0 { + k-- + if k == 0 { + return n / i + } + } + } + + return -1 +} From 175dcb04cf22794069b1828ea4ffac29a1207800 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 6 May 2024 16:18:11 -0700 Subject: [PATCH 943/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index f5bc992..773e217 100644 --- a/Medium.md +++ b/Medium.md @@ -339,3 +339,4 @@ |[2997](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k/)| Minimum Number of Operations to Make Array XOR Equal to K | |[1915](https://leetcode.com/problems/number-of-wonderful-substrings/)| Number of Wonderful Substrings | |[165](https://leetcode.com/problems/compare-version-numbers/)| Compare Version Numbers | +|[1492](https://leetcode.com/problems/the-kth-factor-of-n/)| The kth Factor of n | From 277f77c43354913c14a3262f07eb71ccc56db5d3 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 May 2024 16:45:00 -0700 Subject: [PATCH 944/969] Create L2373.go --- Easy/L2373.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Easy/L2373.go diff --git a/Easy/L2373.go b/Easy/L2373.go new file mode 100644 index 0000000..188b7cc --- /dev/null +++ b/Easy/L2373.go @@ -0,0 +1,25 @@ +package Easy + +func largestLocal(grid [][]int) [][]int { + n := len(grid) + res := make([][]int, n-2) + for i := 0; i < len(res); i++ { + res[i] = make([]int, n-2) + } + + for i := 1; i < n-1; i++ { + for j := 1; j < n-1; j++ { + tmp := 0 + + for k := i - 1; k <= i + 1; k++ { + for l := j - 1; l <= j + 1; l++ { + tmp = max(tmp, grid[k][l]) + } + } + + res[i-1][j-1] = tmp + } + } + + return res +} From f0e1959cdc1e22349a96e520719e24137679ff65 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sun, 12 May 2024 16:45:44 -0700 Subject: [PATCH 945/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 4999300..7ad34f9 100644 --- a/Easy.md +++ b/Easy.md @@ -174,3 +174,4 @@ |[678](https://leetcode.com/problems/valid-parenthesis-string/)| Valid Parenthesis String| |[2000](https://leetcode.com/problems/reverse-prefix-of-word/)| Reverse Prefix of Word| |[2441](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/)| Largest Positive Integer That Exists With Its Negative| +|[2373](https://leetcode.com/problems/largest-local-values-in-a-matrix/)| Largest Local Values in a Matrix| From f771edf2221cd126b10d00469eaa4eb2870a2322 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 May 2024 18:30:13 -0700 Subject: [PATCH 946/969] Create L861.go --- Medium/L861.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Medium/L861.go diff --git a/Medium/L861.go b/Medium/L861.go new file mode 100644 index 0000000..06c031a --- /dev/null +++ b/Medium/L861.go @@ -0,0 +1,19 @@ +package Medium + +func matrixScore(grid [][]int) int { + m, n, res := len(grid), len(grid[0]), 0 + + res += (1 << (n - 1)) * m + + for j := 1; j < n; j++ { + same := 0 + for i := 0; i < m; i++ { + if grid[i][0] == grid[i][j] { + same++ + } + } + res += (1 << (n - 1 - j))*max(same, m - same) + } + + return res +} From 6f7c07a79c83336b9bdff3349b7c5f4591a78707 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Mon, 13 May 2024 18:30:51 -0700 Subject: [PATCH 947/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 773e217..a55c55a 100644 --- a/Medium.md +++ b/Medium.md @@ -340,3 +340,4 @@ |[1915](https://leetcode.com/problems/number-of-wonderful-substrings/)| Number of Wonderful Substrings | |[165](https://leetcode.com/problems/compare-version-numbers/)| Compare Version Numbers | |[1492](https://leetcode.com/problems/the-kth-factor-of-n/)| The kth Factor of n | +|[861](https://leetcode.com/problems/score-after-flipping-matrix/)| Score After Flipping Matrix | From 81f5d5bc1a280d32b0758d16065026c2f2de0306 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 14 May 2024 18:15:24 -0700 Subject: [PATCH 948/969] Create L1219.go --- Medium/L1219.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Medium/L1219.go diff --git a/Medium/L1219.go b/Medium/L1219.go new file mode 100644 index 0000000..476733c --- /dev/null +++ b/Medium/L1219.go @@ -0,0 +1,40 @@ +package Medium + +func getMaximumGold(grid [][]int) int { + var ( + isValid func(x, y int) bool + solve func(x, y int) int + ) + m, n, res := len(grid), len(grid[0]), 0 + dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} + + isValid = func(x, y int) bool { + return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] != 0 + } + + solve = func(x, y int) int { + if !isValid(x, y) { + return 0 + } + + curr := grid[x][y] + grid[x][y] = 0 + maxGold := 0 + for _, d := range dirs { + maxGold = max(maxGold, solve(x+d[0], y+d[1])) + } + + grid[x][y] = curr + return curr + maxGold + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] != 0 { + res = max(res, solve(i, j)) + } + } + } + + return res +} From 7787273b67c2fbffbe801ed6db6601ed752e66e1 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 14 May 2024 18:16:01 -0700 Subject: [PATCH 949/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index a55c55a..848e6c8 100644 --- a/Medium.md +++ b/Medium.md @@ -341,3 +341,4 @@ |[165](https://leetcode.com/problems/compare-version-numbers/)| Compare Version Numbers | |[1492](https://leetcode.com/problems/the-kth-factor-of-n/)| The kth Factor of n | |[861](https://leetcode.com/problems/score-after-flipping-matrix/)| Score After Flipping Matrix | +|[1219](https://leetcode.com/problems/path-with-maximum-gold/)| Path with Maximum Gold | From 09406800103a188aa9cb49bd900c9c0014ab7779 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 May 2024 16:51:49 -0700 Subject: [PATCH 950/969] Create L2331.go --- Easy/L2331.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Easy/L2331.go diff --git a/Easy/L2331.go b/Easy/L2331.go new file mode 100644 index 0000000..0a895c1 --- /dev/null +++ b/Easy/L2331.go @@ -0,0 +1,39 @@ +package Easy + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func evaluateTree(root *TreeNode) bool { + var solve func(root *TreeNode) bool + mp := map[int]bool{ + 0: false, + 1: true, + } + + solve = func(node *TreeNode) bool { + if node.Right == nil && node.Left == nil { + return mp[node.Val] + } + + var left, right bool + if node.Left != nil { + left = solve(node.Left) + } + if node.Right != nil { + right = solve(node.Right) + } + + if node.Val == 2 { + return left || right + } + + return left && right + } + + return solve(root) +} From 04f648502dddac646956271f6b5577af0213c3dc Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 May 2024 16:53:16 -0700 Subject: [PATCH 951/969] Create L1325.go --- Medium/L1325.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L1325.go diff --git a/Medium/L1325.go b/Medium/L1325.go new file mode 100644 index 0000000..bf87c90 --- /dev/null +++ b/Medium/L1325.go @@ -0,0 +1,24 @@ +package Medium + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func removeLeafNodes(root *TreeNode, target int) *TreeNode { + if root.Left != nil { + root.Left = removeLeafNodes(root.Left, target) + } + if root.Right != nil { + root.Right = removeLeafNodes(root.Right, target) + } + + if root.Left == root.Right && root.Val == target { + return nil + } + + return root +} From 09a188263efe072cf7b3ceb3b8c55fc99b1909d9 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 May 2024 16:54:02 -0700 Subject: [PATCH 952/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 848e6c8..6e803af 100644 --- a/Medium.md +++ b/Medium.md @@ -342,3 +342,4 @@ |[1492](https://leetcode.com/problems/the-kth-factor-of-n/)| The kth Factor of n | |[861](https://leetcode.com/problems/score-after-flipping-matrix/)| Score After Flipping Matrix | |[1219](https://leetcode.com/problems/path-with-maximum-gold/)| Path with Maximum Gold | +|[1325](https://leetcode.com/problems/delete-leaves-with-a-given-value/)| Delete Leaves With a Given Value | From 8da366588a4c97ffed393d7516b37e15b1a51a55 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 17 May 2024 16:55:00 -0700 Subject: [PATCH 953/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index 7ad34f9..ba5788d 100644 --- a/Easy.md +++ b/Easy.md @@ -175,3 +175,4 @@ |[2000](https://leetcode.com/problems/reverse-prefix-of-word/)| Reverse Prefix of Word| |[2441](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/)| Largest Positive Integer That Exists With Its Negative| |[2373](https://leetcode.com/problems/largest-local-values-in-a-matrix/)| Largest Local Values in a Matrix| +|[2331](https://leetcode.com/problems/evaluate-boolean-binary-tree/)| Evaluate Boolean Binary Tree| From c7361928f0adf64da177b8e736dfb3413a955b4e Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Jun 2024 07:08:49 -0700 Subject: [PATCH 954/969] Create L1382.go --- Medium/L1382.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Medium/L1382.go diff --git a/Medium/L1382.go b/Medium/L1382.go new file mode 100644 index 0000000..e320fd9 --- /dev/null +++ b/Medium/L1382.go @@ -0,0 +1,32 @@ +package Medium + +func balanceBST(root *TreeNode) *TreeNode { + var ( + sortedLst []*TreeNode + inorder func(node *TreeNode) + createTree func(start, end int) *TreeNode + ) + + inorder = func(node *TreeNode) { + if node == nil { + return + } + inorder(node.Left) + sortedLst = append(sortedLst, node) + inorder(node.Right) + } + + createTree = func(start, end int) *TreeNode { + if start > end { + return nil + } + mid := start + (end-start)/2 + newRoot := sortedLst[mid] + newRoot.Left = createTree(start, mid - 1) + newRoot.Right = createTree(mid + 1, end) + return newRoot + } + + inorder(root) + return createTree(0, len(sortedLst) - 1) +} From 848f829e2e70640affaf0ae4333d2f688e3b5e7c Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 26 Jun 2024 07:09:28 -0700 Subject: [PATCH 955/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 6e803af..f79fbd4 100644 --- a/Medium.md +++ b/Medium.md @@ -343,3 +343,4 @@ |[861](https://leetcode.com/problems/score-after-flipping-matrix/)| Score After Flipping Matrix | |[1219](https://leetcode.com/problems/path-with-maximum-gold/)| Path with Maximum Gold | |[1325](https://leetcode.com/problems/delete-leaves-with-a-given-value/)| Delete Leaves With a Given Value | +|[1382](https://leetcode.com/problems/balance-a-binary-search-tree/)| Balance a Binary Search Tree | From bf077b3f4b8b41379a33437f1bca992125ec0c08 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Jul 2024 13:50:49 -0700 Subject: [PATCH 956/969] Create L2390.py --- Medium/L2390.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Medium/L2390.py diff --git a/Medium/L2390.py b/Medium/L2390.py new file mode 100644 index 0000000..d3eba30 --- /dev/null +++ b/Medium/L2390.py @@ -0,0 +1,11 @@ +class Solution: + def removeStars(self, s: str) -> str: + st = [] + + for c in s: + if c != '*': + st.append(c) + elif st: + st.pop() + + return ''.join(st) From 6131ca398642ca2eafdf0a6318de0644ad2f3794 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Jul 2024 13:51:33 -0700 Subject: [PATCH 957/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index f79fbd4..91aea24 100644 --- a/Medium.md +++ b/Medium.md @@ -344,3 +344,4 @@ |[1219](https://leetcode.com/problems/path-with-maximum-gold/)| Path with Maximum Gold | |[1325](https://leetcode.com/problems/delete-leaves-with-a-given-value/)| Delete Leaves With a Given Value | |[1382](https://leetcode.com/problems/balance-a-binary-search-tree/)| Balance a Binary Search Tree | +|[2390](https://leetcode.com/problems/removing-stars-from-a-string/)| Removing Stars From a String | From 2bba28b65e5426995a3f02cb2906a2f1fb85de35 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 27 Jul 2024 14:32:07 -0700 Subject: [PATCH 958/969] Update L2390.py --- Medium/L2390.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Medium/L2390.py b/Medium/L2390.py index d3eba30..4b085dd 100644 --- a/Medium/L2390.py +++ b/Medium/L2390.py @@ -1,11 +1,15 @@ -class Solution: - def removeStars(self, s: str) -> str: - st = [] +package Medium - for c in s: - if c != '*': - st.append(c) - elif st: - st.pop() +func removeStars(s string) string { + st := []rune{} - return ''.join(st) + for _, c := range s { + if c != '*' { + st = append(st, c) + } else if len(st) > 0 { + st = st[:len(st)-1] + } + } + + return string(st) +} From a9ad4b4ad491bcc9518805c5b63d94be956de75f Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Jul 2024 18:48:43 -0700 Subject: [PATCH 959/969] Create L1834.py --- Medium/L1834.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Medium/L1834.py diff --git a/Medium/L1834.py b/Medium/L1834.py new file mode 100644 index 0000000..02424eb --- /dev/null +++ b/Medium/L1834.py @@ -0,0 +1,27 @@ +class Solution: + def getOrder(self, tasks: List[List[int]]) -> List[int]: + dic=defaultdict(list) + + for i in range(len(tasks)): + dic[tasks[i][0]].append((tasks[i][1],i)) + + + ans=[] + keys=sorted(dic.keys()) + + while keys: + k=keys.pop(0) + pq=dic[k] + heapq.heapify(pq) + time=k + + while pq: + p_time,ind=heapq.heappop(pq) + ans.append(ind) + time+=p_time + while keys: + if keys[0]>time: + break + for item in dic[keys.pop(0)]: + heapq.heappush(pq,item) + return ans From d2184fda748869ecbca936b26292e5bd0668b1c7 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Jul 2024 18:51:36 -0700 Subject: [PATCH 960/969] Update and rename L1834.py to L1834.go --- Medium/L1834.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ Medium/L1834.py | 27 ----------------------- 2 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 Medium/L1834.go delete mode 100644 Medium/L1834.py diff --git a/Medium/L1834.go b/Medium/L1834.go new file mode 100644 index 0000000..69e9408 --- /dev/null +++ b/Medium/L1834.go @@ -0,0 +1,58 @@ +type Task struct { + processTime int + index int +} + +type TaskHeap []Task + +func (h TaskHeap) Len() int { return len(h) } +func (h TaskHeap) Less(i, j int) bool { return h[i].processTime < h[j].processTime || (h[i].processTime == h[j].processTime && h[i].index < h[j].index) } +func (h TaskHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } + +func (h *TaskHeap) Push(x interface{}) { + *h = append(*h, x.(Task)) +} + +func (h *TaskHeap) Pop() interface{} { + old := *h + n := len(old) + item := old[n-1] + *h = old[0 : n-1] + return item +} + +func getOrder(tasks [][]int) []int { + dic := make(map[int][]Task) + for i := 0; i < len(tasks); i++ { + dic[tasks[i][0]] = append(dic[tasks[i][0]], Task{tasks[i][1], i}) + } + + ans := []int{} + keys := []int{} + for k := range dic { + keys = append(keys, k) + } + sort.Ints(keys) + + for len(keys) > 0 { + k := keys[0] + keys = keys[1:] + pq := TaskHeap(dic[k]) + heap.Init(&pq) + time := k + + for pq.Len() > 0 { + task := heap.Pop(&pq).(Task) + ans = append(ans, task.index) + time += task.processTime + for len(keys) > 0 && keys[0] <= time { + for _, item := range dic[keys[0]] { + heap.Push(&pq, item) + } + keys = keys[1:] + } + } + } + + return ans +} diff --git a/Medium/L1834.py b/Medium/L1834.py deleted file mode 100644 index 02424eb..0000000 --- a/Medium/L1834.py +++ /dev/null @@ -1,27 +0,0 @@ -class Solution: - def getOrder(self, tasks: List[List[int]]) -> List[int]: - dic=defaultdict(list) - - for i in range(len(tasks)): - dic[tasks[i][0]].append((tasks[i][1],i)) - - - ans=[] - keys=sorted(dic.keys()) - - while keys: - k=keys.pop(0) - pq=dic[k] - heapq.heapify(pq) - time=k - - while pq: - p_time,ind=heapq.heappop(pq) - ans.append(ind) - time+=p_time - while keys: - if keys[0]>time: - break - for item in dic[keys.pop(0)]: - heapq.heappush(pq,item) - return ans From 209b5618a3901d760095ad065471e6d3b3631086 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Tue, 30 Jul 2024 18:52:13 -0700 Subject: [PATCH 961/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 91aea24..cc85c1c 100644 --- a/Medium.md +++ b/Medium.md @@ -345,3 +345,4 @@ |[1325](https://leetcode.com/problems/delete-leaves-with-a-given-value/)| Delete Leaves With a Given Value | |[1382](https://leetcode.com/problems/balance-a-binary-search-tree/)| Balance a Binary Search Tree | |[2390](https://leetcode.com/problems/removing-stars-from-a-string/)| Removing Stars From a String | +|[1834](https://leetcode.com/problems/single-threaded-cpu/)| Single-Threaded CPU | From 610c2f9810551df6f6530d45cd1964b81dd6f493 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 31 Jul 2024 17:18:03 -0700 Subject: [PATCH 962/969] Create L2.go --- Medium/L2.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/L2.go diff --git a/Medium/L2.go b/Medium/L2.go new file mode 100644 index 0000000..9a9135b --- /dev/null +++ b/Medium/L2.go @@ -0,0 +1,24 @@ +package Meidum + +func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { + dummy := &ListNode{} + current := dummy + carry := 0 + + for l1 != nil || l2 != nil || carry != 0 { + sum := carry + if l1 != nil { + sum += l1.Val + l1 = l1.Next + } + if l2 != nil { + sum += l2.Val + l2 = l2.Next + } + current.Next = &ListNode{Val: sum % 10} + current = current.Next + carry = sum / 10 + } + + return dummy.Next +} From 48de9397ea0bb55a6931c2c7c070ae011445c9d5 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 31 Jul 2024 17:18:44 -0700 Subject: [PATCH 963/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index cc85c1c..20fa7f0 100644 --- a/Medium.md +++ b/Medium.md @@ -346,3 +346,4 @@ |[1382](https://leetcode.com/problems/balance-a-binary-search-tree/)| Balance a Binary Search Tree | |[2390](https://leetcode.com/problems/removing-stars-from-a-string/)| Removing Stars From a String | |[1834](https://leetcode.com/problems/single-threaded-cpu/)| Single-Threaded CPU | +|[2](https://leetcode.com/problems/add-two-numbers/)| Add Two Numbers | From 04bfa912cbff09316fdda9d24294275b015f2893 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 2 Aug 2024 17:59:13 -0700 Subject: [PATCH 964/969] Create L1460.py --- Easy/L1460.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Easy/L1460.py diff --git a/Easy/L1460.py b/Easy/L1460.py new file mode 100644 index 0000000..4bb0bd5 --- /dev/null +++ b/Easy/L1460.py @@ -0,0 +1,3 @@ +class Solution: + def canBeEqual(self, target: List[int], arr: List[int]) -> bool: + return collections.Counter(target) == collections.Counter(arr) From 2c285b140dbf04aed8c04bf2636818d4c139dd57 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Fri, 2 Aug 2024 18:00:51 -0700 Subject: [PATCH 965/969] Delete Easy/L1460.py Delete Py solution --- Easy/L1460.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Easy/L1460.py diff --git a/Easy/L1460.py b/Easy/L1460.py deleted file mode 100644 index 4bb0bd5..0000000 --- a/Easy/L1460.py +++ /dev/null @@ -1,3 +0,0 @@ -class Solution: - def canBeEqual(self, target: List[int], arr: List[int]) -> bool: - return collections.Counter(target) == collections.Counter(arr) From 6fc88540b330429f9486f874ea87dafde0b6b120 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 15 Aug 2024 16:45:47 -0700 Subject: [PATCH 966/969] Create L860.py --- Easy/L860.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Easy/L860.py diff --git a/Easy/L860.py b/Easy/L860.py new file mode 100644 index 0000000..5473705 --- /dev/null +++ b/Easy/L860.py @@ -0,0 +1,17 @@ +class Solution: + def lemonadeChange(self, bills): + five = ten = 0 + for num in bills: + if num == 5: + five += 1 + elif num == 10 and five: + ten += 1 + five -= 1 + elif num == 20 and five and ten: + five -= 1 + ten -= 1 + elif num == 20 and five >= 3: + five -= 3 + else: + return False + return True From 7bcd508fd064f9bec64ab4b28e9b5043af73f387 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Thu, 15 Aug 2024 16:46:19 -0700 Subject: [PATCH 967/969] Update Easy.md --- Easy.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Easy.md b/Easy.md index ba5788d..540a506 100644 --- a/Easy.md +++ b/Easy.md @@ -176,3 +176,4 @@ |[2441](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/)| Largest Positive Integer That Exists With Its Negative| |[2373](https://leetcode.com/problems/largest-local-values-in-a-matrix/)| Largest Local Values in a Matrix| |[2331](https://leetcode.com/problems/evaluate-boolean-binary-tree/)| Evaluate Boolean Binary Tree| +|[860](https://leetcode.com/problems/lemonade-change/)| Lemonade Change| From 2c7509ed5282145db90f0e8d5a9cc982d68abd6d Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Sep 2024 16:27:04 -0700 Subject: [PATCH 968/969] Create L874.py --- Medium/L874.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/L874.py diff --git a/Medium/L874.py b/Medium/L874.py new file mode 100644 index 0000000..66f4f60 --- /dev/null +++ b/Medium/L874.py @@ -0,0 +1,25 @@ +class Solution: + def robotSim(self, commands, obstacles): + obstacle_set = set() + for obs in obstacles: + obstacle_set.add(f"{obs[0]} {obs[1]}") + + directions = [[0, 1], [1, 0], [0, -1], [-1, 0]] # north, east, south, west + d = 0 # direction index + x, y = 0, 0 # initial position + result = 0 + + for command in commands: + if command == -1: # turn right + d = (d + 1) % 4 + elif command == -2: # turn left + d = (d - 1) % 4 + else: + for _ in range(command): + next_x = x + directions[d][0] + next_y = y + directions[d][1] + if f"{next_x} {next_y}" not in obstacle_set: + x, y = next_x, next_y + result = max(result, x * x + y * y) + + return result From 8481175e0d42a92e165081e3475de3d31fcd6e98 Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Wed, 4 Sep 2024 16:27:42 -0700 Subject: [PATCH 969/969] Update Medium.md --- Medium.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Medium.md b/Medium.md index 20fa7f0..b14a62e 100644 --- a/Medium.md +++ b/Medium.md @@ -347,3 +347,4 @@ |[2390](https://leetcode.com/problems/removing-stars-from-a-string/)| Removing Stars From a String | |[1834](https://leetcode.com/problems/single-threaded-cpu/)| Single-Threaded CPU | |[2](https://leetcode.com/problems/add-two-numbers/)| Add Two Numbers | +|[874](https://leetcode.com/problems/walking-robot-simulation/)| Walking Robot Simulation |