Skip to content

Commit da69bcd

Browse files
committed
Added tasks 416-1143
1 parent b867bae commit da69bcd

File tree

8 files changed

+476
-37
lines changed

8 files changed

+476
-37
lines changed

README.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@
4949

5050
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
5151
|-|-|-|-|-|-
52+
| 0438 |[Find All Anagrams in a String](src/main/erlang/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 43 | 100.00
5253

5354
#### Day 6 Breadth First Search Depth First Search
5455

5556
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
5657
|-|-|-|-|-|-
57-
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
58+
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 350 | 100.00
5859

5960
#### Day 7 Breadth First Search Depth First Search
6061

@@ -121,6 +122,7 @@
121122

122123
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
123124
|-|-|-|-|-|-
125+
| 1143 |[Longest Common Subsequence](src/main/erlang/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 2600 | 100.00
124126

125127
#### Day 18 Dynamic Programming
126128

@@ -425,6 +427,7 @@
425427

426428
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
427429
|-|-|-|-|-|-
430+
| 1143 |[Longest Common Subsequence](src/main/erlang/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 2600 | 100.00
428431
| 0072 |[Edit Distance](src/main/erlang/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 251 | 100.00
429432

430433
#### Day 20
@@ -564,6 +567,7 @@
564567

565568
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
566569
|-|-|-|-|-|-
570+
| 0438 |[Find All Anagrams in a String](src/main/erlang/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 43 | 100.00
567571

568572
#### Day 13
569573

@@ -613,7 +617,7 @@
613617

614618
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
615619
|-|-|-|-|-|-
616-
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
620+
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 350 | 100.00
617621

618622
#### Day 2 Matrix Related Problems
619623

@@ -783,7 +787,7 @@
783787

784788
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
785789
|-|-|-|-|-|-
786-
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
790+
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 350 | 100.00
787791

788792
#### Day 10 Dynamic Programming
789793

@@ -801,6 +805,7 @@
801805

802806
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
803807
|-|-|-|-|-|-
808+
| 0438 |[Find All Anagrams in a String](src/main/erlang/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 43 | 100.00
804809

805810
#### Day 13 Hashmap
806811

@@ -859,6 +864,8 @@
859864

860865
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
861866
|-|-|-|-|-|-
867+
| 0543 |[Diameter of Binary Tree](src/main/erlang/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 485 | 100.00
868+
| 0437 |[Path Sum III](src/main/erlang/g0401_0500/s0437_path_sum_iii)| Medium | Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 45 | 100.00
862869

863870
#### Day 8 Binary Search
864871

@@ -894,6 +901,7 @@
894901

895902
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
896903
|-|-|-|-|-|-
904+
| 0416 |[Partition Equal Subset Sum](src/main/erlang/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 585 | 100.00
897905
| 0152 |[Maximum Product Subarray](src/main/erlang/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
898906

899907
#### Day 14 Sliding Window/Two Pointer
@@ -1018,6 +1026,7 @@
10181026
|-|-|-|-|-|-
10191027
| 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
10201028
| 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
1029+
| 0543 |[Diameter of Binary Tree](src/main/erlang/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 485 | 100.00
10211030
| 0226 |[Invert Binary Tree](src/main/erlang/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10221031
| 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
10231032
| 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
@@ -1033,7 +1042,7 @@
10331042

10341043
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10351044
|-|-|-|-|-|-
1036-
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
1045+
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 350 | 100.00
10371046

10381047
#### Udemy Dynamic Programming
10391048

@@ -1044,6 +1053,7 @@
10441053
| 0198 |[House Robber](src/main/erlang/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10451054
| 0070 |[Climbing Stairs](src/main/erlang/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10461055
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
1056+
| 1143 |[Longest Common Subsequence](src/main/erlang/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 2600 | 100.00
10471057
| 0072 |[Edit Distance](src/main/erlang/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 251 | 100.00
10481058
| 0010 |[Regular Expression Matching](src/main/erlang/g0001_0100/s0010_regular_expression_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 2019 | 100.00
10491059

@@ -1362,10 +1372,16 @@
13621372

13631373
| # | Title | Difficulty | Tag | Time, ms | Time, %
13641374
|------|----------------|-------------|-------------|----------|--------
1375+
| 1143 |[Longest Common Subsequence](src/main/erlang/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 2600 | 100.00
13651376
| 0763 |[Partition Labels](src/main/erlang/g0701_0800/s0763_partition_labels)| Medium | String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 5 | 100.00
13661377
| 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
13671378
| 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
13681379
| 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
1380+
| 0543 |[Diameter of Binary Tree](src/main/erlang/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 485 | 100.00
1381+
| 0494 |[Target Sum](src/main/erlang/g0401_0500/s0494_target_sum)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 377 | 100.00
1382+
| 0438 |[Find All Anagrams in a String](src/main/erlang/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 43 | 100.00
1383+
| 0437 |[Path Sum III](src/main/erlang/g0401_0500/s0437_path_sum_iii)| Medium | Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 45 | 100.00
1384+
| 0416 |[Partition Equal Subset Sum](src/main/erlang/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 585 | 100.00
13691385
| 0394 |[Decode String](src/main/erlang/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
13701386
| 0347 |[Top K Frequent Elements](src/main/erlang/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 12 | 100.00
13711387
| 0338 |[Counting Bits](src/main/erlang/g0301_0400/s0338_counting_bits)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 38 | 100.00
@@ -1382,7 +1398,7 @@
13821398
| 0208 |[Implement Trie (Prefix Tree)](src/main/erlang/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 85 | 100.00
13831399
| 0207 |[Course Schedule](src/main/erlang/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 220 | 100.00
13841400
| 0206 |[Reverse Linked List](src/main/erlang/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1385-
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 0 | 100.00
1401+
| 0200 |[Number of Islands](src/main/erlang/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Big_O_Time_O(M\*N)_Space_O(M\*N) | 350 | 100.00
13861402
| 0198 |[House Robber](src/main/erlang/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
13871403
| 0169 |[Majority Element](src/main/erlang/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 2 | 100.00
13881404
| 0155 |[Min Stack](src/main/erlang/g0101_0200/s0155_min_stack)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Big_O_Time_O(1)_Space_O(N) | 16 | 100.00

src/main/erlang/g0101_0200/s0200_number_of_islands/readme.md

+35-32
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,39 @@ An **island** is surrounded by water and is formed by connecting adjacent lands
5151
## Solution
5252

5353
```erlang
54-
%% Definition for a binary tree node.
55-
%%
56-
%% -record(tree_node, {val = 0 :: integer(),
57-
%% left = null :: 'null' | #tree_node{},
58-
%% right = null :: 'null' | #tree_node{}}).
59-
60-
%% @spec right_side_view(Root :: #tree_node{} | null) -> [integer()].
61-
-spec right_side_view(Root :: #tree_node{} | null) -> [integer()].
62-
right_side_view(null) ->
63-
[];
64-
right_side_view(Root) ->
65-
right_side_view_level([Root], []).
66-
67-
%% Helper function to traverse the tree level by level
68-
-spec right_side_view_level(Level :: [#tree_node{}], Acc :: [integer()]) -> [integer()].
69-
right_side_view_level([], Acc) ->
70-
lists:reverse(Acc);
71-
right_side_view_level(Level, Acc) ->
72-
% Get the value of the rightmost node at this level
73-
RightmostValue = lists:last([Node#tree_node.val || Node <- Level]),
74-
% Accumulate the rightmost value
75-
NewAcc = [RightmostValue | Acc],
76-
% Prepare the next level
77-
NextLevel = lists:foldl(fun(Node, AccNextLevel) ->
78-
case Node of
79-
#tree_node{left = null, right = null} -> AccNextLevel;
80-
#tree_node{left = Left, right = Right} ->
81-
AccNextLevel ++ lists:filter(fun(X) -> X =/= null end, [Left, Right])
82-
end
83-
end, [], Level),
84-
% Recursive call to process the next level
85-
right_side_view_level(NextLevel, NewAcc).
54+
-spec num_islands([[char()]]) -> integer().
55+
num_islands(Grid) ->
56+
Table = ets:new(grid, []),
57+
lists:foreach(
58+
fun({Row, I}) ->
59+
lists:foreach(
60+
fun({X, J}) when X =:= $1 ->
61+
ets:insert(Table, {{I, J}});
62+
(_) -> ok
63+
end,
64+
lists:zip(Row, lists:seq(0, length(Row) - 1))
65+
)
66+
end,
67+
lists:zip(Grid, lists:seq(0, length(Grid) - 1))
68+
),
69+
count(Table, 0).
70+
71+
count(Table, Ans) ->
72+
case ets:first(Table) of
73+
'$end_of_table' -> Ans;
74+
{I, J} ->
75+
sink_island(Table, {I, J}),
76+
count(Table, Ans + 1)
77+
end.
78+
79+
sink_island(Table, {I, J}) ->
80+
case ets:member(Table, {I, J}) of
81+
true ->
82+
ets:delete(Table, {I, J}),
83+
sink_island(Table, {I - 1, J}),
84+
sink_island(Table, {I + 1, J}),
85+
sink_island(Table, {I, J - 1}),
86+
sink_island(Table, {I, J + 1});
87+
false -> ok
88+
end.
8689
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
## 416\. Partition Equal Subset Sum
5+
6+
Medium
7+
8+
Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,5,11,5]
13+
14+
**Output:** true
15+
16+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [1,2,3,5]
21+
22+
**Output:** false
23+
24+
**Explanation:** The array cannot be partitioned into equal sum subsets.
25+
26+
**Constraints:**
27+
28+
* `1 <= nums.length <= 200`
29+
* `1 <= nums[i] <= 100`
30+
31+
## Solution
32+
33+
```erlang
34+
-spec can_partition(Nums :: [integer()]) -> boolean().
35+
can_partition(Nums) ->
36+
Sum = lists:sum(Nums),
37+
case Sum rem 2 of
38+
0 ->
39+
Target = Sum div 2,
40+
Dp = lists:foldl(
41+
fun(Num, Acc) ->
42+
maps:fold(
43+
fun(Sum0, true, InnerAcc) ->
44+
case Sum0 + Num =< Target of
45+
true -> InnerAcc#{Sum0 + Num => true};
46+
false -> InnerAcc
47+
end;
48+
(_, false, InnerAcc) -> InnerAcc
49+
end,
50+
Acc#{Num => true},
51+
Acc)
52+
end,
53+
#{0 => true},
54+
Nums),
55+
maps:get(Target, Dp, false);
56+
_ ->
57+
false
58+
end.
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
## 437\. Path Sum III
5+
6+
Medium
7+
8+
Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`.
9+
10+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
15+
16+
**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
17+
18+
**Output:** 3
19+
20+
**Explanation:** The paths that sum to 8 are shown.
21+
22+
**Example 2:**
23+
24+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
25+
26+
**Output:** 3
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range `[0, 1000]`.
31+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
32+
* `-1000 <= targetSum <= 1000`
33+
34+
## Solution
35+
36+
```erlang
37+
%% Definition for a binary tree node.
38+
%%
39+
%% -record(tree_node, {val = 0 :: integer(),
40+
%% left = null :: 'null' | #tree_node{},
41+
%% right = null :: 'null' | #tree_node{}}).
42+
43+
-spec path_sum(Root :: #tree_node{} | null, TargetSum :: integer()) -> integer().
44+
path_sum(null, _TargetSum) -> 0;
45+
path_sum(Root, TargetSum) ->
46+
path_sum(Root, TargetSum, []).
47+
48+
path_sum(null, _TargetSum, _PartialSums) -> 0;
49+
path_sum(Root, TargetSum, PartialSums) ->
50+
NewPartials = [Root#tree_node.val | [Sum + Root#tree_node.val || Sum <- PartialSums]],
51+
EqualPaths = length([Sum || Sum <- NewPartials, Sum =:= TargetSum]),
52+
EqualPaths +
53+
path_sum(Root#tree_node.left, TargetSum, NewPartials) +
54+
path_sum(Root#tree_node.right, TargetSum, NewPartials).
55+
```

0 commit comments

Comments
 (0)