Merge branch '14_k_closest_points_to_origin'
This commit is contained in:
		
						commit
						3bc47dd607
					
				
							
								
								
									
										58
									
								
								14_k_closest_points_to_origin.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								14_k_closest_points_to_origin.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					/* Problem: https://leetcode.com/problems/k-closest-points-to-origin/ */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "lib_leetcode.h"
 | 
				
			||||||
 | 
					#include <algorithm>
 | 
				
			||||||
 | 
					#include <cmath>
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <ostream>
 | 
				
			||||||
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct coord_dist {
 | 
				
			||||||
 | 
					  int x;
 | 
				
			||||||
 | 
					  int y;
 | 
				
			||||||
 | 
					  double distance;
 | 
				
			||||||
 | 
					  bool operator<(const coord_dist &other) const {
 | 
				
			||||||
 | 
					    return distance < other.distance;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::ostream &operator<<(std::ostream &out, const coord_dist &cd) {
 | 
				
			||||||
 | 
					  out << "x: " << cd.x << "\ty: " << cd.y << "\tdistance: " << cd.distance;
 | 
				
			||||||
 | 
					  return out;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::ostream &operator<<(std::ostream &out, std::vector<coord_dist> &cd_vec) {
 | 
				
			||||||
 | 
					  if (!cd_vec.size()) {
 | 
				
			||||||
 | 
					    out << "{}";
 | 
				
			||||||
 | 
					    return out;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  out << "{\n";
 | 
				
			||||||
 | 
					  for (const coord_dist &cd : cd_vec) {
 | 
				
			||||||
 | 
					    out << '\t' << cd << ",\n";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  out << "\b\b\n}";
 | 
				
			||||||
 | 
					  return out;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::vector<std::vector<int>> solution(std::vector<std::vector<int>> &&points,
 | 
				
			||||||
 | 
					                                       int k) {
 | 
				
			||||||
 | 
					  std::vector<coord_dist> distances;
 | 
				
			||||||
 | 
					  for (const std::vector<int> &coordinates : points) {
 | 
				
			||||||
 | 
					    int x = coordinates[0], y = coordinates[1];
 | 
				
			||||||
 | 
					    double distance;
 | 
				
			||||||
 | 
					    distance = std::sqrt(x * x + y * y);
 | 
				
			||||||
 | 
					    distances.push_back({x, y, distance});
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  std::sort(distances.begin(), distances.end());
 | 
				
			||||||
 | 
					  // std::cout << distances << '\n';
 | 
				
			||||||
 | 
					  std::vector<std::vector<int>> kClosest;
 | 
				
			||||||
 | 
					  for (int i = 0; i < k; i++)
 | 
				
			||||||
 | 
					    kClosest.push_back({distances[i].x, distances[i].y});
 | 
				
			||||||
 | 
					  return kClosest;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main() {
 | 
				
			||||||
 | 
					  std::cout << solution({{1, 3}, {-2, 2}}, 1) << '\n'
 | 
				
			||||||
 | 
					            << solution({{3, 3}, {5, -1}, {-2, 4}}, 2) << '\n'
 | 
				
			||||||
 | 
					            << solution({{-5, 4}, {-6, -5}, {4, 6}}, 2) << '\n';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -30,3 +30,5 @@ add_executable(11_letter_case_permutation 11_letter_case_permutation.cpp)
 | 
				
			|||||||
add_executable(11_permutations_in_string 11_permutations_in_string.cpp)
 | 
					add_executable(11_permutations_in_string 11_permutations_in_string.cpp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
add_executable(12_bitonic_point 12_bitonic_point.cpp)
 | 
					add_executable(12_bitonic_point 12_bitonic_point.cpp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_executable(14_k_closest_points_to_origin 14_k_closest_points_to_origin.cpp)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user