Skip to content

Commit 7cedf72

Browse files
committed
Initial commit
1 parent 8368e8c commit 7cedf72

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

CodeForces/116A-Tram.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//author @Nishant
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
int main(){
7+
int n;
8+
cin >> n;
9+
int x, y, curr = 0, capacity = 0;
10+
for(int i=0; i<n; i++){
11+
cin >> x >> y;
12+
curr -= x;
13+
curr += y;
14+
capacity = max(capacity, curr);
15+
}
16+
cout << capacity;
17+
return 0;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
unordered_map<int, int> frstOccur;
5+
frstOccur[0] = 0;
6+
int ans = 0, pref = 0;
7+
for(int i=0; i<nums.size(); ++i){
8+
pref += nums[i] == 0 ? -1 : 1;
9+
auto it = frstOccur.find(pref);
10+
if(it != frstOccur.end()){
11+
ans = max(ans, i + 1 - frstOccur[pref]);
12+
}else{
13+
frstOccur[pref] = i + 1;
14+
}
15+
}
16+
return ans;
17+
}
18+
};

LeetCode/525-ContiguousArray.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
unordered_map<int, int> frstOccur;
5+
frstOccur[0] = 0;
6+
int ans = 0, pref = 0;
7+
for(int i=0; i<nums.size(); ++i){
8+
pref += nums[i] == 0 ? -1 : 1;
9+
auto it = frstOccur.find(pref);
10+
if(it != frstOccur.end()){
11+
ans = max(ans, i + 1 - frstOccur[pref]);
12+
}else{
13+
frstOccur[pref] = i + 1;
14+
}
15+
}
16+
return ans;
17+
}
18+
};

0 commit comments

Comments
 (0)