Skip to content

Commit 72a8a93

Browse files
Merge branch 'CodeHarborHub:main' into leetcode23
2 parents 464615a + 1d42653 commit 72a8a93

File tree

71 files changed

+4348
-242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4348
-242
lines changed

641-design-circular-deque.md

Lines changed: 486 additions & 0 deletions
Large diffs are not rendered by default.

643-maximum-average-subarray-I.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
id: maximum-average-subarray-1
3+
title: Maximum Average Subarray I (Leetcode)
4+
sidebar_label: 0643-MaximumAverageSubarrayI
5+
description: You are given an integer array nums consisting of n elements, and an integer k.Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
6+
---
7+
8+
## Problem Description
9+
10+
| Problem Statement | Solution Link | LeetCode Profile |
11+
| :---------------- | :------------ | :--------------- |
12+
| [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/) | [Maximum Average Subarray I Solution on LeetCode](https://leetcode.com/problems/maximum-average-subarray-i/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) |
13+
14+
15+
## Problem Description
16+
17+
You are given an integer array nums consisting of n elements, and an integer k.
18+
19+
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
20+
21+
### Examples
22+
23+
#### Example 1
24+
25+
- **Input:** $nums = [1,12,-5,-6,50,3], k = 4$
26+
- **Output:** $12.75000$
27+
- **Explanation:** Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
28+
29+
30+
#### Example 2
31+
32+
- **Input:** $nums = [5], k = 1$
33+
- **Output:** $5.00000$
34+
35+
### Constraints
36+
37+
- $n == nums.length$
38+
- $1 <= k <= n <= 105$
39+
- $-104 <= nums[i] <= 104$
40+
41+
42+
### Intuition
43+
44+
The goal of this code is to find the maximum average value of any contiguous subarray of length k within the given array nums. The solution uses a sliding window approach to efficiently compute the sum of elements for each subarray of length k. Initially, it calculates the sum of the first k elements. Then, for each subsequent subarray, it adjusts the sum by adding the next element in the sequence and subtracting the first element of the previous subarray, thereby maintaining a running sum. This approach ensures that the sum is recalculated in constant time, making the algorithm efficient. The maximum sum encountered is then divided by k to obtain the maximum average.
45+
46+
47+
### Approach
48+
49+
1. **Initialization:**
50+
51+
- Create a variable result to store the maximum average.
52+
- Create a variable number and initialize it to the lowest possible double value using std::numeric_limits<double>::lowest().
53+
- Create a variable temp to keep track of the current sum of the subarray of length k.
54+
- Create a variable n and set it equal to k.
55+
56+
2. **Calculate Initial Sum:**
57+
58+
- Iterate through the first k elements of the array nums and calculate their sum. Store this sum in temp.
59+
60+
3. **Sliding Window Calculation:**
61+
62+
- Use a for loop to iterate through the array starting from the element at index k.
63+
- For each iteration, update temp by adding the current element (nums[i + k - 1]) and subtracting the element that is no longer in the subarray (nums[i - 1]).
64+
- Update number to be the maximum of temp and number.
65+
66+
4. **Calculate Maximum Average:**
67+
68+
- Divide number by k to get the maximum average of any subarray of length k.
69+
70+
5. **Return Result:**
71+
72+
- Return result, which now holds the maximum average.
73+
74+
### Solution Code
75+
76+
#### Python
77+
78+
```py
79+
import math
80+
81+
class Solution:
82+
def findMaxAverage(self, nums: List[int], k: int) -> float:
83+
result = 0.0
84+
number = -math.inf
85+
temp = 0.0
86+
n = k
87+
88+
# Calculate the initial sum of the first k elements
89+
for i in range(k):
90+
temp += nums[i]
91+
92+
number = max(temp, number)
93+
94+
# Sliding window calculation
95+
for i in range(1, len(nums) - k + 1):
96+
temp = temp + nums[i + k - 1] - nums[i - 1]
97+
number = max(temp, number)
98+
99+
result = number / k
100+
return result
101+
```
102+
103+
#### Java
104+
105+
```java
106+
public class Solution {
107+
public double findMaxAverage(int[] nums, int k) {
108+
double result = 0.0;
109+
double number = Double.NEGATIVE_INFINITY;
110+
double temp = 0.0;
111+
int n = k;
112+
113+
// Calculate the initial sum of the first k elements
114+
for (int i = 0; i < k; i++) {
115+
temp += nums[i];
116+
}
117+
118+
number = Math.max(temp, number);
119+
120+
// Sliding window calculation
121+
for (int i = 1; i <= nums.length - k; i++) {
122+
temp = temp + nums[i + k - 1] - nums[i - 1];
123+
number = Math.max(temp, number);
124+
}
125+
126+
result = number / (double) k;
127+
return result;
128+
}
129+
}
130+
```
131+
132+
#### C++
133+
134+
```cpp
135+
#include <limits>
136+
class Solution {
137+
public:
138+
double findMaxAverage(vector<int>& nums, int k) {
139+
double result;
140+
double number = std::numeric_limits<double>::lowest();
141+
double temp = 0;
142+
int n = k;
143+
144+
for (int i = 0; i <= nums.size() - k ; ++i){
145+
if (i == 0) {
146+
while (n != 0) {
147+
temp += nums[i + n - 1] ;
148+
n--;
149+
}
150+
}
151+
152+
else {temp = temp + nums[i + k - 1] - nums[i - 1];}
153+
number = max(temp , number);
154+
}
155+
156+
result = number / static_cast<double>(k);
157+
return result;
158+
}
159+
};
160+
```
161+
162+
### Conclusion
163+
164+
The provided code implements a solution to find the maximum average of any contiguous subarray of length k within the given array nums. It employs a sliding window approach to efficiently compute the sum of elements for each subarray. The algorithm iterates through the array once, updating the sum and maximum average as it progresses. The time complexity of the solution is $O(n)$, where nn is the number of elements in the array nums, as it makes a single pass through the array. The space complexity is $O(1)$ since the algorithm uses only a constant amount of extra space regardless of the input size.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: pseudo-class-and-pseudo-elements
3+
title: Pseudo class and Pseudo elements
4+
sidebar_label: Pseudo class and Pseudo elements
5+
tags: [css, introduction, web development, markup language, hyper text, web pages, career opportunities, personal growth, web-development, web design, web pages, websites, career opportunities, contribute to the web, stay relevant, express yourself, learn other technologies, have fun,pseudo classes,pseudo elements]
6+
description: In this tutorial you will learn about Pseudo classes and Pseudo elements in CSS
7+
---
8+
9+
Pseudo-classes are used to define the special states of an element. They are typically prefixed with a colon (`:`).
10+
11+
12+
1. `:hover` : Applies when the user designates an element (with a pointing device), but does not activate it. Often used to change the appearance of a button when the mouse pointer is over it.
13+
14+
```css
15+
button:hover {
16+
background-color: lightblue;
17+
}
18+
```
19+
20+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
21+
<button type="submit">Submit</button>
22+
<style>{`
23+
button:hover {
24+
background-color: lightblue;
25+
}
26+
`}</style>
27+
</BrowserWindow>
28+
29+
30+
2. `:focus` : Applies when an element has received focus (e.g., when clicked on or tabbed to).
31+
32+
```css
33+
input:focus {
34+
border-color: blue;
35+
}
36+
```
37+
38+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
39+
<input type="text" required/>
40+
<style>
41+
{`input:focus {
42+
border-color: blue;
43+
}`}
44+
</style>
45+
</BrowserWindow>
46+
47+
3. `:nth-child(n)` : Matches elements based on their position in a group of siblings.
48+
49+
```css
50+
li:nth-child(2) {
51+
color: green;
52+
}
53+
```
54+
55+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
56+
<ul>
57+
<li>Red</li>
58+
<li>Green</li>
59+
<li>BLue</li>
60+
</ul>
61+
<style>
62+
{`
63+
li:nth-child(2) {
64+
color: green;
65+
}
66+
`}
67+
</style>
68+
</BrowserWindow>
69+
70+
71+
4. `:first-child` : Applies to the first child of its parent.
72+
73+
```css
74+
.container div:first-child {
75+
color: blue;
76+
font-weight: bold;
77+
}
78+
```
79+
80+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
81+
<div className="pseudo_container">
82+
<div>Hello</div>
83+
<div>World</div>
84+
</div>
85+
<style>
86+
{`.pseudo_container div:first-child {
87+
color: blue;
88+
font-weight: bold;
89+
}`}
90+
</style>
91+
</BrowserWindow>
92+
93+
5. `:nth-of-type(n)` : Matches elements based on their position among siblings of the same type.
94+
95+
```css
96+
div:nth-of-type(3)
97+
{
98+
color: red;
99+
}
100+
```
101+
102+
## Pseudo Elements
103+
104+
1. `::before` : Inserts content before an element's actual content.
105+
106+
```css
107+
p::before {
108+
content: "Note: ";
109+
font-weight: bold;
110+
}
111+
```
112+
113+
2. `::after` : Inserts content after an element's actual content.
114+
115+
```css
116+
p::after {
117+
content: " - Read more";
118+
font-style: italic;
119+
}
120+
```
121+
122+
3. `::first-line` : Applies styles to the first line of a block-level element.
123+
124+
```css
125+
p::first-line {
126+
color: red;
127+
font-weight: bold;
128+
}
129+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advanced MongoDB",
3+
"position": 25,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Dive deeper into advanced MongoDB concepts and techniques."
7+
}
8+
}

0 commit comments

Comments
 (0)