Skip to content

Commit 9a18785

Browse files
committed
Practise 18-Jul-2020
1 parent 963ef6f commit 9a18785

6 files changed

+389
-0
lines changed

array/connect_ropes_with_min_cost.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
There are given N ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
6+
7+
Output:
8+
For each testcase, print the minimum cost to connect all the ropes.
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ N ≤ 106
13+
1 ≤ L[i] ≤ 106
14+
15+
Example:
16+
Input:
17+
2
18+
4
19+
4 3 2 6
20+
5
21+
4 2 7 6 9
22+
23+
Output:
24+
29
25+
62
26+
27+
Explanation:
28+
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
29+
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
30+
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
31+
3) Finally connect the two ropes and all ropes have connected.
32+
33+
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38
34+
*/
35+
36+
37+
38+
39+
40+
41+
42+
#include<bits/stdc++.h>
43+
using namespace std;
44+
45+
int main(){
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL);
48+
cout.tie(NULL);
49+
int t;
50+
cin>>t;
51+
while(t--){
52+
int n;
53+
cin>>n;
54+
int a[n];
55+
for(int i=0;i<n;i++) cin>>a[i];
56+
57+
priority_queue<int, vector<int>, greater<int> > minh; // min heap
58+
59+
for(int i=0;i<n;i++)
60+
minh.push(a[i]);
61+
62+
long long cost=0;
63+
while(minh.size()>=2){
64+
int r1=minh.top();
65+
minh.pop();
66+
int r2=minh.top();
67+
minh.pop();
68+
cost+=r1+r2;
69+
minh.push(r1+r2);
70+
}
71+
72+
cout<<cost<<endl;
73+
}
74+
75+
return 0;
76+
}

array/k_closest_points_to_origin.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Given a list of points on the 2-D plane and an integer K. The task is to find K closest points to the origin and print them.
3+
4+
Note: The distance between two points on a plane is the Euclidean distance.
5+
6+
Examples:
7+
8+
Input : point = [[3, 3], [5, -1], [-2, 4]], K = 2
9+
Output : [[3, 3], [-2, 4]]
10+
Square of Distance of origin from this point is
11+
(3, 3) = 18
12+
(5, -1) = 26
13+
(-2, 4) = 20
14+
So rhe closest two points are [3, 3], [-2, 4].
15+
16+
Input : point = [[1, 3], [-2, 2]], K = 1
17+
Output : [[-2, 2]]
18+
Square of Distance of origin from this point is
19+
(1, 3) = 10
20+
(-2, 2) = 8
21+
So the closest point to origin is (-2, 2)
22+
*/
23+
24+
25+
26+
27+
28+
#include<bits/stdc++.h>
29+
using namespace std;
30+
31+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
32+
int n=points.size();
33+
priority_queue<pair<int, pair<int, int> > > maxh; // max heap
34+
for(int i=0;i<n;i++){
35+
int x=points[i][0];
36+
int y=points[i][1];
37+
int dist=x*x+y*y;
38+
maxh.push({dist, {x, y} });
39+
if(maxh.size()>k) maxh.pop();
40+
}
41+
int i=0;
42+
vector<vector<int> > res;
43+
while(!maxh.empty()){
44+
pair<int, int> p=maxh.top().second;
45+
maxh.pop();
46+
int x=p.first;
47+
int y=p.second;
48+
res.push_back({x, y});
49+
}
50+
return res;
51+
}
52+
53+
int main(){
54+
int t;
55+
cin>>t;
56+
while(t--){
57+
int n;
58+
cin>>n;
59+
vector<vector<int> > points;
60+
for(int i=0;i<n;i++){
61+
int x,y;
62+
cin>>x>>y;
63+
points.push_back({x,y});
64+
}
65+
int k;
66+
cin>>k;
67+
vector<vector<int> > res;
68+
res=kClosest(points, k);
69+
for(int i=0; i<k;i++){
70+
cout<<res[i][0]<<" "<<res[i][1]<<endl;
71+
}
72+
}
73+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//*
2+
Given an array of positive integers and two positive integers K1 and K2. Find sum of all elements greater tha K1th and smaller than K2th smallest elements of array.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N, denoting the length of the array. Next line contains N space seperated integers of the array. Third line contains two space seperated integers denoting K1th and K2th smallest elements.
6+
7+
Output:
8+
For each test case, output the sum of all the elements between K1th and K2th smallest elements.
9+
10+
Constraints:
11+
1 <= T <= 100
12+
1 <= N <= 107
13+
1 <= K1, K2 <= 1014
14+
15+
Example:
16+
Input
17+
2
18+
7
19+
20 8 22 4 12 10 14
20+
3 6
21+
6
22+
10 2 50 12 48 13
23+
2 6
24+
25+
Output:
26+
26
27+
73
28+
29+
Explanation:
30+
Test Case 1:
31+
3rd smallest element is 10
32+
6th smallest element is 20
33+
Sum of all element between K1 & K2 is 12 + 14 = 26
34+
*/
35+
36+
37+
38+
39+
40+
41+
42+
#include<bits/stdc++.h>
43+
using namespace std;
44+
45+
46+
pair<long long, long long> getKthSmall(long long a[], long long n, long long k1, long long k2){
47+
priority_queue<int> maxh; // max heap
48+
long long k=max(k1, k2);
49+
for(long long i=0;i<n;i++){
50+
maxh.push(a[i]);
51+
if(maxh.size()>k) maxh.pop();
52+
}
53+
long long mink=min(k1, k2);
54+
long long lg=maxh.top(); // large
55+
while(maxh.size()>mink){
56+
maxh.pop();
57+
};
58+
long long sm=maxh.top(); // small
59+
60+
pair<long long, long long> res;
61+
res.first=sm;
62+
res.second=lg;
63+
return res;
64+
}
65+
66+
int main(){
67+
ios_base::sync_with_stdio(false);
68+
cin.tie(NULL);
69+
cout.tie(NULL);
70+
int t;
71+
cin>>t;
72+
while(t--){
73+
long long n, k1, k2;
74+
cin>>n;
75+
long long a[n];
76+
for(long long i=0;i<n;i++) cin>>a[i];
77+
cin>>k1>>k2;
78+
pair<long long, long long > sk=getKthSmall(a, n, k1, k2);
79+
long long sk1=sk.first; // small
80+
long long sk2=sk.second; // large
81+
long long sum=0;
82+
for(long long i=0;i<n;i++){
83+
if(a[i]>sk1 && a[i]<sk2){
84+
sum+=a[i];
85+
}
86+
}
87+
cout<<sum<<endl;
88+
}
89+
90+
return 0;
91+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
2+
3+
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
4+
5+
Example:
6+
7+
Input: citations = [0,1,3,5,6]
8+
Output: 3
9+
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
10+
received 0, 1, 3, 5, 6 citations respectively.
11+
Since the researcher has 3 papers with at least 3 citations each and the remaining
12+
two with no more than 3 citations each, her h-index is 3.
13+
Note:
14+
15+
If there are several possible values for h, the maximum one is taken as the h-index.
16+
17+
Follow up:
18+
19+
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
20+
Could you solve it in logarithmic time complexity?
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int hIndex(vector<int>& citations) {
33+
int n=citations.size();
34+
if(!n) return 0;
35+
int si=0, ei=n-1, mid;
36+
while(si<=ei){
37+
mid=si+(ei-si)/2;
38+
if(citations[mid]==n-mid) return citations[mid];
39+
else if(citations[mid]>n-mid) ei=mid-1;
40+
else si=mid+1;
41+
}
42+
return n-si;
43+
}
44+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
2+
3+
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
4+
5+
Example:
6+
7+
Input: citations = [0,1,3,5,6]
8+
Output: 3
9+
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
10+
received 0, 1, 3, 5, 6 citations respectively.
11+
Since the researcher has 3 papers with at least 3 citations each and the remaining
12+
two with no more than 3 citations each, her h-index is 3.
13+
Note:
14+
15+
If there are several possible values for h, the maximum one is taken as the h-index.
16+
17+
Follow up:
18+
19+
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
20+
Could you solve it in logarithmic time complexity?
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int hIndex(vector<int>& citations) {
33+
int n=citations.size();
34+
if(!n) return 0;
35+
int si=0, ei=n-1, mid;
36+
while(si<=ei){
37+
mid=si+(ei-si)/2;
38+
if(citations[mid]==n-mid) return citations[mid];
39+
else if(citations[mid]>n-mid) ei=mid-1;
40+
else si=mid+1;
41+
}
42+
return n-si;
43+
}
44+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
2+
3+
(Here, the distance between two points on a plane is the Euclidean distance.)
4+
5+
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
6+
7+
8+
9+
Example 1:
10+
11+
Input: points = [[1,3],[-2,2]], K = 1
12+
Output: [[-2,2]]
13+
Explanation:
14+
The distance between (1, 3) and the origin is sqrt(10).
15+
The distance between (-2, 2) and the origin is sqrt(8).
16+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
17+
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
18+
Example 2:
19+
20+
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
21+
Output: [[3,3],[-2,4]]
22+
(The answer [[-2,4],[3,3]] would also be accepted.)
23+
24+
25+
Note:
26+
27+
1 <= K <= points.length <= 10000
28+
-10000 < points[i][0] < 10000
29+
-10000 < points[i][1] < 10000
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
41+
int n=points.size();
42+
priority_queue<pair<int, pair<int, int> > > maxh; // max heap
43+
for(int i=0;i<n;i++){
44+
int x=points[i][0];
45+
int y=points[i][1];
46+
int dist=x*x+y*y;
47+
maxh.push({dist, {x, y} });
48+
if(maxh.size()>k) maxh.pop();
49+
}
50+
int i=0;
51+
vector<vector<int> > res;
52+
while(!maxh.empty()){
53+
pair<int, int> p=maxh.top().second;
54+
maxh.pop();
55+
int x=p.first;
56+
int y=p.second;
57+
res.push_back({x, y});
58+
}
59+
return res;
60+
}
61+
};

0 commit comments

Comments
 (0)