189. 轮转数组
This commit is contained in:
parent
bb448434ba
commit
7fc0c30cf1
|
|
@ -1,12 +1,12 @@
|
||||||
pub fn make_arr(s: &str) -> Vec<Option<i32>> {
|
pub fn make_arr(s: &str) -> Vec<i32> {
|
||||||
s.trim_matches(&['[', ']'][..])
|
s.trim_matches(&['[', ']'][..])
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
let x = x.trim();
|
let x = x.trim();
|
||||||
if x == "null" {
|
if x == "null" {
|
||||||
None
|
-1
|
||||||
} else {
|
} else {
|
||||||
Some(x.parse::<i32>().unwrap())
|
x.parse::<i32>().unwrap()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
|
|
||||||
79
src/main.rs
79
src/main.rs
|
|
@ -1,61 +1,44 @@
|
||||||
mod arr;
|
mod arr;
|
||||||
struct Solution;
|
struct Solution;
|
||||||
|
|
||||||
use crate::arr::make_matrix;
|
use crate::arr::make_arr;
|
||||||
use std::cmp::{max, min};
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 {
|
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
|
||||||
let n = n as usize;
|
let k = k as usize % nums.len();
|
||||||
let mut m = 0;
|
if k == 0 {
|
||||||
let languages: Vec<HashSet<i32>> = languages
|
return;
|
||||||
.into_iter()
|
|
||||||
.map(|x| x.into_iter().collect())
|
|
||||||
.collect();
|
|
||||||
let mut ans = 1e9 as i32;
|
|
||||||
let mut is_friend = vec![false; friendships.len()];
|
|
||||||
for i in 0..friendships.len() {
|
|
||||||
let ua = friendships[i][0] as usize - 1;
|
|
||||||
let ub = friendships[i][1] as usize - 1;
|
|
||||||
m = max(m,ua);
|
|
||||||
m = max(m,ub);
|
|
||||||
let lang1 = &languages[ua];
|
|
||||||
let lang2 = &languages[ub];
|
|
||||||
let intersection: HashSet<_> = lang1.intersection(&lang2).cloned().collect();
|
|
||||||
if !intersection.is_empty() {
|
|
||||||
is_friend[i] = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for lang in 1..n + 1 {
|
let mut temp = nums[0];
|
||||||
let mut added = vec![false; m + 1];
|
let mut j = k;
|
||||||
let mut temp_ans = 0;
|
let mut count = 0;
|
||||||
for i in 0..friendships.len() {
|
let mut start = 0;
|
||||||
if is_friend[i] {
|
let mut next_start = start + 1;
|
||||||
continue;
|
while count < nums.len() {
|
||||||
}
|
let temp_b = nums[j];
|
||||||
let ua = friendships[i][0] as usize - 1;
|
nums[j] = temp;
|
||||||
let ub = friendships[i][1] as usize - 1;
|
temp = temp_b;
|
||||||
let lang1 = &languages[ua];
|
if j == start {
|
||||||
let lang2 = &languages[ub];
|
j = next_start;
|
||||||
if !(lang1.contains(&(lang as i32)) || added[ua]) {
|
start = next_start;
|
||||||
added[ua] = true;
|
next_start = start + 1;
|
||||||
temp_ans += 1;
|
temp = nums[j];
|
||||||
}
|
if j >= k {
|
||||||
if !(lang2.contains(&(lang as i32)) || added[ub]) {
|
break;
|
||||||
added[ub] = true;
|
|
||||||
temp_ans += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ans = min(ans, temp_ans);
|
if j > start && j < k {
|
||||||
|
next_start = j + 1;
|
||||||
|
}
|
||||||
|
j += k;
|
||||||
|
j %= nums.len();
|
||||||
|
count += 1;
|
||||||
}
|
}
|
||||||
ans
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let lang = make_matrix("[[4,7,2,14,6],[15,13,6,3,2,7,10,8,12,4,9],[16],[10],[10,3],[4,12,8,1,16,5,15,17,13],[4,13,15,8,17,3,6,14,5,10],[11,4,13,8,3,14,5,7,15,6,9,17,2,16,12],[4,14,6],[16,17,9,3,11,14,10,12,1,8,13,4,5,6],[14],[7,14],[17,15,10,3,2,12,16,14,1,7,9,6,4]]");
|
let mut arr = make_arr("[1,2,3,4,5,6]");
|
||||||
let friendships = make_matrix("[[4,11],[3,5],[7,10],[10,12],[5,7],[4,5],[3,8],[1,5],[1,6],[7,8],[4,12],[2,4],[8,9],[3,10],[4,7],[5,12],[4,9],[1,4],[2,8],[1,2],[3,4],[5,10],[2,7],[1,7],[1,8],[8,10],[1,9],[1,10],[6,7],[3,7],[8,12],[7,9],[9,11],[2,5],[2,3]]");
|
let sl = Solution::rotate(&mut arr, 4);
|
||||||
let sl = Solution::minimum_teachings(17, lang, friendships);
|
println!("{:?}", arr);
|
||||||
println!("{:?}", sl);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue