Skip to content

feat: add solutions to lcp problem: No.61 #3138

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 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions lcp/LCP 61. 气温变化趋势/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%

<!-- solution:start -->

### 方法一:动态规划
### 方法一:一次遍历

我们用变量 $f$ 维护当前趋势相同的连续天数,用变量 $ans$ 维护最大的连续天数。

遍历数组,对于第 $i$ 天,记两地的气温变化趋势分别为 $x$ 和 $y$,如果 $x$ 和 $y$ 均为 $0$ 或者 $x$ 和 $y$ 均为正数或负数,则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势相同,此时 $f$ 自增 $1$,并更新 $ans$;否则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势不同,此时 $f$ 重置为 $0$。

最终返回 $ans$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -68,10 +68,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%
class Solution:
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
ans = f = 0
n = len(temperatureA)
for i in range(n - 1):
x = temperatureA[i + 1] - temperatureA[i]
y = temperatureB[i + 1] - temperatureB[i]
for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):
x, y = a2 - a1, b2 - b1
if x == y == 0 or x * y > 0:
f += 1
ans = max(ans, f)
Expand Down Expand Up @@ -140,6 +138,51 @@ func temperatureTrend(temperatureA []int, temperatureB []int) int {
}
```

#### TypeScript

```ts
function temperatureTrend(temperatureA: number[], temperatureB: number[]): number {
let [ans, f] = [0, 0];
for (let i = 0; i < temperatureA.length - 1; ++i) {
let x = temperatureA[i + 1] - temperatureA[i];
let y = temperatureB[i + 1] - temperatureB[i];
if ((x === 0 && y === 0) || x * y > 0) {
ans = Math.max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn temperature_trend(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 {
let mut ans = 0;
let mut f = 0;

for i in 0..temperature_a.len() - 1 {
let x = temperature_a[i + 1] - temperature_a[i];
let y = temperature_b[i + 1] - temperature_b[i];

if (x == 0 && y == 0) || (x > 0 && y > 0) || (x < 0 && y < 0) {
f += 1;
if f > ans {
ans = f;
}
} else {
f = 0;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
6 changes: 2 additions & 4 deletions lcp/LCP 61. 气温变化趋势/Solution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class Solution:
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
ans = f = 0
n = len(temperatureA)
for i in range(n - 1):
x = temperatureA[i + 1] - temperatureA[i]
y = temperatureB[i + 1] - temperatureB[i]
for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):
x, y = a2 - a1, b2 - b1
if x == y == 0 or x * y > 0:
f += 1
ans = max(ans, f)
Expand Down
22 changes: 22 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn temperature_trend(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 {
let mut ans = 0;
let mut f = 0;

for i in 0..temperature_a.len() - 1 {
let x = temperature_a[i + 1] - temperature_a[i];
let y = temperature_b[i + 1] - temperature_b[i];

if (x == 0 && y == 0) || (x > 0 && y > 0) || (x < 0 && y < 0) {
f += 1;
if f > ans {
ans = f;
}
} else {
f = 0;
}
}

ans
}
}
13 changes: 13 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function temperatureTrend(temperatureA: number[], temperatureB: number[]): number {
let [ans, f] = [0, 0];
for (let i = 0; i < temperatureA.length - 1; ++i) {
let x = temperatureA[i + 1] - temperatureA[i];
let y = temperatureB[i + 1] - temperatureB[i];
if ((x === 0 && y === 0) || x * y > 0) {
ans = Math.max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
Loading