From e6bc4bc084268820455da262950d9df85ffd0c59 Mon Sep 17 00:00:00 2001 From: Abhishek Tripathi <42455093+abhishektripathi66@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:34:10 +0000 Subject: [PATCH] adding Shortest Subarray to removed to make --- .github/workflows/mdbook.yml | 60 ---------------- .github/workflows/static.yml | 43 ----------- ...tSubarraytobeRemovedtoMakeArraySorted.java | 71 +++++++++++++++++++ 3 files changed, 71 insertions(+), 103 deletions(-) delete mode 100644 .github/workflows/mdbook.yml delete mode 100644 .github/workflows/static.yml create mode 100644 src/Coding Questions/Leetcode/ShortestSubarraytobeRemovedtoMakeArraySorted.java diff --git a/.github/workflows/mdbook.yml b/.github/workflows/mdbook.yml deleted file mode 100644 index 109cc83..0000000 --- a/.github/workflows/mdbook.yml +++ /dev/null @@ -1,60 +0,0 @@ -# Sample workflow for building and deploying a mdBook site to GitHub Pages -# -# To get started with mdBook see: https://rust-lang.github.io/mdBook/index.html -# -name: Deploy mdBook site to Pages - -on: - # Runs on pushes targeting the default branch - push: - branches: ["master"] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages -permissions: - contents: read - pages: write - id-token: write - -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. -concurrency: - group: "pages" - cancel-in-progress: false - -jobs: - # Build job - build: - runs-on: ubuntu-latest - env: - MDBOOK_VERSION: 0.4.36 - steps: - - uses: actions/checkout@v4 - - name: Install mdBook - run: | - curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf -y | sh - rustup update - cargo install --version ${MDBOOK_VERSION} mdbook - - name: Setup Pages - id: pages - uses: actions/configure-pages@v5 - - name: Build with mdBook - run: mdbook build - - name: Upload artifact - uses: actions/upload-pages-artifact@v3 - with: - path: ./book - - # Deployment job - deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - needs: build - steps: - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v4 diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml deleted file mode 100644 index 0ba8230..0000000 --- a/.github/workflows/static.yml +++ /dev/null @@ -1,43 +0,0 @@ -# Simple workflow for deploying static content to GitHub Pages -name: Deploy static content to Pages - -on: - # Runs on pushes targeting the default branch - push: - branches: ["master"] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages -permissions: - contents: read - pages: write - id-token: write - -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. -concurrency: - group: "pages" - cancel-in-progress: false - -jobs: - # Single deploy job since we're just deploying - deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Setup Pages - uses: actions/configure-pages@v5 - - name: Upload artifact - uses: actions/upload-pages-artifact@v3 - with: - # Upload entire repository - path: '.' - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v4 diff --git a/src/Coding Questions/Leetcode/ShortestSubarraytobeRemovedtoMakeArraySorted.java b/src/Coding Questions/Leetcode/ShortestSubarraytobeRemovedtoMakeArraySorted.java new file mode 100644 index 0000000..588a01a --- /dev/null +++ b/src/Coding Questions/Leetcode/ShortestSubarraytobeRemovedtoMakeArraySorted.java @@ -0,0 +1,71 @@ +/** + * 1574. Shortest Subarray to be Removed to Make Array Sorted +Solved +Medium +Topics +Companies +Hint +Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. + +Return the length of the shortest subarray to remove. + +A subarray is a contiguous subsequence of the array. + + + +Example 1: + +Input: arr = [1,2,3,10,4,2,3,5] +Output: 3 +Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. +Another correct solution is to remove the subarray [3,10,4]. +Example 2: + +Input: arr = [5,4,3,2,1] +Output: 4 +Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1]. +Example 3: + +Input: arr = [1,2,3] +Output: 0 +Explanation: The array is already non-decreasing. We do not need to remove any elements. + + */ + +public class ShortestSubarraytobeRemovedtoMakeArraySorted { + + public int findLengthOfShortestSubarray(int[] arr) { + int n = arr.length; + + // Step 1: Find the longest non-decreasing prefix + int left = 0; + while (left + 1 < n && arr[left] <= arr[left + 1]) { + left++; + } + + // If the entire array is already sorted + if (left == n - 1) return 0; + + // Step 2: Find the longest non-decreasing suffix + int right = n - 1; + while (right > 0 && arr[right - 1] <= arr[right]) { + right--; + } + + // Step 3: Find the minimum length to remove by comparing prefix and suffix + int result = Math.min(n - left - 1, right); + + // Step 4: Use two pointers to find the smallest middle part to remove + int i = 0, j = right; + while (i <= left && j < n) { + if (arr[i] <= arr[j]) { + result = Math.min(result, j - i - 1); + i++; + } else { + j++; + } + } + + return result; + } +}