From 9fcf9a857b8ed7f82cd536d8a29210e8f7c8131c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 21 Jun 2024 08:51:26 +0800 Subject: [PATCH 1/3] feat: add solutions to lcp problem: No.61 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No.61.气温变化趋势 --- .../README.md" | 45 +++++++++++++++++++ .../Solution.rs" | 22 +++++++++ .../Solution.ts" | 13 ++++++ 3 files changed, 80 insertions(+) create mode 100644 "lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.rs" create mode 100644 "lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.ts" diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" index 7c60811922218..45e754b42c534 100644 --- "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" @@ -140,6 +140,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, temperature_b: Vec) -> 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 + } +} +``` + diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.rs" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.rs" new file mode 100644 index 0000000000000..77627aa18c69e --- /dev/null +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.rs" @@ -0,0 +1,22 @@ +impl Solution { + pub fn temperature_trend(temperature_a: Vec, temperature_b: Vec) -> 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 + } +} diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.ts" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.ts" new file mode 100644 index 0000000000000..549abe92aa928 --- /dev/null +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.ts" @@ -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; +} From 7604cd48ab3897df9d9a19ea4ff0200916240dad Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 21 Jun 2024 08:54:11 +0800 Subject: [PATCH 2/3] fix: data --- .../README.md" | 6 ++---- .../Solution.py" | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" index 45e754b42c534..168eaaf954af8 100644 --- "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" @@ -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) diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.py" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.py" index 99e64a0106d93..9aa394a215500 100644 --- "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.py" +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/Solution.py" @@ -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) From 7c408bb5c68cbd67b9d514e076dc432d7e8b1a7c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 21 Jun 2024 08:57:52 +0800 Subject: [PATCH 3/3] fix: update --- .../README.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" index 168eaaf954af8..14bac274a24df 100644 --- "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" @@ -50,7 +50,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94% -### 方法一:动态规划 +### 方法一:一次遍历 我们用变量 $f$ 维护当前趋势相同的连续天数,用变量 $ans$ 维护最大的连续天数。 @@ -58,7 +58,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94% 最终返回 $ans$ 即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。