Skip to content

Commit 6879e2d

Browse files
authored
update difficulty (#191)
1 parent 0ba2a42 commit 6879e2d

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
| 1657 | Determine if Two Strings Are Close | [Ruby](./algorithms/ruby/1657-determine-if-two-strings-are-close.rb) | Medium |
415415
| 1658 | Minimum Operations to Reduce X to Zero | [Ruby](./algorithms/ruby/1658-minimum-operations-to-reduce-x-to-zero.rb) | Medium |
416416
| 1662 | Check If Two String Arrays are Equivalent | [Ruby](./algorithms/ruby/1662-check-if-two-string-arrays-are-equivalent.rb) | Easy |
417+
| 1671 | Minimum Number of Removals to Make Mountain Array | [Ruby](./algorithms/ruby/1671-minimum-number-of-removals-to-make-mountain-array.rb) | Hard |
417418
| 1675 | Minimize Deviation in Array | [Ruby](./algorithms/ruby/1675-minimize-deviation-in-array.rb) | Hard |
418419
| 1679 | Max Number of K-Sum Pairs | [Ruby](./algorithms/ruby/1679-max-number-of-k-sum-pairs.rb) | Medium |
419420
| 1680 | Concatenation of Consecutive Binary Numbers | [Ruby](./algorithms/ruby/1680-concatenation-of-consecutive-binary-numbers.rb) | Medium |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
# 1671. Minimum Number of Removals to Make Mountain Array
4+
# Hard
5+
# https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array
6+
7+
=begin
8+
You may recall that an array arr is a mountain array if and only if:
9+
* arr.length >= 3
10+
* There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
11+
* arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
12+
* arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
13+
Given an integer array nums​​​, return the minimum number of elements to remove to make nums​​​ a mountain array.
14+
15+
Example 1:
16+
Input: nums = [1,3,1]
17+
Output: 0
18+
Explanation: The array itself is a mountain array so we do not need to remove any elements.
19+
20+
Example 2:
21+
Input: nums = [2,1,1,5,6,2,3,1]
22+
Output: 3
23+
Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].
24+
25+
Constraints:
26+
* 3 <= nums.length <= 1000
27+
* 1 <= nums[i] <= 109
28+
* It is guaranteed that you can make a mountain array out of nums.
29+
=end
30+
31+
# @param {Integer[]} nums
32+
# @return {Integer}
33+
def minimum_mountain_removals(nums)
34+
n = nums.size
35+
inc = Array.new(n, 1)
36+
dec = Array.new(n, 1)
37+
38+
(1...n).each do |i|
39+
(0...i).each do |j|
40+
inc[i] = [inc[i], inc[j] + 1].max if nums[i] > nums[j]
41+
end
42+
end
43+
44+
(n - 2).downto(0) do |i|
45+
(i + 1...n).each do |j|
46+
dec[i] = [dec[i], dec[j] + 1].max if nums[i] > nums[j]
47+
end
48+
end
49+
50+
max_mountain = 0
51+
(1...n - 1).each do |i|
52+
if inc[i] > 1 && dec[i] > 1
53+
max_mountain = [max_mountain, inc[i] + dec[i] - 1].max
54+
end
55+
end
56+
57+
n - max_mountain
58+
end
59+
60+
# ********************#
61+
# TEST #
62+
# ********************#
63+
64+
require "test/unit"
65+
class Test_minimum_mountain_removals < Test::Unit::TestCase
66+
def test_
67+
assert_equal 0, minimum_mountain_removals([1, 3, 1])
68+
assert_equal 3, minimum_mountain_removals([2, 1, 1, 5, 6, 2, 3, 1])
69+
end
70+
end

algorithms/ruby/2684-maximum-number-of-moves-in-a-grid.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def max_moves(grid)
5555
require "test/unit"
5656
class Test_max_moves < Test::Unit::TestCase
5757
def test_
58-
assert_equal 3, max_moves([[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]])
59-
assert_equal 0, max_moves([[3,2,4],[2,1,9],[1,1,7]])
58+
assert_equal 3, max_moves([[2, 4, 3, 5], [5, 4, 9, 3], [3, 4, 2, 11], [10, 9, 13, 15]])
59+
assert_equal 0, max_moves([[3, 2, 4], [2, 1, 9], [1, 1, 7]])
6060
end
6161
end

0 commit comments

Comments
 (0)