Skip to content

Commit 9dad890

Browse files
committed
leetcode weekly 318
1 parent 97318e3 commit 9dad890

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
vector<int> applyOperations(vector<int> &nums)
32+
{
33+
int n=nums.size();
34+
vector<int> ans(n,0);
35+
int pos=0;
36+
for(int i=0;i<n;i++){
37+
if(nums[i]){
38+
if(i+1<n && nums[i]==nums[i+1]){
39+
ans[pos++]=nums[i]*2;
40+
i++;
41+
}
42+
else{
43+
ans[pos++]=nums[i];
44+
}
45+
}
46+
}
47+
return ans;
48+
}
49+
};
50+
51+
/* -----------------END OF PROGRAM --------------------*/
52+
/*
53+
* stuff you should look before submission
54+
* constraint and time limit
55+
* int overflow
56+
* special test case (n=0||n=1||n=2)
57+
* don't get stuck on one approach if you get wrong answer
58+
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 maximumSubarraySum(vector<int> &nums, int k)
32+
{
33+
int n=nums.size();
34+
long long sum=0,maxo=0;
35+
36+
map<int,int> check;
37+
for(int i=0;i<k-1;i++){
38+
sum+=nums[i];
39+
check[nums[i]]++;
40+
}
41+
for(int i=k-1;i<n;i++){
42+
check[nums[i]]++;
43+
sum+=nums[i];
44+
if(check.size()==k){
45+
maxo=max(maxo,sum);
46+
}
47+
if(check[nums[i-k+1]]==1){
48+
check.erase(nums[i-k+1]);
49+
}
50+
else{
51+
check[nums[i-k+1]]--;
52+
}
53+
sum-=nums[i-k+1];
54+
}
55+
return maxo;
56+
}
57+
};
58+
59+
/* -----------------END OF PROGRAM --------------------*/
60+
/*
61+
* stuff you should look before submission
62+
* constraint and time limit
63+
* int overflow
64+
* special test case (n=0||n=1||n=2)
65+
* don't get stuck on one approach if you get wrong answer
66+
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
class Solution
28+
{
29+
public:
30+
long long totalCost(vector<int> &costs, int k, int candidates)
31+
{
32+
int n = costs.size();
33+
34+
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
35+
vector<bool> check(n,false);
36+
37+
int s=0,e=n-1;
38+
for(int i=0;i<candidates;i++)
39+
{
40+
if(s<=e){
41+
pq.push({costs[i],i});
42+
s=i+1;
43+
}
44+
}
45+
for(int i=n-1;i>=n-candidates;i--)
46+
{
47+
if(s<=e){
48+
pq.push({costs[i],i});
49+
e=i-1;
50+
}
51+
}
52+
53+
long long ans=0;
54+
for(int i=0;i<k;i++){
55+
auto temp1=pq.top();
56+
ans+=temp1.first;
57+
pq.pop();
58+
check[temp1.second]=true;
59+
if(temp1.second<s && s<=e){
60+
pq.push({costs[s],s});
61+
s++;
62+
}
63+
else if(temp1.second>e && s<=e){
64+
pq.push({costs[e],e});
65+
e--;
66+
}
67+
}
68+
return ans;
69+
}
70+
};
71+
72+
73+
/* -----------------END OF PROGRAM --------------------*/
74+
/*
75+
* stuff you should look before submission
76+
* constraint and time limit
77+
* int overflow
78+
* special test case (n=0||n=1||n=2)
79+
* don't get stuck on one approach if you get wrong answer
80+
*/

0 commit comments

Comments
 (0)