Skip to content

Commit 1d3a72b

Browse files
committed
Added tasks 1-7
1 parent cba50fe commit 1d3a72b

File tree

9 files changed

+1784
-2
lines changed

9 files changed

+1784
-2
lines changed

README.md

+1,224-2
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 1\. Two Sum
5+
6+
Easy
7+
8+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9+
10+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
11+
12+
You can return the answer in any order.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,7,11,15], target = 9
17+
18+
**Output:** [0,1]
19+
20+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,2,4], target = 6
25+
26+
**Output:** [1,2]
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [3,3], target = 6
31+
32+
**Output:** [0,1]
33+
34+
**Constraints:**
35+
36+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
39+
* **Only one valid answer exists.**
40+
41+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
42+
43+
## Solution
44+
45+
```erlang
46+
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
47+
% #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
48+
% #AI_can_be_used_to_solve_the_task #2025_01_05_Time_3_(97.50%)_Space_65.32_(7.50%)
49+
50+
-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].
51+
two_sum(Nums, Target) ->
52+
two_sum(Nums, Target, #{}, 0).
53+
54+
two_sum([], _, _, _) -> undefined;
55+
two_sum([H|T], Target, M, Index) ->
56+
case M of
57+
#{ Target-H := Pair } -> [Pair, Index];
58+
_ -> two_sum(T, Target, M#{ H => Index }, Index + 1)
59+
end.
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
## 2\. Add Two Numbers
5+
6+
Medium
7+
8+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
9+
10+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
15+
16+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
17+
18+
**Output:** [7,0,8]
19+
20+
**Explanation:** 342 + 465 = 807.
21+
22+
**Example 2:**
23+
24+
**Input:** l1 = [0], l2 = [0]
25+
26+
**Output:** [0]
27+
28+
**Example 3:**
29+
30+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
31+
32+
**Output:** [8,9,9,9,0,0,0,1]
33+
34+
**Constraints:**
35+
36+
* The number of nodes in each linked list is in the range `[1, 100]`.
37+
* `0 <= Node.val <= 9`
38+
* It is guaranteed that the list represents a number that does not have leading zeros.
39+
40+
## Solution
41+
42+
```erlang
43+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
44+
% #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
45+
% #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
46+
% #2025_01_05_Time_1_(77.78%)_Space_63.11_(100.00%)
47+
48+
%% Definition for singly-linked list.
49+
%%
50+
%% -record(list_node, {val = 0 :: integer(),
51+
%% next = null :: 'null' | #list_node{}}).
52+
53+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null) -> #list_node{} | null.
54+
add_two_numbers(L1, L2) ->
55+
{Result, _} = add_two_numbers(L1, L2, 0),
56+
Result.
57+
58+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null, Carry :: integer()) -> {#list_node{} | null, integer()}.
59+
add_two_numbers(null, null, 0) ->
60+
{null, 0};
61+
add_two_numbers(L1, L2, Carry) ->
62+
X = if L1 =/= null -> L1#list_node.val; true -> 0 end,
63+
Y = if L2 =/= null -> L2#list_node.val; true -> 0 end,
64+
Sum = Carry + X + Y,
65+
NewCarry = Sum div 10,
66+
NewNode = #list_node{val = Sum rem 10},
67+
{Next1, _} = if L1 =/= null -> {L1#list_node.next, 0}; true -> {null, 0} end,
68+
{Next2, _} = if L2 =/= null -> {L2#list_node.next, 0}; true -> {null, 0} end,
69+
{NextResult, _} = add_two_numbers(Next1, Next2, NewCarry),
70+
{NewNode#list_node{next = NextResult}, NewCarry}.
71+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
## 3\. Longest Substring Without Repeating Characters
5+
6+
Medium
7+
8+
Given a string `s`, find the length of the **longest substring** without repeating characters.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "abcabcbb"
13+
14+
**Output:** 3
15+
16+
**Explanation:** The answer is "abc", with the length of 3.
17+
18+
**Example 2:**
19+
20+
**Input:** s = "bbbbb"
21+
22+
**Output:** 1
23+
24+
**Explanation:** The answer is "b", with the length of 1.
25+
26+
**Example 3:**
27+
28+
**Input:** s = "pwwkew"
29+
30+
**Output:** 3
31+
32+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
33+
34+
**Example 4:**
35+
36+
**Input:** s = ""
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
43+
* `s` consists of English letters, digits, symbols and spaces.
44+
45+
## Solution
46+
47+
```erlang
48+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
49+
% #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
50+
% #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
51+
% #2025_01_08_Time_11_(100.00%)_Space_61.60_(60.00%)
52+
53+
-spec length_of_longest_substring(S :: unicode:unicode_binary()) -> integer().
54+
length_of_longest_substring(S) ->
55+
do(S, 0, #{}, 0, 1).
56+
57+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc0) when is_map_key(Char, PrevPos) ->
58+
PrevIndex = map_get(Char, PrevPos),
59+
Acc1 = Index - PrevIndex,
60+
Acc = min(Acc1, Acc0),
61+
do(Rest, Index + 1, PrevPos#{Char => Index}, max(Max, Acc), Acc + 1);
62+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) when Acc > Max ->
63+
do(Rest, Index + 1, PrevPos#{Char => Index}, Acc, Acc + 1);
64+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) ->
65+
do(Rest, Index + 1, PrevPos#{Char => Index}, Max, Acc + 1);
66+
67+
do(<<>>, _, _, Max, _) -> Max.
68+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
## 4\. Median of Two Sorted Arrays
5+
6+
Hard
7+
8+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
9+
10+
The overall run time complexity should be `O(log (m+n))`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums1 = [1,3], nums2 = [2]
15+
16+
**Output:** 2.00000
17+
18+
**Explanation:** merged array = [1,2,3] and median is 2.
19+
20+
**Example 2:**
21+
22+
**Input:** nums1 = [1,2], nums2 = [3,4]
23+
24+
**Output:** 2.50000
25+
26+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
27+
28+
**Example 3:**
29+
30+
**Input:** nums1 = [0,0], nums2 = [0,0]
31+
32+
**Output:** 0.00000
33+
34+
**Example 4:**
35+
36+
**Input:** nums1 = [], nums2 = [1]
37+
38+
**Output:** 1.00000
39+
40+
**Example 5:**
41+
42+
**Input:** nums1 = [2], nums2 = []
43+
44+
**Output:** 2.00000
45+
46+
**Constraints:**
47+
48+
* `nums1.length == m`
49+
* `nums2.length == n`
50+
* `0 <= m <= 1000`
51+
* `0 <= n <= 1000`
52+
* `1 <= m + n <= 2000`
53+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
54+
55+
## Solution
56+
57+
```erlang
58+
% #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
59+
% #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
60+
% #2025_01_08_Time_1_(100.00%)_Space_65.96_(100.00%)
61+
62+
-spec find_median_sorted_arrays(Nums1 :: [integer()], Nums2 :: [integer()]) -> float().
63+
find_median_sorted_arrays(Nums1, Nums2) ->
64+
Len = length(Nums1) + length(Nums2),
65+
find_median_sorted_arrays(Nums1, Nums2, [], -1, 0, 0, Len rem 2, Len div 2).
66+
find_median_sorted_arrays(_, _, [H|T], Index, _, _, Even, Index) ->
67+
if Even =:= 1 -> H;
68+
true -> (H + lists:nth(1, T)) / 2
69+
end;
70+
find_median_sorted_arrays([], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) ->
71+
find_median_sorted_arrays([], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
72+
find_median_sorted_arrays([H1|T1], [], MyArr, Index, Indx1, Indx2, Even, Middle) ->
73+
find_median_sorted_arrays(T1, [], [H1|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
74+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) when H1 < H2 ->
75+
find_median_sorted_arrays(T1, [H2|T2], [H1|MyArr], Index + 1, Indx1 + 1, Indx2, Even, Middle);
76+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle)->
77+
find_median_sorted_arrays([H1|T1], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle).
78+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
## 5\. Longest Palindromic Substring
5+
6+
Medium
7+
8+
Given a string `s`, return _the longest palindromic substring_ in `s`.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "babad"
13+
14+
**Output:** "bab" **Note:** "aba" is also a valid answer.
15+
16+
**Example 2:**
17+
18+
**Input:** s = "cbbd"
19+
20+
**Output:** "bb"
21+
22+
**Example 3:**
23+
24+
**Input:** s = "a"
25+
26+
**Output:** "a"
27+
28+
**Example 4:**
29+
30+
**Input:** s = "ac"
31+
32+
**Output:** "a"
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 1000`
37+
* `s` consist of only digits and English letters.
38+
39+
## Solution
40+
41+
```erlang
42+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
43+
% #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
44+
% #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
45+
% #2025_01_08_Time_179_(100.00%)_Space_59.84_(100.00%)
46+
47+
-spec longest_palindrome(S :: unicode:unicode_binary()) -> unicode:unicode_binary().
48+
longest_palindrome(S) ->
49+
Length = byte_size(S),
50+
case Length of
51+
0 -> <<>>; % Return empty binary if input is empty
52+
_ ->
53+
% Initialize variables for the best palindrome
54+
{Start, Len} = lists:foldl(
55+
fun(Index, {BestStart, BestLen}) ->
56+
% Find the longest palindrome for odd and even length centers
57+
{NewStart1, NewLen1} = find_longest_palindrome(S, Index, Index),
58+
{NewStart2, NewLen2} = find_longest_palindrome(S, Index, Index + 1),
59+
% Choose the longer of the two found palindromes
60+
case NewLen1 >= NewLen2 of
61+
true -> update_best(BestStart, BestLen, NewStart1, NewLen1);
62+
false -> update_best(BestStart, BestLen, NewStart2, NewLen2)
63+
end
64+
end,
65+
{0, 0}, % Initial best palindrome start and length
66+
lists:seq(0, Length - 1) % Iterate through all positions
67+
),
68+
% Extract the longest palindromic substring
69+
binary:part(S, Start, Len)
70+
end.
71+
72+
% Helper function to find the longest palindrome around the center
73+
-spec find_longest_palindrome(S :: unicode:unicode_binary(), L :: integer(), R :: integer()) -> {integer(), integer()}.
74+
find_longest_palindrome(S, L, R) ->
75+
Length = byte_size(S),
76+
case (L >= 0 andalso R < Length andalso binary:part(S, L, 1) =:= binary:part(S, R, 1)) of
77+
true ->
78+
find_longest_palindrome(S, L - 1, R + 1);
79+
false ->
80+
{L + 1, R - L - 1}
81+
end.
82+
83+
% Helper function to update the best palindrome found so far
84+
-spec update_best(BestStart :: integer(), BestLen :: integer(), NewStart :: integer(), NewLen :: integer()) -> {integer(), integer()}.
85+
update_best(BestStart, BestLen, NewStart, NewLen) ->
86+
case NewLen > BestLen of
87+
true -> {NewStart, NewLen};
88+
false -> {BestStart, BestLen}
89+
end.
90+
```

0 commit comments

Comments
 (0)