Skip to content

[pull] main from doocs:main #513

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 5 commits into from
Apr 5, 2023
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
85 changes: 84 additions & 1 deletion lcp/LCP 61. 气温变化趋势/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,105 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:动态规划**

我们用变量 $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$ 为数组长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

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]
if x == y == 0 or x * y > 0:
f += 1
ans = max(ans, f)
else:
f = 0
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int temperatureTrend(int[] temperatureA, int[] temperatureB) {
int ans = 0, f = 0;
for (int i = 0; i < temperatureA.length - 1; ++i) {
int x = temperatureA[i + 1] - temperatureA[i];
int y = temperatureB[i + 1] - temperatureB[i];
if ((x == 0 && y == 0) || x * y > 0) {
ans = Math.max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
int ans = 0, f = 0;
for (int i = 0; i < temperatureA.size() - 1; ++i) {
int x = temperatureA[i + 1] - temperatureA[i];
int y = temperatureB[i + 1] - temperatureB[i];
if ((x == 0 && y == 0) || x * y > 0) {
ans = max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
};
```

### **Go**

```go
func temperatureTrend(temperatureA []int, temperatureB []int) int {
ans, f := 0, 0
for i := range temperatureA[1:] {
x := temperatureA[i+1] - temperatureA[i]
y := temperatureB[i+1] - temperatureB[i]
if (x == 0 && y == 0) || x*y > 0 {
f++
ans = max(ans, f)
} else {
f = 0
}
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
16 changes: 16 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
int ans = 0, f = 0;
for (int i = 0; i < temperatureA.size() - 1; ++i) {
int x = temperatureA[i + 1] - temperatureA[i];
int y = temperatureB[i + 1] - temperatureB[i];
if ((x == 0 && y == 0) || x * y > 0) {
ans = max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
};
21 changes: 21 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func temperatureTrend(temperatureA []int, temperatureB []int) int {
ans, f := 0, 0
for i := range temperatureA[1:] {
x := temperatureA[i+1] - temperatureA[i]
y := temperatureB[i+1] - temperatureB[i]
if (x == 0 && y == 0) || x*y > 0 {
f++
ans = max(ans, f)
} else {
f = 0
}
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
15 changes: 15 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int temperatureTrend(int[] temperatureA, int[] temperatureB) {
int ans = 0, f = 0;
for (int i = 0; i < temperatureA.length - 1; ++i) {
int x = temperatureA[i + 1] - temperatureA[i];
int y = temperatureB[i + 1] - temperatureB[i];
if ((x == 0 && y == 0) || x * y > 0) {
ans = Math.max(ans, ++f);
} else {
f = 0;
}
}
return ans;
}
}
13 changes: 13 additions & 0 deletions lcp/LCP 61. 气温变化趋势/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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]
if x == y == 0 or x * y > 0:
f += 1
ans = max(ans, f)
else:
f = 0
return ans
115 changes: 114 additions & 1 deletion lcp/LCP 62. 交通枢纽/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,135 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:统计入度和出度**

我们创建两个数组 $ind$ 和 $outd$,分别用于记录每个点的入度和出度,用哈希表 $s$ 保存每个节点。

接下来,遍历每个节点 $c$,如果存在 $ind[c]$ 等于节点总数减去 $1$,并且 $outd[c]=0$,说明存在满足条件的交通枢纽,返回 $c$。

否则遍历结束,返回 $-1$。

时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是节点数量以及路径的数量。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def transportationHub(self, path: List[List[int]]) -> int:
ind = Counter()
outd = Counter()
s = set()
for a, b in path:
s.add(a)
s.add(b)
outd[a] += 1
ind[b] += 1
for c in s:
if ind[c] == len(s) - 1 and outd[c] == 0:
return c
return -1
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int transportationHub(int[][] path) {
int[] ind = new int[1001];
int[] outd = new int[1001];
Set<Integer> s = new HashSet<>();
for (int[] p : path) {
int a = p[0], b = p[1];
s.add(a);
s.add(b);
ind[b]++;
outd[a]++;
}
for (int c : s) {
if (ind[c] == s.size() - 1 && outd[c] == 0) {
return c;
}
}
return -1;
}
}
```

### **C++**

```cpp
class Solution {
public:
int transportationHub(vector<vector<int>>& path) {
int ind[1001]{};
int outd[1001]{};
unordered_set<int> s;
for (auto& p : path) {
int a = p[0], b = p[1];
s.insert(a);
s.insert(b);
ind[b]++;
outd[a]++;
}
for (int c : s) {
if (ind[c] == s.size() - 1 && outd[c] == 0) {
return c;
}
}
return -1;
}
};
```

### **Go**

```go
func transportationHub(path [][]int) int {
ind := [1001]int{}
outd := [1001]int{}
s := map[int]struct{}{}
for _, p := range path {
a, b := p[0], p[1]
s[a] = struct{}{}
s[b] = struct{}{}
outd[a]++
ind[b]++
}
for c := range s {
if ind[c] == len(s)-1 && outd[c] == 0 {
return c
}
}
return -1
}
```

### **TypeScript**

```ts
function transportationHub(path: number[][]): number {
const ind: number[] = new Array(1001).fill(0);
const outd: number[] = new Array(1001).fill(0);
const s: Set<number> = new Set();
for (const [a, b] of path) {
s.add(a);
s.add(b);
ind[b]++;
outd[a]++;
}
for (const c of s) {
if (ind[c] === s.size - 1 && outd[c] === 0) {
return c;
}
}
return -1;
}
```

### **...**
Expand Down
21 changes: 21 additions & 0 deletions lcp/LCP 62. 交通枢纽/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int transportationHub(vector<vector<int>>& path) {
int ind[1001]{};
int outd[1001]{};
unordered_set<int> s;
for (auto& p : path) {
int a = p[0], b = p[1];
s.insert(a);
s.insert(b);
ind[b]++;
outd[a]++;
}
for (int c : s) {
if (ind[c] == s.size() - 1 && outd[c] == 0) {
return c;
}
}
return -1;
}
};
18 changes: 18 additions & 0 deletions lcp/LCP 62. 交通枢纽/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func transportationHub(path [][]int) int {
ind := [1001]int{}
outd := [1001]int{}
s := map[int]struct{}{}
for _, p := range path {
a, b := p[0], p[1]
s[a] = struct{}{}
s[b] = struct{}{}
outd[a]++
ind[b]++
}
for c := range s {
if ind[c] == len(s)-1 && outd[c] == 0 {
return c
}
}
return -1
}
20 changes: 20 additions & 0 deletions lcp/LCP 62. 交通枢纽/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int transportationHub(int[][] path) {
int[] ind = new int[1001];
int[] outd = new int[1001];
Set<Integer> s = new HashSet<>();
for (int[] p : path) {
int a = p[0], b = p[1];
s.add(a);
s.add(b);
ind[b]++;
outd[a]++;
}
for (int c : s) {
if (ind[c] == s.size() - 1 && outd[c] == 0) {
return c;
}
}
return -1;
}
}
14 changes: 14 additions & 0 deletions lcp/LCP 62. 交通枢纽/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def transportationHub(self, path: List[List[int]]) -> int:
ind = Counter()
outd = Counter()
s = set()
for a, b in path:
s.add(a)
s.add(b)
outd[a] += 1
ind[b] += 1
for c in s:
if ind[c] == len(s) - 1 and outd[c] == 0:
return c
return -1
Loading