From 441ec43bd7249d7e114305f1dee656967c461441 Mon Sep 17 00:00:00 2001 From: Remy Wang Date: Wed, 30 Oct 2024 17:04:24 +0100 Subject: [PATCH] update difficulty --- README.md | 1 + ...mber-of-removals-to-make-mountain-array.rb | 70 +++++++++++++++++++ .../2684-maximum-number-of-moves-in-a-grid.rb | 4 +- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb diff --git a/README.md b/README.md index be2fab8..6e1349f 100644 --- a/README.md +++ b/README.md @@ -414,6 +414,7 @@ | 1657 | Determine if Two Strings Are Close | [Ruby](./algorithms/ruby/1657-determine-if-two-strings-are-close.rb) | Medium | | 1658 | Minimum Operations to Reduce X to Zero | [Ruby](./algorithms/ruby/1658-minimum-operations-to-reduce-x-to-zero.rb) | Medium | | 1662 | Check If Two String Arrays are Equivalent | [Ruby](./algorithms/ruby/1662-check-if-two-string-arrays-are-equivalent.rb) | Easy | +| 1671 | Minimum Number of Removals to Make Mountain Array | [Ruby](./algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb) | Hard | | 1675 | Minimize Deviation in Array | [Ruby](./algorithms/ruby/1675-minimize-deviation-in-array.rb) | Hard | | 1679 | Max Number of K-Sum Pairs | [Ruby](./algorithms/ruby/1679-max-number-of-k-sum-pairs.rb) | Medium | | 1680 | Concatenation of Consecutive Binary Numbers | [Ruby](./algorithms/ruby/1680-concatenation-of-consecutive-binary-numbers.rb) | Medium | diff --git a/algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb b/algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb new file mode 100644 index 0000000..feafc5b --- /dev/null +++ b/algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +# 1671. Minimum Number of Removals to Make Mountain Array +# Hard +# https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array + +=begin +You may recall that an array arr is a mountain array if and only if: +* arr.length >= 3 +* There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that: + * arr[0] < arr[1] < ... < arr[i - 1] < arr[i] + * arr[i] > arr[i + 1] > ... > arr[arr.length - 1] +Given an integer array nums​​​, return the minimum number of elements to remove to make nums​​​ a mountain array. + +Example 1: +Input: nums = [1,3,1] +Output: 0 +Explanation: The array itself is a mountain array so we do not need to remove any elements. + +Example 2: +Input: nums = [2,1,1,5,6,2,3,1] +Output: 3 +Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1]. + +Constraints: +* 3 <= nums.length <= 1000 +* 1 <= nums[i] <= 109 +* It is guaranteed that you can make a mountain array out of nums. +=end + +# @param {Integer[]} nums +# @return {Integer} +def minimum_mountain_removals(nums) + n = nums.size + inc = Array.new(n, 1) + dec = Array.new(n, 1) + + (1...n).each do |i| + (0...i).each do |j| + inc[i] = [inc[i], inc[j] + 1].max if nums[i] > nums[j] + end + end + + (n - 2).downto(0) do |i| + (i + 1...n).each do |j| + dec[i] = [dec[i], dec[j] + 1].max if nums[i] > nums[j] + end + end + + max_mountain = 0 + (1...n - 1).each do |i| + if inc[i] > 1 && dec[i] > 1 + max_mountain = [max_mountain, inc[i] + dec[i] - 1].max + end + end + + n - max_mountain +end + +# ********************# +# TEST # +# ********************# + +require "test/unit" +class Test_minimum_mountain_removals < Test::Unit::TestCase + def test_ + assert_equal 0, minimum_mountain_removals([1, 3, 1]) + assert_equal 3, minimum_mountain_removals([2, 1, 1, 5, 6, 2, 3, 1]) + end +end diff --git a/algorithms/ruby/2684-maximum-number-of-moves-in-a-grid.rb b/algorithms/ruby/2684-maximum-number-of-moves-in-a-grid.rb index d1c4a9e..0942fff 100644 --- a/algorithms/ruby/2684-maximum-number-of-moves-in-a-grid.rb +++ b/algorithms/ruby/2684-maximum-number-of-moves-in-a-grid.rb @@ -55,7 +55,7 @@ def max_moves(grid) require "test/unit" class Test_max_moves < Test::Unit::TestCase def test_ - assert_equal 3, max_moves([[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]) - assert_equal 0, max_moves([[3,2,4],[2,1,9],[1,1,7]]) + assert_equal 3, max_moves([[2, 4, 3, 5], [5, 4, 9, 3], [3, 4, 2, 11], [10, 9, 13, 15]]) + assert_equal 0, max_moves([[3, 2, 4], [2, 1, 9], [1, 1, 7]]) end end