Skip to content

Commit 5dacccb

Browse files
committedMay 7, 2023
1964. Find the Longest Valid Obstacle Course at Each Position
1 parent 15429c3 commit 5dacccb

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
 

Diff for: ‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
| 1768 | Merge Strings Alternately | [Ruby](./algorithms/ruby/1768-merge-strings-alternately.rb) | Easy |
183183
| 1822 | Sign of the Product of an Array | [Ruby](./algorithms/ruby/1822-sign-of-the-product-of-an-array.rb) | Easy |
184184
| 1857 | Largest Color Value in a Directed Graph | [Ruby](./algorithms/ruby/1857-largest-color-value-in-a-directed-graph.rb) | Hard |
185+
| 1964 | Find the Longest Valid Obstacle Course at Each Position | [Ruby](./algorithms/ruby/1964-find-the-longest-valid-obstacle-course-at-each-position.rb) | Hard |
185186
| 2131 | Longest Palindrome by Concatenating Two Letter Words | [Ruby](./algorithms/ruby/2131-longest-palindrome-by-concatenating-two-letter-words.rb) | Medium |
186187
| 2187 | Minimum Time to Complete Trips | [Ruby](./algorithms/ruby/2187-minimum-time-to-complete-trips.rb) [Python3](./algorithms/python3/2187-minimum-time-to-complete-trips.py) | Medium |
187188
| 2215 | Find the Difference of Two Arrays | [Ruby](./algorithms/ruby/2215-find-the-difference-of-two-arrays.rb) | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
# 1964. Find the Longest Valid Obstacle Course at Each Position
4+
# https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position
5+
# Hard
6+
7+
=begin
8+
You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle.
9+
10+
For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles such that:
11+
12+
You choose any number of obstacles between 0 and i inclusive.
13+
You must include the ith obstacle in the course.
14+
You must put the chosen obstacles in the same order as they appear in obstacles.
15+
Every obstacle (except the first) is taller than or the same height as the obstacle immediately before it.
16+
Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above.
17+
18+
Example 1:
19+
Input: obstacles = [1,2,3,2]
20+
Output: [1,2,3,3]
21+
Explanation: The longest valid obstacle course at each position is:
22+
- i = 0: [1], [1] has length 1.
23+
- i = 1: [1,2], [1,2] has length 2.
24+
- i = 2: [1,2,3], [1,2,3] has length 3.
25+
- i = 3: [1,2,3,2], [1,2,2] has length 3.
26+
27+
Example 2:
28+
Input: obstacles = [2,2,1]
29+
Output: [1,2,1]
30+
Explanation: The longest valid obstacle course at each position is:
31+
- i = 0: [2], [2] has length 1.
32+
- i = 1: [2,2], [2,2] has length 2.
33+
- i = 2: [2,2,1], [1] has length 1.
34+
35+
Example 3:
36+
Input: obstacles = [3,1,5,6,4,2]
37+
Output: [1,1,2,3,2,2]
38+
Explanation: The longest valid obstacle course at each position is:
39+
- i = 0: [3], [3] has length 1.
40+
- i = 1: [3,1], [1] has length 1.
41+
- i = 2: [3,1,5], [3,5] has length 2. [1,5] is also valid.
42+
- i = 3: [3,1,5,6], [3,5,6] has length 3. [1,5,6] is also valid.
43+
- i = 4: [3,1,5,6,4], [3,4] has length 2. [1,4] is also valid.
44+
- i = 5: [3,1,5,6,4,2], [1,2] has length 2.
45+
46+
Constraints:
47+
n == obstacles.length
48+
1 <= n <= 105
49+
1 <= obstacles[i] <= 107
50+
=end
51+
52+
# @param {Integer[]} obstacles
53+
# @return {Integer[]}
54+
def longest_obstacle_course_at_each_position(obstacles)
55+
obstacle_path = []
56+
ans = []
57+
for i in 0..obstacles.length - 1
58+
if obstacle_path.empty? || (obstacles[i] >= obstacle_path.last)
59+
obstacle_path << obstacles[i]
60+
ans[i] = obstacle_path.count
61+
else
62+
idx = obstacle_path.bsearch_index { |obstacle| obstacle > obstacles[i] }
63+
obstacle_path[idx] = obstacles[i]
64+
ans[i] = idx + 1
65+
end
66+
end
67+
68+
ans
69+
end
70+
71+
# **************** #
72+
# TEST #
73+
# **************** #
74+
75+
require "test/unit"
76+
class Test_longest_obstacle_course_at_each_position < Test::Unit::TestCase
77+
def test_
78+
assert_equal [1, 2, 3, 3], longest_obstacle_course_at_each_position([1, 2, 3, 2])
79+
assert_equal [1, 2, 1], longest_obstacle_course_at_each_position([2, 2, 1])
80+
assert_equal [1, 1, 2, 3, 2, 2], longest_obstacle_course_at_each_position([3, 1, 5, 6, 4, 2])
81+
end
82+
end

0 commit comments

Comments
 (0)
Please sign in to comment.