Skip to content

Commit 72074c0

Browse files
authored
Added tasks 399-918
1 parent b064cfe commit 72074c0

File tree

31 files changed

+1264
-0
lines changed

31 files changed

+1264
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
166166
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
167167
|-|-|-|-|-|-
168168
| 0053 |[Maximum Subarray](src/main/go/g0001_0100/s0053_maximum_subarray/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
169+
| 0918 |[Maximum Sum Circular Subarray](src/main/go/g0901_1000/s0918_maximum_sum_circular_subarray/solution.go)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 0 | 100.00
169170

170171
#### Day 6
171172

@@ -511,6 +512,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
511512

512513
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
513514
|-|-|-|-|-|-
515+
| 0433 |[Minimum Genetic Mutation](src/main/go/g0401_0500/s0433_minimum_genetic_mutation/solution.go)| Medium | String, Hash_Table, Breadth_First_Search | 0 | 100.00
514516
| 0127 |[Word Ladder](src/main/go/g0101_0200/s0127_word_ladder/solution.go)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 17 | 97.47
515517

516518
#### Day 13 Graph Theory
@@ -1045,6 +1047,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
10451047
| 0228 |[Summary Ranges](src/main/go/g0201_0300/s0228_summary_ranges/solution.go)| Easy | Array | 0 | 100.00
10461048
| 0056 |[Merge Intervals](src/main/go/g0001_0100/s0056_merge_intervals/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 0 | 100.00
10471049
| 0057 |[Insert Interval](src/main/go/g0001_0100/s0057_insert_interval/solution.go)| Medium | Array | 0 | 100.00
1050+
| 0452 |[Minimum Number of Arrows to Burst Balloons](src/main/go/g0401_0500/s0452_minimum_number_of_arrows_to_burst_balloons/solution.go)| Medium | Array, Sorting, Greedy, LeetCode_75_Intervals | 32 | 94.66
10481051

10491052
#### Top Interview 150 Stack
10501053

@@ -1096,13 +1099,15 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
10961099
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10971100
|-|-|-|-|-|-
10981101
| 0199 |[Binary Tree Right Side View](src/main/go/g0101_0200/s0199_binary_tree_right_side_view/solution.go)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/BFS | 0 | 100.00
1102+
| 0637 |[Average of Levels in Binary Tree](src/main/go/g0601_0700/s0637_average_of_levels_in_binary_tree/solution.go)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
10991103
| 0102 |[Binary Tree Level Order Traversal](src/main/go/g0101_0200/s0102_binary_tree_level_order_traversal/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
11001104
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/go/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.go)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
11011105

11021106
#### Top Interview 150 Binary Search Tree
11031107

11041108
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11051109
|-|-|-|-|-|-
1110+
| 0530 |[Minimum Absolute Difference in BST](src/main/go/g0501_0600/s0530_minimum_absolute_difference_in_bst/solution.go)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 0 | 100.00
11061111
| 0230 |[Kth Smallest Element in a BST](src/main/go/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.go)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
11071112
| 0098 |[Validate Binary Search Tree](src/main/go/g0001_0100/s0098_validate_binary_search_tree/solution.go)| 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
11081113

@@ -1113,13 +1118,16 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11131118
| 0200 |[Number of Islands](src/main/go/g0101_0200/s0200_number_of_islands/solution.go)| 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) | 2 | 87.44
11141119
| 0130 |[Surrounded Regions](src/main/go/g0101_0200/s0130_surrounded_regions/solution.go)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 0 | 100.00
11151120
| 0133 |[Clone Graph](src/main/go/g0101_0200/s0133_clone_graph/solution.go)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 0 | 100.00
1121+
| 0399 |[Evaluate Division](src/main/go/g0301_0400/s0399_evaluate_division/solution.go)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS | 0 | 100.00
11161122
| 0207 |[Course Schedule](src/main/go/g0201_0300/s0207_course_schedule/solution.go)| 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) | 0 | 100.00
11171123
| 0210 |[Course Schedule II](src/main/go/g0201_0300/s0210_course_schedule_ii/solution.go)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 0 | 100.00
11181124

11191125
#### Top Interview 150 Graph BFS
11201126

11211127
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11221128
|-|-|-|-|-|-
1129+
| 0909 |[Snakes and Ladders](src/main/go/g0901_1000/s0909_snakes_and_ladders/solution.go)| Medium | Array, Breadth_First_Search, Matrix | 0 | 100.00
1130+
| 0433 |[Minimum Genetic Mutation](src/main/go/g0401_0500/s0433_minimum_genetic_mutation/solution.go)| Medium | String, Hash_Table, Breadth_First_Search | 0 | 100.00
11231131
| 0127 |[Word Ladder](src/main/go/g0101_0200/s0127_word_ladder/solution.go)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 17 | 97.47
11241132

11251133
#### Top Interview 150 Trie
@@ -1147,13 +1155,15 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11471155
|-|-|-|-|-|-
11481156
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/go/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.go)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00
11491157
| 0148 |[Sort List](src/main/go/g0101_0200/s0148_sort_list/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 7 | 87.91
1158+
| 0427 |[Construct Quad Tree](src/main/go/g0401_0500/s0427_construct_quad_tree/solution.go)| Medium | Array, Tree, Matrix, Divide_and_Conquer | 0 | 100.00
11501159
| 0023 |[Merge k Sorted Lists](src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 0 | 100.00
11511160

11521161
#### Top Interview 150 Kadane's Algorithm
11531162

11541163
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11551164
|-|-|-|-|-|-
11561165
| 0053 |[Maximum Subarray](src/main/go/g0001_0100/s0053_maximum_subarray/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1166+
| 0918 |[Maximum Sum Circular Subarray](src/main/go/g0901_1000/s0918_maximum_sum_circular_subarray/solution.go)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 0 | 100.00
11571167

11581168
#### Top Interview 150 Binary Search
11591169

@@ -1173,6 +1183,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11731183
|-|-|-|-|-|-
11741184
| 0215 |[Kth Largest Element in an Array](src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, LeetCode_75_Heap/Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 17 | 83.49
11751185
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.go)| Medium | Array, Heap_Priority_Queue | 21 | 90.40
1186+
| 0502 |[IPO](src/main/go/g0501_0600/s0502_ipo/solution.go)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 62 | 91.95
11761187
| 0295 |[Find Median from Data Stream](src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 59 | 96.70
11771188

11781189
#### Top Interview 150 Bit Manipulation
@@ -1742,15 +1753,24 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
17421753
| # | Title | Difficulty | Tag | Time, ms | Time, %
17431754
|------|----------------|-------------|-------------|----------|---------
17441755
| 1143 |[Longest Common Subsequence](src/main/go/g1101_1200/s1143_longest_common_subsequence/solution.go)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, LeetCode_75_DP/Multidimensional, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 11 | 84.94
1756+
| 0918 |[Maximum Sum Circular Subarray](src/main/go/g0901_1000/s0918_maximum_sum_circular_subarray/solution.go)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Dynamic_Programming_I_Day_5, Top_Interview_150_Kadane's_Algorithm | 0 | 100.00
1757+
| 0909 |[Snakes and Ladders](src/main/go/g0901_1000/s0909_snakes_and_ladders/solution.go)| Medium | Array, Breadth_First_Search, Matrix, Top_Interview_150_Graph_BFS | 0 | 100.00
17451758
| 0763 |[Partition Labels](src/main/go/g0701_0800/s0763_partition_labels/solution.go)| Medium | String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
17461759
| 0739 |[Daily Temperatures](src/main/go/g0701_0800/s0739_daily_temperatures/solution.go)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, LeetCode_75_Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 5 | 96.26
17471760
| 0647 |[Palindromic Substrings](src/main/go/g0601_0700/s0647_palindromic_substrings/solution.go)| Medium | String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 0 | 100.00
1761+
| 0637 |[Average of Levels in Binary Tree](src/main/go/g0601_0700/s0637_average_of_levels_in_binary_tree/solution.go)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Top_Interview_150_Binary_Tree_BFS | 0 | 100.00
17481762
| 0560 |[Subarray Sum Equals K](src/main/go/g0501_0600/s0560_subarray_sum_equals_k/solution.go)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 11 | 93.74
17491763
| 0543 |[Diameter of Binary Tree](src/main/go/g0501_0600/s0543_diameter_of_binary_tree/solution.go)| 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) | 0 | 100.00
1764+
| 0530 |[Minimum Absolute Difference in BST](src/main/go/g0501_0600/s0530_minimum_absolute_difference_in_bst/solution.go)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Top_Interview_150_Binary_Search_Tree | 0 | 100.00
1765+
| 0502 |[IPO](src/main/go/g0501_0600/s0502_ipo/solution.go)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue, Top_Interview_150_Heap | 62 | 91.95
17501766
| 0494 |[Target Sum](src/main/go/g0401_0500/s0494_target_sum/solution.go)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 3 | 84.35
1767+
| 0452 |[Minimum Number of Arrows to Burst Balloons](src/main/go/g0401_0500/s0452_minimum_number_of_arrows_to_burst_balloons/solution.go)| Medium | Array, Sorting, Greedy, LeetCode_75_Intervals, Top_Interview_150_Intervals | 32 | 94.66
17511768
| 0438 |[Find All Anagrams in a String](src/main/go/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.go)| 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) | 0 | 100.00
17521769
| 0437 |[Path Sum III](src/main/go/g0401_0500/s0437_path_sum_iii/solution.go)| Medium | Depth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1770+
| 0433 |[Minimum Genetic Mutation](src/main/go/g0401_0500/s0433_minimum_genetic_mutation/solution.go)| Medium | String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 0 | 100.00
1771+
| 0427 |[Construct Quad Tree](src/main/go/g0401_0500/s0427_construct_quad_tree/solution.go)| Medium | Array, Tree, Matrix, Divide_and_Conquer, Top_Interview_150_Divide_and_Conquer | 0 | 100.00
17531772
| 0416 |[Partition Equal Subset Sum](src/main/go/g0401_0500/s0416_partition_equal_subset_sum/solution.go)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 4 | 97.02
1773+
| 0399 |[Evaluate Division](src/main/go/g0301_0400/s0399_evaluate_division/solution.go)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS, Top_Interview_150_Graph_General | 0 | 100.00
17541774
| 0394 |[Decode String](src/main/go/g0301_0400/s0394_decode_string/solution.go)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, LeetCode_75_Stack, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17551775
| 0392 |[Is Subsequence](src/main/go/g0301_0400/s0392_is_subsequence/solution.go)| Easy | String, Dynamic_Programming, Two_Pointers, LeetCode_75_Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17561776
| 0383 |[Ransom Note](src/main/go/g0301_0400/s0383_ransom_note/solution.go)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String, Top_Interview_150_Hashmap | 0 | 100.00
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
399\. Evaluate Division
2+
3+
Medium
4+
5+
You are given an array of variable pairs `equations` and an array of real numbers `values`, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and `values[i]` represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.
6+
7+
You are also given some `queries`, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.
8+
9+
Return _the answers to all queries_. If a single answer cannot be determined, return `-1.0`.
10+
11+
**Note:** The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
12+
13+
**Example 1:**
14+
15+
**Input:** equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
16+
17+
**Output:** [6.00000,0.50000,-1.00000,1.00000,-1.00000]
18+
19+
**Explanation:** Given: _a / b = 2.0_, _b / c = 3.0_ queries are: _a / c = ?_, _b / a = ?_, _a / e = ?_, _a / a = ?_, _x / x = ?_ return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
20+
21+
**Example 2:**
22+
23+
**Input:** equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
24+
25+
**Output:** [3.75000,0.40000,5.00000,0.20000]
26+
27+
**Example 3:**
28+
29+
**Input:** equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
30+
31+
**Output:** [0.50000,2.00000,-1.00000,-1.00000]
32+
33+
**Constraints:**
34+
35+
* `1 <= equations.length <= 20`
36+
* `equations[i].length == 2`
37+
* <code>1 <= A<sub>i</sub>.length, B<sub>i</sub>.length <= 5</code>
38+
* `values.length == equations.length`
39+
* `0.0 < values[i] <= 20.0`
40+
* `1 <= queries.length <= 20`
41+
* `queries[i].length == 2`
42+
* <code>1 <= C<sub>j</sub>.length, D<sub>j</sub>.length <= 5</code>
43+
* <code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package s0399_evaluate_division
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Graph #Union_Find #Shortest_Path
4+
// #LeetCode_75_Graphs/DFS #Top_Interview_150_Graph_General
5+
// #2025_05_24_Time_0_ms_(100.00%)_Space_4.07_MB_(91.71%)
6+
7+
type Solution struct {
8+
root map[string]string
9+
rate map[string]float64
10+
}
11+
12+
func calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {
13+
sol := &Solution{
14+
root: make(map[string]string),
15+
rate: make(map[string]float64),
16+
}
17+
n := len(equations)
18+
for _, equation := range equations {
19+
x := equation[0]
20+
y := equation[1]
21+
sol.root[x] = x
22+
sol.root[y] = y
23+
sol.rate[x] = 1.0
24+
sol.rate[y] = 1.0
25+
}
26+
for i := 0; i < n; i++ {
27+
x := equations[i][0]
28+
y := equations[i][1]
29+
sol.union(x, y, values[i])
30+
}
31+
result := make([]float64, len(queries))
32+
for i, query := range queries {
33+
x := query[0]
34+
y := query[1]
35+
if _, exists := sol.root[x]; !exists {
36+
result[i] = -1.0
37+
continue
38+
}
39+
if _, exists := sol.root[y]; !exists {
40+
result[i] = -1.0
41+
continue
42+
}
43+
rootX := sol.findRoot(x, x, 1.0)
44+
rootY := sol.findRoot(y, y, 1.0)
45+
if rootX == rootY {
46+
result[i] = sol.rate[x] / sol.rate[y]
47+
} else {
48+
result[i] = -1.0
49+
}
50+
}
51+
return result
52+
}
53+
54+
func (sol *Solution) union(x, y string, v float64) {
55+
rootX := sol.findRoot(x, x, 1.0)
56+
rootY := sol.findRoot(y, y, 1.0)
57+
sol.root[rootX] = rootY
58+
r1 := sol.rate[x]
59+
r2 := sol.rate[y]
60+
sol.rate[rootX] = v * r2 / r1
61+
}
62+
63+
func (sol *Solution) findRoot(originalX, x string, r float64) string {
64+
if sol.root[x] == x {
65+
sol.root[originalX] = x
66+
sol.rate[originalX] = r * sol.rate[x]
67+
return x
68+
}
69+
return sol.findRoot(originalX, sol.root[x], r*sol.rate[x])
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package s0399_evaluate_division
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestCalcEquation(t *testing.T) {
9+
equations := [][]string{{"a", "b"}, {"b", "c"}}
10+
values := []float64{2.0, 3.0}
11+
queries := [][]string{{"a", "c"}, {"b", "a"}, {"a", "e"}, {"a", "a"}, {"x", "x"}}
12+
expected := []float64{6.0, 0.5, -1.0, 1.0, -1.0}
13+
assert.Equal(t, expected, calcEquation(equations, values, queries))
14+
}
15+
16+
func TestCalcEquation2(t *testing.T) {
17+
equations := [][]string{{"a", "b"}, {"b", "c"}, {"bc", "cd"}}
18+
values := []float64{1.5, 2.5, 5.0}
19+
queries := [][]string{{"a", "c"}, {"c", "b"}, {"bc", "cd"}, {"cd", "bc"}}
20+
expected := []float64{3.75, 0.4, 5.0, 0.2}
21+
assert.Equal(t, expected, calcEquation(equations, values, queries))
22+
}
23+
24+
func TestCalcEquation3(t *testing.T) {
25+
equations := [][]string{{"a", "b"}}
26+
values := []float64{0.5}
27+
queries := [][]string{{"a", "b"}, {"b", "a"}, {"a", "c"}, {"x", "y"}}
28+
expected := []float64{0.5, 2.0, -1.0, -1.0}
29+
assert.Equal(t, expected, calcEquation(equations, values, queries))
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package s0427_construct_quad_tree
2+
3+
type Node struct {
4+
Val bool
5+
IsLeaf bool
6+
TopLeft *Node
7+
TopRight *Node
8+
BottomLeft *Node
9+
BottomRight *Node
10+
}

0 commit comments

Comments
 (0)