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(&['[', ']'][..])
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim();
|
||||
if x == "null" {
|
||||
None
|
||||
-1
|
||||
} else {
|
||||
Some(x.parse::<i32>().unwrap())
|
||||
x.parse::<i32>().unwrap()
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
|
|
|
|||
79
src/main.rs
79
src/main.rs
|
|
@ -1,61 +1,44 @@
|
|||
mod arr;
|
||||
struct Solution;
|
||||
|
||||
use crate::arr::make_matrix;
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::arr::make_arr;
|
||||
impl Solution {
|
||||
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 {
|
||||
let n = n as usize;
|
||||
let mut m = 0;
|
||||
let languages: Vec<HashSet<i32>> = languages
|
||||
.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;
|
||||
}
|
||||
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
|
||||
let k = k as usize % nums.len();
|
||||
if k == 0 {
|
||||
return;
|
||||
}
|
||||
for lang in 1..n + 1 {
|
||||
let mut added = vec![false; m + 1];
|
||||
let mut temp_ans = 0;
|
||||
for i in 0..friendships.len() {
|
||||
if is_friend[i] {
|
||||
continue;
|
||||
}
|
||||
let ua = friendships[i][0] as usize - 1;
|
||||
let ub = friendships[i][1] as usize - 1;
|
||||
let lang1 = &languages[ua];
|
||||
let lang2 = &languages[ub];
|
||||
if !(lang1.contains(&(lang as i32)) || added[ua]) {
|
||||
added[ua] = true;
|
||||
temp_ans += 1;
|
||||
}
|
||||
if !(lang2.contains(&(lang as i32)) || added[ub]) {
|
||||
added[ub] = true;
|
||||
temp_ans += 1;
|
||||
let mut temp = nums[0];
|
||||
let mut j = k;
|
||||
let mut count = 0;
|
||||
let mut start = 0;
|
||||
let mut next_start = start + 1;
|
||||
while count < nums.len() {
|
||||
let temp_b = nums[j];
|
||||
nums[j] = temp;
|
||||
temp = temp_b;
|
||||
if j == start {
|
||||
j = next_start;
|
||||
start = next_start;
|
||||
next_start = start + 1;
|
||||
temp = nums[j];
|
||||
if j >= k {
|
||||
break;
|
||||
}
|
||||
}
|
||||
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() {
|
||||
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 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::minimum_teachings(17, lang, friendships);
|
||||
println!("{:?}", sl);
|
||||
let mut arr = make_arr("[1,2,3,4,5,6]");
|
||||
let sl = Solution::rotate(&mut arr, 4);
|
||||
println!("{:?}", arr);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue