Skip to content

Commit c9eaad7

Browse files
authored
Merge pull request #342 from Yashgabani845/yash-work1
Leetcode solution 11 to 16 added
2 parents 4e295ef + 967efb0 commit c9eaad7

File tree

6 files changed

+1318
-0
lines changed

6 files changed

+1318
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
id: Container With Most Water
3+
title: Container With Most Water (LeetCode)
4+
sidebar_label: 0011-Container With Most Water
5+
tags:
6+
- Array
7+
- Two Pointers
8+
- Greedy
9+
description: Given n non-negative integers representing the heights of vertical lines, find the two lines that together with the x-axis form a container, such that the container contains the most water.
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- |
16+
| [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Container With Most Water Solution on LeetCode](https://leetcode.com/problems/container-with-most-water/solutions/3701708/best-method-c-java-python-beginner-friendly/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |
17+
18+
## Problem Statement
19+
20+
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the ith line are `(i, 0)` and `(i, height[i])`.
21+
22+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
23+
24+
Return the maximum amount of water a container can store.
25+
26+
Notice that you may not slant the container.
27+
28+
### Examples
29+
30+
**Example 1:**
31+
32+
![Example 1](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
33+
34+
- **Input**: `height = [1,8,6,2,5,4,8,3,7]`
35+
- **Output**: `49`
36+
- **Explanation**: The above vertical lines are represented by array `[1,8,6,2,5,4,8,3,7]`. In this case, the max area of water (blue section) the container can contain is `49`.
37+
38+
**Example 2:**
39+
40+
- **Input**: `height = [1,1]`
41+
- **Output**: `1`
42+
43+
### Constraints
44+
45+
- `n == height.length`
46+
- `2 <= n <= 10^5`
47+
- `0 <= height[i] <= 10^4`
48+
49+
## Solution
50+
51+
### Approach
52+
53+
#### Intuition
54+
55+
The two-pointer technique starts with the widest container and moves the pointers inward based on the comparison of heights. Increasing the width of the container can only lead to a larger area if the height of the new boundary is greater. By moving the pointers towards the center, we explore containers with the potential for greater areas.
56+
57+
#### Algorithm
58+
59+
1. **Initialize the variables**:
60+
- `left` to represent the left pointer, starting at the beginning of the container (index 0).
61+
- `right` to represent the right pointer, starting at the end of the container (index `height.size() - 1`).
62+
- `maxArea` to keep track of the maximum area found, initially set to 0.
63+
64+
2. **Enter a loop using the condition `left < right`**, which means the pointers have not crossed each other yet.
65+
66+
3. **Calculate the current area**:
67+
- Use the `min` function to find the minimum height between the left and right pointers.
68+
- Multiply the minimum height by the width, which is the difference between the indices of the pointers: `(right - left)`.
69+
- Store this value in the `currentArea` variable.
70+
71+
4. **Update the maximum area**:
72+
- Use the `max` function to compare the `currentArea` with the `maxArea`.
73+
- If the `currentArea` is greater than the `maxArea`, update `maxArea` with the `currentArea`.
74+
75+
5. **Move the pointers inward**:
76+
- Check if the height at the `left` pointer is smaller than the height at the `right` pointer.
77+
- If so, increment the `left` pointer, moving it towards the center of the container.
78+
- Otherwise, decrement the `right` pointer, also moving it towards the center.
79+
80+
6. **Repeat steps 3 to 5** until the pointers meet (`left >= right`), indicating that all possible containers have been explored.
81+
82+
7. **Return the `maxArea`**, which represents the maximum area encountered among all the containers.
83+
84+
### Code
85+
86+
#### C++ Implementation
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int maxArea(vector<int>& height) {
92+
int left = 0;
93+
int right = height.size() - 1;
94+
int maxArea = 0;
95+
96+
while (left < right) {
97+
int currentArea = min(height[left], height[right]) * (right - left);
98+
maxArea = max(maxArea, currentArea);
99+
100+
if (height[left] < height[right]) {
101+
left++;
102+
} else {
103+
right--;
104+
}
105+
}
106+
107+
return maxArea;
108+
}
109+
};
110+
```
111+
112+
#### Java Implementation
113+
114+
```java
115+
class Solution {
116+
public int maxArea(int[] height) {
117+
int left = 0;
118+
int right = height.length - 1;
119+
int maxArea = 0;
120+
121+
while (left < right) {
122+
int currentArea = Math.min(height[left], height[right]) * (right - left);
123+
maxArea = Math.max(maxArea, currentArea);
124+
125+
if (height[left] < height[right]) {
126+
left++;
127+
} else {
128+
right--;
129+
}
130+
}
131+
132+
return maxArea;
133+
}
134+
}
135+
```
136+
137+
#### Python Implementation
138+
139+
```python
140+
class Solution:
141+
def maxArea(self, height: List[int]) -> int:
142+
left = 0
143+
right = len(height) - 1
144+
maxArea = 0
145+
146+
while left < right:
147+
currentArea = min(height[left], height[right]) * (right - left)
148+
maxArea = max(maxArea, currentArea)
149+
150+
if height[left] < height[right]:
151+
left += 1
152+
else:
153+
right -= 1
154+
155+
return maxArea
156+
```
157+
158+
### Complexity Analysis
159+
160+
- **Time Complexity**: $O(n)$, where n is the length of the array `height`. We traverse the list only once using two pointers from both ends of the array towards the center.
161+
- **Space Complexity**: $O(1)$, we use a constant amount of extra space for the pointers and variables.
162+
163+
## Conclusion
164+
165+
The two-pointer approach is efficient for solving the Container With Most Water problem, allowing us to explore all possible containers by adjusting the pointers based on the heights of the lines, ensuring that we find the container with the maximum possible area.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
id: Integer to Roman
3+
title: Integer to Roman (LeetCode)
4+
sidebar_label: 0012-Integer to Roman
5+
tags:
6+
- Hash Table
7+
- Math
8+
- String
9+
description: Convert a given integer to a Roman numeral using specific rules for the Roman numeral system.
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- |
16+
| [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Integer to Roman Solution on LeetCode](https://leetcode.com/problems/integer-to-roman/solutions/3216797/easiest-beginner-friendly-sol-c-java-python/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |
17+
18+
## Problem Statement
19+
20+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D`, and `M`.
21+
22+
| Symbol | Value |
23+
| :----- | :---- |
24+
| I | 1 |
25+
| V | 5 |
26+
| X | 10 |
27+
| L | 50 |
28+
| C | 100 |
29+
| D | 500 |
30+
| M | 1000 |
31+
32+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five, we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
33+
34+
- `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
35+
- `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
36+
- `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
37+
38+
Given an integer, convert it to a Roman numeral.
39+
40+
### Examples
41+
42+
**Example 1:**
43+
44+
- **Input**: `num = 3749`
45+
- **Output**: `"MMMDCCXLIX"`
46+
- **Explanation**:
47+
- 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
48+
- 700 = DCC as 500 (D) + 100 (C) + 100 (C)
49+
- 40 = XL as 10 (X) less than 50 (L)
50+
- 9 = IX as 1 (I) less than 10 (X)
51+
52+
**Example 2:**
53+
54+
- **Input**: `num = 58`
55+
- **Output**: `"LVIII"`
56+
- **Explanation**:
57+
- 50 = L
58+
- 8 = VIII
59+
60+
**Example 3:**
61+
62+
- **Input**: `num = 1994`
63+
- **Output**: `"MCMXCIV"`
64+
- **Explanation**:
65+
- 1000 = M
66+
- 900 = CM
67+
- 90 = XC
68+
- 4 = IV
69+
70+
### Constraints
71+
72+
- `1 <= num <= 3999`
73+
74+
## Solution
75+
76+
### Approach
77+
78+
#### Intuition
79+
80+
To convert an integer to a Roman numeral, we need to repeatedly subtract the largest possible Roman numeral value from the integer while appending the corresponding symbol to the result string. We use predefined pairs of integers and their Roman numeral representations to guide this process.
81+
82+
#### Algorithm
83+
84+
1. **Initialize the Roman numeral string**:
85+
- Create an empty string `Roman` to store the resulting Roman numeral.
86+
87+
2. **Create a list of integer-Roman pairs**:
88+
- Use a list of pairs to store the values and symbols of Roman numerals in descending order of values.
89+
90+
3. **Iterate through the list of pairs**:
91+
- For each pair, check if the input integer is greater than or equal to the Roman numeral value.
92+
- If it is, add the corresponding symbol to the `Roman` string and subtract the corresponding value from the input integer.
93+
- Repeat this process until the input integer becomes zero.
94+
95+
4. **Return the Roman numeral string**:
96+
- After processing all the pairs, return the `Roman` string containing the converted Roman numeral.
97+
98+
### Code
99+
100+
#### C++ Implementation
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
string intToRoman(int num) {
106+
string Roman = "";
107+
vector<pair<int, string>> storeIntRoman = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}};
108+
for (int i = 0; i < storeIntRoman.size(); i++) {
109+
while (num >= storeIntRoman[i].first) {
110+
Roman += storeIntRoman[i].second;
111+
num -= storeIntRoman[i].first;
112+
}
113+
}
114+
return Roman;
115+
}
116+
};
117+
```
118+
119+
#### Java Implementation
120+
121+
```java
122+
class Solution {
123+
public String intToRoman(int num) {
124+
String Roman = "";
125+
int[][] storeIntRoman = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}};
126+
for (int i = 0; i < storeIntRoman.length; i++) {
127+
while (num >= storeIntRoman[i][0]) {
128+
Roman += storeIntRoman[i][1];
129+
num -= storeIntRoman[i][0];
130+
}
131+
}
132+
return Roman;
133+
}
134+
}
135+
```
136+
137+
#### Python Implementation
138+
139+
```python
140+
class Solution:
141+
def intToRoman(self, num: int) -> str:
142+
Roman = ""
143+
storeIntRoman = [[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"], [90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"], [5, "V"], [4, "IV"], [1, "I"]]
144+
for i in range(len(storeIntRoman)):
145+
while num >= storeIntRoman[i][0]:
146+
Roman += storeIntRoman[i][1]
147+
num -= storeIntRoman[i][0]
148+
return Roman
149+
```
150+
151+
### Complexity Analysis
152+
153+
- **Time Complexity**: $O(1)$ - The algorithm iterates through a constant number of values (13 in this case), so the time complexity is constant.
154+
- **Space Complexity**: $O(1)$ - The amount of extra space used is constant, determined by the fixed size of the `storeIntRoman` vector.
155+
156+
## Conclusion
157+
158+
The provided solutions efficiently convert an integer to a Roman numeral by iterating through predefined Roman numeral values and symbols. This approach ensures that the conversion adheres to the rules of the Roman numeral system while maintaining constant time and space complexity.
159+

0 commit comments

Comments
 (0)