본문 바로가기

HackerRank

[HackerRank] Breaking the Records in Rust

반응형
use std::env;
use std::fs::File;
use std::io::{self, BufRead, Write};

/*
 * Complete the 'breakingRecords' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts INTEGER_ARRAY scores as parameter.
 */

fn breakingRecords(scores: &[i32]) -> Vec<i32> {  
    let mut result = vec![0, 0];
    let mut min = &scores[0];
    let mut max = &scores[0];
    
    for score in scores {
        if score > max {
            max = score;
            result[0] += 1;
        }
        else if score < min {
            min = score;
            result[1] += 1;
        }
    }
    
    return result;   
}

fn main() {
    let stdin = io::stdin();
    let mut stdin_iterator = stdin.lock().lines();

    let mut fptr = File::create(env::var("OUTPUT_PATH").unwrap()).unwrap();

    let n = stdin_iterator.next().unwrap().unwrap().trim().parse::<i32>().unwrap();

    let scores: Vec<i32> = stdin_iterator.next().unwrap().unwrap()
        .trim_end()
        .split(' ')
        .map(|s| s.to_string().parse::<i32>().unwrap())
        .collect();

    let result = breakingRecords(&scores);

    for i in 0..result.len() {
        write!(&mut fptr, "{}", result[i]).ok();

        if i != result.len() - 1 {
            write!(&mut fptr, " ").ok();
        }
    }

    writeln!(&mut fptr).ok();
}
반응형

'HackerRank' 카테고리의 다른 글

[HackerRank] Subarray Division in Rust  (0) 2023.05.07
[HackerRank] Number Line Jumps in Rust  (0) 2023.05.01
[HackerRank] Apple and Orange in Rust  (0) 2023.05.01
[HackerRank] Grading Students in Rust  (0) 2023.04.30