Skip to content

Commit bfbd367

Browse files
committed
Added tasks 17-98
1 parent 4bede53 commit bfbd367

File tree

26 files changed

+1670
-0
lines changed

26 files changed

+1670
-0
lines changed

README.md

+83
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 17\. Letter Combinations of a Phone Number
5+
6+
Medium
7+
8+
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**.
9+
10+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
11+
12+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
13+
14+
**Example 1:**
15+
16+
**Input:** digits = "23"
17+
18+
**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"]
19+
20+
**Example 2:**
21+
22+
**Input:** digits = ""
23+
24+
**Output:** []
25+
26+
**Example 3:**
27+
28+
**Input:** digits = "2"
29+
30+
**Output:** ["a","b","c"]
31+
32+
**Constraints:**
33+
34+
* `0 <= digits.length <= 4`
35+
* `digits[i]` is a digit in the range `['2', '9']`.
36+
37+
## Solution
38+
39+
```erlang
40+
-spec letter_combinations(Digits :: unicode:unicode_binary()) -> [unicode:unicode_binary()].
41+
letter_combinations(Digits) ->
42+
Res = lists:foldl(fun(Digit, Acc) ->
43+
case Digit of
44+
<<"2">> ->
45+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"abc">>], Accbin <- Acc];
46+
<<"3">> ->
47+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"def">>], Accbin <- Acc];
48+
<<"4">> ->
49+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"ghi">>], Accbin <- Acc];
50+
<<"5">> ->
51+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"jkl">>], Accbin <- Acc];
52+
<<"6">> ->
53+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"mno">>], Accbin <- Acc];
54+
<<"7">> ->
55+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"pqrs">>], Accbin <- Acc];
56+
<<"8">> ->
57+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"tuv">>], Accbin <- Acc];
58+
<<"9">> ->
59+
[<<Accbin/binary, Byte/binary>> || Byte <- [<<A>> || <<A:8>> <= <<"wxyz">>], Accbin <- Acc]
60+
end
61+
end, [<<>>], [<<B>> || <<B:8>> <= Digits]),
62+
case Res of
63+
[<<>>] -> [];
64+
_ -> Res
65+
end.
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/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+
```erlang
40+
-spec longest_valid_parentheses(S :: unicode:unicode_binary()) -> integer().
41+
longest_valid_parentheses(S) ->
42+
Max1 = scan(S, 0, 0, 0, 1),
43+
Max2 = scan(reverse_binary(S), 0, 0, 0, -1),
44+
max(Max1, Max2).
45+
46+
-spec scan(S :: unicode:unicode_binary(), Left :: integer(), Right :: integer(), Max :: integer(), Direction :: integer()) -> integer().
47+
scan(<<>>, _, _, Max, _) ->
48+
Max;
49+
scan(<<Ch, Rest/binary>>, Left, Right, Max, Direction) ->
50+
{NewLeft, NewRight} =
51+
case Ch of
52+
$( -> {Left + 1, Right};
53+
$) -> {Left, Right + 1};
54+
_ -> {Left, Right}
55+
end,
56+
NewMax =
57+
case NewLeft == NewRight of
58+
true -> max(Max, NewLeft + NewRight);
59+
false -> Max
60+
end,
61+
{ResetLeft, ResetRight} =
62+
case Direction of
63+
1 when NewRight > NewLeft -> {0, 0}; % Reset if right > left in forward scan
64+
-1 when NewLeft > NewRight -> {0, 0}; % Reset if left > right in backward scan
65+
_ -> {NewLeft, NewRight}
66+
end,
67+
scan(Rest, ResetLeft, ResetRight, NewMax, Direction).
68+
69+
% Helper to reverse a binary string manually.
70+
-spec reverse_binary(S :: binary()) -> binary().
71+
reverse_binary(S) ->
72+
reverse_binary(S, <<>>).
73+
74+
-spec reverse_binary(S :: binary(), Acc :: binary()) -> binary().
75+
reverse_binary(<<>>, Acc) ->
76+
Acc;
77+
reverse_binary(<<Ch, Rest/binary>>, Acc) ->
78+
reverse_binary(Rest, <<Ch, Acc/binary>>).
79+
80+
-spec max(A :: integer(), B :: integer()) -> integer().
81+
max(A, B) when A >= B -> A;
82+
max(_, B) -> B.
83+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/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+
```erlang
45+
-spec search(Nums :: [integer()], Target :: integer()) -> integer().
46+
47+
search(Nums, Target) ->
48+
search(Nums, Target, 0, length(Nums) - 1).
49+
50+
search(_, _, L, R) when L > R -> -1;
51+
search(Nums, Target, L, R) ->
52+
Mid = (L + R) div 2,
53+
Val = lists:nth(Mid + 1, Nums),
54+
Start = lists:nth(L + 1, Nums),
55+
case Val of
56+
Target -> Mid;
57+
_ ->
58+
if
59+
(Val >= Start andalso Target >= Start andalso Target < Val)
60+
orelse (Val < Start andalso (Target >= Start orelse Target < Val)) ->
61+
search(Nums, Target, L, Mid - 1);
62+
true ->
63+
search(Nums, Target, Mid + 1, R)
64+
end
65+
end.
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/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+
```erlang
42+
-spec search_range(Nums :: [integer()], Target :: integer()) -> [integer()].
43+
search_range(Nums, Target) ->
44+
[helper(Nums, Target, false), helper(Nums, Target, true)].
45+
46+
-spec helper(Nums :: [integer()], Target :: integer(), Equals :: boolean()) -> integer().
47+
helper(Nums, Target, Equals) ->
48+
helper(Nums, Target, Equals, 0, length(Nums) - 1, -1).
49+
50+
-spec helper(Nums :: [integer()], Target :: integer(), Equals :: boolean(), L :: integer(), R :: integer(), Result :: integer()) -> integer().
51+
helper(_, _, _, L, R, Result) when L > R ->
52+
Result;
53+
helper(Nums, Target, Equals, L, R, Result) ->
54+
Mid = L + (R - L) div 2,
55+
MidValue = lists:nth(Mid + 1, Nums), % Erlang lists are 1-based
56+
NewResult =
57+
case MidValue == Target of
58+
true -> Mid;
59+
false -> Result
60+
end,
61+
case MidValue < Target orelse (MidValue == Target andalso Equals) of
62+
true -> helper(Nums, Target, Equals, Mid + 1, R, NewResult);
63+
false -> helper(Nums, Target, Equals, L, Mid - 1, NewResult)
64+
end.
65+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 35\. Search Insert Position
5+
6+
Easy
7+
8+
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
9+
10+
You must write an algorithm with `O(log n)` runtime complexity.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,3,5,6], target = 5
15+
16+
**Output:** 2
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [1,3,5,6], target = 2
21+
22+
**Output:** 1
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [1,3,5,6], target = 7
27+
28+
**Output:** 4
29+
30+
**Example 4:**
31+
32+
**Input:** nums = [1,3,5,6], target = 0
33+
34+
**Output:** 0
35+
36+
**Example 5:**
37+
38+
**Input:** nums = [1], target = 0
39+
40+
**Output:** 0
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
45+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
46+
* `nums` contains **distinct** values sorted in **ascending** order.
47+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
48+
49+
## Solution
50+
51+
```erlang
52+
-spec search_insert(Nums :: [integer()], Target :: integer()) -> integer().
53+
search_insert(Nums, Target) ->
54+
search_insert(Nums, Target, 1, length(Nums)) - 1.
55+
56+
search_insert(Nums, Target, Low, High) when Low > High ->
57+
Low;
58+
59+
search_insert(Nums, Target, Low, High) ->
60+
Mid = Low + (High - Low) div 2,
61+
Mid_value = lists:nth(Mid, Nums),
62+
Result = if
63+
Mid_value == Target -> Mid;
64+
Mid_value > Target -> search_insert(Nums, Target, Low, Mid - 1);
65+
true -> search_insert(Nums, Target, Mid + 1, High)
66+
end.
67+
```

0 commit comments

Comments
 (0)