From a3fe15de7976ccb68f1cc4ec17c6c675b6783f58 Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Wed, 25 Dec 2024 20:28:19 +0530 Subject: [PATCH 01/10] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af19992..b558173 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # leetcode-solutions -This repository contains detailed solutions to popular leetcode problems in multiple programming languages. \ No newline at end of file +This repository contains detailed solutions to popular leetcode problems in multiple programming languages. + +Check out the full list of problems categorized by topics and patterns at [algomaster.io](https://algomaster.io/practice/dsa-patterns) From d3e2157e5c05341194774930e56a3511fe90b775 Mon Sep 17 00:00:00 2001 From: Hiten Sharma Date: Sat, 18 Jan 2025 14:55:01 +0000 Subject: [PATCH 02/10] :adhesive_bandage: fixed java > counting bits > Approach 3 --- java/counting-bits.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/java/counting-bits.md b/java/counting-bits.md index e81deee..7aa31f9 100644 --- a/java/counting-bits.md +++ b/java/counting-bits.md @@ -1,9 +1,23 @@ # [Leetcode 338: Counting Bits](https://leetcode.com/problems/counting-bits/) ## Approaches -- [Approach 1: Naive Approach](#approach-1-naive-approach) -- [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) -- [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) +- [Leetcode 338: Counting Bits](#leetcode-338-counting-bits) + - [Approaches](#approaches) + - [Approach 1: Naive Approach](#approach-1-naive-approach) + - [Intuition](#intuition) + - [Code](#code) + - [Time Complexity](#time-complexity) + - [Space Complexity](#space-complexity) + - [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) + - [Intuition](#intuition-1) + - [Code](#code-1) + - [Time Complexity](#time-complexity-1) + - [Space Complexity](#space-complexity-1) + - [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) + - [Intuition](#intuition-2) + - [Code](#code-2) + - [Time Complexity](#time-complexity-2) + - [Space Complexity](#space-complexity-2) --- @@ -81,9 +95,9 @@ public int[] countBits(int n) { for (int i = 1; i <= n; i++) { if (i == pow) { pow *= 2; // Move to the next power of two - x = 1; + x = i; } - res[i] = res[x++] + 1; // Use the result from a smaller index plus one + res[i] = res[i-x] + 1; // Use the result from a smaller index plus one } return res; } From 0383e89129b535408613caa34070c3bdc18cc092 Mon Sep 17 00:00:00 2001 From: Hiten Sharma Date: Sat, 18 Jan 2025 15:04:57 +0000 Subject: [PATCH 03/10] fixed TOC changed due to extension --- java/counting-bits.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/java/counting-bits.md b/java/counting-bits.md index 7aa31f9..6fd4f40 100644 --- a/java/counting-bits.md +++ b/java/counting-bits.md @@ -1,23 +1,9 @@ # [Leetcode 338: Counting Bits](https://leetcode.com/problems/counting-bits/) ## Approaches -- [Leetcode 338: Counting Bits](#leetcode-338-counting-bits) - - [Approaches](#approaches) - - [Approach 1: Naive Approach](#approach-1-naive-approach) - - [Intuition](#intuition) - - [Code](#code) - - [Time Complexity](#time-complexity) - - [Space Complexity](#space-complexity) - - [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) - - [Intuition](#intuition-1) - - [Code](#code-1) - - [Time Complexity](#time-complexity-1) - - [Space Complexity](#space-complexity-1) - - [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) - - [Intuition](#intuition-2) - - [Code](#code-2) - - [Time Complexity](#time-complexity-2) - - [Space Complexity](#space-complexity-2) +- [Approach 1: Naive Approach](#approach-1-naive-approach) +- [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) +- [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) --- From 6af5f77bcfbcdc2a2f4e5dfc7b8ebcadc1ed5d2d Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Sat, 1 Feb 2025 09:12:40 +0530 Subject: [PATCH 04/10] Rename 3sum.md to 3Sum.md --- python/{3sum.md => 3Sum.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename python/{3sum.md => 3Sum.md} (100%) diff --git a/python/3sum.md b/python/3Sum.md similarity index 100% rename from python/3sum.md rename to python/3Sum.md From 439c0cc59b543ecec19ada2be4f8e9107072f2cb Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Mon, 10 Feb 2025 20:58:22 +0530 Subject: [PATCH 05/10] update table of content - counting bits --- java/counting-bits.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/java/counting-bits.md b/java/counting-bits.md index 6fd4f40..afe1842 100644 --- a/java/counting-bits.md +++ b/java/counting-bits.md @@ -1,9 +1,23 @@ # [Leetcode 338: Counting Bits](https://leetcode.com/problems/counting-bits/) ## Approaches -- [Approach 1: Naive Approach](#approach-1-naive-approach) -- [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) -- [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) +- [Leetcode 338: Counting Bits](#leetcode-338-counting-bits) + - [Approaches](#approaches) + - [Approach 1: Naive Approach](#approach-1-naive-approach) + - [Intuition](#intuition) + - [Code](#code) + - [Time Complexity](#time-complexity) + - [Space Complexity](#space-complexity) + - [Approach 2: DP with Last Significant Bit](#approach-2-dp-with-last-significant-bit) + - [Intuition](#intuition-1) + - [Code](#code-1) + - [Time Complexity](#time-complexity-1) + - [Space Complexity](#space-complexity-1) + - [Approach 3: DP with Highest Power of Two](#approach-3-dp-with-highest-power-of-two) + - [Intuition](#intuition-2) + - [Code](#code-2) + - [Time Complexity](#time-complexity-2) + - [Space Complexity](#space-complexity-2) --- @@ -83,7 +97,7 @@ public int[] countBits(int n) { pow *= 2; // Move to the next power of two x = i; } - res[i] = res[i-x] + 1; // Use the result from a smaller index plus one + res[i] = res[i - x] + 1; // Use the result from a smaller index plus one } return res; } From 2998feb59c1b2dbed4183556eb9f25da005ed550 Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Mon, 10 Feb 2025 21:05:45 +0530 Subject: [PATCH 06/10] fix error related to integer overflow --- c++/single-number-iii.md | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/c++/single-number-iii.md b/c++/single-number-iii.md index d209f38..a4b3e85 100644 --- a/c++/single-number-iii.md +++ b/c++/single-number-iii.md @@ -37,16 +37,6 @@ std::vector singleNumber(std::vector& nums) { return result; } - -int main() { - std::vector nums = {1, 2, 1, 3, 2, 5}; - std::vector result = singleNumber(nums); - std::cout << "The numbers that appear once are: "; - for (int num : result) { - std::cout << num << " "; - } - return 0; -} ``` ### Time Complexity: @@ -82,7 +72,7 @@ std::vector singleNumber(std::vector& nums) { } // Find rightmost set bit different between two unique numbers - int rightmost_set_bit = xor_all & -xor_all; + unsigned int rightmost_set_bit = xor_all & -static_cast(xor_all); int num1 = 0, num2 = 0; @@ -97,21 +87,11 @@ std::vector singleNumber(std::vector& nums) { return {num1, num2}; } - -int main() { - std::vector nums = {1, 2, 1, 3, 2, 5}; - std::vector result = singleNumber(nums); - std::cout << "The numbers that appear once are: "; - for (int num : result) { - std::cout << num << " "; - } - return 0; -} ``` - ### Time Complexity: - O(n), where n is the number of elements in the vector. Each element is processed a constant number of times. ### Space Complexity: - O(1), as we are using fixed extra space regardless of input size. + From 3bcc3a67ae862b5aba6e84d79449910f18a9259f Mon Sep 17 00:00:00 2001 From: Abhishek Khelge <71659323+Abhishek-Khelge@users.noreply.github.com> Date: Tue, 18 Feb 2025 19:03:10 +0530 Subject: [PATCH 07/10] Update house-robber.md --- java/house-robber.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/house-robber.md b/java/house-robber.md index 1a0248c..416b614 100644 --- a/java/house-robber.md +++ b/java/house-robber.md @@ -52,7 +52,7 @@ To optimize the recursive approach, we can use memoization to store the results ```java class Solution { public int rob(int[] nums) { - int[] memo = new int[nums.length + 1]; + int[] memo = new int[nums.length]; Arrays.fill(memo, -1); return robFrom(0, nums, memo); } From c3aa56749cac0eb607271a7e280f0fa7cf39db8b Mon Sep 17 00:00:00 2001 From: eliphosif <48781875+eliphosif@users.noreply.github.com> Date: Sun, 8 Jun 2025 15:32:24 +0530 Subject: [PATCH 08/10] docs: correct inline conditional syntax in intersection-of-two-linked-lists.md docs: correct inline conditional syntax in getIntersectionNode function Previously, the function attempted to use inline `if` expressions, which are not valid in Go. This change replaces them with proper `if` statements, ensuring correct pointer traversal in finding the intersection node of two linked lists. --- go/intersection-of-two-linked-lists.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/go/intersection-of-two-linked-lists.md b/go/intersection-of-two-linked-lists.md index 7d6b4d6..15886b9 100644 --- a/go/intersection-of-two-linked-lists.md +++ b/go/intersection-of-two-linked-lists.md @@ -92,8 +92,17 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { // Both pointers move through both lists for a != b { // When reaching the end of a list, switch to the head of the other list - a = if a == nil { headB } else { a.Next } - b = if b == nil { headA } else { b.Next } + if a == nil { + a = headB + } else { + a = a.Next + } + + if b == nil { + b = headA + } else { + b = b.Next + } } return a // or b, both are the intersection node or nil if no intersection From baebd18551a3107a768f20a439f4b4f915b89b2d Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Wed, 2 Jul 2025 11:45:02 +0530 Subject: [PATCH 09/10] add .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5226356 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.py +*.json +*.csv \ No newline at end of file From 3331c34a4975a635489d5029535eaba453ff4643 Mon Sep 17 00:00:00 2001 From: Ashish Pratap Singh Date: Wed, 2 Jul 2025 11:48:02 +0530 Subject: [PATCH 10/10] Update min-cost-climbing-stairs.md --- python/min-cost-climbing-stairs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/min-cost-climbing-stairs.md b/python/min-cost-climbing-stairs.md index 48563d8..40380ad 100644 --- a/python/min-cost-climbing-stairs.md +++ b/python/min-cost-climbing-stairs.md @@ -73,7 +73,7 @@ Using dynamic programming, we fill an array with the minimum costs from the base ```python def minCostClimbingStairs(cost): n = len(cost) - dp = [0] * (n + 1) + dp = [0] * (n + 2) # Start calculating the minimum cost from the top to the base. for i in range(n - 1, -1, -1):