Skip to content

Commit f0e3190

Browse files
authored
K-Closest-Points-to-Origin in C++ (codedecks-in#131)
* Added solution in c++ for #973 * Added name and details in Contributors * Update README.md * Update README.md * Update README.md * Update README.md * Added Solution to 3 Sum problem in C++ * Update README.md * Delete 3sum.cpp * Update README.md
1 parent 413fd01 commit f0e3190

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Diff for: C++/k-closest-points-to-origin.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
class Solution {
10+
public:
11+
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
12+
vector<pair<double,int>> vec;
13+
for(int i=0;i<points.size();i++){//for loop for calculating distances and pushing it back in the vector
14+
double distance = std::sqrt(std::pow(points[i][0], 2)+std::pow(points[i][1], 2));
15+
vec.push_back(make_pair(distance,i));
16+
}
17+
sort(vec.begin(), vec.end());//sorting the vector according to distances
18+
vector<vector<int>> f;//final points vector
19+
for(int i=0;i<K;i++){
20+
f.push_back(points[vec[i].second]);//pushing in the points accordingly
21+
}
22+
return f;
23+
}
24+
};

Diff for: README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
9595
</div>
9696
<br/>
9797

98+
# Sort
99+
100+
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
101+
| ---- | --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
102+
| 973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [C++](./C++/k-closest-points-to-origin.cpp) | _O(n)_ | _O(1)_ | Medium | | |
103+
104+
<br/>
105+
<div align="right">
106+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
107+
</div>
108+
<br/>
109+
98110
# Array
99111

100112
| # | Title | Solution | Time | Space | Difficulty | Note | Video Explaination |
@@ -409,7 +421,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
409421

410422
## Authors
411423

412-
- | [Gourav Rusiya](https://github.com/GouravRusiya30/) <br> <img src="https://github.com/GouravRusiya30.png" width="100" height="100">
424+
* | [Gourav Rusiya](https://github.com/GouravRusiya30/) <br> <img src="https://github.com/GouravRusiya30.png" width="100" height="100">
413425

414426
<br>
415427

@@ -445,7 +457,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
445457
| [Saurav Prateek](https://github.com/SauravP97) <br> <img src="https://avatars3.githubusercontent.com/u/26816418?s=460&u=4b5222d5b6db79389efba062714c9dfba263732f&v=4" width="100" height="100"> | India | Java | [Github](https://github.com/SauravP97) <br> [Codechef](https://www.codechef.com/users/srvptk) <br> [Codeforces](https://codeforces.com/profile/srvptk97) <br> [Leetcode](https://leetcode.com/srvptk97) |
446458
| [Anushka Verma](https://github.com/verma-anushka) <br> <img src="https://avatars0.githubusercontent.com/u/46157435?s=400&u=aa3db9cf25b4a9644a16607d0c8bd6c98763a62d&v=4" width="100" height="100"> | India | C++ | [Portfolio](https://verma-anushka.github.io/anushkaverma/) <br> [LeetCode](https://leetcode.com/anushka_verma/) |
447459
| [James H](https://github.com/HoangJames) <br> <img src="https://avatars2.githubusercontent.com/u/15057250?s=460&v=4" width="100" height="100"> | United Kingdom | C++ | [Github](https://github.com/HoangJames) |
448-
460+
| [Franchis N. Saikia](https://github.com/Francode007) <br> <img src="https://avatars0.githubusercontent.com/u/63102937?s=400&v=4" width="100" height="100"> | India | C++ | [Github](https://github.com/Francode007) |
449461
<br/>
450462
<div align="right">
451463
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)