Skip to content

Commit 2ca4468

Browse files
committed
Added erlang
1 parent b71615c commit 2ca4468

File tree

43 files changed

+1228
-17
lines changed

Some content is hidden

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

43 files changed

+1228
-17
lines changed
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
22
% #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
3-
% #Big_O_Time_O(n*k_log_k)_Space_O(n) #2025_01_12_Time_23_(_%)_Space_73.88_(100.00%)
3+
% #Big_O_Time_O(n*k_log_k)_Space_O(n) #2025_01_15_Time_13_(90.00%)_Space_73.34_(100.00%)
44

55
-spec group_anagrams(Strs :: [unicode:unicode_binary()]) -> [[unicode:unicode_binary()]].
66
group_anagrams(Strs) ->
7-
group_anagrams(Strs, dict:new()).
7+
AnagramMap = group_anagrams_helper(Strs, #{}),
8+
maps:values(AnagramMap).
89

9-
-spec group_anagrams(Strs :: [unicode:unicode_binary()], Acc :: dict:dict()) -> [[unicode:unicode_binary()]].
10-
group_anagrams([], Acc) ->
11-
dict:fold(fun(_, Val, AccAcc) -> [Val | AccAcc] end, [], Acc);
12-
group_anagrams([Word | Rest], Acc) ->
13-
Key = create_key(Word),
14-
NewAcc = case dict:find(Key, Acc) of
15-
{ok, List} -> dict:store(Key, [Word | List], Acc);
16-
error -> dict:store(Key, [Word], Acc)
17-
end,
18-
group_anagrams(Rest, NewAcc).
10+
group_anagrams_helper([Head | Tail], Acc) ->
11+
NewString = sort_string(Head),
12+
NewMap = case maps:is_key(NewString, Acc) of
13+
true ->
14+
maps:update(NewString, [Head | maps:get(NewString, Acc)], Acc);
15+
false ->
16+
maps:put(NewString, [Head], Acc)
17+
end,
18+
group_anagrams_helper(Tail, NewMap);
19+
group_anagrams_helper([], Acc) ->
20+
Acc.
1921

20-
-spec create_key(Word :: unicode:unicode_binary()) -> unicode:unicode_binary().
21-
create_key(Word) ->
22-
CharList = unicode:characters_to_list(Word), % Convert the binary string to a list of characters
23-
SortedList = lists:sort(CharList), % Sort the characters to create a canonical key
24-
list_to_binary(SortedList). % Convert back to binary for the dictionary key
22+
sort_string(Str) ->
23+
StrList = binary_to_list(Str), % Convert string to list of characters
24+
SortedList = lists:sort(StrList), % Sort the list of characters
25+
list_to_binary(SortedList).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting
2+
% #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix
3+
% #Big_O_Time_O(n_log_n)_Space_O(n) #2025_01_13_Time_8_(100.00%)_Space_70.88_(100.00%)
4+
5+
-spec merge(Intervals :: [[integer()]]) -> [[integer()]].
6+
merge(Intervals) ->
7+
SortedIntervals = lists:sort(fun([Start1, _], [Start2, _]) -> Start1 =< Start2 end, Intervals),
8+
lists:reverse(
9+
lists:foldl(fun([Start, Finish], Acc) ->
10+
case Acc of
11+
[] -> [[Start, Finish]];
12+
[[PrevStart, PrevFinish] | Rest] ->
13+
if PrevFinish >= Start ->
14+
%% Merge overlapping intervals
15+
[[PrevStart, erlang:max(PrevFinish, Finish)] | Rest];
16+
true ->
17+
%% No overlap
18+
[[Start, Finish] | Acc]
19+
end
20+
end
21+
end, [], SortedIntervals)
22+
).
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
56\. Merge Intervals
2+
3+
Medium
4+
5+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
6+
7+
**Example 1:**
8+
9+
**Input:** intervals = [[1,3],[2,6],[8,10],[15,18]]
10+
11+
**Output:** [[1,6],[8,10],[15,18]]
12+
13+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
14+
15+
**Example 2:**
16+
17+
**Input:** intervals = [[1,4],[4,5]]
18+
19+
**Output:** [[1,5]]
20+
21+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
26+
* `intervals[i].length == 2`
27+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
2+
% #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
3+
% #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
4+
% #2025_01_13_Time_0_(100.00%)_Space_58.34_(100.00%)
5+
6+
-spec unique_paths(M :: integer(), N :: integer()) -> integer().
7+
unique_paths(M, N) ->
8+
Ncr = fun(N, R) ->
9+
lists:foldl(
10+
fun(I, Acc) ->
11+
(Acc * (I + N - R)) div I
12+
end,
13+
1,
14+
lists:seq(1, R)
15+
)
16+
end,
17+
Ncr(M + N - 2, erlang:min(M, N) - 1).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
62\. Unique Paths
2+
3+
Medium
4+
5+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
6+
7+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
8+
9+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
14+
15+
**Input:** m = 3, n = 7
16+
17+
**Output:** 28
18+
19+
**Example 2:**
20+
21+
**Input:** m = 3, n = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
26+
27+
1. Right -> Down -> Down
28+
29+
2. Down -> Down -> Right
30+
31+
3. Down -> Right -> Down
32+
33+
**Constraints:**
34+
35+
* `1 <= m, n <= 100`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
% #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
2+
% #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
3+
% #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
4+
% #2025_01_13_Time_0_(100.00%)_Space_58.51_(100.00%)
5+
6+
-spec climb_stairs(N :: integer()) -> integer().
7+
climb_stairs(N) ->
8+
StairsRecursive = fun StairsRecursive(Res, Last, Curr) ->
9+
if Curr == N ->
10+
Res;
11+
true ->
12+
StairsRecursive(Res + Last, Res, Curr + 1)
13+
end
14+
end,
15+
if N =< 2 ->
16+
N;
17+
true ->
18+
StairsRecursive(2,1,2)
19+
end.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
70\. Climbing Stairs
2+
3+
Easy
4+
5+
You are climbing a staircase. It takes `n` steps to reach the top.
6+
7+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** 2
14+
15+
**Explanation:** There are two ways to climb to the top.
16+
17+
1. 1 step + 1 step
18+
19+
2. 2 steps
20+
21+
**Example 2:**
22+
23+
**Input:** n = 3
24+
25+
**Output:** 3
26+
27+
**Explanation:** There are three ways to climb to the top.
28+
29+
1. 1 step + 1 step + 1 step
30+
31+
2. 1 step + 2 steps
32+
33+
3. 2 steps + 1 step
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 45`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
% #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
2+
% #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
3+
% #Udemy_Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n2)
4+
% #2025_01_13_Time_251_(100.00%)_Space_122.91_(100.00%)
5+
6+
-spec min_distance(Word1 :: unicode:unicode_binary(), Word2 :: unicode:unicode_binary()) -> integer().
7+
min_distance(Word1, Word2) ->
8+
Word1List = [" " | unicode:characters_to_list(Word1)],
9+
Word2List = [" " | unicode:characters_to_list(Word2)],
10+
Len1 = length(Word1List) - 1,
11+
Len2 = length(Word2List) - 1,
12+
13+
Map = build_distance_map(Word1List, Word2List),
14+
maps:get({Len1, Len2}, Map).
15+
16+
build_distance_map(Word1List, Word2List) ->
17+
Indices1 = lists:zip(Word1List, lists:seq(0, length(Word1List) - 1)),
18+
Indices2 = lists:zip(Word2List, lists:seq(0, length(Word2List) - 1)),
19+
lists:foldl(
20+
fun({Ch1, I}, MapAcc1) ->
21+
lists:foldl(
22+
fun({Ch2, J}, MapAcc2) ->
23+
Value = calculate_distance(Ch1, Ch2, I, J, MapAcc2),
24+
maps:put({I, J}, Value, MapAcc2)
25+
end,
26+
MapAcc1,
27+
Indices2
28+
)
29+
end,
30+
maps:new(),
31+
Indices1
32+
).
33+
34+
calculate_distance(_, _, 0, J, _) -> J;
35+
calculate_distance(_, _, I, 0, _) -> I;
36+
calculate_distance(Ch, Ch, I, J, Map) ->
37+
maps:get({I-1, J-1}, Map);
38+
calculate_distance(_, _, I, J, Map) ->
39+
Min1 = min(maps:get({I-1, J}, Map), maps:get({I-1, J-1}, Map)),
40+
Min2 = min(Min1, maps:get({I, J-1}, Map)),
41+
Min2 + 1.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
72\. Edit Distance
2+
3+
Hard
4+
5+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
6+
7+
You have the following three operations permitted on a word:
8+
9+
* Insert a character
10+
* Delete a character
11+
* Replace a character
12+
13+
**Example 1:**
14+
15+
**Input:** word1 = "horse", word2 = "ros"
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
horse -> rorse (replace 'h' with 'r')
22+
23+
rorse -> rose (remove 'r')
24+
25+
rose -> ros (remove 'e')
26+
27+
**Example 2:**
28+
29+
**Input:** word1 = "intention", word2 = "execution"
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
intention -> inention (remove 't')
36+
37+
inention -> enention (replace 'i' with 'e')
38+
39+
enention -> exention (replace 'n' with 'x')
40+
41+
exention -> exection (replace 'n' with 'c')
42+
43+
exection -> execution (insert 'u')
44+
45+
**Constraints:**
46+
47+
* `0 <= word1.length, word2.length <= 500`
48+
* `word1` and `word2` consist of lowercase English letters.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
% #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
2+
% #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
3+
% #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1)
4+
% #2025_01_13_Time_0_(100.00%)_Space_61.38_(100.00%)
5+
6+
-spec search_matrix(Matrix :: [[integer()]], Target :: integer()) -> boolean().
7+
search_matrix(Matrix, Target) ->
8+
% Get the total number of rows and columns
9+
{Rows, Cols} = {length(Matrix), length(hd(Matrix))},
10+
11+
% Perform binary search
12+
binary_search(Matrix, Rows, Cols, Target, 0, Rows * Cols - 1).
13+
14+
% Function to perform binary search
15+
-spec binary_search([[integer()]], integer(), integer(), integer(), integer(), integer()) -> boolean().
16+
binary_search(Matrix, Rows, Cols, Target, Left, Right) ->
17+
if
18+
Left > Right ->
19+
false; % If left index exceeds the right one, return false
20+
true ->
21+
% Calculate mid index and get corresponding row and column
22+
Mid = (Left + Right) div 2,
23+
{Row, Col} = {Mid div Cols, Mid rem Cols},
24+
25+
% Fetch the value from the matrix
26+
Value = lists:nth(Col + 1, lists:nth(Row + 1, Matrix)),
27+
28+
% If the value equals the target, return true
29+
if
30+
Value == Target ->
31+
true;
32+
Value < Target ->
33+
% If the value is less than the target, search in the right half
34+
binary_search(Matrix, Rows, Cols, Target, Mid + 1, Right);
35+
true ->
36+
% If the value is greater than the target, search in the left half
37+
binary_search(Matrix, Rows, Cols, Target, Left, Mid - 1)
38+
end
39+
end.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy