From 4c13a39af1d2702546b0915697513885289896df Mon Sep 17 00:00:00 2001 From: Ganesh S Kudva Date: Sat, 18 Feb 2023 08:04:23 -0800 Subject: [PATCH 001/279] 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 002/279] 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 003/279] 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 004/279] 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 005/279] 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 006/279] 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 007/279] 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 008/279] 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 009/279] 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 010/279] 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 011/279] 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 012/279] 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 013/279] 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 014/279] 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 015/279] 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 016/279] 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 017/279] 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 018/279] 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 019/279] 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 020/279] 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 021/279] 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 022/279] 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 023/279] 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 024/279] 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 025/279] 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 026/279] 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 027/279] 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 028/279] 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 029/279] 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 030/279] 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 031/279] 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 032/279] 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 033/279] 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 034/279] 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 035/279] 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 036/279] 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 037/279] 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 038/279] 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 039/279] 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 040/279] 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 041/279] 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 042/279] 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 043/279] 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 044/279] 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 045/279] 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 046/279] 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 047/279] 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 048/279] 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 049/279] 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 050/279] 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 051/279] 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 052/279] 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 053/279] 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 054/279] 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 055/279] 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 056/279] 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 057/279] 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 058/279] 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 059/279] 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 060/279] 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 061/279] 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 062/279] 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 063/279] 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 064/279] 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 065/279] 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 066/279] 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 067/279] 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 068/279] 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 069/279] 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 070/279] 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 071/279] 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 072/279] 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 073/279] 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 074/279] 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 075/279] 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 076/279] 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 077/279] 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 078/279] 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 079/279] 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 080/279] 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 081/279] 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 082/279] 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 083/279] 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 084/279] 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 085/279] 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 086/279] 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 087/279] 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 088/279] 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 089/279] 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 090/279] 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 091/279] 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 092/279] 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 093/279] 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 094/279] 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 095/279] 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 096/279] 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 097/279] 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 098/279] 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 099/279] 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 100/279] 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 101/279] 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 102/279] 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 103/279] 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 104/279] 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 105/279] 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 106/279] 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 107/279] 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 108/279] 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 109/279] 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 110/279] 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 111/279] 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 112/279] 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 113/279] 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 114/279] 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 115/279] 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 116/279] 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 117/279] 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 118/279] 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 119/279] 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 120/279] 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 121/279] 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 122/279] 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 123/279] 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 124/279] 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 125/279] 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 126/279] 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 127/279] 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 128/279] 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 129/279] 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 130/279] 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 131/279] 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 132/279] 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 133/279] 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 134/279] 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 135/279] 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 136/279] 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 137/279] 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 138/279] 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 139/279] 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 140/279] 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 141/279] 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 142/279] 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 143/279] 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 144/279] 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 145/279] 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 146/279] 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 147/279] 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 148/279] 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 149/279] 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 150/279] 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 151/279] 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 152/279] 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 153/279] 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 154/279] 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 155/279] 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 156/279] 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 157/279] 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 158/279] 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 159/279] 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 160/279] 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 161/279] 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 162/279] 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 163/279] 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 164/279] 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 165/279] 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 166/279] 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 167/279] 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 168/279] 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 169/279] 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 170/279] 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 171/279] 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 172/279] 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 173/279] 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 174/279] 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 175/279] 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 176/279] 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 177/279] 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 178/279] 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 179/279] 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 180/279] 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 181/279] 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 182/279] 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 183/279] 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 184/279] 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 185/279] 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 186/279] 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 187/279] 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 188/279] 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 189/279] 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 190/279] 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 191/279] 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 192/279] 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 193/279] 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 194/279] 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 195/279] 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 196/279] 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 197/279] 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 198/279] 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 199/279] 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 200/279] 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 201/279] 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 202/279] 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 203/279] 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 204/279] 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 205/279] 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 206/279] 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 207/279] 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 208/279] 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 209/279] 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 210/279] 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 211/279] 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 212/279] 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 213/279] 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 214/279] 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 215/279] 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 216/279] 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 217/279] 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 218/279] 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 219/279] 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 220/279] 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 221/279] 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 222/279] 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 223/279] 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 224/279] 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 225/279] 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 226/279] 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 227/279] 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 228/279] 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 229/279] 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 230/279] 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 231/279] 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 232/279] 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 233/279] 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 234/279] 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 235/279] 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 236/279] 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 237/279] 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 238/279] 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 239/279] 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 240/279] 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 241/279] 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 242/279] 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 243/279] 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 244/279] 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 245/279] 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 246/279] 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 247/279] 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 248/279] 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 249/279] 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 250/279] 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 251/279] 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 252/279] 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 253/279] 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 254/279] 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 255/279] 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 256/279] 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 257/279] 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 258/279] 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 259/279] 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 260/279] 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 261/279] 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 262/279] 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 263/279] 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 264/279] 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 265/279] 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 266/279] 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 267/279] 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 268/279] 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 269/279] 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 270/279] 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 271/279] 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 272/279] 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 273/279] 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 274/279] 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 275/279] 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 276/279] 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 277/279] 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 278/279] 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 279/279] 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 |