카테고리 없음
leetcode 1610 문제 풀이
벤치마킹
2022. 5. 1. 22:04
https://leetcode.com/problems/maximum-number-of-visible-points/
Maximum Number of Visible Points - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
아이디어: view point 에서 angle 각도를 구해서 정렬 한뒤에 sort -> 그위에 upper_bound 를 통해서 index 갯수를 전체 탐색
nlog(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #define PI 3.1415926535 using namespace std; double arr[200001]; class Solution { public: int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) { int felxAnswer = 0; int cnt = 0; for (auto &pos : points) { if (pos == location) { felxAnswer++; continue; } double tmp = getDegree(pos[0] - location[0], pos[1] - location[1]); arr[cnt++] = tmp; arr[cnt++] = tmp + 360.0f; } sort(arr, arr + cnt); int range = 0; int ret_max = 0; for (int i = 0; i < cnt; i++) { range = upper_bound(arr + i, arr + cnt, arr[i] + angle) - (arr + i); ret_max = range > ret_max ? range : ret_max; } return ret_max + felxAnswer; } double getDegree(int x, int y) { double ret = atan2(y, x) * 180 / PI; return ret; } }; | cs |