From fc0b4598c1a65194dd23090ec0c3d0ad52d21810 Mon Sep 17 00:00:00 2001 From: Luca Lombardo Date: Sat, 30 Sep 2023 15:38:17 +0200 Subject: [PATCH] solved find peak element --- 2023_09_25/find-peak-element/Cargo.toml | 8 +++ 2023_09_25/find-peak-element/README.md | 63 +++++++++++++++++++++++ 2023_09_25/find-peak-element/image.png | Bin 0 -> 14754 bytes 2023_09_25/find-peak-element/src/main.rs | 29 +++++++++++ 4 files changed, 100 insertions(+) create mode 100644 2023_09_25/find-peak-element/Cargo.toml create mode 100644 2023_09_25/find-peak-element/README.md create mode 100644 2023_09_25/find-peak-element/image.png create mode 100644 2023_09_25/find-peak-element/src/main.rs diff --git a/2023_09_25/find-peak-element/Cargo.toml b/2023_09_25/find-peak-element/Cargo.toml new file mode 100644 index 0000000..c99094c --- /dev/null +++ b/2023_09_25/find-peak-element/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "find-peak-element" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023_09_25/find-peak-element/README.md b/2023_09_25/find-peak-element/README.md new file mode 100644 index 0000000..dd71926 --- /dev/null +++ b/2023_09_25/find-peak-element/README.md @@ -0,0 +1,63 @@ +> **Problem**: https://leetcode.com/problems/find-peak-element/submissions/ + +# Comments on the solution + +We can use two different approach, that are theorethically equivalent in terms of space complexity, but in the reality the second one is more efficient, as we can see in the leetcode results + +## First approach + +```rust +fn find_peak_element(nums: Vec) -> i32 { + let (mut left, mut right) = (0, nums.len() - 1); + + while left) -> i32 { + return Self::find_peak_element_recursive(&nums, 0, nums.len() - 1) as i32; +} + +fn find_peak_element_recursive(nums: &Vec, left: usize, right: usize) -> usize { + if left == right { + return left; + } + + let mid = left + (right - left) / 2; + + if nums[mid] < nums[mid + 1] { + return Self::find_peak_element_recursive(nums, mid + 1, right); + } else { + return Self::find_peak_element_recursive(nums, left, mid); + } +} +``` + +This implementation uses a recursive helper function that takes the input vector, the left and right indices of the current subarray being searched. The helper function returns the index of the peak element. The main function simply calls the helper function with the initial left and right indices. This implementation has the same time complexity as the previous one, but may use less memory due to the reduced number of variables used. However, the difference in memory usage is negligible and I did this just for obtaining an higher score in leetcode + +![](https://i.imgur.com/5LfVVJu.png) + +### Complexity + +- **Time complexity**: `O(log(n))` because we are doing a binary search +- **Space complexity**: `O(1)` because we are not using any extra space diff --git a/2023_09_25/find-peak-element/image.png b/2023_09_25/find-peak-element/image.png new file mode 100644 index 0000000000000000000000000000000000000000..666d9d56f5f8fa43005784cbc3e7df3a7f62a552 GIT binary patch literal 14754 zcmc(GWmJ^m`zA=Yh|-}Tpfu90BB7KBNOucEcXx+Kw}^t0LkiN;-NV2DLwAbg5c}Y7 z|GQuI!=Bx9cISh_Ff;G_+<9HseNW_DWjP{zYJ4;_G@{q?uil}dVIaWI*|^x?->*hC zJm5b}=a;Y5alyj}*DL}J?IGIhS1;5(vUcY@eeSO=b{`H8>{&P`hCF`$a8|BP=DGOu z!#k?4N;MzE!%z$B(>g^a8jYVLG}w=uqn;O+zL&uiN3X}|iKTVIa!NqK2ls|9ueG#r zAJg`lJa68g>em#iIubt`s6L+F7a!&`H8V4VNBrj@4^JbSLnHn7NHC-ch5Yv)dDP(K z|M?M*@BidUXF(T?SGSLq4l){=XvA*Uq65E+TAX#@bF}g)1#N9@_!fV7P3ulJ=IcG^ zX)(aQ!rXt>F&z|k@2)zE!`#;_zBXg|RBbtj@jv8LiheDzA$HIGqtyT;a zVdPgA(=>1J8tZC@Rs~}9{=LDEx1yo~f5vsesXih$wp5dD4N8tbBL`j|E>a?s-5)Mx zD@GU-cbH}=&?e6JAYY%B(pW0$pEYR1XYG{q%Ze`y#H!rirRg{=eIFJeKxRNN23}aF ztqX+qT*35{Ph{aWu`Zr;N7`xn!8tlzvlrHq45l-b=6Q zAY}ImP8_vs1j&%zV;Xsa0V(YK_yd)v==x$Pq*xJkSf&PnOI98Gm& zBxCU!FUU@cOqtTCeG_V&avZ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xAFg|Dl2DYCckh(r30RpXqXm$*pSjyPgJK0$3 zPSES0gQ*sP!wF1s^lvSd%uj{f_w)?BHks*t4qgC;2vqMMf?~GuAgg(A5m_^8PiJ`1 z$iU}NMiRBd2Q=)iKdb%e!j6n!$%33l&=zds?0;^NCg;-Y0ZkYT34D$QguEYosp|50R!GLAk#{pDBJg0^^@cTOh z1A}$Z2_YcchR6wDta_DBb8&Go^76jU{sojr3Bc{|ktG71spvEV@^5YMn5@iiT02)3 zU}ms!0VL^;<-@ny+JVFC#hSsl^FPf0v8V`Cpf6uuiqoq!ZEM`F2|BIB$+p;nvC~Au zsQ_VcS{fx-5dcx)b*~7>&X3@=vB>@I|qx`DurBTp-1NO}PX#fBK literal 0 HcmV?d00001 diff --git a/2023_09_25/find-peak-element/src/main.rs b/2023_09_25/find-peak-element/src/main.rs new file mode 100644 index 0000000..e936a35 --- /dev/null +++ b/2023_09_25/find-peak-element/src/main.rs @@ -0,0 +1,29 @@ +struct Solution; + +impl Solution { + pub fn find_peak_element(nums: Vec) -> i32 { + // 1. Call recursive function + return Self::find_peak_element_recursive(&nums, 0, nums.len() - 1) as i32; + } + + // 2. Recursive function: take the vector, and the start and end indexes as arguments + pub fn find_peak_element_recursive(nums: &Vec, left: usize, right: usize) -> usize { + // 3. Base case: if left and right are the same, return left + if left == right { + return left; + } + // 4. Find the middle index + let mid = left + (right - left) / 2; + // 5. If mid is less than mid + 1, return the recursive function, with start at mid + 1 + if nums[mid] < nums[mid + 1] { + return Self::find_peak_element_recursive(nums, mid + 1, right); + } else { + // 6. Else, return the recursive function, with end at mid + return Self::find_peak_element_recursive(nums, left, mid); + } + } +} + +fn main() { + println!("{}", Solution::find_peak_element(vec![1, 2, 3, 1])); +}