forked from jackyzha0/quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
chore/translate-new-notes #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # 11. Контейнер с наибольшим количеством воды (Medium) (<https://leetcode.com/problems/container-with-most-water/>) | ||
|
|
||
| > Дан целочисленный массив height длины n. | ||
| > Проведены n вертикальных линий, такие что конечные точки i-й линии — это (i, 0) и (i, height[i]). | ||
| > Найдите две линии, которые вместе с осью x образуют контейнер, содержащий максимальное количество воды. | ||
| > Верните максимальное количество воды, которое может удержать контейнер. | ||
| > Обратите внимание, что наклонять контейнер нельзя. | ||
| > Ограничения: - n == height.length - 2 <= n <= 10^5 - 0 <= height[i] <= 10^4 | ||
|
|
||
| ```ts | ||
| function maxArea(height: number[]): number { | ||
| let left = 0, | ||
| right = height.length - 1 | ||
| let area = 0 | ||
|
|
||
| while (left < right) { | ||
| const leftValue = height[left], | ||
| rightValue = height[right] | ||
| area = Math.max(area, Math.min(leftValue, rightValue) * (right - left)) | ||
|
|
||
| if (leftValue < rightValue) { | ||
| left++ | ||
| } else { | ||
| right-- | ||
| } | ||
| } | ||
|
|
||
| return area | ||
| } | ||
|
|
||
| // Локальная проверка: | ||
| console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])) // 49 | ||
| console.log(maxArea([1, 7, 2, 5, 4, 7, 3, 6])) // 36 | ||
| console.log(maxArea([1, 1])) // 1 | ||
| console.log(maxArea([2, 2, 2])) // 4 | ||
| ``` | ||
|
|
||
| ```md | ||
| Пример 1: | ||
|
|
||
| Ввод: height = [1,8,6,2,5,4,8,3,7] | ||
| Вывод: 49 | ||
| Объяснение: Вышеуказанные вертикальные линии представлены массивом [1,8,6,2,5,4,8,3,7]. | ||
| В этом случае максимальная площадь воды (синяя область), которую может удержать контейнер, равна 49. | ||
|
|
||
| Пример 2: | ||
|
|
||
| Ввод: height = [1,1] | ||
| Вывод: 1 | ||
| ``` | ||
|
|
||
| #leetcode |
71 changes: 71 additions & 0 deletions
71
content-ru/leetcode/Array/128-longest-consecutive-sequence.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # 128. Longest Consecutive Sequence (?) (<https://leetcode.com/problems/longest-consecutive-sequence/>) | ||
|
|
||
| > 128. | ||
| > | ||
| > Longest Consecutive Sequence (https://leetcode.com/problems/longest-consecutive-sequence/) Дан неотсортированный массив целых чисел nums, верните длину самой длинной последовательности идущих подряд элементов. | ||
| > Необходимо написать алгоритм, работающий за O(n). | ||
|
|
||
| ```ts | ||
| function longestConsecutive(nums: number[]): number { | ||
| let maxCnt = 0, | ||
| cnt = 1 | ||
| const st = new Set<number>(nums) | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| if (st.has(nums[i] - 1)) continue | ||
| for (let j = 1; j < nums.length + 1; j++) { | ||
| // является ли число началом последовательности? | ||
| if (st.has(nums[i] + j)) { | ||
| cnt += 1 | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| maxCnt = cnt > maxCnt ? cnt : maxCnt | ||
| cnt = 1 | ||
| } | ||
| return maxCnt | ||
| } | ||
|
|
||
| // O(n^2) brute force | ||
| // function longestConsecutive(nums: number[]): number { | ||
| // let maxCnt = 0, | ||
| // cnt = 1 | ||
| // for (let i = 0; i < nums.length; i++) { | ||
| // for (let j = 1; j < nums.length - 1; j++) { | ||
| // if (nums.includes(nums[i] + j)) { | ||
| // cnt += 1 | ||
| // } else { | ||
| // break | ||
| // } | ||
| // } | ||
| // maxCnt = cnt > maxCnt ? cnt : maxCnt | ||
| // cnt = 1 | ||
| // } | ||
| // return maxCnt | ||
| // }; | ||
|
|
||
| // Local check: | ||
| console.log(longestConsecutive([100, 4, 200, 1, 3, 2])) // 4 | ||
| console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])) // 9 | ||
| console.log(longestConsecutive([1, 0, 1, 2])) // 3 | ||
| ``` | ||
|
|
||
| ```md | ||
| Example 1: | ||
|
|
||
| Input: nums = [100,4,200,1,3,2] | ||
| Output: 4 | ||
| Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. | ||
|
|
||
| Example 2: | ||
|
|
||
| Input: nums = [0,3,7,2,5,8,4,6,0,1] | ||
| Output: 9 | ||
|
|
||
| Example 3: | ||
| Input: nums = [1,0,1,2] | ||
| Output: 3 | ||
| ``` | ||
|
|
||
| #leetcode | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win
Iterate over unique values to preserve O(n).
Duplicate copies of the smallest value pass the predecessor check and each rescan the entire sequence. For example, many
1s followed by2..50001can make this quadratic and violate the problem’s required complexity.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents