Skip to content

Commit 4ac1090

Browse files
committed
leetcode weekly 326
1 parent 948cc77 commit 4ac1090

8 files changed

+318
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int n,m;
12+
cin>>n>>m;
13+
long long sum=0;
14+
// bucket
15+
priority_queue<int,vector<int>,greater<int>> pq;
16+
for(int i=0;i<n;i++){
17+
int temp;
18+
cin>>temp;
19+
pq.push(temp);
20+
}
21+
for(int i=0;i<m;i++){
22+
int temp;
23+
cin>>temp;
24+
pq.pop();
25+
pq.push(temp);
26+
}
27+
while(!pq.empty()){
28+
sum+=pq.top();
29+
pq.pop();
30+
}
31+
cout<<sum<<endl;
32+
return 0;
33+
}
34+
int main()
35+
{
36+
int testCase=1;
37+
cin>>testCase;
38+
while(testCase--){
39+
solve();
40+
}
41+
return 0;
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int n,k;
12+
cin>>n>>k;
13+
vector<int> v(n);
14+
int first=n,second=1;
15+
for(int i=0;i<n;i++){
16+
if(i&1){
17+
v[i]=second++;
18+
}
19+
else{
20+
v[i]=first--;
21+
}
22+
}
23+
for(int i=0;i<n;i++){
24+
cout<<v[i]<<" ";
25+
}
26+
cout<<endl;
27+
return 0;
28+
}
29+
int main()
30+
{
31+
int testCase=1;
32+
cin>>testCase;
33+
while(testCase--){
34+
solve();
35+
}
36+
return 0;
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
long long n;
12+
cin>>n;
13+
vector<long long> v(n);
14+
for(long long i=0;i<n;i++){
15+
cin>>v[i];
16+
}
17+
sort(v.begin(),v.end());
18+
// step 1
19+
for(long long i=0;i+1<n;i++){
20+
if(v[i]==v[i+1]){
21+
cout<<"NO"<<endl;
22+
return 0;
23+
}
24+
}
25+
// step 2
26+
for(int i=2;i<=n;i++){
27+
vector<int>count(i,0);
28+
for(int j=0;j<n;j++){
29+
count[v[j]%i]++;
30+
}
31+
if(*min_element(count.begin(),count.end())>=2){
32+
cout<<"NO"<<endl;
33+
return 0;
34+
}
35+
}
36+
37+
cout<<"YES"<<endl;
38+
return 0;
39+
}
40+
int main()
41+
{
42+
int testCase=1;
43+
cin>>testCase;
44+
while(testCase--){
45+
solve();
46+
}
47+
return 0;
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
int countDigits(int num)
15+
{
16+
int count=0;
17+
int temp=num;
18+
while(temp){
19+
int d=temp%10;
20+
if(!(num%d)){
21+
count++;
22+
}
23+
temp/=10;
24+
}
25+
return count;
26+
}
27+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
int distinctPrimeFactors(vector<int> &nums)
15+
{
16+
17+
vector<int> check(10001,0);
18+
vector<int> prime;
19+
set<int> s;
20+
21+
for(int i=2;i<=10000;i++){
22+
if (check[i] == 0)
23+
{
24+
prime.push_back(i);
25+
for(int j=i;j<=10000;j+=i){
26+
check[j]++;
27+
}
28+
}
29+
}
30+
// size of prime is 1229
31+
32+
for(auto x:nums){
33+
for(auto y:prime){
34+
if(x%y==0){
35+
s.insert(y);
36+
}
37+
}
38+
39+
}
40+
return s.size();
41+
}
42+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
string num_to_str(int n){
15+
string s="";
16+
while(n>0){
17+
s=char(n%10+'0')+s;
18+
n/=10;
19+
}
20+
return s;
21+
}
22+
int compareString(string s1,string s2){
23+
if(s1.size()>s2.size()){
24+
return 1;
25+
}
26+
else if(s1.size()<s2.size()){
27+
return -1;
28+
}
29+
else{
30+
for(int i=0;i<s1.size();i++){
31+
if(int(s1[i]-'0')>int(s2[i]-'0')){
32+
return 1;
33+
}
34+
else if(int(s1[i]-'0')<int(s2[i]-'0')){
35+
return -1;
36+
}
37+
}
38+
return 0;
39+
}
40+
}
41+
42+
int minimumPartition(string s, int k)
43+
{
44+
for(auto x:s){
45+
if(int(x-'0')>k){
46+
return -1;
47+
}
48+
}
49+
50+
string k1=num_to_str(k);
51+
int count=0;
52+
// as previous substring
53+
string temp="";
54+
55+
for(int i=0;i<s.size();i++){
56+
string temp2=temp+s[i];
57+
if(compareString(temp2,k1)==1){
58+
// no need to add in previous substring
59+
// one new substring will start
60+
if(temp.size()==0){
61+
return -1;
62+
}
63+
else{
64+
// cout<<"temp is "<<temp<<endl;
65+
count++;
66+
temp=s[i];
67+
}
68+
}
69+
else{
70+
if(temp.size()==0){
71+
count++;
72+
}
73+
temp=temp2;
74+
}
75+
76+
}
77+
// cout<<"temp is "<<temp<<endl;
78+
79+
return count;
80+
}
81+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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<int> closestPrimes(int left, int right)
15+
{
16+
vector<int> check(1e6+1,0);
17+
vector<int> prime;
18+
for(int i=2;i<=1e6;i++){
19+
if(check[i]==0){
20+
prime.push_back(i);
21+
for(int j=i;j<=1e6;j+=i){
22+
check[j]++;
23+
}
24+
}
25+
}
26+
// size of prime is 78498
27+
int mini=INT_MAX;
28+
int a=-1,b=-1;
29+
for(int i=0;i+1<prime.size();i++){
30+
if(prime[i]>=left && prime[i+1]<=right){
31+
if(prime[i+1]-prime[i]<mini){
32+
mini=prime[i+1]-prime[i];
33+
a=prime[i];
34+
b=prime[i+1];
35+
}
36+
}
37+
}
38+
return {a,b};
39+
}
40+
};

Template/cpp.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@
568568
"}",
569569
"int main()",
570570
"{",
571-
" long long testCase;",
571+
" int testCase=1;",
572572
" cin>>testCase;",
573573
" while(testCase--){",
574574
" solve();",

0 commit comments

Comments
 (0)