Skip to content

Commit 3b89381

Browse files
committed
Added tasks 124-138
1 parent f03d086 commit 3b89381

File tree

6 files changed

+295
-0
lines changed

6 files changed

+295
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@
560560

561561
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
562562
|-|-|-|-|-|-
563+
| 0138 |[Copy List with Random Pointer](src/main/erlang/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Array, Bit_Manipulation | 6 | 100.00
563564

564565
#### Day 15
565566

@@ -916,6 +917,7 @@
916917

917918
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
918919
|-|-|-|-|-|-
920+
| 0136 |[Single Number](src/main/erlang/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
919921
| 0007 |[Reverse Integer](src/main/erlang/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 244 | 100.00
920922
| 0009 |[Palindrome Number](src/main/erlang/g0001_0100/s0009_palindrome_number)| Easy | Math | 1 | 100.00
921923

@@ -974,6 +976,7 @@
974976
|-|-|-|-|-|-
975977
| 0024 |[Swap Nodes in Pairs](src/main/erlang/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
976978
| 0021 |[Merge Two Sorted Lists](src/main/erlang/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
979+
| 0138 |[Copy List with Random Pointer](src/main/erlang/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Array, Bit_Manipulation | 6 | 100.00
977980

978981
#### Udemy Tree Stack Queue
979982

@@ -982,6 +985,7 @@
982985
| 0094 |[Binary Tree Inorder Traversal](src/main/erlang/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
983986
| 0102 |[Binary Tree Level Order Traversal](src/main/erlang/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 12 | 100.00
984987
| 0104 |[Maximum Depth of Binary Tree](src/main/erlang/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
988+
| 0124 |[Binary Tree Maximum Path Sum](src/main/erlang/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 100.00
985989
| 0098 |[Validate Binary Search Tree](src/main/erlang/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
986990

987991
#### Udemy Trie and Heap
@@ -1111,6 +1115,7 @@
11111115

11121116
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11131117
|-|-|-|-|-|-
1118+
| 0136 |[Single Number](src/main/erlang/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
11141119
| 0015 |[3Sum](src/main/erlang/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 1387 | 100.00
11151120

11161121
#### Day 2 Array
@@ -1298,6 +1303,7 @@
12981303

12991304
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13001305
|-|-|-|-|-|-
1306+
| 0136 |[Single Number](src/main/erlang/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
13011307

13021308
## Algorithms
13031309

@@ -1307,6 +1313,11 @@
13071313
| 0739 |[Daily Temperatures](src/main/erlang/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 94 | 100.00
13081314
| 0647 |[Palindromic Substrings](src/main/erlang/g0601_0700/s0647_palindromic_substrings)| Medium | String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 17 | 100.00
13091315
| 0560 |[Subarray Sum Equals K](src/main/erlang/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 47 | 100.00
1316+
| 0138 |[Copy List with Random Pointer](src/main/erlang/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Array, Bit_Manipulation | 6 | 100.00
1317+
| 0136 |[Single Number](src/main/erlang/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1318+
| 0131 |[Palindrome Partitioning](src/main/erlang/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 24 | 100.00
1319+
| 0128 |[Longest Consecutive Sequence](src/main/erlang/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 42 | 100.00
1320+
| 0124 |[Binary Tree Maximum Path Sum](src/main/erlang/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 4 | 100.00
13101321
| 0121 |[Best Time to Buy and Sell Stock](src/main/erlang/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 89 | 100.00
13111322
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/erlang/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 169 | 100.00
13121323
| 0104 |[Maximum Depth of Binary Tree](src/main/erlang/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 124\. Binary Tree Maximum Path Sum
5+
6+
Hard
7+
8+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
9+
10+
The **path sum** of a path is the sum of the node's values in the path.
11+
12+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
17+
18+
**Input:** root = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
27+
28+
**Input:** root = [-10,9,20,null,null,15,7]
29+
30+
**Output:** 42
31+
32+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
37+
* `-1000 <= Node.val <= 1000`
38+
39+
## Solution
40+
41+
```erlang
42+
%% Definition for a binary tree node.
43+
%%
44+
%% -record(tree_node, {val = 0 :: integer(),
45+
%% left = null :: 'null' | #tree_node{},
46+
%% right = null :: 'null' | #tree_node{}}).
47+
48+
-spec max_path_sum(Root :: #tree_node{} | null) -> integer().
49+
max_path_sum(#tree_node{val = Val, left = null, right = null}) ->
50+
Val;
51+
max_path_sum(Root) ->
52+
element(2, max_path(Root, -1001)).
53+
54+
-spec max_path(Node :: #tree_node{} | null, Max :: integer()) -> {integer(), integer()}.
55+
max_path(null, Max) ->
56+
{0, Max};
57+
max_path(#tree_node{val = Val, left = Left, right = Right}, Max) ->
58+
{LVal, LMax} = max_path(Left, Max),
59+
{RVal, RMax} = max_path(Right, Max),
60+
LocalMax = lists:max([LVal + Val, RVal + Val, Val]),
61+
GlobalMax = lists:max([Max, Val, Val + LVal, Val + RVal, Val + LVal + RVal, LMax, RMax]),
62+
{LocalMax, GlobalMax}.
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
## 128\. Longest Consecutive Sequence
5+
6+
Medium
7+
8+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
9+
10+
You must write an algorithm that runs in `O(n)` time.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [100,4,200,1,3,2]
15+
16+
**Output:** 4
17+
18+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [0,3,7,2,5,8,4,6,0,1]
23+
24+
**Output:** 9
25+
26+
**Constraints:**
27+
28+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
30+
31+
## Solution
32+
33+
```erlang
34+
-spec longest_consecutive(Nums :: [integer()]) -> integer().
35+
longest_consecutive(Nums) ->
36+
Sorted = lists:sort(Nums),
37+
SeqLens = count_consecutive(Sorted, [1]),
38+
lists:max(SeqLens).
39+
40+
-spec count_consecutive(Nums :: [integer()], SeqLens :: [integer()]) -> [integer()].
41+
count_consecutive([H1, H2 | Tail], [SeqHead | SeqTail]) ->
42+
NewLen = case H1 - H2 of
43+
-1 -> SeqHead + 1; % Consecutive elements
44+
0 -> SeqHead; % Same element, do nothing
45+
_ -> 1 % Not consecutive, reset count
46+
end,
47+
count_consecutive([H2 | Tail], [NewLen | [SeqHead | SeqTail]]);
48+
count_consecutive([_], SeqLens) ->
49+
SeqLens; % End of list with remaining sequence lengths
50+
count_consecutive([], [_]) ->
51+
[0]. % Single number case
52+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
## 131\. Palindrome Partitioning
5+
6+
Medium
7+
8+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return all possible palindrome partitioning of `s`.
9+
10+
A **palindrome** string is a string that reads the same backward as forward.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aab"
15+
16+
**Output:** [["a","a","b"],["aa","b"]]
17+
18+
**Example 2:**
19+
20+
**Input:** s = "a"
21+
22+
**Output:** [["a"]]
23+
24+
**Constraints:**
25+
26+
* `1 <= s.length <= 16`
27+
* `s` contains only lowercase English letters.
28+
29+
## Solution
30+
31+
```erlang
32+
-spec partition(S :: unicode:unicode_binary()) -> [[unicode:unicode_binary()]].
33+
partition(S) ->
34+
Len = byte_size(S),
35+
dfs(S, Len, 0, [], []).
36+
37+
% Base case: when we've processed the entire string
38+
dfs(_S, Len, L, Subs, Ans) when L =:= Len ->
39+
[lists:reverse(Subs) | Ans];
40+
41+
% Recursive case: try all possible partitions
42+
dfs(S, Len, L, Subs, Ans) ->
43+
partition_from_position(S, Len, L, L, Subs, Ans).
44+
45+
partition_from_position(S, Len, L, R, Subs, Ans) when R < Len ->
46+
Substring = binary:part(S, L, R - L + 1),
47+
NewAns = case is_palindrome(Substring) of
48+
true ->
49+
dfs(S, Len, R + 1, [Substring | Subs], Ans);
50+
false ->
51+
Ans
52+
end,
53+
partition_from_position(S, Len, L, R + 1, Subs, NewAns);
54+
partition_from_position(_S, _Len, _L, _R, _Subs, Ans) ->
55+
Ans.
56+
57+
% Helper function to check if a binary is a palindrome
58+
is_palindrome(Bin) ->
59+
List = binary_to_list(Bin),
60+
List =:= lists:reverse(List).
61+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
## 136\. Single Number
5+
6+
Easy
7+
8+
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
9+
10+
You must implement a solution with a linear runtime complexity and use only constant extra space.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [2,2,1]
15+
16+
**Output:** 1
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [4,1,2,1,2]
21+
22+
**Output:** 4
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [1]
27+
28+
**Output:** 1
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
33+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
34+
* Each element in the array appears twice except for one element which appears only once.
35+
36+
## Solution
37+
38+
```erlang
39+
-spec single_number(Nums :: [integer()]) -> integer().
40+
single_number(Nums) ->
41+
%% using lists:foldl to calculate the xor of all elements in the list
42+
lists:foldl(fun(X, Acc) -> X bxor Acc end, 0, Nums).
43+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
## 138\. Copy List with Random Pointer
5+
6+
Medium
7+
8+
A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or `null`.
9+
10+
Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) of the list. The deep copy should consist of exactly `n` **brand new** nodes, where each new node has its value set to the value of its corresponding original node. Both the `next` and `random` pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. **None of the pointers in the new list should point to nodes in the original list**.
11+
12+
For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`.
13+
14+
Return _the head of the copied linked list_.
15+
16+
The linked list is represented in the input/output as a list of `n` nodes. Each node is represented as a pair of `[val, random_index]` where:
17+
18+
* `val`: an integer representing `Node.val`
19+
* `random_index`: the index of the node (range from `0` to `n-1`) that the `random` pointer points to, or `null` if it does not point to any node.
20+
21+
Your code will **only** be given the `head` of the original linked list.
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2019/12/18/e1.png)
26+
27+
**Input:** head = \[\[7,null],[13,0],[11,4],[10,2],[1,0]]
28+
29+
**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]]
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2019/12/18/e2.png)
34+
35+
**Input:** head = \[\[1,1],[2,1]]
36+
37+
**Output:** [[1,1],[2,1]]
38+
39+
**Example 3:**
40+
41+
**![](https://assets.leetcode.com/uploads/2019/12/18/e3.png)**
42+
43+
**Input:** head = \[\[3,null],[3,0],[3,null]]
44+
45+
**Output:** [[3,null],[3,0],[3,null]]
46+
47+
**Constraints:**
48+
49+
* `0 <= n <= 1000`
50+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
51+
* `Node.random` is `null` or is pointing to some node in the linked list.
52+
53+
## Solution
54+
55+
```erlang
56+
-spec single_number(Nums :: [integer()]) -> integer().
57+
single_number(Nums) ->
58+
Frequencies = lists:foldl(fun count_frequency/2, #{}, Nums),
59+
{Key, _} = lists:keyfind(1, 2, maps:to_list(Frequencies)),
60+
Key.
61+
62+
-spec count_frequency(Num :: integer(), Map :: map()) -> map().
63+
count_frequency(Num, Map) ->
64+
maps:update_with(Num, fun(V) -> V + 1 end, 1, Map).
65+
```

0 commit comments

Comments
 (0)