Skip to content

Commit b71615c

Browse files
committed
Added erlang
1 parent 5dea16b commit b71615c

File tree

70 files changed

+2491
-307
lines changed

Some content is hidden

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

70 files changed

+2491
-307
lines changed

README.md

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

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<source>src/main/dart</source>
9696
<source>src/main/c</source>
9797
<source>src/main/js</source>
98+
<source>src/main/erlang</source>
9899
</sources>
99100
</configuration>
100101
</execution>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
% #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
% #AI_can_be_used_to_solve_the_task #2025_01_05_Time_3_(97.50%)_Space_65.32_(7.50%)
4+
5+
-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].
6+
two_sum(Nums, Target) ->
7+
two_sum(Nums, Target, #{}, 0).
8+
9+
two_sum([], _, _, _) -> undefined;
10+
two_sum([H|T], Target, M, Index) ->
11+
case M of
12+
#{ Target-H := Pair } -> [Pair, Index];
13+
_ -> two_sum(T, Target, M#{ H => Index }, Index + 1)
14+
end.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
% #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
% #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
4+
% #2025_01_05_Time_1_(77.78%)_Space_63.11_(100.00%)
5+
6+
%% Definition for singly-linked list.
7+
%%
8+
%% -record(list_node, {val = 0 :: integer(),
9+
%% next = null :: 'null' | #list_node{}}).
10+
11+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null) -> #list_node{} | null.
12+
add_two_numbers(L1, L2) ->
13+
{Result, _} = add_two_numbers(L1, L2, 0),
14+
Result.
15+
16+
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null, Carry :: integer()) -> {#list_node{} | null, integer()}.
17+
add_two_numbers(null, null, 0) ->
18+
{null, 0};
19+
add_two_numbers(L1, L2, Carry) ->
20+
X = if L1 =/= null -> L1#list_node.val; true -> 0 end,
21+
Y = if L2 =/= null -> L2#list_node.val; true -> 0 end,
22+
Sum = Carry + X + Y,
23+
NewCarry = Sum div 10,
24+
NewNode = #list_node{val = Sum rem 10},
25+
{Next1, _} = if L1 =/= null -> {L1#list_node.next, 0}; true -> {null, 0} end,
26+
{Next2, _} = if L2 =/= null -> {L2#list_node.next, 0}; true -> {null, 0} end,
27+
{NextResult, _} = add_two_numbers(Next1, Next2, NewCarry),
28+
{NewNode#list_node{next = NextResult}, NewCarry}.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
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.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
% #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
% #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
4+
% #2025_01_08_Time_11_(100.00%)_Space_61.60_(60.00%)
5+
6+
-spec length_of_longest_substring(S :: unicode:unicode_binary()) -> integer().
7+
length_of_longest_substring(S) ->
8+
do(S, 0, #{}, 0, 1).
9+
10+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc0) when is_map_key(Char, PrevPos) ->
11+
PrevIndex = map_get(Char, PrevPos),
12+
Acc1 = Index - PrevIndex,
13+
Acc = min(Acc1, Acc0),
14+
do(Rest, Index + 1, PrevPos#{Char => Index}, max(Max, Acc), Acc + 1);
15+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) when Acc > Max ->
16+
do(Rest, Index + 1, PrevPos#{Char => Index}, Acc, Acc + 1);
17+
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) ->
18+
do(Rest, Index + 1, PrevPos#{Char => Index}, Max, Acc + 1);
19+
20+
do(<<>>, _, _, Max, _) -> Max.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**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.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
% #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
3+
% #2025_01_08_Time_1_(100.00%)_Space_65.96_(100.00%)
4+
5+
-spec find_median_sorted_arrays(Nums1 :: [integer()], Nums2 :: [integer()]) -> float().
6+
find_median_sorted_arrays(Nums1, Nums2) ->
7+
Len = length(Nums1) + length(Nums2),
8+
find_median_sorted_arrays(Nums1, Nums2, [], -1, 0, 0, Len rem 2, Len div 2).
9+
find_median_sorted_arrays(_, _, [H|T], Index, _, _, Even, Index) ->
10+
if Even =:= 1 -> H;
11+
true -> (H + lists:nth(1, T)) / 2
12+
end;
13+
find_median_sorted_arrays([], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) ->
14+
find_median_sorted_arrays([], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
15+
find_median_sorted_arrays([H1|T1], [], MyArr, Index, Indx1, Indx2, Even, Middle) ->
16+
find_median_sorted_arrays(T1, [], [H1|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle);
17+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) when H1 < H2 ->
18+
find_median_sorted_arrays(T1, [H2|T2], [H1|MyArr], Index + 1, Indx1 + 1, Indx2, Even, Middle);
19+
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle)->
20+
find_median_sorted_arrays([H1|T1], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)