├── .github └── workflows │ ├── cd.yml │ └── ci.yml ├── Cargo.toml ├── .gitignore ├── src └── lib.rs ├── LICENSE └── README.md /.github/workflows/cd.yml: -------------------------------------------------------------------------------- 1 | name: CD 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: Set up Rust 14 | uses: actions-rs/toolchain@v1 15 | with: 16 | toolchain: stable 17 | override: true 18 | - name: Build project 19 | run: cargo build --release 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Set up Rust 17 | uses: actions-rs/toolchain@v1 18 | with: 19 | toolchain: stable 20 | override: true 21 | - name: Run tests 22 | run: cargo test 23 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest-subsequence-with-decreasing-adjacent-difference" 3 | version = "0.1.0" 4 | edition = "2021" 5 | authors = ["aliezzahn "] 6 | description = "A Rust library to find the length of the longest subsequence with decreasing adjacent differences." 7 | repository = "https://github.com/aliezzahn/longest-subsequence-with-decreasing-adjacent-difference" 8 | license = "MIT" 9 | keywords = ["subsequence", "dynamic-programming", "algorithm"] 10 | categories = ["algorithms"] 11 | 12 | [dependencies] 13 | 14 | [dev-dependencies] -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | #.idea/ 22 | 23 | # Added by cargo 24 | 25 | /target 26 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /// A library to find the length of the longest subsequence with decreasing adjacent differences. 2 | pub struct Solution; 3 | 4 | impl Solution { 5 | pub fn longest_subsequence(nums: Vec) -> i32 { 6 | let n = nums.len(); 7 | if n == 0 { 8 | return 0; 9 | } 10 | 11 | let mut dp = vec![1; n]; // dp[i] stores the length of the longest subsequence ending at i 12 | let mut max_len = 1; 13 | 14 | for i in 1..n { 15 | for j in 0..i { 16 | let diff_i_j = (nums[i] - nums[j]).abs(); 17 | let diff_j_prev = if j > 0 { (nums[j] - nums[j - 1]).abs() } else { i32::MAX }; 18 | 19 | if j == 0 || diff_i_j < diff_j_prev { 20 | dp[i] = dp[i].max(dp[j] + 1); 21 | } 22 | } 23 | max_len = max_len.max(dp[i]); 24 | } 25 | 26 | max_len 27 | } 28 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 aliezza hn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Longest Subsequence with Decreasing Adjacent Difference 2 | 3 | A Rust library to find the length of the longest subsequence with decreasing adjacent differences. 4 | 5 | ![CI](https://github.com/aliezzahn/longest-subsequence-with-decreasing-adjacent-difference/actions/workflows/ci.yml/badge.svg) 6 | ![CD](https://github.com/aliezzahn/longest-subsequence-with-decreasing-adjacent-difference/actions/workflows/cd.yml/badge.svg) 7 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 8 | 9 | ## Overview 10 | 11 | Given a sequence of integers, this library computes the length of the longest subsequence where the difference between adjacent elements is strictly decreasing. 12 | 13 | ## Installation 14 | 15 | Add the following to your `Cargo.toml`: 16 | 17 | ```toml 18 | [dependencies] 19 | longest-subsequence-with-decreasing-adjacent-difference = "0.1" 20 | ``` 21 | 22 | ## Usage 23 | 24 | ```rust 25 | use longest_subsequence_with_decreasing_adjacent_difference::Solution; 26 | 27 | fn main() { 28 | let nums = vec![10, 20, 10, 19, 10, 20]; 29 | let result = Solution::longest_subsequence(nums); 30 | println!("Length of the longest subsequence: {}", result); 31 | } 32 | ``` 33 | 34 | ## Example Output 35 | 36 | For the input `[10, 20, 10, 19, 10, 20]`, the output will be: 37 | 38 | ``` 39 | Length of the longest subsequence: 5 40 | ``` 41 | 42 | ## Running Tests 43 | 44 | To run the tests, use the following command: 45 | 46 | ```bash 47 | cargo test 48 | ``` 49 | 50 | ## License 51 | 52 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 53 | 54 | ## Contributing 55 | 56 | Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/aliezzahn/longest-subsequence-with-decreasing-adjacent-difference). 57 | 58 | ## Contact 59 | 60 | For questions or feedback, feel free to reach out: 61 | 62 | - Email: [aliezzahn@gmail.com](mailto:aliezzahn@gmail.com) 63 | - GitHub: [aliezzahn](https://github.com/aliezzahn) 64 | --------------------------------------------------------------------------------