对于[Swift]LeetCode646.最长数对链|MaximumLengthofPairChain感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于"Maximumlengthexcee
对于[Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于"Maximum length exceeded"错误的解决办法、#Es 问题 --source [n/a, actual length: [3.4kb], max length: 2kb]}、2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当、2022-05-18:假设数组a和数组b为两组信号: 1) length(b) <= length(a); 2) 对于任意0<=i<length(b), 有b[i+1] - b[i] == a[i+1的宝贵知识。
本文目录一览:- [Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain
- "Maximum length exceeded"错误的解决办法
- #Es 问题 --source [n/a, actual length: [3.4kb], max length: 2kb]}
- 2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当
- 2022-05-18:假设数组a和数组b为两组信号: 1) length(b) <= length(a); 2) 对于任意0<=i<length(b), 有b[i+1] - b[i] == a[i+1
[Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain
You are given n
pairs of numbers. In every pair,the first number is always smaller than the second number.
Now,we define a pair (c,d)
can follow another pair (a,b)
if and only if b < c
. Chain of pairs can be formed in this fashion.
Given a set of pairs,find the length longest chain which can be formed. You needn‘t use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2],[2,3],[3,4]] Output: 2 Explanation: The longest chain is [1,2] -> [3,4]
Note:
- The number of given pairs will be in the range [1,1000].
给出 n
个数对。 在每一个数对中,第一个数字总是比第二个数字小。
现在,我们定义一种跟随关系,当且仅当 b < c
时,数对(c,d)
才可以跟在 (a,b)
后面。我们用这种形式来构造一个数对链。
给定一个对数集合,找出能够形成的最长数对链的长度。你不需要用到所有的数对,你可以以任何顺序选择其中的一些数对来构造。
示例 :
输入: [[1,4]] 输出: 2 解释: 最长的数对链是 [1,4]
注意:
- 给出数对的个数在 [1,1000] 范围内。
1 class Solution { 2 func findLongestChain(_ pairs: [[Int]]) -> Int { 3 var pairs = pairs 4 var res:Int = 0 5 var end:Int = Int.min 6 pairs.sort(by:{(a:[Int],b:[Int]) -> Bool in 7 return a[1] < b[1] 8 }) 9 for pair in pairs 10 { 11 if pair[0] > end 12 { 13 res += 1 14 end = pair[1] 15 } 16 } 17 return res 18 } 19 }
284ms
1 class Solution { 2 func findLongestChain(_ pairs: [[Int]]) -> Int { 3 let sortPairs = pairs.sorted { $0[1] < $1[1] } 4 var ans = 0,curEnd = Int.min 5 for pair in sortPairs { 6 if pair[0] > curEnd { 7 ans += 1 8 curEnd = pair[1] 9 } 10 } 11 return ans 12 } 13 }
296ms
1 class Solution { 2 func findLongestChain(_ pairs: [[Int]]) -> Int { 3 guard pairs.count > 1 else{ 4 return pairs.count 5 } 6 let sorted = pairs.sorted(by: {$0[1] < $1[1]}) 7 var count : Int = 1 8 var currentPair = sorted[0] 9 for pair in sorted{ 10 if pair.first! > currentPair.last!{ 11 count += 1 12 currentPair = pair 13 } 14 } 15 return count 16 } 17 }
1260ms
1 sample 1260 ms submission 2 class Solution { 3 func findLongestChain(_ pairs: [[Int]]) -> Int { 4 let n = pairs.count 5 if n <= 1{ return n } 6 7 let sorted = pairs.sorted { $0[0] < $1[0] } 8 var f = [Int](repeating: 1,count: n) 9 var result = 1 10 11 for i in 1..<n { 12 for j in 0..<i { 13 if sorted[j][1] < sorted[i][0] { 14 f[i] = max(f[i],f[j] + 1) 15 result = max(result,f[i]) 16 } 17 } 18 } 19 20 return result 21 } 22 }
1272ms
1 class Solution { 2 func findLongestChain(_ pairs: [[Int]]) -> Int { 3 guard pairs.count > 1 else { return 1 } 4 let pairs = pairs.sorted(by: { 5 return $0[0] < $1[0] 6 }) 7 8 let n = pairs.count 9 var dp: [Int] = Array(repeating: 0,count: n) 10 dp[0] = 1 11 12 for i in 1 ..< n { 13 for j in 0 ..< i { 14 if pairs[i][0] > pairs[j][1] { 15 dp[i] = max(dp[i],dp[j]) 16 } 17 } 18 dp[i] += 1 19 } 20 21 return dp[n - 1] 22 } 23 }
"Maximum length exceeded"错误的解决办法
如:
</ system.web.extensions >
这是解决 ajax传输字节数大于默认值时的设置。
#Es 问题 --source [n/a, actual length: [3.4kb], max length: 2kb]}
bulk异常:
source[n/a, actual length: [3.4kb], max length: 2kb]}
2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当
2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当 A[i] 和 A[j] 共用一个大于 1 的公因数时,A[i] 和 A[j] 之间才有一条边。返回图中最大连通组件的大小。
福大大 答案2021-08-02:
算出每个的公因数,然后并查集。
时间复杂度: O(N*sqrt(V))。
空间复杂度: O(N)。
代码用golang编写。代码如下:
package main
import (
"fmt"
"math"
)
func main() {
arr := []int{
2, 3, 6, 7, 4, 12, 21, 39}
ret := largestComponentSize2(arr)
fmt.Println(ret)
}
func largestComponentSize2(arr []int) int {
N := len(arr)
// arr中,N个位置,在并查集初始时,每个位置自己是一个集合
unionFind := NewUnionFind(N)
// key 某个因子 value 哪个位置拥有这个因子
fatorsMap := make(map[int]int)
for i := 0; i < N; i++ {
num := arr[i]
// 求出根号N, -> limit
limit := int(math.Sqrt(float64(num)))
for j := 1; j <= limit; j++ {
if num%j == 0 {
if j != 1 {
if _, ok := fatorsMap[j]; !ok {
fatorsMap[j] = i
} else {
unionFind.union(fatorsMap[j], i)
}
}
other := num / j
if other != 1 {
if _, ok := fatorsMap[other]; !ok {
fatorsMap[other] = i
} else {
unionFind.union(fatorsMap[other], i)
}
}
}
}
}
return unionFind.maxSize()
}
// O(1)
// m,n 要是正数,不能有任何一个等于0
func gcd(a int, b int) int {
if b == 0 {
return a
} else {
return gcd(b, a%b)
}
}
type UnionFind struct {
parents []int
sizes []int
help []int
}
func NewUnionFind(N int) *UnionFind {
res := &UnionFind{
}
res.parents = make([]int, N)
res.sizes = make([]int, N)
res.help = make([]int, N)
for i := 0; i < N; i++ {
res.parents[i] = i
res.sizes[i] = 1
}
return res
}
func (this *UnionFind) maxSize() int {
ans := 0
for _, size := range this.sizes {
ans = getMax(ans, size)
}
return ans
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
func (this *UnionFind) find(i int) int {
hi := 0
for i != this.parents[i] {
this.help[hi] = i
hi++
i = this.parents[i]
}
for hi--; hi >= 0; hi-- {
this.parents[this.help[hi]] = i
}
return i
}
func (this *UnionFind) union(i int, j int) {
f1 := this.find(i)
f2 := this.find(j)
if f1 != f2 {
big := twoSelectOne(this.sizes[f1] >= this.sizes[f2], f1, f1)
small := twoSelectOne(big == f1, f2, f1)
this.parents[small] = big
this.sizes[big] = this.sizes[f1] + this.sizes[f2]
}
}
func twoSelectOne(c bool, a int, b int) int {
if c {
return a
} else {
return b
}
}
执行结果如下:
左神java代码
2022-05-18:假设数组a和数组b为两组信号: 1) length(b) <= length(a); 2) 对于任意0<=i<length(b), 有b[i+1] - b[i] == a[i+1
2022-05-18:假设数组a和数组b为两组信号:
- length(b) <= length(a);
- 对于任意0<=i<length(b), 有b[i+1] - b[i] == a[i+1] - a[i]。 那么就称信号b和信号a一致,记为b==a, 给你好多b数组,假设有m个: b0数组、b1数组... 给你好多a数组,假设有n个: a0数组、a1数组... 返回一个长度为m的结果数组ans,ans[i]表示 : bi数组和多少个a数组一致。 来自字节飞书团队。
答案2022-05-18:
前缀树。
代码用rust编写。代码如下:
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
fn main() {
let mut bs: Vec<Vec<isize>> = vec![vec![1, 7, 5], vec![2, 8, 6, 24]];
let mut as0: Vec<Vec<isize>> = vec![vec![1, 7, 5], vec![2, 8, 6, 24]];
let ans = same_teams_array(&mut bs, &mut as0);
println!("ans = {:?}", ans);
}
fn same_teams_array(bs: &mut Vec<Vec<isize>>, as0: &mut Vec<Vec<isize>>) -> Vec<isize> {
let m = bs.len() as isize;
let mut root = Arc::new(Mutex::new(TrieNode::new()));
let mut cur = Arc::clone(&mut root);
let mut cur2 = Arc::clone(&mut cur);
for i in 0..m {
let k = bs[i as usize].len() as isize;
cur = Arc::clone(&mut root);
for j in 1..k {
let diff = bs[i as usize][j as usize] - bs[i as usize][(j - 1) as usize];
if !cur.lock().unwrap().nexts.contains_key(&diff) {
cur.lock()
.unwrap()
.nexts
.insert(diff, Arc::new(Mutex::new(TrieNode::new())));
}
let c2 = Arc::clone(&mut cur.lock().unwrap().nexts.get(&diff).unwrap());
cur = c2;
}
cur.lock().unwrap().indices.push(i);
}
let mut ans: Vec<isize> = vec![];
for _i in 0..m {
ans.push(0);
}
let n = as0.len() as isize;
for i in 0..n {
let k = as0[i as usize].len() as isize;
cur = Arc::clone(&mut root);
for j in 1..k {
let diff = as0[i as usize][j as usize] - as0[i as usize][(j - 1) as usize];
if !cur.lock().unwrap().nexts.contains_key(&diff) {
break;
}
let c2 = Arc::clone(&mut cur.lock().unwrap().nexts.get(&diff).unwrap());
cur = c2;
for index in &cur.lock().unwrap().indices {
ans[(*index) as usize] += 1;
}
}
}
return ans;
}
pub struct TrieNode {
pub indices: Vec<isize>,
pub nexts: HashMap<isize, Arc<Mutex<TrieNode>>>,
}
impl TrieNode {
pub fn new() -> Self {
Self {
indices: vec![],
nexts: HashMap::new(),
}
}
}
执行结果如下:
左神java代码
今天的关于[Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain的分享已经结束,谢谢您的关注,如果想了解更多关于"Maximum length exceeded"错误的解决办法、#Es 问题 --source [n/a, actual length: [3.4kb], max length: 2kb]}、2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当、2022-05-18:假设数组a和数组b为两组信号: 1) length(b) <= length(a); 2) 对于任意0<=i<length(b), 有b[i+1] - b[i] == a[i+1的相关知识,请在本站进行查询。
本文标签: