-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
70 changed files
with
2,491 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table | ||
% #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n) | ||
% #AI_can_be_used_to_solve_the_task #2025_01_05_Time_3_(97.50%)_Space_65.32_(7.50%) | ||
|
||
-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()]. | ||
two_sum(Nums, Target) -> | ||
two_sum(Nums, Target, #{}, 0). | ||
|
||
two_sum([], _, _, _) -> undefined; | ||
two_sum([H|T], Target, M, Index) -> | ||
case M of | ||
#{ Target-H := Pair } -> [Pair, Index]; | ||
_ -> two_sum(T, Target, M#{ H => Index }, Index + 1) | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
1\. Two Sum | ||
|
||
Easy | ||
|
||
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_. | ||
|
||
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice. | ||
|
||
You can return the answer in any order. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [2,7,11,15], target = 9 | ||
|
||
**Output:** [0,1] | ||
|
||
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [3,2,4], target = 6 | ||
|
||
**Output:** [1,2] | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [3,3], target = 6 | ||
|
||
**Output:** [0,1] | ||
|
||
**Constraints:** | ||
|
||
* <code>2 <= nums.length <= 10<sup>4</sup></code> | ||
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code> | ||
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code> | ||
* **Only one valid answer exists.** | ||
|
||
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity? |
28 changes: 28 additions & 0 deletions
28
src/main/erlang/g0001_0100/s0002_add_two_numbers/Solution.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion | ||
% #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15 | ||
% #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task | ||
% #2025_01_05_Time_1_(77.78%)_Space_63.11_(100.00%) | ||
|
||
%% Definition for singly-linked list. | ||
%% | ||
%% -record(list_node, {val = 0 :: integer(), | ||
%% next = null :: 'null' | #list_node{}}). | ||
|
||
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null) -> #list_node{} | null. | ||
add_two_numbers(L1, L2) -> | ||
{Result, _} = add_two_numbers(L1, L2, 0), | ||
Result. | ||
|
||
-spec add_two_numbers(L1 :: #list_node{} | null, L2 :: #list_node{} | null, Carry :: integer()) -> {#list_node{} | null, integer()}. | ||
add_two_numbers(null, null, 0) -> | ||
{null, 0}; | ||
add_two_numbers(L1, L2, Carry) -> | ||
X = if L1 =/= null -> L1#list_node.val; true -> 0 end, | ||
Y = if L2 =/= null -> L2#list_node.val; true -> 0 end, | ||
Sum = Carry + X + Y, | ||
NewCarry = Sum div 10, | ||
NewNode = #list_node{val = Sum rem 10}, | ||
{Next1, _} = if L1 =/= null -> {L1#list_node.next, 0}; true -> {null, 0} end, | ||
{Next2, _} = if L2 =/= null -> {L2#list_node.next, 0}; true -> {null, 0} end, | ||
{NextResult, _} = add_two_numbers(Next1, Next2, NewCarry), | ||
{NewNode#list_node{next = NextResult}, NewCarry}. |
35 changes: 35 additions & 0 deletions
35
src/main/erlang/g0001_0100/s0002_add_two_numbers/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
2\. Add Two Numbers | ||
|
||
Medium | ||
|
||
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. | ||
|
||
You may assume the two numbers do not contain any leading zero, except the number 0 itself. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg) | ||
|
||
**Input:** l1 = [2,4,3], l2 = [5,6,4] | ||
|
||
**Output:** [7,0,8] | ||
|
||
**Explanation:** 342 + 465 = 807. | ||
|
||
**Example 2:** | ||
|
||
**Input:** l1 = [0], l2 = [0] | ||
|
||
**Output:** [0] | ||
|
||
**Example 3:** | ||
|
||
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] | ||
|
||
**Output:** [8,9,9,9,0,0,0,1] | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in each linked list is in the range `[1, 100]`. | ||
* `0 <= Node.val <= 9` | ||
* It is guaranteed that the list represents a number that does not have leading zeros. |
20 changes: 20 additions & 0 deletions
20
src/main/erlang/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window | ||
% #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings | ||
% #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task | ||
% #2025_01_08_Time_11_(100.00%)_Space_61.60_(60.00%) | ||
|
||
-spec length_of_longest_substring(S :: unicode:unicode_binary()) -> integer(). | ||
length_of_longest_substring(S) -> | ||
do(S, 0, #{}, 0, 1). | ||
|
||
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc0) when is_map_key(Char, PrevPos) -> | ||
PrevIndex = map_get(Char, PrevPos), | ||
Acc1 = Index - PrevIndex, | ||
Acc = min(Acc1, Acc0), | ||
do(Rest, Index + 1, PrevPos#{Char => Index}, max(Max, Acc), Acc + 1); | ||
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) when Acc > Max -> | ||
do(Rest, Index + 1, PrevPos#{Char => Index}, Acc, Acc + 1); | ||
do(<<Char, Rest/binary>>, Index, PrevPos, Max, Acc) -> | ||
do(Rest, Index + 1, PrevPos#{Char => Index}, Max, Acc + 1); | ||
|
||
do(<<>>, _, _, Max, _) -> Max. |
40 changes: 40 additions & 0 deletions
40
...rlang/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
3\. Longest Substring Without Repeating Characters | ||
|
||
Medium | ||
|
||
Given a string `s`, find the length of the **longest substring** without repeating characters. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "abcabcbb" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The answer is "abc", with the length of 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "bbbbb" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The answer is "b", with the length of 1. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "pwwkew" | ||
|
||
**Output:** 3 | ||
|
||
**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. | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "" | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code> | ||
* `s` consists of English letters, digits, symbols and spaces. |
20 changes: 20 additions & 0 deletions
20
src/main/erlang/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
% #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer | ||
% #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task | ||
% #2025_01_08_Time_1_(100.00%)_Space_65.96_(100.00%) | ||
|
||
-spec find_median_sorted_arrays(Nums1 :: [integer()], Nums2 :: [integer()]) -> float(). | ||
find_median_sorted_arrays(Nums1, Nums2) -> | ||
Len = length(Nums1) + length(Nums2), | ||
find_median_sorted_arrays(Nums1, Nums2, [], -1, 0, 0, Len rem 2, Len div 2). | ||
find_median_sorted_arrays(_, _, [H|T], Index, _, _, Even, Index) -> | ||
if Even =:= 1 -> H; | ||
true -> (H + lists:nth(1, T)) / 2 | ||
end; | ||
find_median_sorted_arrays([], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) -> | ||
find_median_sorted_arrays([], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle); | ||
find_median_sorted_arrays([H1|T1], [], MyArr, Index, Indx1, Indx2, Even, Middle) -> | ||
find_median_sorted_arrays(T1, [], [H1|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle); | ||
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle) when H1 < H2 -> | ||
find_median_sorted_arrays(T1, [H2|T2], [H1|MyArr], Index + 1, Indx1 + 1, Indx2, Even, Middle); | ||
find_median_sorted_arrays([H1|T1], [H2|T2], MyArr, Index, Indx1, Indx2, Even, Middle)-> | ||
find_median_sorted_arrays([H1|T1], T2, [H2|MyArr], Index + 1, Indx1, Indx2 + 1, Even, Middle). |
50 changes: 50 additions & 0 deletions
50
src/main/erlang/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
4\. Median of Two Sorted Arrays | ||
|
||
Hard | ||
|
||
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays. | ||
|
||
The overall run time complexity should be `O(log (m+n))`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums1 = [1,3], nums2 = [2] | ||
|
||
**Output:** 2.00000 | ||
|
||
**Explanation:** merged array = [1,2,3] and median is 2. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums1 = [1,2], nums2 = [3,4] | ||
|
||
**Output:** 2.50000 | ||
|
||
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums1 = [0,0], nums2 = [0,0] | ||
|
||
**Output:** 0.00000 | ||
|
||
**Example 4:** | ||
|
||
**Input:** nums1 = [], nums2 = [1] | ||
|
||
**Output:** 1.00000 | ||
|
||
**Example 5:** | ||
|
||
**Input:** nums1 = [2], nums2 = [] | ||
|
||
**Output:** 2.00000 | ||
|
||
**Constraints:** | ||
|
||
* `nums1.length == m` | ||
* `nums2.length == n` | ||
* `0 <= m <= 1000` | ||
* `0 <= n <= 1000` | ||
* `1 <= m + n <= 2000` | ||
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code> |
48 changes: 48 additions & 0 deletions
48
src/main/erlang/g0001_0100/s0005_longest_palindromic_substring/Solution.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming | ||
% #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming | ||
% #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n) | ||
% #2025_01_08_Time_179_(100.00%)_Space_59.84_(100.00%) | ||
|
||
-spec longest_palindrome(S :: unicode:unicode_binary()) -> unicode:unicode_binary(). | ||
longest_palindrome(S) -> | ||
Length = byte_size(S), | ||
case Length of | ||
0 -> <<>>; % Return empty binary if input is empty | ||
_ -> | ||
% Initialize variables for the best palindrome | ||
{Start, Len} = lists:foldl( | ||
fun(Index, {BestStart, BestLen}) -> | ||
% Find the longest palindrome for odd and even length centers | ||
{NewStart1, NewLen1} = find_longest_palindrome(S, Index, Index), | ||
{NewStart2, NewLen2} = find_longest_palindrome(S, Index, Index + 1), | ||
% Choose the longer of the two found palindromes | ||
case NewLen1 >= NewLen2 of | ||
true -> update_best(BestStart, BestLen, NewStart1, NewLen1); | ||
false -> update_best(BestStart, BestLen, NewStart2, NewLen2) | ||
end | ||
end, | ||
{0, 0}, % Initial best palindrome start and length | ||
lists:seq(0, Length - 1) % Iterate through all positions | ||
), | ||
% Extract the longest palindromic substring | ||
binary:part(S, Start, Len) | ||
end. | ||
|
||
% Helper function to find the longest palindrome around the center | ||
-spec find_longest_palindrome(S :: unicode:unicode_binary(), L :: integer(), R :: integer()) -> {integer(), integer()}. | ||
find_longest_palindrome(S, L, R) -> | ||
Length = byte_size(S), | ||
case (L >= 0 andalso R < Length andalso binary:part(S, L, 1) =:= binary:part(S, R, 1)) of | ||
true -> | ||
find_longest_palindrome(S, L - 1, R + 1); | ||
false -> | ||
{L + 1, R - L - 1} | ||
end. | ||
|
||
% Helper function to update the best palindrome found so far | ||
-spec update_best(BestStart :: integer(), BestLen :: integer(), NewStart :: integer(), NewLen :: integer()) -> {integer(), integer()}. | ||
update_best(BestStart, BestLen, NewStart, NewLen) -> | ||
case NewLen > BestLen of | ||
true -> {NewStart, NewLen}; | ||
false -> {BestStart, BestLen} | ||
end. |
34 changes: 34 additions & 0 deletions
34
src/main/erlang/g0001_0100/s0005_longest_palindromic_substring/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
5\. Longest Palindromic Substring | ||
|
||
Medium | ||
|
||
Given a string `s`, return _the longest palindromic substring_ in `s`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "babad" | ||
|
||
**Output:** "bab" **Note:** "aba" is also a valid answer. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "cbbd" | ||
|
||
**Output:** "bb" | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "a" | ||
|
||
**Output:** "a" | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "ac" | ||
|
||
**Output:** "a" | ||
|
||
**Constraints:** | ||
|
||
* `1 <= s.length <= 1000` | ||
* `s` consist of only digits and English letters. |
42 changes: 42 additions & 0 deletions
42
src/main/erlang/g0001_0100/s0006_zigzag_conversion/Solution.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
% #Medium #String #2025_01_08_Time_203_(100.00%)_Space_60.52_(100.00%) | ||
|
||
%% Define the function specification | ||
-spec convert(S :: unicode:unicode_binary(), NumRows :: integer()) -> unicode:unicode_binary(). | ||
convert(S, NumRows) -> | ||
%% Convert the input string to a list of characters | ||
CharList = unicode:characters_to_list(S), | ||
Length = length(CharList), | ||
%% Handle edge cases | ||
if | ||
NumRows == 1; NumRows >= Length -> | ||
S; | ||
true -> | ||
%% Initialize a list of empty lists (rows) with length NumRows | ||
Rows = lists:map(fun(_) -> [] end, lists:seq(1, NumRows)), | ||
%% Build the zigzag pattern | ||
ZigzagRows = build_zigzag(CharList, Rows, 0, true), | ||
%% Concatenate all rows to form the result | ||
unicode:characters_to_binary(lists:append(ZigzagRows)) | ||
end. | ||
|
||
%% Recursive function to build zigzag rows | ||
-spec build_zigzag([integer()], [[integer()]], integer(), boolean()) -> [[integer()]]. | ||
build_zigzag([], Rows, _, _) -> | ||
Rows; | ||
build_zigzag([Char | Rest], Rows, CurrentRow, GoingDown) -> | ||
%% Add the character to the current row | ||
UpdatedRows = update_row(Rows, CurrentRow, Char), | ||
%% Determine the new direction and row index | ||
{NewDirection, NewRow} = case {CurrentRow, GoingDown} of | ||
{0, _} -> {true, 1}; | ||
{Row, _} when Row == length(Rows) - 1 -> {false, Row - 1}; | ||
{Row, true} -> {true, Row + 1}; | ||
{Row, false} -> {false, Row - 1} | ||
end, | ||
%% Recursively build the zigzag pattern | ||
build_zigzag(Rest, UpdatedRows, NewRow, NewDirection). | ||
|
||
%% Helper function to update the row with the new character | ||
-spec update_row([[integer()]], integer(), integer()) -> [[integer()]]. | ||
update_row(Rows, Index, Char) -> | ||
lists:sublist(Rows, Index) ++ [lists:nth(Index + 1, Rows) ++ [Char]] ++ lists:nthtail(Index + 1, Rows). |
Oops, something went wrong.