-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstonegame.cpp
More file actions
48 lines (40 loc) · 727 Bytes
/
stonegame.cpp
File metadata and controls
48 lines (40 loc) · 727 Bytes
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
44
45
46
47
48
//question link
//https://leetcode.com/problems/stone-game/
#include <iostream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
bool cmp(int x, int y)
{
return x > y;
}
bool stoneGame(vector<int> &piles)
{
int n = piles.size();
sort(piles.begin(), piles.end(), cmp);
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n - 1; i++)
{
if (i % 2 == 0)
sum1 += piles[i];
else
{
sum2 += piles[i];
}
}
if (sum1 > sum2)
return true;
else
{
return false;
}
return false;
return true;
}
int main()
{
vector<int> t = {5, 3, 4, 5};
cout << stoneGame(t);
return 0;
}