mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 14:41:34 +08:00
Add solution 674
This commit is contained in:
@ -87,5 +87,5 @@ func counterKthNum(m, n, mid int) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0662.Maximum-Width-of-Binary-Tree/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0676.Implement-Magic-Dictionary/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0674.Longest-Continuous-Increasing-Subsequence/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,76 @@
|
||||
# [674. Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an unsorted array of integers `nums`, return *the length of the longest **continuous increasing subsequence** (i.e. subarray)*. The subsequence must be **strictly** increasing.
|
||||
|
||||
A **continuous increasing subsequence** is defined by two indices `l` and `r` (`l < r`) such that it is `[nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]` and for each `l <= i < r`, `nums[i] < nums[i + 1]`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [1,3,5,4,7]
|
||||
Output: 3
|
||||
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
|
||||
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
|
||||
4.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [2,2,2,2,2]
|
||||
Output: 1
|
||||
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
|
||||
increasing.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `0 <= nums.length <= 10^4`
|
||||
- `10^9 <= nums[i] <= 10^9`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。这一题和第 128 题有区别。这一题要求子序列必须是连续下标,所以变简单了。扫描一遍数组,记下连续递增序列的长度,动态维护这个最大值,最后输出即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func findLengthOfLCIS(nums []int) int {
|
||||
if len(nums) == 0 {
|
||||
return 0
|
||||
}
|
||||
res, length := 1, 1
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if nums[i] > nums[i-1] {
|
||||
length++
|
||||
} else {
|
||||
res = max(res, length)
|
||||
length = 1
|
||||
}
|
||||
}
|
||||
return max(res, length)
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0668.Kth-Smallest-Number-in-Multiplication-Table/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0676.Implement-Magic-Dictionary/">下一页➡️</a></p>
|
||||
</div>
|
@ -89,6 +89,6 @@ func (this *MagicDictionary) Search(word string) bool {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0668.Kth-Smallest-Number-in-Multiplication-Table/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0674.Longest-Continuous-Increasing-Subsequence/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0682.Baseball-Game/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -77,6 +77,7 @@ type: docs
|
||||
|0605|Can Place Flowers|[Go]({{< relref "/ChapterFour/0605.Can-Place-Flowers.md" >}})|Easy||||31.9%|
|
||||
|0628|Maximum Product of Three Numbers|[Go]({{< relref "/ChapterFour/0628.Maximum-Product-of-Three-Numbers.md" >}})|Easy| O(n)| O(1)||47.0%|
|
||||
|0661|Image Smoother|[Go]({{< relref "/ChapterFour/0661.Image-Smoother.md" >}})|Easy||||52.2%|
|
||||
|0674|Longest Continuous Increasing Subsequence|[Go]({{< relref "/ChapterFour/0674.Longest-Continuous-Increasing-Subsequence.md" >}})|Easy||||46.0%|
|
||||
|0695|Max Area of Island|[Go]({{< relref "/ChapterFour/0695.Max-Area-of-Island.md" >}})|Medium||||64.4%|
|
||||
|0697|Degree of an Array|[Go]({{< relref "/ChapterFour/0697.Degree-of-an-Array.md" >}})|Easy||||54.4%|
|
||||
|0713|Subarray Product Less Than K|[Go]({{< relref "/ChapterFour/0713.Subarray-Product-Less-Than-K.md" >}})|Medium| O(n)| O(1)||40.4%|
|
||||
|
@ -195,7 +195,7 @@ func peakIndexInMountainArray(A []int) int {
|
||||
|1283|Find the Smallest Divisor Given a Threshold|[Go]({{< relref "/ChapterFour/1283.Find-the-Smallest-Divisor-Given-a-Threshold.md" >}})|Medium||||49.3%|
|
||||
|1300|Sum of Mutated Array Closest to Target|[Go]({{< relref "/ChapterFour/1300.Sum-of-Mutated-Array-Closest-to-Target.md" >}})|Medium||||43.2%|
|
||||
|1649|Create Sorted Array through Instructions|[Go]({{< relref "/ChapterFour/1649.Create-Sorted-Array-through-Instructions.md" >}})|Hard||||36.2%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.3%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.4%|
|
||||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
|
||||
|
||||
|
||||
|
@ -42,7 +42,7 @@ type: docs
|
||||
|1052|Grumpy Bookstore Owner|[Go]({{< relref "/ChapterFour/1052.Grumpy-Bookstore-Owner.md" >}})|Medium| O(n log n)| O(1) ||55.7%|
|
||||
|1074|Number of Submatrices That Sum to Target|[Go]({{< relref "/ChapterFour/1074.Number-of-Submatrices-That-Sum-to-Target.md" >}})|Hard| O(n^3)| O(n) |❤️|61.5%|
|
||||
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||43.7%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.3%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.4%|
|
||||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
|
||||
|
||||
|
||||
|
@ -48,7 +48,7 @@ type: docs
|
||||
|0895|Maximum Frequency Stack|[Go]({{< relref "/ChapterFour/0895.Maximum-Frequency-Stack.md" >}})|Hard| O(n)| O(n) ||62.2%|
|
||||
|0901|Online Stock Span|[Go]({{< relref "/ChapterFour/0901.Online-Stock-Span.md" >}})|Medium| O(n)| O(n) ||61.2%|
|
||||
|0907|Sum of Subarray Minimums|[Go]({{< relref "/ChapterFour/0907.Sum-of-Subarray-Minimums.md" >}})|Medium| O(n)| O(n)|❤️|33.2%|
|
||||
|0921|Minimum Add to Make Parentheses Valid|[Go]({{< relref "/ChapterFour/0921.Minimum-Add-to-Make-Parentheses-Valid.md" >}})|Medium| O(n)| O(n)||74.7%|
|
||||
|0921|Minimum Add to Make Parentheses Valid|[Go]({{< relref "/ChapterFour/0921.Minimum-Add-to-Make-Parentheses-Valid.md" >}})|Medium| O(n)| O(n)||74.6%|
|
||||
|0946|Validate Stack Sequences|[Go]({{< relref "/ChapterFour/0946.Validate-Stack-Sequences.md" >}})|Medium| O(n)| O(n)||63.5%|
|
||||
|1003|Check If Word Is Valid After Substitutions|[Go]({{< relref "/ChapterFour/1003.Check-If-Word-Is-Valid-After-Substitutions.md" >}})|Medium| O(n)| O(1)||56.2%|
|
||||
|1019|Next Greater Node In Linked List|[Go]({{< relref "/ChapterFour/1019.Next-Greater-Node-In-Linked-List.md" >}})|Medium| O(n)| O(1)||58.2%|
|
||||
|
@ -84,7 +84,7 @@ type: docs
|
||||
|1004|Max Consecutive Ones III|[Go]({{< relref "/ChapterFour/1004.Max-Consecutive-Ones-III.md" >}})|Medium| O(n)| O(1) ||60.6%|
|
||||
|1093|Statistics from a Large Sample|[Go]({{< relref "/ChapterFour/1093.Statistics-from-a-Large-Sample.md" >}})|Medium| O(n)| O(1) ||49.4%|
|
||||
|1234|Replace the Substring for Balanced String|[Go]({{< relref "/ChapterFour/1234.Replace-the-Substring-for-Balanced-String.md" >}})|Medium||||34.4%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.3%|
|
||||
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.4%|
|
||||
|1695|Maximum Erasure Value|[Go]({{< relref "/ChapterFour/1695.Maximum-Erasure-Value.md" >}})|Medium||||49.6%|
|
||||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
|
||||
|
||||
|
@ -343,6 +343,7 @@ headless: true
|
||||
- [0661.Image-Smoother]({{< relref "/ChapterFour/0661.Image-Smoother.md" >}})
|
||||
- [0662.Maximum-Width-of-Binary-Tree]({{< relref "/ChapterFour/0662.Maximum-Width-of-Binary-Tree.md" >}})
|
||||
- [0668.Kth-Smallest-Number-in-Multiplication-Table]({{< relref "/ChapterFour/0668.Kth-Smallest-Number-in-Multiplication-Table.md" >}})
|
||||
- [0674.Longest-Continuous-Increasing-Subsequence]({{< relref "/ChapterFour/0674.Longest-Continuous-Increasing-Subsequence.md" >}})
|
||||
- [0676.Implement-Magic-Dictionary]({{< relref "/ChapterFour/0676.Implement-Magic-Dictionary.md" >}})
|
||||
- [0682.Baseball-Game]({{< relref "/ChapterFour/0682.Baseball-Game.md" >}})
|
||||
- [0684.Redundant-Connection]({{< relref "/ChapterFour/0684.Redundant-Connection.md" >}})
|
||||
|
Reference in New Issue
Block a user