Skip to content

Commit 0ddb15d

Browse files
committed
Good bye 2020
1 parent b15b883 commit 0ddb15d

37 files changed

+3225
-62
lines changed

Diff for: LeetCode/practice/152.Maximum_Product_Subarray.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
int maxo=nums[0],mini=nums[0];
5+
int ans=nums[0];
6+
for(int i=1;i<nums.size();i++)
7+
{
8+
int temp=nums[i];
9+
int temp_max=temp*maxo;
10+
int temp_min=temp*mini;
11+
maxo=max({temp,temp_min,temp_max});
12+
mini=min({temp,temp_min,temp_max});
13+
ans=max(ans,maxo);
14+
}
15+
return ans;
16+
}
17+
};

Diff for: LeetCode/practice/204-Count_Primes.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int countPrimes(int n) {
4+
int count=0;
5+
vector<int> prime(n+1,0);
6+
for(int i=2;i*i<n;i++)
7+
{
8+
if(prime[i]==0)
9+
{
10+
for(int j=i*i;j<n;j+=i)
11+
prime[j]=i;
12+
}
13+
}
14+
for(int i=2;i<n;i++)
15+
{
16+
if(prime[i]==0)
17+
{
18+
count++;
19+
}
20+
}
21+
return count;
22+
}
23+
};

Diff for: LeetCode/practice/221-Maximal_Square.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maximalSquare(vector<vector<char>>& matrix) {
4+
int m=matrix[0].size();
5+
int n=matrix.size();
6+
vector<vector<int>> dp(n+1,vector<int>(m+1,0));
7+
int ans=0;
8+
for(int i=1;i<=n;i++)
9+
{
10+
for(int j=1;j<=m;j++)
11+
{
12+
if(matrix[i-1][j-1]=='1')
13+
{
14+
dp[i][j]=1+min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]});
15+
ans=max(ans,dp[i][j]);
16+
}
17+
else
18+
dp[i][j]=0;
19+
}
20+
}
21+
return ans*ans;
22+
}
23+
};

Diff for: LeetCode/practice/263-Ugly_Number.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool isUgly(int num) {
4+
if(num==0)
5+
return false;
6+
while(num%2==0){
7+
num/=2;
8+
}
9+
while(num%3==0){
10+
num/=3;
11+
}
12+
while(num%5==0){
13+
num/=5;
14+
}
15+
if(num==1)
16+
return true;
17+
else
18+
return false;
19+
}
20+
};

Diff for: LeetCode/practice/264-Ugly_Number_II.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int nthUglyNumber(int n) {
4+
int i2=0,i3=0,i5=0;
5+
int a=2,b=3,c=5;
6+
vector<int> v(n);
7+
v[0]=1;
8+
int num=1;
9+
while(num<n)
10+
{
11+
int mini=min({a,b,c});
12+
v[num]=mini;
13+
num++;
14+
if(mini==a)
15+
{
16+
i2++;
17+
a=2*v[i2];
18+
}
19+
if(mini==b)
20+
{
21+
i3++;
22+
b=3*v[i3];
23+
}
24+
if(mini==c){
25+
i5++;
26+
c=5*v[i5];
27+
}
28+
}
29+
return v[n-1];
30+
}
31+
};

Diff for: LeetCode/practice/279-Perfect_Squares.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int numSquares(int n) {
4+
if(floor(sqrt(n))==ceil(sqrt(n)))
5+
return 1;
6+
while(n%4==0)
7+
{
8+
n/=4;
9+
}
10+
if(n%8==7)
11+
return 4;
12+
for(int i=1;i*i<=n;i++)
13+
{
14+
int temp=sqrt(n-(i*i));
15+
if((temp*temp)==(n-(i*i)))
16+
return 2;
17+
}
18+
return 3;
19+
}
20+
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int lengthOfLIS(vector<int>& nums) {
4+
int n=nums.size();
5+
vector<int> dp(n,1);
6+
for(int i=1;i<n;i++)
7+
{
8+
for(int j=0;j<i;j++)
9+
{
10+
if(nums[i]>nums[j]&&dp[j]<dp[i]+1)
11+
dp[i]=max(dp[i],dp[j]+1);
12+
}
13+
}
14+
return *max_element(dp.begin(),dp.end());
15+
}
16+
};
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class NumMatrix {
2+
vector<vector<int>> dp;
3+
public:
4+
NumMatrix(vector<vector<int>>& matrix) {
5+
if(matrix.size() == 0) return;
6+
int n=matrix.size();
7+
int m=matrix[0].size();
8+
dp=vector<vector<int>>(n+1,vector<int>(m+1,0));
9+
for(int i=1;i<=n;i++)
10+
{
11+
for(int j=1;j<=m;j++)
12+
{
13+
dp[i][j]=matrix[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
14+
15+
}
16+
}
17+
}
18+
int sumRegion(int row1, int col1, int row2, int col2) {
19+
// cout<<"row is "<<row1<<endl;
20+
return dp[row2+1][col2+1]-dp[row2+1][col1]-dp[row1][col2+1]+dp[row1][col1];
21+
}
22+
};
23+
24+
/**
25+
* Your NumMatrix object will be instantiated and called as such:
26+
* NumMatrix* obj = new NumMatrix(matrix);
27+
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
28+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int maximumProduct(vector<int>& nums) {
4+
int n=nums.size();
5+
sort(nums.begin(),nums.end(),greater<int>());
6+
int ans=1;
7+
ans=(nums[0]*nums[1]*nums[2]);
8+
int temp=nums[n-1]*nums[n-2]*nums[0];
9+
ans=max(ans,temp);
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int findLengthOfLCIS(vector<int>& nums) {
4+
int count=0,maxo=1;
5+
int n=nums.size();
6+
if(n==1)
7+
return 1;
8+
if(n==0)
9+
return 0;
10+
count=1;
11+
int temp=nums[0];
12+
for(int i=1;i<n;i++)
13+
{
14+
if(nums[i]>temp)
15+
count++;
16+
else
17+
count=1;
18+
maxo=max(maxo,count);
19+
temp=nums[i];
20+
}
21+
return maxo;
22+
}
23+
};

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Contain file of my [competitive programming](https://en.wikipedia.org/wiki/Compe
77
- [Atcoder](https://atcoder.jp/home)
88
- [CSES](https://cses.fi/problemset/)
99
- [Facebook hacker cup](https://www.facebook.com/codingcompetitions/hacker-cup)
10+
- [open kattis](https://open.kattis.com/)
1011

1112

1213
Executable codes to contest problems in [c++](https://medium.com/sololearn/reasons-to-love-c-11c7c2f23d88)

Diff for: codechef/practice/ARRGAME.cpp

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
#include <ext/pb_ds/assoc_container.hpp>
7+
#include <ext/pb_ds/tree_policy.hpp>
8+
using namespace std;
9+
using namespace __gnu_pbds;
10+
typedef long long ll ;
11+
typedef vector<ll> vl;
12+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
13+
// define values.
14+
// #define mod 1000000007
15+
#define phi 1.618
16+
/* Abbrevations */
17+
#define ff first
18+
#define ss second
19+
#define mp make_pair
20+
#define line cout<<endl;
21+
#define pb push_back
22+
#define Endl "\n"
23+
// loops
24+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
25+
// Some print
26+
#define no cout<<"No"<<endl;
27+
#define yes cout<<"Yes"<<endl;
28+
// sort
29+
#define all(V) (V).begin(),(V).end()
30+
#define srt(V) sort(all(V))
31+
#define srtGreat(V) sort(all(V),greater<ll>())
32+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
33+
// some extra
34+
#define sz(V) ll(V.size())
35+
/* ONLINE JUDGE */
36+
// #ifdef ONLINE_JUDGE
37+
// freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
38+
// #endif
39+
// template
40+
template <typename T>
41+
T mymax(T x,T y)
42+
{
43+
return (x>y)?x:y;
44+
}
45+
// function
46+
47+
ll power(ll x,ll y,ll mod)
48+
{
49+
ll res=1;
50+
// x=x%mod;
51+
while(y>0)
52+
{
53+
if(y%2==1)
54+
{
55+
res*=x;
56+
// res=res%mod;
57+
}
58+
y/=2; x*=x; // x=x%mod;
59+
}
60+
return res;
61+
}
62+
ll str_to_num(string s)
63+
{
64+
return stoi(s);
65+
}
66+
67+
string num_to_str(ll num)
68+
{
69+
return to_string(num);
70+
}
71+
// datatype definination
72+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
73+
74+
/* ascii value
75+
A=65,Z=90,a=97,z=122
76+
*/
77+
/* -----------------------------------------------------------------------------------*/
78+
79+
ll solve()
80+
{
81+
ll n;
82+
cin>>n;
83+
ll maxo=0;
84+
ll count=0;
85+
vl v(n);
86+
forin(v,n);
87+
vl ans;
88+
for(ll i=0;i<n;i++)
89+
{
90+
if(v[i]==0)
91+
count++;
92+
else
93+
{
94+
if(count>0)
95+
ans.pb(count);
96+
count=0;
97+
}
98+
}
99+
if(count>0)
100+
ans.pb(count);
101+
if(ans.size()==0)
102+
no
103+
else
104+
{
105+
srtGreat(ans);
106+
if(ans.size()==1)
107+
{
108+
if(ans[0]&1)
109+
yes
110+
else
111+
no
112+
}
113+
else
114+
{
115+
if(ans[0]&1&&(ans[0]+1)/2>ans[1])
116+
yes
117+
else
118+
no
119+
}
120+
}
121+
return 0;
122+
}
123+
124+
int main()
125+
{
126+
speed;
127+
// freopen("input.txt","r",stdin);
128+
// freopen("output.txt","w",stdout);
129+
ll TestCase=1;
130+
cin>>TestCase;
131+
while(TestCase--)
132+
{
133+
solve();
134+
}
135+
}
136+
137+
/* stuff you should look before submission
138+
* int overflow
139+
* special test case (n=0||n=1||n=2)
140+
* don't get stuck on one approach if you get wrong answer
141+
*/

0 commit comments

Comments
 (0)