Skip to content

Commit 0447bd7

Browse files
committed
added new dsa solutions
1 parent c206211 commit 0447bd7

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

docusaurus.config.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,25 @@ const config = {
140140
value: '<hr style="margin: 0.3rem 0;">',
141141
},
142142

143-
{
144-
to: "/web-dev/",
145-
html: '<span class="nav-emoji">🌐</span> Web Dev',
146-
},
143+
// {
144+
// to: "/web-dev/",
145+
// html: '<span class="nav-emoji">🌐</span> Web Dev',
146+
// },
147147

148148
{
149149
type: "html",
150150
value: '<hr style="margin: 0.3rem 0;">',
151151
},
152152

153153
{
154-
to: "/dsa/",
155-
html: '<span class="nav-emoji">🧠</span> DSA',
154+
type: "html",
155+
value: `<div class="dropdown">
156+
<a class="dropbtn" href="/dsa/"> DSA&nbsp; </a>
157+
<div class="dropdown-content">
158+
<a href="/dsa-problems/" class="nav__icons"> Problems </a> <br />
159+
<a href="/dsa-solutions/" class="nav__icons"> Solutions </a>
160+
</div>
161+
</div>`,
156162
},
157163
],
158164
},

dsa-problems/leetcode-problems/0000-0099.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const problems = [
1414
"problemName": "1. Two Sum",
1515
"difficulty": "Easy",
1616
"leetCodeLink": "https://leetcode.com/problems/two-sum/",
17-
"solutionLink": "#"
17+
"solutionLink": "/dsa-solutions/lc-solutions/0000-0099/two-sum"
1818
},
1919
{
2020
"problemName": "2. Add Two Numbers",

dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ tags:
1313
description: "This is a solution to the Two Sum problem on LeetCode."
1414
---
1515

16-
We have provided multiple solutions to the Two Sum problem on LeetCode. The problem is to find two numbers in an array that add up to a target sum. We have provided solutions using a brute force approach, a hash table, and the two-pointer technique. The hash table approach is the most efficient among the three approaches and is recommended for large inputs.
16+
## Problem Description
1717

1818
|Problem Statement | Solution Link | LeetCode Profile|
1919
|:---|:---|:---|
2020
| [Two Sum on LeetCode](https://leetcode.com/problems/two-sum/) | [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) | [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) |
2121

22-
LeetCode provides the [Two Sum problem](https://leetcode.com/problems/two-sum/) with the following problem statement:
23-
24-
### Problem Description
22+
## Problem Description
2523

2624
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.
2725

@@ -31,18 +29,23 @@ You can return the answer in any order.
3129

3230
### Examples
3331

34-
```text
32+
**`Example 1:`**
33+
34+
```plaintext
3535
Input: nums = [2,7,11,15], target = 9
3636
Output: [0,1]
37-
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
38-
```
37+
```
3938

40-
```text
39+
**`Example 2:`**
40+
41+
```plaintext
4142
Input: nums = [3,2,4], target = 6
4243
Output: [1,2]
4344
```
4445

45-
```text
46+
**`Example 3:`**
47+
48+
```plaintext
4649
Input: nums = [3,3], target = 6
4750
Output: [0,1]
4851
```
@@ -54,11 +57,15 @@ Output: [0,1]
5457
- `-10^9 <= target <= 10^9`
5558
- Only one valid answer exists.
5659

60+
**Follow up:** Can you come up with an algorithm that is less than <code>$$O(n^2)$$</code> time complexity?
61+
62+
---
63+
5764
## Solution for Two Sum Problem
5865

59-
### Intuition
66+
### Intuition and Approach
6067

61-
The problem can be solved using a brute force approach, a hash table, or the two-pointer technique. The brute force approach has a time complexity of **_O(n<sup>2</sup>)_**, the hash table approach has a time complexity of **_O(n)_**, and the two-pointer technique has a time complexity of **_O(n log n)_**.
68+
The problem can be solved using a brute force approach, a hash table, or the two-pointer technique. The brute force approach has a time complexity of $$O(n^2)$$, while the hash table and two-pointer techniques have a time complexity of $$O(n)$$. The hash table approach is the most efficient and is recommended for large inputs.
6269

6370
### Approach 1: Brute Force (Naive)
6471

@@ -177,16 +184,16 @@ function twoSumProblem() {
177184

178185
#### Complexity Analysis
179186

180-
- Time Complexity: **_O(n<sup>2</sup>)_**
181-
- Space Complexity: **_O(1)_**
187+
- Time Complexity: $$O(n^2)$$
188+
- Space Complexity: $$O(1)$$
182189
- Where `n` is the length of the input array `nums`.
183-
- The time complexity is **_O(n<sup>2</sup>)_** because we are iterating through the array twice.
184-
- The space complexity is **_O(1)_** because we are not using any extra space.
190+
- The time complexity is $$O(n^2)$$ because we are iterating through the array twice.
191+
- The space complexity is $$O(1)$$ because we are not using any extra space.
185192
- This approach is not efficient and is not recommended for large inputs.
186193

187194
### Approach 2: Using Hash Table
188195

189-
We can improve the time complexity of the brute force approach by using a hash table to store the elements and their indices. While we iterate through the array, we check if the difference between the target and the current element exists in the hash table. If it does, we return the indices of the current element and the element that makes up the target sum.
196+
We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indices in a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check if the complement is present in the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. If the complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array.
190197

191198
#### Implementation
192199

@@ -314,19 +321,19 @@ function twoSumProblem() {
314321

315322
#### Complexity Analysis
316323

317-
- Time Complexity: **_O(n)_**
318-
- Space Complexity: **_O(n)_**
324+
- Time Complexity: $$O(n)$$
325+
- Space Complexity: $$O(n)$$
319326
- Where `n` is the length of the input array `nums`.
320-
- The time complexity is **_O(n)_** because we iterate through the array only once.
321-
- The space complexity is **_O(n)_** because we use a hash table to store the elements and their indices.
327+
- The time complexity is $$O(n)$$ because we iterate through the array only once.
328+
- The space complexity is $$O(n)$$ because we use a hash table to store the elements and their indices.
322329
- This approach is more efficient than the brute force approach and is recommended for large inputs.
323-
- The hash table lookup has an average time complexity of **_O(1)_**.
324-
- The space complexity is **_O(n)_** because we store at most `n` elements in the hash table.
325-
- The total time complexity is **_O(n)_**. and the total space complexity is **_O(n)_**.
330+
- The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient.
331+
- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table.
332+
- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$.
326333

327334
### Approach 3: Using Two Pointers
328335

329-
We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, one at the beginning and one at the end. We move the pointers based on the sum of the elements at the two pointers. If the sum is equal to the target, we return the indices of the two elements. If the sum is less than the target, we move the left pointer to the right. If the sum is greater than the target, we move the right pointer to the left.
336+
We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, `left` and `right`, to find the two numbers that add up to the target sum. We initialize `left` to `0` and `right` to `nums.length - 1`. We calculate the sum of the two numbers at the `left` and `right` pointers. If the sum is equal to the target, we return the indices `[left, right]`. If the sum is less than the target, we increment the `left` pointer. If the sum is greater than the target, we decrement the `right` pointer. If no pair is found, we return an empty array.
330337

331338
#### Implementation
332339

@@ -504,25 +511,21 @@ function twoSumProblem() {
504511

505512
#### Complexity Analysis
506513

507-
- Time Complexity: **_O(n log n)_**
508-
- Space Complexity: **_O(n)_**
514+
- Time Complexity: $$O(n \log n)$$
515+
- Space Complexity: $$O(n)$$
509516
- Where `n` is the length of the input array `nums`.
510-
- The time complexity is **_O(n log n)_** because we sort the array.
511-
- The space complexity is **_O(n)_** because we store the indices of the elements in the sorted array.
517+
- The time complexity is $$O(n \log n)$$ because we sort the array.
518+
- The space complexity is $$O(n)$$ because we store the indices of the elements in the sorted array.
512519
- This approach is efficient and is recommended for large inputs.
513-
- The total time complexity is **_O(n log n)_**. and the total space complexity is **_O(n)_**.
520+
- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$.
514521

515522
:::tip Note
516523
**Which is the best approach? and why?**
517524

518-
The hash table approach is the most efficient among the three approaches. It has a time complexity of **_O(n)_** and is recommended for large inputs. The two-pointer approach also has a time complexity of **_O(n log n)_** but requires sorting the array, which may not be necessary in some cases. The brute force approach is the least efficient with a time complexity of **_O(n<sup>2</sup>)_** and is not recommended for large inputs.
519-
:::
525+
The hash table approach is the most efficient and is recommended for large inputs. The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient. The time complexity of the hash table approach is $$O(n)$$, which is better than the brute force approach with a time complexity of $$O(n^2)$$ and the two-pointer approach with a time complexity of $$O(n \log n)$$. The space complexity of the hash table approach is $$O(n)$$, which is the same as the two-pointer approach. Therefore, the hash table approach is the best approach for this problem.
520526

521-
## Summary
527+
:::
522528

523-
In this problem, we learned how to find two numbers in an array that add up to a target sum. We explored three different approaches to solve this problem:
529+
## Conclusion
524530

525-
1. The brute force approach with a time complexity of **_O(n<sup>2</sup>)_**.
526-
2. The hash table approach with a time complexity of **_O(n)_**.
527-
3. The two-pointer approach with a time complexity of **_O(n log n)_**.
528-
4. The hash table approach is the most efficient and is recommended for large inputs.
531+
In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs.

0 commit comments

Comments
 (0)