Rust One-Liner
TL;DR give me the answer:\nRust\nimpl Solution {\n    pub fn largest_altitude(gain: Vec<i32>) -> i32 {\n        gain.into_iter().fold((0, 0), |(c, m), x| (c + x
# TL;DR give me the answer:\n```Rust\nimpl Solution {\n    pub fn largest_altitude(gain: Vec<i32>) -> i32 {\n        gain.into_iter().fold((0, 0), |(c, m), x| (c + x, m.max(c + x))).1\n    }\n}\n```\n\n\n# Approach\n\n## 1. Base intuition\nThis is a "find the maximum" problem which means we can apply dynamic programming principles to it. The final solution is a bottom-up (iterative) approach.\nFor this problem, we need to find the maximum altitude of *any* point in the graph. Mathematically, we are given differences in altitude between points, which might be thought of as a derivative. Thus we should compute the discrete summation and track the maximum value of the sum at each step.\n```Rust\n```\n\n## 2. Refining into one line\nWe can start by recognizing there is no need to store anything but the *running* maximum:\n```Rust\npub fn largest_altitude(gain: Vec<i32>) -> i32 {\n    let mut current_altitude = 0;\n    let mut maximum_altitude = 0;\n    for gain_iterator in 0..(gain.len() - 1) {\n        current_altitude += gain[gain_iterator];\n        if (current_altitude > maximum_altitude){\n            maximum_alititude = current_altitude;\n        }\n    }\n    return maximum_altitude;\n}\n```\nBy utilising iterators on gain instead of `usize` (which is what `gain_iterator` defaults to otherwise):\n```Rust\nfor gain_iterator in gain.into_iter() {\n    current_altitude += gain_iterator;\n```\nBut the magic really happens with `fold()`.\nFirst, we can take a hint from `current_altitude` and `maximum_altitude` as our *closure*:\n\n```Rust\n(0, 0) == (current_altitude, maximum_altitude)\n```\n\nThen, we need to update current altitude by the difference given to `fold()` by each iterator: `|(0, 0), gain_iterator|`.\n\nWe know this tuple needs to be updated with the new altitude:\n```Rust\n(current_altitude + gain_iterator, ...\n```\nand the running maximum between that new altitude and the previous maximum:\n```Rust\n... maximum_altitude.max(current_altitude + gain_iterator) )\n```\nSince the variable names don\'t matter, let\'s abbreviate:\n- c = current_altitude\n- m = maximum_altitude\n- x = gain_iterator  \n\nThe closure expression is now:\n```Rust\n|(c, m), x| (c + x, m.max(c + x))\n```\n... which we use in `fold()`:\n```Rust\ngain.into_iter().fold((0, 0), |(c, m), x| (c + x, m.max(c + x)))\n```\nAnd we can easily access the maximum element of the tuple using `.1`:\n```Rust\ngain.into_iter().fold((0, 0), |(c, m), x| (c + x, m.max(c + x))).1\n```\nA `return` statement is unnecessary too, because by dropping the `;` at the end of this line, it becomes an evaluation.\n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nimpl Solution {\n    pub fn largest_altitude(gain: Vec<i32>) -> i32 {\n        gain.into_iter().fold((0, 0), |(c, m), x| (c + x, m.max(c + x))).1\n    }\n}\n```
['Rust']