Skip to content

Commit 77087d3

Browse files
authored
Added tasks 31-136
1 parent 71d9cff commit 77087d3

File tree

42 files changed

+2813
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2813
-2
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Large diffs are not rendered by default.

src/main/ruby/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ Given a string `s`, find the length of the **longest substring** without repeati
4545
## Solution
4646

4747
```ruby
48-
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
49-
5048
# @param {String} s
5149
# @return {Integer}
5250
def length_of_longest_substring(s)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 31\. Next Permutation
5+
6+
Medium
7+
8+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
9+
10+
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
11+
12+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3]
17+
18+
**Output:** [1,3,2]
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,2,1]
23+
24+
**Output:** [1,2,3]
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [1,1,5]
29+
30+
**Output:** [1,5,1]
31+
32+
**Example 4:**
33+
34+
**Input:** nums = [1]
35+
36+
**Output:** [1]
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* `0 <= nums[i] <= 100`
42+
43+
## Solution
44+
45+
```ruby
46+
# @param {Integer[]} nums
47+
# @return {Void} Do not return anything, modify nums in-place instead.
48+
def next_permutation(nums)
49+
return if nums.nil? || nums.length <= 1
50+
51+
i = nums.length - 2
52+
while i >= 0 && nums[i] >= nums[i + 1]
53+
i -= 1
54+
end
55+
56+
if i >= 0
57+
j = nums.length - 1
58+
while nums[j] <= nums[i]
59+
j -= 1
60+
end
61+
swap_next(nums, i, j)
62+
end
63+
64+
reverse_next(nums, i + 1, nums.length - 1)
65+
end
66+
67+
private
68+
69+
def swap_next(nums, i, j)
70+
temp = nums[i]
71+
nums[i] = nums[j]
72+
nums[j] = temp
73+
end
74+
75+
def reverse_next(nums, i, j)
76+
while i < j
77+
swap_next(nums, i, j)
78+
i += 1
79+
j -= 1
80+
end
81+
end
82+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 32\. Longest Valid Parentheses
5+
6+
Hard
7+
8+
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "(()"
13+
14+
**Output:** 2
15+
16+
**Explanation:** The longest valid parentheses substring is "()".
17+
18+
**Example 2:**
19+
20+
**Input:** s = ")()())"
21+
22+
**Output:** 4
23+
24+
**Explanation:** The longest valid parentheses substring is "()()".
25+
26+
**Example 3:**
27+
28+
**Input:** s = ""
29+
30+
**Output:** 0
31+
32+
**Constraints:**
33+
34+
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
35+
* `s[i]` is `'('`, or `')'`.
36+
37+
## Solution
38+
39+
```ruby
40+
# @param {String} s
41+
# @return {Integer}
42+
def longest_valid_parentheses(s)
43+
max = 0
44+
left = 0
45+
right = 0
46+
n = s.length
47+
ch = nil
48+
49+
(0...n).each do |i|
50+
ch = s[i]
51+
if ch == '('
52+
left += 1
53+
else
54+
right += 1
55+
end
56+
57+
if right > left
58+
left = 0
59+
right = 0
60+
end
61+
62+
if left == right
63+
max = [max, left + right].max
64+
end
65+
end
66+
67+
left = 0
68+
right = 0
69+
70+
(n - 1).downto(0) do |i|
71+
ch = s[i]
72+
if ch == '('
73+
left += 1
74+
else
75+
right += 1
76+
end
77+
78+
if left > right
79+
left = 0
80+
right = 0
81+
end
82+
83+
if left == right
84+
max = [max, left + right].max
85+
end
86+
end
87+
88+
max
89+
end
90+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 33\. Search in Rotated Sorted Array
5+
6+
Medium
7+
8+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
9+
10+
Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`.
11+
12+
Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`.
13+
14+
You must write an algorithm with `O(log n)` runtime complexity.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,5,6,7,0,1,2], target = 0
19+
20+
**Output:** 4
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [4,5,6,7,0,1,2], target = 3
25+
26+
**Output:** -1
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [1], target = 0
31+
32+
**Output:** -1
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 5000`
37+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
38+
* All values of `nums` are **unique**.
39+
* `nums` is an ascending array that is possibly rotated.
40+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
41+
42+
## Solution
43+
44+
```ruby
45+
# @param {Integer[]} nums
46+
# @param {Integer} target
47+
# @return {Integer}
48+
def search(nums, target)
49+
lo = 0
50+
hi = nums.length - 1
51+
52+
while lo <= hi
53+
mid = ((hi - lo) >> 1) + lo
54+
55+
return mid if target == nums[mid]
56+
57+
if nums[lo] <= nums[mid]
58+
if nums[lo] <= target && target <= nums[mid]
59+
hi = mid - 1
60+
else
61+
lo = mid + 1
62+
end
63+
elsif nums[mid] <= target && target <= nums[hi]
64+
lo = mid + 1
65+
else
66+
hi = mid - 1
67+
end
68+
end
69+
70+
-1
71+
end
72+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 34\. Find First and Last Position of Element in Sorted Array
5+
6+
Medium
7+
8+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value.
9+
10+
If `target` is not found in the array, return `[-1, -1]`.
11+
12+
You must write an algorithm with `O(log n)` runtime complexity.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [5,7,7,8,8,10], target = 8
17+
18+
**Output:** [3,4]
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [5,7,7,8,8,10], target = 6
23+
24+
**Output:** [-1,-1]
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [], target = 0
29+
30+
**Output:** [-1,-1]
31+
32+
**Constraints:**
33+
34+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
35+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
36+
* `nums` is a non-decreasing array.
37+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
38+
39+
## Solution
40+
41+
```ruby
42+
# @param {Integer[]} nums
43+
# @param {Integer} target
44+
# @return {Integer[]}
45+
def search_range(nums, target)
46+
ans = [helper(nums, target, false), helper(nums, target, true)]
47+
ans
48+
end
49+
50+
private
51+
52+
def helper(nums, target, equals)
53+
l = 0
54+
r = nums.length - 1
55+
result = -1
56+
57+
while l <= r
58+
mid = l + (r - l) / 2
59+
result = mid if nums[mid] == target
60+
if nums[mid] < target || (nums[mid] == target && equals)
61+
l = mid + 1
62+
else
63+
r = mid - 1
64+
end
65+
end
66+
67+
result
68+
end
69+
```

0 commit comments

Comments
 (0)