Skip to content

Commit a1ea3a0

Browse files
committed
leetcode 1372
1 parent ddd0d2a commit a1ea3a0

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int main()
4+
{
5+
int n;
6+
map<int, int> mp;
7+
cin>>n;
8+
9+
vector<int> arr(n);
10+
vector<vector<int>> ans;
11+
for (int i = 0; i < n; ++i)
12+
{
13+
cin>>arr[i];
14+
mp[arr[i]]++;
15+
}
16+
priority_queue<pair<int, int>> q;
17+
for (auto it : mp)
18+
{
19+
q.push({it.second, it.first});
20+
}
21+
22+
while (q.size() > 2)
23+
{
24+
vector<pair<int, int>> v(3);
25+
for (int i = 0; i < 3; ++i)
26+
{
27+
v[i] = q.top();
28+
q.pop();
29+
}
30+
vector<int> pos;
31+
for (int k = 0; k < 3; ++k)
32+
{
33+
pos.push_back(v[k].second);
34+
v[k].first--;
35+
if (v[k].first){
36+
q.push(v[k]);
37+
}
38+
}
39+
sort(pos.rbegin(),pos.rend());
40+
ans.push_back(pos);
41+
}
42+
cout << ans.size() << endl;
43+
for (int i = 0; i < ans.size(); ++i)
44+
{
45+
cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << endl;
46+
}
47+
return 0;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution
23+
{
24+
public:
25+
int longestZigZag(TreeNode *root)
26+
{
27+
// first -> right, second -> left
28+
queue<pair<TreeNode *, pair<int, int>>> q;
29+
q.push(make_pair(root, make_pair(0, 0))); // push root node with left and right directions as 0
30+
31+
int maxLen = 0;
32+
while (!q.empty())
33+
{
34+
TreeNode *node = q.front().first;
35+
int lenRight = q.front().second.first;
36+
int lenLeft = q.front().second.second;
37+
q.pop();
38+
39+
maxLen = max(maxLen, max(lenRight, lenLeft));
40+
41+
if (node->left)
42+
{
43+
q.push(make_pair(node->left, make_pair(0, lenRight + 1))); // push left child with left direction as 0 and right direction incremented by 1
44+
}
45+
if (node->right)
46+
{
47+
q.push(make_pair(node->right, make_pair(lenLeft + 1, 0))); // push right child with left direction incremented by 1 and right direction as 0
48+
}
49+
}
50+
51+
return maxLen;
52+
}
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
vector<bool> kidsWithCandies(vector<int> &candies, int extraCandies)
15+
{
16+
vector<bool> ans;
17+
int maxo=0;
18+
for(auto x:candies){
19+
maxo=max(maxo,x);
20+
}
21+
for(auto x:candies){
22+
if(x+extraCandies>=maxo){
23+
ans.push_back(true);
24+
}
25+
else{
26+
ans.push_back(false);
27+
}
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)