Skip to content

Commit 69a007a

Browse files
authored
added codes
1 parent a72d1e7 commit 69a007a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3606
-0
lines changed

130

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
void dfs(vector<vector<char>>& board, int x, int y, int m, int n){
3+
if(x<0 || x>=m || y<0 || y>=n || board[x][y]!='O') return;
4+
board[x][y]='#';
5+
dfs(board, x+1, y, m, n);
6+
dfs(board, x-1, y, m, n);
7+
dfs(board, x, y+1, m, n);
8+
dfs(board, x, y-1, m, n);
9+
}
10+
public:
11+
void solve(vector<vector<char>>& board) {
12+
int m = board.size(), n=board[0].size();
13+
if(m==0) return;
14+
for(int i=0;i<m;i++){
15+
//left boundary
16+
if(board[i][0]=='O') dfs(board, i, 0, m, n);
17+
//right boundary
18+
if(board[i][n-1]=='O') dfs(board, i, n-1, m, n);
19+
}
20+
21+
for(int i=0;i<n;i++){
22+
if(board[0][i]=='O') dfs(board, 0, i, m, n);
23+
if(board[m-1][i]=='O') dfs(board, m-1, i, m, n);
24+
}
25+
26+
for(int i=0;i<m;i++){
27+
for(int j=0;j<n;j++){
28+
if(board[i][j]=='O')
29+
board[i][j]='X';
30+
if(board[i][j]=='#')
31+
board[i][j]='O';
32+
}
33+
}
34+
}
35+
};

135

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int candy(vector<int> &A) {
4+
vector<int>candy(A.size(),1);
5+
int n=A.size();
6+
7+
if(n==1)
8+
return 1;
9+
10+
// left to right traverse
11+
for(int i=1;i<n;i++){
12+
if(A[i-1]<A[i] && candy[i-1]>=candy[i])
13+
candy[i]=candy[i-1]+1;
14+
15+
}
16+
// right to left
17+
18+
for(int i=n-2;i>=0;i--){
19+
if(A[i]>A[i+1] && candy[i+1]>=candy[i])
20+
candy[i]=candy[i+1]+1;
21+
}
22+
23+
int sum=0;
24+
for(int i=0;i<n;i++)
25+
sum+=candy[i];
26+
27+
return sum;
28+
29+
30+
}
31+
};

141

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
bool hasCycle(ListNode *head) {
12+
if(head==NULL)
13+
return false;
14+
ListNode* slow=head;
15+
ListNode* fast=head->next;
16+
17+
if(head->next==NULL)
18+
return false;
19+
while(slow!=fast){
20+
if(slow==NULL || fast==NULL)
21+
return false;
22+
23+
slow=slow->next;
24+
fast=fast->next;
25+
if(fast==NULL)
26+
return false;
27+
fast=fast->next;
28+
if(slow==NULL || fast==NULL)
29+
return false;
30+
}
31+
return true;
32+
33+
}
34+
};

143

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
void reorderList(ListNode* head) {
16+
if (head == NULL || head->next == NULL) // Handling cases where list is empty or has only one element
17+
return;
18+
19+
vector<int> v;
20+
ListNode* p = head;
21+
22+
// Store values of nodes in the list
23+
while (p != NULL) {
24+
v.push_back(p->val);
25+
p = p->next;
26+
}
27+
28+
p = head;
29+
int i = 0;
30+
int j = v.size() - 1;
31+
32+
while (i <= j) {
33+
p->val = v[i];
34+
p = p->next;
35+
if (i != j) { // Avoid accessing same element twice if list has odd number of elements
36+
p->val = v[j];
37+
p = p->next;
38+
}
39+
i++;
40+
j--;
41+
}
42+
}
43+
};
44+
45+
// class Solution {
46+
// public:
47+
// void reorderList(ListNode* head) {
48+
// ListNode* p=head;
49+
// vector<int>v;
50+
51+
// while(p!=NULL){
52+
// v.push_back(p->val);
53+
// p=p->next;
54+
// }
55+
// if(v.size()%2==0){
56+
// int i=0;
57+
// ListNode* q=head;
58+
// while(q!=NULL){
59+
// q->val=v[i];
60+
// q=q->next;
61+
// // int r=abs(v.size()-i);
62+
// q->val=v[v.size()-i];
63+
// q=q->next;
64+
// i++;
65+
// }
66+
// }
67+
// else{
68+
// int i=0;
69+
// ListNode* q=head;
70+
// while(q->next!=NULL){
71+
// q->val=v[i];
72+
// q=q->next;
73+
// q->val=v[v.size()-i];
74+
// q=q->next;
75+
// i++;
76+
// }
77+
// q->val=v[v.size()/2];
78+
// }
79+
80+
// }
81+
// };

144'

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
void f(TreeNode* root,vector<int>&v){
15+
if(root==NULL)
16+
return;
17+
v.push_back(root->val);
18+
f(root->left,v);
19+
f(root->right,v);
20+
21+
}
22+
vector<int> preorderTraversal(TreeNode* root) {
23+
vector<int>ans;
24+
f(root,ans);
25+
return ans;
26+
27+
}
28+
};

145

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
15+
void f(TreeNode* root,vector<int>&v){
16+
if(root==NULL)
17+
return;
18+
f(root->left,v);
19+
f(root->right,v);
20+
v.push_back(root->val);
21+
22+
23+
}
24+
vector<int> postorderTraversal(TreeNode* root) {
25+
vector<int>ans;
26+
f(root,ans);
27+
return ans;
28+
29+
}
30+
31+
32+
};

149

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
4+
int maxPoints(vector<vector<int>>& points) {
5+
if (points.size() <= 1) {
6+
return points.size();
7+
}
8+
9+
int maxPointsOnLine = 2; // Minimum is 2 points on the same line
10+
11+
for (int i = 0; i < points.size(); i++) {
12+
unordered_map<double, int> slopeCount;
13+
14+
for (int j = 0; j < points.size(); j++) {
15+
if (i != j) {
16+
int x1 = points[i][0];
17+
int y1 = points[i][1];
18+
int x2 = points[j][0];
19+
int y2 = points[j][1];
20+
21+
double slope = (x2 - x1 == 0) ? numeric_limits<double>::infinity() : (double)(y2 - y1) / (x2 - x1);
22+
slopeCount[slope]++;
23+
maxPointsOnLine = max(maxPointsOnLine, slopeCount[slope]+1);
24+
}
25+
}
26+
}
27+
28+
return maxPointsOnLine;
29+
}
30+
};

150

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public:
3+
4+
int evalRPN(vector<string>& to) {
5+
stack<string>s;
6+
7+
8+
for(int i=0;i<to.size();i++){
9+
if(to[i]=="+" || to[i]=="-"|| to[i]=="*" || to[i]=="/"){
10+
string a=s.top();
11+
s.pop();
12+
string b=s.top();
13+
s.pop();
14+
15+
int a1=stoi(a);
16+
int b1=stoi(b);
17+
18+
if(to[i]=="-"){
19+
int r=b1-a1;
20+
s.push(to_string(r));
21+
}
22+
if(to[i]=="+"){
23+
int r=a1+b1;
24+
s.push(to_string(r));
25+
26+
}
27+
if(to[i]=="*"){
28+
int r=a1*b1;
29+
s.push(to_string(r));
30+
31+
}
32+
if(to[i]=="/"){
33+
if(a1!=0){
34+
int r=b1/a1;
35+
s.push(to_string(r));
36+
}
37+
else
38+
return 0;
39+
40+
}
41+
42+
43+
}
44+
else{
45+
s.push(to[i]);
46+
}
47+
}
48+
49+
string p=s.top();
50+
return stoi(p);
51+
}
52+
};

154

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
int p=*min_element(nums.begin(),nums.end());
5+
return p;
6+
7+
}
8+
};

155

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class MinStack {
2+
public:
3+
4+
stack<int>st;
5+
vector<int>mm;
6+
int n=0,m=0;
7+
MinStack() {
8+
9+
}
10+
11+
void push(int val) {
12+
st.push(val);
13+
if(n!=0){
14+
val=min(val,mm[n-1]);
15+
}
16+
if(m==n){
17+
mm.push_back(val);
18+
m+=1;
19+
}else
20+
mm[n]=val;
21+
n+=1;
22+
}
23+
24+
void pop() {
25+
st.pop();
26+
n--;
27+
}
28+
29+
int top() {
30+
return st.top();
31+
32+
}
33+
34+
int getMin() {
35+
return mm[n-1];
36+
}
37+
};
38+
39+
/**
40+
* Your MinStack object will be instantiated and called as such:
41+
* MinStack* obj = new MinStack();
42+
* obj->push(val);
43+
* obj->pop();
44+
* int param_3 = obj->top();
45+
* int param_4 = obj->getMin();
46+
*/

0 commit comments

Comments
 (0)