File tree Expand file tree Collapse file tree 4 files changed +61
-1
lines changed
973.K-Closest-Points-to-Origin
974.Subarray-Sums-Divisible-by-K
976.Largest-Perimeter-Triangle Expand file tree Collapse file tree 4 files changed +61
-1
lines changed Original file line number Diff line number Diff line change
1
+
2
+ bool cmp (const vector<int > &p1, const vector<int > &p2){
3
+ return (p1[0 ]*p1[0 ] + p1[1 ]*p1[1 ]) < (p2[0 ]*p2[0 ] + p2[1 ]*p2[1 ]);
4
+ }
5
+
6
+ class Solution {
7
+ public:
8
+ vector<vector<int >> kClosest (vector<vector<int >>& points, int K) {
9
+ sort (points.begin (), points.end (), cmp);
10
+ return vector (points.begin (), points.begin ()+K);
11
+ }
12
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int subarraysDivByK (vector<int >& A, int K) {
4
+ int sum = 0 ;
5
+ int length = A.size ();
6
+ vector<int > mod (K,0 );
7
+ for (int i = 0 ; i < length; i++) {
8
+ sum += A[i];
9
+ mod[((sum % K) + K) % K]++; // as the sum can be negative, taking modulo twice
10
+ }
11
+
12
+ sum = 0 ;
13
+ for (int i = 0 ; i < K; i++) {
14
+ if (mod[i] > 1 ) {
15
+ sum += (mod[i] - 1 ) * mod[i] / 2 ;
16
+ }
17
+ }
18
+ sum += mod[0 ];
19
+ return sum;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ bool cmp (int a, int b){
2
+ return a > b;
3
+ }
4
+ class Solution {
5
+ public:
6
+ int largestPerimeter (vector<int >& A) {
7
+ sort (A.begin (), A.end (), cmp);
8
+ for (int i=0 ;i<A.size ();i++){
9
+ for (int j=i+1 ;j<A.size ();j++){
10
+ if (A[j] < A[i]/2 )
11
+ break ;
12
+ for (int k=j+1 ;k<A.size ();k++){
13
+ if (A[k] + A[j] > A[i])
14
+ return A[i] + A[j] + A[k];
15
+ break ;
16
+ }
17
+ }
18
+ }
19
+ return 0 ;
20
+ }
21
+ };
Original file line number Diff line number Diff line change 663
663
664
664
[ 958.Check-Completeness-of-a-Binary-Tree] ( Algorithms/958.Check-Completeness-of-a-Binary-Tree/solution.cpp )
665
665
666
- [ 959.Regions-Cut-By-Slashes] ( Algorithms/959.Regions-Cut-By-Slashes/solution.cpp )
666
+ [ 959.Regions-Cut-By-Slashes] ( Algorithms/959.Regions-Cut-By-Slashes/solution.cpp )
667
+
668
+ [ 973.K-Closest-Points-to-Origin] ( Algorithms/973.K-Closest-Points-to-Origin/solution.cpp )
669
+
670
+ [ 974.Subarray-Sums-Divisible-by-K] ( Algorithms/974.Subarray-Sums-Divisible-by-K/solution.cpp )
671
+
672
+ [ 976.Largest-Perimeter-Triangle] ( Algorithms/976.Largest-Perimeter-Triangle/solution.cpp )
You can’t perform that action at this time.
0 commit comments