반응형
vector의 각 element를 동일한 만큼 증가시킴 => map 메서드로 하나씩 처리한 뒤 collect 메서드로 결합.
vector의 filter 메서드로 필터링 후 count 구할 수 있음.
use std::io::{self, BufRead};
/*
* Complete the 'countApplesAndOranges' function below.
*
* The function accepts following parameters:
* 1. INTEGER s
* 2. INTEGER t
* 3. INTEGER a
* 4. INTEGER b
* 5. INTEGER_ARRAY apples
* 6. INTEGER_ARRAY oranges
*/
fn countApplesAndOranges(s: i32, t: i32, a: i32, b: i32, apples: &[i32], oranges: &[i32]) {
//println!("s : {}, t : {}, a : {}, b : {}, apples : {:?}, oranges : {:?}", s, t, a, b, apples, oranges);
let apples1: Vec<_> = apples.iter().map(|&x| x+a).collect();
//println!("apples1 : {:?}", apples1);
let oranges1: Vec<_> = oranges.iter().map(|&x| x+b).collect();
//println!("oranges1 : {:?}", oranges1);
println!("{}", apples1.iter().filter(|&x| *x >= s && *x <= t).count());
println!("{}", oranges1.iter().filter(|&x| *x >= s && *x <= t).count());
}
fn main() {
let stdin = io::stdin();
let mut stdin_iterator = stdin.lock().lines();
let first_multiple_input: Vec<String> = stdin_iterator.next().unwrap().unwrap()
.split(' ')
.map(|s| s.to_string())
.collect();
let s = first_multiple_input[0].trim().parse::<i32>().unwrap();
let t = first_multiple_input[1].trim().parse::<i32>().unwrap();
let second_multiple_input: Vec<String> = stdin_iterator.next().unwrap().unwrap()
.split(' ')
.map(|s| s.to_string())
.collect();
let a = second_multiple_input[0].trim().parse::<i32>().unwrap();
let b = second_multiple_input[1].trim().parse::<i32>().unwrap();
let third_multiple_input: Vec<String> = stdin_iterator.next().unwrap().unwrap()
.split(' ')
.map(|s| s.to_string())
.collect();
let m = third_multiple_input[0].trim().parse::<i32>().unwrap();
let n = third_multiple_input[1].trim().parse::<i32>().unwrap();
let apples: Vec<i32> = stdin_iterator.next().unwrap().unwrap()
.trim_end()
.split(' ')
.map(|s| s.to_string().parse::<i32>().unwrap())
.collect();
let oranges: Vec<i32> = stdin_iterator.next().unwrap().unwrap()
.trim_end()
.split(' ')
.map(|s| s.to_string().parse::<i32>().unwrap())
.collect();
countApplesAndOranges(s, t, a, b, &apples, &oranges);
}
반응형
'HackerRank' 카테고리의 다른 글
[HackerRank] Subarray Division in Rust (0) | 2023.05.07 |
---|---|
[HackerRank] Breaking the Records in Rust (0) | 2023.05.06 |
[HackerRank] Number Line Jumps in Rust (0) | 2023.05.01 |
[HackerRank] Grading Students in Rust (0) | 2023.04.30 |