Skip to content

Commit c6eb832

Browse files
committed
add 274,324,746,914,915
1 parent 60090e4 commit c6eb832

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

Algorithms/274.H-Index/solution.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
};

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@
429429

430430
[273.Integer-to-English-Words](Algorithms/273.Integer-to-English-Words/solution.cpp)
431431

432+
[274.H-Index](Algorithms/274.H-Index/solution.cpp)
433+
432434
[278.First-Bad-Version](Algorithms/278.First-Bad-Version/solution.cpp)
433435

434436
[283.Move-Zeroes](Algorithms/283.Move-Zeroes/solution.cpp)
@@ -453,6 +455,8 @@
453455

454456
[319.Bulb-Switcher](Algorithms/319.Bulb-Switcher/solution.cpp)
455457

458+
[324.Wiggle-Sort-II](Algorithms/324.Wiggle-Sort-II/solution.cpp)
459+
456460
[326.Power-of-Three](Algorithms/326.Power-of-Three/solution.cpp)
457461

458462
[328.Odd-Even-Linked-List](Algorithms/328.Odd-Even-Linked-List/solution.cpp)
@@ -579,6 +583,8 @@
579583

580584
[680.Valid-Palindrome-II](Algorithms/680.Valid-Palindrome-II/solution.cpp)
581585

586+
[746.Min-Cost-Climbing-Stairs](Algorithms/746.Min-Cost-Climbing-Stairs/solution.cpp)
587+
582588
[762.Prime-Number-of-Set-Bits-in-Binary-Representation](Algorithms/762.Prime-Number-of-Set-Bits-in-Binary-Representation/solution.cpp)
583589

584590
[824.Goat-Latin](Algorithms/824.Goat-Latin/solution.cpp)
@@ -589,6 +595,10 @@
589595

590596
[897.Increasing-Order-Search-Tree](Algorithms/897.Increasing-Order-Search-Tree/solution.cpp)
591597

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+
592602
[925.Long-Pressed-Name](Algorithms/925.Long-Pressed-Name/solution.cpp)
593603

594604
[926.Flip-String-to-Monotone-Increasing](Algorithms/926.Flip-String-to-Monotone-Increasing/solution.cpp)

0 commit comments

Comments
 (0)