Skip to content

Commit 16c55e8

Browse files
committed
leetcode weekly 322
1 parent 95e607e commit 16c55e8

3 files changed

+220
-0
lines changed

LeetCode/2490. Circular Sentence.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
7+
8+
/* ascii value
9+
A=65,Z=90,a=97,z=122
10+
*/
11+
12+
/* --------------------MAIN PROGRAM----------------------------*/
13+
// to run ctrl+b
14+
const ll INF=1e18;
15+
const ll mod1=1e9+7;
16+
const ll mod2=998244353;
17+
// Techniques :
18+
// divide into cases, brute force, pattern finding
19+
// sort, greedy, binary search, two pointer
20+
// transform into graph
21+
22+
// Experience :
23+
// Cp is nothing but only observation and mathematics.
24+
25+
26+
//Add main code here
27+
28+
class Solution
29+
{
30+
public:
31+
bool isCircularSentence(string sentence)
32+
{
33+
vector<string> v;
34+
string temp="";
35+
for (auto x : sentence)
36+
{
37+
if(x==' '){
38+
if(temp!=""){
39+
v.push_back(temp);
40+
temp="";
41+
}
42+
}
43+
else{
44+
temp+=x;
45+
}
46+
}
47+
if(temp!=""){
48+
v.push_back(temp);
49+
}
50+
// for(auto x:v){
51+
// cout<<"x is "<<x<<endl;
52+
// }
53+
int n=v.size();
54+
for(int i=0;i+1<n;i++){
55+
if(v[i+1][0]!=v[i][v[i].size()-1]){
56+
return false;
57+
}
58+
}
59+
if(v[0][0]!=v[n-1][v[n-1].size()-1]){
60+
return false;
61+
}
62+
return true;
63+
}
64+
};
65+
66+
/* -----------------END OF PROGRAM --------------------*/
67+
/*
68+
* stuff you should look before submission
69+
* constraint and time limit
70+
* int overflow
71+
* special test case (n=0||n=1||n=2)
72+
* don't get stuck on one approach if you get wrong answer
73+
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
7+
8+
/* ascii value
9+
A=65,Z=90,a=97,z=122
10+
*/
11+
12+
/* --------------------MAIN PROGRAM----------------------------*/
13+
// to run ctrl+b
14+
const ll INF=1e18;
15+
const ll mod1=1e9+7;
16+
const ll mod2=998244353;
17+
// Techniques :
18+
// divide into cases, brute force, pattern finding
19+
// sort, greedy, binary search, two pointer
20+
// transform into graph
21+
22+
// Experience :
23+
// Cp is nothing but only observation and mathematics.
24+
25+
26+
//Add main code here
27+
28+
class Solution
29+
{
30+
public:
31+
long long dividePlayers(vector<int> &skill)
32+
{
33+
map<int,int> m;
34+
long long totalSum=0;
35+
for(auto x:skill){
36+
m[x]++;
37+
totalSum+=x;
38+
}
39+
int n=skill.size();
40+
n/=2;
41+
if(totalSum%n){
42+
return -1;
43+
}
44+
long long sum=totalSum/n;
45+
46+
long long group=0,ans=0;
47+
for(auto x:skill){
48+
if(m[x]>0 && m[sum-x]>0){
49+
group++;
50+
ans+=(x*(sum-x));
51+
m[x]--;
52+
m[sum-x]--;
53+
}
54+
}
55+
if(group==n){
56+
return ans;
57+
}
58+
else{
59+
return -1;
60+
}
61+
}
62+
};
63+
64+
/* -----------------END OF PROGRAM --------------------*/
65+
/*
66+
* stuff you should look before submission
67+
* constraint and time limit
68+
* int overflow
69+
* special test case (n=0||n=1||n=2)
70+
* don't get stuck on one approach if you get wrong answer
71+
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
7+
8+
/* ascii value
9+
A=65,Z=90,a=97,z=122
10+
*/
11+
12+
/* --------------------MAIN PROGRAM----------------------------*/
13+
// to run ctrl+b
14+
const ll INF=1e18;
15+
const ll mod1=1e9+7;
16+
const ll mod2=998244353;
17+
// Techniques :
18+
// divide into cases, brute force, pattern finding
19+
// sort, greedy, binary search, two pointer
20+
// transform into graph
21+
22+
// Experience :
23+
// Cp is nothing but only observation and mathematics.
24+
25+
26+
//Add main code here
27+
28+
class Solution
29+
{
30+
public:
31+
int mini=INT_MAX;
32+
set<int> v;
33+
void dfs(int root,vector<vector<int>> &graph, vector<int> &visited){
34+
if(visited[root]==1)
35+
return;
36+
visited[root]=1;
37+
for(auto x:graph[root]){
38+
if(visited[x]==0){
39+
v.insert(x);
40+
dfs(x,graph,visited);
41+
}
42+
}
43+
return ;
44+
}
45+
46+
int minScore(int n, vector<vector<int>> &roads)
47+
{
48+
map<pair<int,int>,int> m;
49+
for(auto x:roads){
50+
m[{min(x[0],x[1]),max(x[0],x[1])}]=x[2];
51+
}
52+
vector<vector<int>> graph(n+1);
53+
for(auto x:roads){
54+
graph[x[0]].push_back(x[1]);
55+
graph[x[1]].push_back(x[0]);
56+
}
57+
vector<int> visited(n+1,0);
58+
dfs(1,graph,visited);
59+
for (auto y : m)
60+
{
61+
if(v.find(y.first.first)!=v.end() || v.find(y.first.second)!=v.end()){
62+
mini=min(mini,y.second);
63+
}
64+
}
65+
return mini;
66+
}
67+
};
68+
69+
/* -----------------END OF PROGRAM --------------------*/
70+
/*
71+
* stuff you should look before submission
72+
* constraint and time limit
73+
* int overflow
74+
* special test case (n=0||n=1||n=2)
75+
* don't get stuck on one approach if you get wrong answer
76+
*/

0 commit comments

Comments
 (0)