/* * @lc app=leetcode id=1299 lang=rust * * [1299] Replace Elements with Greatest Element on Right Side * * https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/description/ * * algorithms * Easy (77.50%) * Total Accepted: 17.6K * Total Submissions: 22.7K * Testcase Example: '[17,18,5,4,6,1]' * * Given an array arr, replace every element in that array with the greatest * element among the elements to its right, and replace the last element with * -1. * * After doing so, return the array. * * * Example 1: * Input: arr = [17,18,5,4,6,1] * Output: [18,6,6,6,1,-1] * * * Constraints: * * * 1 <= arr.length <= 10^4 * 1 <= arr[i] <= 10^5 * */ impl Solution { pub fn replace_elements(mut arr: Vec) -> Vec { let mut premax = -1; for i in arr.iter_mut().rev(){ let t = *i; *i = premax; premax = max(premax, t); } arr } } // pub structSolution; use std::collections::HashMap; use std::fmt::Debug; use std::hash::Hash; use std::cmp::max; use std::cmp::min; #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res }