Skip to content

Commit ebbf31a

Browse files
committed
leetcode 71
1 parent c7d8cf9 commit ebbf31a

File tree

2 files changed

+71
-83
lines changed

2 files changed

+71
-83
lines changed

Codeforces/1452B-Toy_Blocks.cpp

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,32 @@
44
*/
55
#include <bits/stdc++.h>
66
using namespace std;
7-
typedef long long ll ;
8-
typedef vector<ll> vl;
9-
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
10-
// define values.
11-
// #define mod 1000000007
12-
#define phi 1.618
13-
/* Abbrevations */
14-
#define ff first
15-
#define ss second
16-
#define mp make_pair
17-
#define line cout<<endl;
18-
#define pb push_back
19-
#define Endl "\n"
20-
// loops
21-
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
22-
// Some print
23-
#define no cout<<"NO"<<endl;
24-
#define yes cout<<"YES"<<endl;
25-
#define cc ll test;cin>>test;while(test--)
26-
// sort
27-
#define all(V) (V).begin(),(V).end()
28-
#define srt(V) sort(all(V))
29-
#define srtGreat(V) sort(all(V),greater<ll>())
30-
// function
7+
typedef long long ll;
318

32-
ll power(ll x,ll y,ll mod)
9+
int solve()
3310
{
34-
ll res=1;
35-
// x=x%mod;
36-
while(y>0)
11+
int n;
12+
cin >> n;
13+
vector<int> v(n);
14+
int maxo = 0;
15+
long long sum = 0;
16+
for (auto &x : v)
3717
{
38-
if(y%2==1)
39-
{
40-
res*=x;
41-
// res=res%mod;
42-
}
43-
y/=2; x*=x; // x=x%mod;
18+
cin >> x;
19+
maxo = max(maxo, x);
20+
sum += x;
4421
}
45-
return res;
46-
}
47-
// datatype definination
48-
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
49-
50-
/* ascii value
51-
A=65,Z=90,a=97,z=122
52-
*/
53-
/* -----------------------------------------------------------------------------------*/
54-
55-
56-
ll solve()
57-
{
58-
ll n;
59-
cin>>n;
60-
vl v(n);
61-
forin(v,n);
62-
srt(v);
63-
ll sum=0;
64-
for(ll i=0;i<n;i++)
65-
{
66-
sum+=v[i];
67-
}
68-
ll mini=LONG_MAX;
69-
if((v[(n-1)]*(n-1))==sum)
70-
{
71-
cout<<0<<endl;
72-
return 0;
73-
}
74-
ll maxo=v[n-1];
75-
ll temp_sum=max((maxo*(n-1)),(sum));
76-
if((temp_sum%(n-1))!=0)
77-
temp_sum+=(n-1);
78-
temp_sum/=(n-1);
79-
temp_sum*=(n-1);
80-
// cout<<"hi "<<endl;
81-
cout<<abs(temp_sum-sum)<<endl;
22+
long long k = max((long long)maxo, ((sum + n - 2) / (n - 1)));
23+
cout << (k * (n - 1)) - sum << endl;
8224
return 0;
8325
}
84-
8526
int main()
8627
{
87-
//freopen("input.txt"a, "r", stdin);
88-
pan;
89-
// solve();
90-
cc
28+
int testCase = 1;
29+
cin >> testCase;
30+
while (testCase--)
9131
{
9232
solve();
9333
}
9434
return 0;
95-
}
96-
97-
/* stuff you should look before submission
98-
* int overflow
99-
* special test case (n=0||n=1||n=2)
100-
* don't get stuck on one approach if you get wrong answer
101-
*/
35+
}

LeetCode/71.Simplify Path.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 simplifyPath(string path)
15+
{
16+
17+
stack<string> st;
18+
string res;
19+
20+
for (int i = 0; i < path.size(); ++i)
21+
{
22+
if (path[i] == '/')
23+
continue;
24+
string temp="";
25+
while (i < path.size() && path[i] != '/')
26+
{
27+
temp += path[i];
28+
++i;
29+
}
30+
if (temp == ".")
31+
continue;
32+
else if (temp == "..")
33+
{
34+
if (!st.empty())
35+
st.pop();
36+
}
37+
else{
38+
st.push(temp);
39+
}
40+
}
41+
42+
while (!st.empty())
43+
{
44+
res = "/" + st.top() + res;
45+
st.pop();
46+
}
47+
48+
if (res.size() == 0){
49+
return "/";
50+
}
51+
52+
return res;
53+
}
54+
};

0 commit comments

Comments
 (0)