File tree Expand file tree Collapse file tree 6 files changed +126
-0
lines changed
746.Min-Cost-Climbing-Stairs
914.X-of-a-Kind-in-a-Deck-of-Cards
915.Partition-Array-into-Disjoint-Intervals Expand file tree Collapse file tree 6 files changed +126
-0
lines changed Original file line number Diff line number Diff line change
1
+ bool cmp (int i, int j){
2
+ return i>j;
3
+ }
4
+
5
+ class Solution {
6
+ public:
7
+ int hIndex (vector<int >& citations) {
8
+ sort (citations.begin (),citations.end (),cmp);
9
+ int i;
10
+ for (i=0 ;i<citations.size ();i++){
11
+ if (citations[i]<(i+1 ))
12
+ return i;
13
+ }
14
+ return i;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ void wiggleSort (vector<int >& nums) {
4
+ int n = nums.size ();
5
+
6
+ // Find a median.
7
+ auto midptr = nums.begin () + n / 2 ;
8
+ nth_element (nums.begin (), midptr, nums.end ());
9
+ int mid = *midptr;
10
+
11
+ // Index-rewiring.
12
+ #define A (i ) nums[(1 +2 *(i)) % (n|1 )]
13
+
14
+ // 3-way-partition-to-wiggly in O(n) time with O(1) space.
15
+ int i = 0 , j = 0 , k = n - 1 ;
16
+ while (j <= k) {
17
+ if (A (j) > mid)
18
+ swap (A (i++), A (j++));
19
+ else if (A (j) < mid)
20
+ swap (A (j), A (k--));
21
+ else
22
+ j++;
23
+ }
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minCostClimbingStairs (vector<int >& cost) {
4
+ vector<int > dp (cost.size (),0 );
5
+ int dp1 = cost[0 ];
6
+ int dp2 = cost[1 ];
7
+ int cur;
8
+ for (int i=2 ;i<cost.size ();i++){
9
+ cur = min (dp1,dp2) + cost[i];
10
+ dp1 = dp2;
11
+ dp2 = cur;
12
+ }
13
+
14
+ return min (dp1,dp2);
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int get_gcd (int a, int b) {
4
+ int h = max (a, b);
5
+ int l = min (a, b);
6
+
7
+ if (l == 0 ) return h;
8
+
9
+ int m = h % l;
10
+
11
+ while (m) {
12
+ h = l;
13
+ l = m;
14
+ m = h % l;
15
+ }
16
+ return l;
17
+ }
18
+
19
+ bool hasGroupsSizeX (vector<int >& deck) {
20
+ map<int , int > count;
21
+
22
+ for (auto i:deck){
23
+ if (count.find (i) != count.end ())
24
+ count[i] += 1 ;
25
+ else
26
+ count[i] = 1 ;
27
+ }
28
+
29
+ int min_gcd = count.begin ()->second ;
30
+ int last = min_gcd;
31
+ for (auto i=count.begin ();i!=count.end ();i++){
32
+ min_gcd = min (min_gcd, get_gcd (i->second ,last));
33
+ last = i->second ;
34
+ }
35
+ return min_gcd>=2 ;
36
+ }
37
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int partitionDisjoint (vector<int >& A) {
4
+ vector<int > max_left (A.size ());
5
+ vector<int > min_right (A.size ());
6
+ max_left[0 ] = A[0 ];
7
+ for (int i=1 ;i<max_left.size ();i++){
8
+ max_left[i] = max (A[i],max_left[i-1 ]);
9
+ }
10
+
11
+ min_right.back () = A.back ();
12
+ for (int i=min_right.size ()-2 ;i>=0 ;i--){
13
+ min_right[i] = min (A[i],min_right[i+1 ]);
14
+ }
15
+
16
+ for (int i=0 ;i<A.size ()-1 ;i++){
17
+ if (max_left[i]<=min_right[i+1 ])
18
+ return i+1 ;
19
+ }
20
+ return 0 ;
21
+ }
22
+ };
Original file line number Diff line number Diff line change 429
429
430
430
[ 273.Integer-to-English-Words] ( Algorithms/273.Integer-to-English-Words/solution.cpp )
431
431
432
+ [ 274.H-Index] ( Algorithms/274.H-Index/solution.cpp )
433
+
432
434
[ 278.First-Bad-Version] ( Algorithms/278.First-Bad-Version/solution.cpp )
433
435
434
436
[ 283.Move-Zeroes] ( Algorithms/283.Move-Zeroes/solution.cpp )
453
455
454
456
[ 319.Bulb-Switcher] ( Algorithms/319.Bulb-Switcher/solution.cpp )
455
457
458
+ [ 324.Wiggle-Sort-II] ( Algorithms/324.Wiggle-Sort-II/solution.cpp )
459
+
456
460
[ 326.Power-of-Three] ( Algorithms/326.Power-of-Three/solution.cpp )
457
461
458
462
[ 328.Odd-Even-Linked-List] ( Algorithms/328.Odd-Even-Linked-List/solution.cpp )
579
583
580
584
[ 680.Valid-Palindrome-II] ( Algorithms/680.Valid-Palindrome-II/solution.cpp )
581
585
586
+ [ 746.Min-Cost-Climbing-Stairs] ( Algorithms/746.Min-Cost-Climbing-Stairs/solution.cpp )
587
+
582
588
[ 762.Prime-Number-of-Set-Bits-in-Binary-Representation] ( Algorithms/762.Prime-Number-of-Set-Bits-in-Binary-Representation/solution.cpp )
583
589
584
590
[ 824.Goat-Latin] ( Algorithms/824.Goat-Latin/solution.cpp )
589
595
590
596
[ 897.Increasing-Order-Search-Tree] ( Algorithms/897.Increasing-Order-Search-Tree/solution.cpp )
591
597
598
+ [ 914.X-of-a-Kind-in-a-Deck-of-Cards] ( Algorithms/914.X-of-a-Kind-in-a-Deck-of-Cards/solution.cpp )
599
+
600
+ [ 915.Partition-Array-into-Disjoint-Intervals] ( Algorithms/915.Partition-Array-into-Disjoint-Intervals/solution.cpp )
601
+
592
602
[ 925.Long-Pressed-Name] ( Algorithms/925.Long-Pressed-Name/solution.cpp )
593
603
594
604
[ 926.Flip-String-to-Monotone-Increasing] ( Algorithms/926.Flip-String-to-Monotone-Increasing/solution.cpp )
You can’t perform that action at this time.
0 commit comments