Skip to content

Commit 21a0564

Browse files
authored
Merge branch 'smv1999:master' into main
2 parents 67ebf0f + 06e13e5 commit 21a0564

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Src : LeetCode
3+
--------------
4+
5+
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.
6+
The path sum of a path is the sum of the node's values in the path.
7+
Given the root of a binary tree, return the maximum path sum of any path.
8+
9+
Input: root = [-10,9,20,null,null,15,7]
10+
Output: 42
11+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
/**The logic is very simple. The maximum path sum will always consist of a node and its left and right sub-tree maximum path sum. As the node values can be negative also so
28+
4 cases comes here .
29+
1) A node and its both sub-trees will constitute the sum
30+
2) Only a node and its left sub-tree will constitute the sum
31+
3) Only a node and its right sub-tree will constitute the sum
32+
4) Only one node will constitute the sum
33+
*/
34+
int maxPathSum(TreeNode* root) {
35+
if(root==NULL)
36+
return 0;
37+
int ans=root->val,sum;
38+
sum=helper(root,ans);
39+
return max(ans,sum);
40+
}
41+
42+
int helper(TreeNode* root,int &ans){
43+
if(root==NULL)
44+
return 0;
45+
int l=helper(root->left,ans);
46+
int r=helper(root->right,ans);
47+
if(l<0)
48+
l=0;
49+
if(r<0)
50+
r=0;
51+
//Case 1 and 4 is checked here
52+
ans=max(ans,l+r+root->val);
53+
//Case 2 and 3 is checked here
54+
return max(l+root->val,r+root->val);
55+
}
56+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Task. Given 𝑛 points on a plane, find the smallest distance between a pair of two (different) points. Recall
2+
# that the distance between points (𝑥1, 𝑦1) and (𝑥2, 𝑦2) is equal to √︀((𝑥1 − 𝑥2)² + (𝑦1 − 𝑦2)²)
3+
4+
import math
5+
def dist(p1, p2):
6+
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
7+
8+
def closest_split_pair(p_x, p_y, delta, best_pair):
9+
ln_x = len(p_x) # store length - quicker
10+
mx_x = p_x[ln_x // 2][0] # select midpoint on x-sorted array
11+
12+
# Create a subarray of points not further than delta from midpoint on x-sorted array
13+
s_y = [x for x in p_y if mx_x - delta <= x[0] <= mx_x + delta]
14+
15+
best = delta # assign delta value to best
16+
ln_y = len(s_y) # store length of subarray for quickness
17+
for i in range(ln_y - 1):
18+
for j in range(i+1, min(i + 5, ln_y)): # We have to check only next 5 points;
19+
p, q = s_y[i], s_y[j]
20+
dst = dist(p, q)
21+
if dst < best:
22+
best_pair = p, q
23+
best = dst
24+
return best_pair[0], best_pair[1], best
25+
26+
27+
def brute(ax):
28+
mi = dist(ax[0], ax[1])
29+
p1 = ax[0]
30+
p2 = ax[1]
31+
ln_ax = len(ax)
32+
if ln_ax == 2:
33+
return p1, p2, mi
34+
for i in range(ln_ax-1):
35+
for j in range(i + 1, ln_ax):
36+
if i != 0 and j != 1:
37+
d = dist(ax[i], ax[j])
38+
if d < mi: # Update min_dist and points
39+
mi = d
40+
p1, p2 = ax[i], ax[j]
41+
return p1, p2, mi
42+
43+
44+
def closest_pair(ax, ay):
45+
ln_ax = len(ax) # It's quicker to assign variable
46+
if ln_ax <= 3:
47+
return brute(ax) # A call to bruteforce comparison
48+
mid = ln_ax // 2 # Division without remainder, need int
49+
Qx = ax[:mid] # Two-part split
50+
Rx = ax[mid:]
51+
52+
midpoint = ax[mid][0]
53+
Qy = list()
54+
Ry = list()
55+
for x in ay: # split ay into 2 arrays using midpoint
56+
if x[0] < midpoint:
57+
Qy.append(x)
58+
else:
59+
Ry.append(x)
60+
# Call recursively both arrays after split
61+
(p1, q1, mi1) = closest_pair(Qx, Qy)
62+
(p2, q2, mi2) = closest_pair(Rx, Ry)
63+
64+
# Determine smaller distance between points of 2 arrays
65+
if mi1 <= mi2:
66+
d = mi1
67+
mn = (p1, q1)
68+
else:
69+
d = mi2
70+
mn = (p2, q2)
71+
72+
# Call function to account for points on the boundary
73+
(p3, q3, mi3) = closest_split_pair(ax, ay, d, mn)
74+
# Determine smallest distance for the array
75+
if d <= mi3:
76+
return mn[0], mn[1], d
77+
else:
78+
return p3, q3, mi3
79+
80+
81+
def solution(a):
82+
ax = sorted(a, key=lambda x: x[0]) # Presorting x-wise O(nlogn)
83+
ay = sorted(a, key=lambda x: (x[1], x[0])) # Presorting y-wise then x-wise O(nlogn)
84+
p1, p2, mi = closest_pair(ax, ay) # Recursive D&C function
85+
return mi
86+
87+
88+
# Input
89+
points = list()
90+
n = int(input())
91+
for i in range(n):
92+
points.append([int(i) for i in input().split()])
93+
94+
print(solution(points))
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
DESCRIPTION :
3+
Given an array of integers arr[] and a target number k, write a program to find all unique combinations in arr[]
4+
such that the sum of all integers in the combination is equal to k.
5+
The same repeated number may be chosen from arr[] unlimited number of times.
6+
7+
NOTE :
8+
1. All numbers will be positive integers.
9+
2. Elements in a combination (a1, a2, …, ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
10+
3. The combinations themselves must be sorted in ascending order.
11+
4. The solution set must not contain duplicate combinations.
12+
13+
EXAMPLE :
14+
15+
Given array 2,3,6,7 and target 7,
16+
A solution set is:
17+
[2, 2, 3]
18+
19+
CONSTRAINTS:
20+
1 <= N <= 30
21+
1 <= A[i] <= 20
22+
1 <= B <= 100
23+
24+
SOLUTION APPROACH :
25+
26+
In every recursion run, you either include the element in the combination or you don’t.
27+
To account for multiple occurrences of an element,
28+
make sure you call the next function without incrementing the current index.
29+
30+
"""
31+
# SOLUTION IN PYTHON3
32+
33+
34+
class Solution:
35+
# @param A : list of integers
36+
# @param B : integer
37+
# @return a list of list of integers
38+
def combinationSum(self, A, B):
39+
# Remove duplicate and sort since each element can be repeated
40+
# in combination either way.
41+
# Removing duplicates here helps us avoid removing them from the final result.
42+
A=sorted(set(A))
43+
# if the target sum is less than 2 or the lis A is empty,
44+
# then retirn an empty list
45+
if B<2 or len(A)==0:
46+
return []
47+
48+
49+
result=[]
50+
comb=[]
51+
index=0
52+
self.find(result, comb, A, B, index)
53+
return result
54+
55+
def find(self, result, comb, A, B, index):
56+
# The recursion will nest until we hit the target. At this point we append and return.
57+
if sum(comb) == B:
58+
result.append(comb[:])
59+
return
60+
# If the sum is bigger, we also terminate the recursion chain.
61+
if sum(comb)>B:
62+
return
63+
64+
for i in range(index, len(A)):
65+
comb.append(A[i])
66+
# Start from same index since an element can be repeated.
67+
self.find(result, comb, A, B, i)
68+
comb.pop()
69+
70+
'''
71+
72+
Sample Input : A = [2,3,5], B = 8
73+
Output:
74+
[
75+
[2,2,2,2],
76+
[2,3,3],
77+
[3,5]
78+
]
79+
80+
EXPLANATION:
81+
All the unique combinations whose sum is 8 is printed.
82+
2+2+2+2 = 8
83+
2+3+3 = 8
84+
3+5 = 8
85+
86+
COMPLEXITY ANALYSIS :
87+
88+
Time Complexity -> O(l^k)
89+
Where l is the length of the array, k is the length of the longest possible combination (namely target / minInArray).
90+
91+
Space Complexity -> O(k)
92+
for storing the path, which could be k long at most.
93+
94+
'''

0 commit comments

Comments
 (0)