Skip to content

Commit 17cac9a

Browse files
committed
Added erlang
1 parent 2ca4468 commit 17cac9a

File tree

62 files changed

+2131
-0
lines changed

Some content is hidden

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

62 files changed

+2131
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table
2+
% #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
3+
% #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
4+
% #2025_01_18_Time_1_(100.00%)_Space_60.03_(100.00%)
5+
6+
-spec word_break(S :: unicode:unicode_binary(), WordDict :: [unicode:unicode_binary()]) -> boolean().
7+
word_break(S, WordDict) ->
8+
% Initialize ETS table for memoization
9+
ets:new(memo, [set, named_table]),
10+
% Process word dict to include sizes
11+
Words = [{Word, byte_size(Word)} || Word <- WordDict],
12+
% Get result
13+
Result = breakable(S, Words),
14+
% Clean up
15+
ets:delete(memo),
16+
Result.
17+
18+
-spec breakable(binary(), [{binary(), integer()}]) -> boolean().
19+
breakable(<<>>, _Words) ->
20+
true;
21+
breakable(S, Words) ->
22+
case ets:lookup(memo, S) of
23+
[{_, Result}] ->
24+
Result;
25+
[] ->
26+
Result = try_words(S, Words, Words),
27+
ets:insert(memo, {S, Result}),
28+
Result
29+
end.
30+
31+
try_words(_S, [], _AllWords) ->
32+
false;
33+
try_words(S, [{Word, Len} | Rest], AllWords) ->
34+
case S of
35+
<<Prefix:Len/binary, Remaining/binary>> when Prefix =:= Word ->
36+
case breakable(Remaining, AllWords) of
37+
true -> true;
38+
false -> try_words(S, Rest, AllWords)
39+
end;
40+
_ ->
41+
try_words(S, Rest, AllWords)
42+
end.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
139\. Word Break
2+
3+
Medium
4+
5+
Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words.
6+
7+
**Note** that the same word in the dictionary may be reused multiple times in the segmentation.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "leetcode", wordDict = ["leet","code"]
12+
13+
**Output:** true
14+
15+
**Explanation:** Return true because "leetcode" can be segmented as "leet code".
16+
17+
**Example 2:**
18+
19+
**Input:** s = "applepenapple", wordDict = ["apple","pen"]
20+
21+
**Output:** true
22+
23+
**Explanation:** Return true because "applepenapple" can be segmented as "apple pen apple".
24+
25+
Note that you are allowed to reuse a dictionary word.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 300`
36+
* `1 <= wordDict.length <= 1000`
37+
* `1 <= wordDict[i].length <= 20`
38+
* `s` and `wordDict[i]` consist of only lowercase English letters.
39+
* All the strings of `wordDict` are **unique**.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Design #Linked_List
2+
% #Doubly_Linked_List #Udemy_Linked_List #Big_O_Time_O(1)_Space_O(capacity)
3+
% #2025_01_18_Time_312_(100.00%)_Space_273.78_(100.00%)
4+
5+
%% Persistent Term Keys
6+
-define(CAPACITY_KEY, {lru_cache, capacity}).
7+
-define(CACHE_TABLE, lru_cache_cache_table).
8+
-define(TTL_TABLE, lru_cache_ttl_table).
9+
10+
%% API Specifications
11+
-spec lru_cache_init_(Capacity :: integer()) -> ok.
12+
lru_cache_init_(Capacity) ->
13+
persistent_term:put(?CAPACITY_KEY, Capacity),
14+
case ets:info(?CACHE_TABLE) of
15+
undefined ->
16+
ets:new(?CACHE_TABLE, [set, public, named_table]),
17+
ets:new(?TTL_TABLE, [ordered_set, public, named_table]);
18+
_ ->
19+
ets:delete_all_objects(?CACHE_TABLE),
20+
ets:delete_all_objects(?TTL_TABLE)
21+
end,
22+
ok.
23+
24+
-spec lru_cache_get(Key :: integer()) -> integer().
25+
lru_cache_get(Key) ->
26+
case extract(Key) of
27+
{Key, Value} ->
28+
insert(Key, Value),
29+
Value;
30+
-1 ->
31+
-1
32+
end.
33+
34+
-spec lru_cache_put(Key :: integer(), Value :: integer()) -> ok.
35+
lru_cache_put(Key, Value) ->
36+
_ = extract(Key),
37+
insert(Key, Value),
38+
evict(),
39+
ok.
40+
41+
%% Internal Functions
42+
extract(Key) ->
43+
case ets:lookup(?CACHE_TABLE, Key) of
44+
[{Key, Uniq, Value}] ->
45+
ets:delete(?TTL_TABLE, Uniq),
46+
{Key, Value};
47+
[] ->
48+
-1
49+
end.
50+
51+
insert(Key, Value) ->
52+
Uniq = unique_integer(),
53+
ets:insert(?CACHE_TABLE, {Key, Uniq, Value}),
54+
ets:insert(?TTL_TABLE, {Uniq, Key}).
55+
56+
evict() ->
57+
Capacity = persistent_term:get(?CAPACITY_KEY),
58+
CurrentSize = ets:info(?CACHE_TABLE, size),
59+
if
60+
CurrentSize > Capacity ->
61+
Uniq = ets:first(?TTL_TABLE),
62+
[{_, Key}] = ets:lookup(?TTL_TABLE, Uniq),
63+
ets:delete(?TTL_TABLE, Uniq),
64+
ets:delete(?CACHE_TABLE, Key);
65+
true ->
66+
ok
67+
end.
68+
69+
unique_integer() ->
70+
erlang:unique_integer([monotonic]).
71+
72+
%% Your functions will be called as such:
73+
%% lru_cache_init_(Capacity),
74+
%% Param_1 = lru_cache_get(Key),
75+
%% lru_cache_put(Key, Value),
76+
77+
%% lru_cache_init_ will be called before every test case, in which you can do some necessary initializations.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
146\. LRU Cache
2+
3+
Medium
4+
5+
Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**.
6+
7+
Implement the `LRUCache` class:
8+
9+
* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`.
10+
* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`.
11+
* `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key.
12+
13+
The functions `get` and `put` must each run in `O(1)` average time complexity.
14+
15+
**Example 1:**
16+
17+
**Input** ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
18+
19+
**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4]
20+
21+
**Explanation:**
22+
23+
LRUCache lRUCache = new LRUCache(2);
24+
25+
lRUCache.put(1, 1); // cache is {1=1}
26+
27+
lRUCache.put(2, 2); // cache is {1=1, 2=2}
28+
29+
lRUCache.get(1); // return 1
30+
31+
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
32+
33+
lRUCache.get(2); // returns -1 (not found)
34+
35+
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
36+
37+
lRUCache.get(1); // return -1 (not found)
38+
39+
lRUCache.get(3); // return 3
40+
41+
lRUCache.get(4); // return 4
42+
43+
**Constraints:**
44+
45+
* `1 <= capacity <= 3000`
46+
* <code>0 <= key <= 10<sup>4</sup></code>
47+
* <code>0 <= value <= 10<sup>5</sup></code>
48+
* At most <code>2 * 10<sup>5</sup></code> calls will be made to `get` and `put`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
2+
% #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N))
3+
% #2025_01_18_Time_43_(100.00%)_Space_102.77_(100.00%)
4+
5+
%% Definition for singly-linked list.
6+
%%
7+
%% -record(list_node, {val = 0 :: integer(),
8+
%% next = null :: 'null' | #list_node{}}).
9+
10+
%% @spec sort_list(Head :: #list_node{} | null) -> #list_node{} | null.
11+
-spec sort_list(Head :: #list_node{} | null) -> #list_node{} | null.
12+
sort_list(Head) ->
13+
List = node_to_list(Head, []),
14+
SortedList = lists:sort(fun(X, Y) -> X > Y end, List),
15+
list_to_node(SortedList, null).
16+
17+
%% Converts a linked list to an Erlang list.
18+
-spec node_to_list(Node :: #list_node{} | null, Acc :: [integer()]) -> [integer()].
19+
node_to_list(null, Acc) ->
20+
Acc;
21+
node_to_list(#list_node{val = Val, next = Next}, Acc) ->
22+
node_to_list(Next, [Val | Acc]).
23+
24+
%% Converts an Erlang list to a linked list.
25+
-spec list_to_node(List :: [integer()], Node :: #list_node{} | null) -> #list_node{} | null.
26+
list_to_node([], Node) ->
27+
Node;
28+
list_to_node([H | T], Node) ->
29+
list_to_node(T, #list_node{val = H, next = Node}).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
148\. Sort List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
10+
11+
**Input:** head = [4,2,1,3]
12+
13+
**Output:** [1,2,3,4]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
18+
19+
**Input:** head = [-1,5,3,4,0]
20+
21+
**Output:** [-1,0,3,4,5]
22+
23+
**Example 3:**
24+
25+
**Input:** head = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
32+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
33+
34+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
% #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
% #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
3+
% #Big_O_Time_O(N)_Space_O(1) #2025_01_18_Time_0_(100.00%)_Space_63.79_(100.00%)
4+
5+
-spec max_product(Nums :: [integer()]) -> integer().
6+
max_product(Nums) ->
7+
max_product(Nums, 1, 1, -(1 bsl 31)).
8+
9+
-spec max_product(Nums :: [integer()], integer(), integer(), integer()) -> integer().
10+
max_product([], _, _, MaxProduct) ->
11+
MaxProduct;
12+
max_product([H|T], MaxCurrent, MinCurrent, MaxProduct) ->
13+
% The new maximum and minimum products are derived by comparing
14+
% the current number, its product with the maximum so far, and its product with the minimum so far
15+
NewMaxCurrent = max(max(H, H * MaxCurrent), H * MinCurrent),
16+
NewMinCurrent = min(min(H, H * MaxCurrent), H * MinCurrent),
17+
NewMaxProduct = max(MaxProduct, NewMaxCurrent),
18+
max_product(T, NewMaxCurrent, NewMinCurrent, NewMaxProduct).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
152\. Maximum Product Subarray
2+
3+
Medium
4+
5+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
6+
7+
The test cases are generated so that the answer will fit in a **32-bit** integer.
8+
9+
A **subarray** is a contiguous subsequence of the array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,3,-2,4]
14+
15+
**Output:** 6
16+
17+
**Explanation:** [2,3] has the largest product 6.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [-2,0,-1]
22+
23+
**Output:** 0
24+
25+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
30+
* `-10 <= nums[i] <= 10`
31+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
% #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
2+
% #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
3+
% #2025_01_18_Time_0_(100.00%)_Space_60.97_(100.00%)
4+
5+
-spec find_min(Nums :: [integer()]) -> integer().
6+
find_min([N]) ->
7+
N;
8+
find_min(Nums) ->
9+
Count = length(Nums),
10+
Mid = Count div 2,
11+
Left = lists:sublist(Nums, Mid),
12+
Right = lists:nthtail(Mid, Nums),
13+
MinLeft = find_min(Left),
14+
MinRight = find_min(Right),
15+
erlang:min(MinLeft, MinRight).
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
153\. Find Minimum in Rotated Sorted Array
2+
3+
Medium
4+
5+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
6+
7+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
8+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
9+
10+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
11+
12+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
13+
14+
You must write an algorithm that runs in `O(log n) time.`
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [3,4,5,1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [4,5,6,7,0,1,2]
27+
28+
**Output:** 0
29+
30+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [11,13,15,17]
35+
36+
**Output:** 11
37+
38+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
39+
40+
**Constraints:**
41+
42+
* `n == nums.length`
43+
* `1 <= n <= 5000`
44+
* `-5000 <= nums[i] <= 5000`
45+
* All the integers of `nums` are **unique**.
46+
* `nums` is sorted and rotated between `1` and `n` times.

0 commit comments

Comments
 (0)