Skip to content

Commit 9c389a2

Browse files
Merge branch 'akkupy:main' into main
2 parents aac0cd2 + 0e68ebb commit 9c389a2

19 files changed

+633
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
l, r = 0, len(height)-1
4+
h = max(height)
5+
res = 0
6+
7+
while l < r:
8+
ar = min( [height[l] , height[r]] ) * (r-l)
9+
if ar > res:
10+
res = ar
11+
if height[l] < height[r]:
12+
l += 1
13+
else:
14+
r -= 1
15+
if (r - l) * h <= res:
16+
break
17+
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
// This is the solution for Find Median From Data Stream question from Leetcode
3+
// https://leetcode.com/problems/find-median-from-data-stream/description/
4+
5+
class MedianFinder {
6+
public:
7+
priority_queue<int> max_heap;
8+
priority_queue<int,vector<int>,greater<int>> min_heap;
9+
10+
MedianFinder() {
11+
12+
}
13+
14+
void addNum(int num) {
15+
if(max_heap.size() > 0 && num > max_heap.top())
16+
{
17+
min_heap.push(num);
18+
}
19+
else
20+
{
21+
max_heap.push(num);
22+
}
23+
if(max_heap.size() > min_heap.size()+1)
24+
{
25+
min_heap.push(max_heap.top());
26+
max_heap.pop();
27+
}
28+
if(min_heap.size() > max_heap.size()+1)
29+
{
30+
max_heap.push(min_heap.top());
31+
min_heap.pop();
32+
}
33+
}
34+
35+
double findMedian() {
36+
if(max_heap.size() == min_heap.size())
37+
{
38+
return (max_heap.top()+min_heap.top())/2.0;
39+
}
40+
if(max_heap.size() > min_heap.size())
41+
{
42+
return max_heap.top();
43+
}
44+
else
45+
{
46+
return min_heap.top();
47+
}
48+
}
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// This is the solution for Implement Queue Using Stack question from Leetcode
3+
// https://leetcode.com/problems/implement-queue-using-stacks/description/
4+
5+
class MyQueue {
6+
stack<int> st;
7+
public:
8+
MyQueue() {
9+
10+
}
11+
12+
void push(int x) {
13+
insertAtBottom(st, x);
14+
}
15+
16+
void insertAtBottom(stack<int> &st, int &val){
17+
if(st.empty()){
18+
st.push(val);
19+
return;
20+
}
21+
22+
int temp= st.top();
23+
st.pop();
24+
insertAtBottom(st, val);
25+
st.push(temp);
26+
}
27+
28+
int pop() {
29+
int val= st.top();
30+
st.pop();
31+
return val;
32+
}
33+
34+
int peek() {
35+
return st.top();
36+
}
37+
38+
bool empty() {
39+
return st.empty();
40+
}
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// This is the solution for Implement Stack Using Queue question from Leetcode
3+
// https://leetcode.com/problems/implement-stack-using-queues/description/
4+
5+
class MyStack {
6+
queue<int> q;
7+
public:
8+
MyStack() {
9+
10+
}
11+
12+
void push(int x) {
13+
int size= q.size();
14+
q.push(x);
15+
while(size!=0){
16+
int temp= q.front();
17+
q.pop();
18+
q.push(temp);
19+
size--;
20+
}
21+
}
22+
23+
int pop() {
24+
int val= q.front();
25+
q.pop();
26+
return val;
27+
}
28+
29+
int top() {
30+
return q.front();
31+
}
32+
33+
bool empty() {
34+
return q.empty();
35+
}
36+
};

LeetCodeQuestions/JumpGame.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
// This is the solution for Jump Game question from Leetcode
3+
// https://leetcode.com/problems/jump-game/description/
4+
5+
class Solution {
6+
public:
7+
bool canJump(vector<int>& nums) {
8+
int l= nums.size()-1;
9+
for(int i=l; i>=0; i--){
10+
if(i + nums[i]>=l){
11+
l=i;
12+
}
13+
}
14+
return l==0;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
// This is the solution for Merge Two Sorted LinkedList question from Leetcode
3+
// https://leetcode.com/problems/merge-two-sorted-lists/description/
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode() : val(0), next(nullptr) {}
11+
* ListNode(int x) : val(x), next(nullptr) {}
12+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
void change(ListNode* &prev, ListNode* &curr, ListNode* &newHead){
18+
if(prev==NULL){
19+
newHead= curr;
20+
prev= curr;
21+
curr= curr->next;
22+
}
23+
else{
24+
prev->next= curr;
25+
prev= curr;
26+
curr= curr->next;
27+
}
28+
prev->next= NULL;
29+
}
30+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
31+
32+
ListNode* prev= NULL;
33+
ListNode* newHead= NULL;
34+
while(list1!=NULL && list2!=NULL){
35+
if(list1->val<list2->val){
36+
change(prev, list1, newHead);
37+
}else{
38+
change(prev, list2, newHead);
39+
}
40+
}
41+
while(list1!=NULL){
42+
change(prev, list1, newHead);
43+
}
44+
while(list2!=NULL){
45+
change(prev, list2, newHead);
46+
}
47+
return newHead;
48+
}
49+
};

LeetCodeQuestions/MinStack.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
// This is the solution for Min Stack question from Leetcode
3+
// https://leetcode.com/problems/min-stack/description/
4+
5+
class MinStack {
6+
stack<long> st;
7+
int minElement= INT_MAX;
8+
public:
9+
MinStack() {}
10+
11+
void push(int val) {
12+
if(val< minElement){
13+
st.push(2ll*val - minElement);
14+
minElement= val;
15+
}else{
16+
st.push(val);
17+
}
18+
}
19+
20+
void pop() {
21+
22+
if(st.empty())
23+
return;
24+
25+
if(st.top()<minElement){
26+
minElement= 2ll*minElement - st.top();
27+
}
28+
st.pop();
29+
30+
}
31+
32+
int top() {
33+
if(st.top()<minElement){
34+
return minElement;
35+
}
36+
return st.top();
37+
}
38+
39+
int getMin() {
40+
return minElement;
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// This is the solution for Minimum Add to Make Parenthesis Valid question from Leetcode
3+
// https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
4+
5+
class Solution {
6+
public:
7+
int minAddToMakeValid(string s) {
8+
stack<char> st;
9+
for(int i=0;i<s.length();i++){
10+
if(st.empty()){
11+
st.push(s[i]);
12+
continue;
13+
}
14+
char t= st.top();
15+
if(t=='(' && s[i]==')')
16+
st.pop();
17+
else
18+
st.push(s[i]);
19+
}
20+
return st.size();
21+
}
22+
};

LeetCodeQuestions/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
# Leet Code Questions
1+
# LeetCode Questions
2+
3+
This folder contains solutions to various LeetCode coding problems. Each subfolder corresponds to a specific problem and contains the code solution along with explanations where necessary.
4+
5+
## Table of Contents
6+
7+
- [Problem 1: Title of Problem 1]
8+
- [Java Solution]
9+
- Explanation: Provide a brief explanation of the problem and your approach.
10+
11+
- [Problem 2: Title of Problem 2]
12+
- [Java Solution]
13+
- Explanation: Describe the problem and how you solved it.
14+
15+
<!-- Add more problems and solutions as needed -->
16+
17+
## Getting Started
18+
19+
You can clone this repository and navigate to the specific problem folder to view the code and explanations. Each problem folder may contain multiple solutions in different programming languages.
20+
21+
cd LeetCodeQuestions/Problem1/

LeetCodeQuestions/ReverseInteger.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is the solution for Revserse Integer question from Leetcode
2+
//https://leetcode.com/problems/reverse-integer/submissions/
3+
4+
class Solution {
5+
public:
6+
int reverse(int x) {
7+
int rv=0;
8+
while(x!=0){
9+
if(rv>(INT_MAX/10) || rv<(INT_MIN/10)){
10+
return 0;
11+
}
12+
rv=rv*10+x%10;
13+
x=x/10;
14+
}
15+
return rv;
16+
}
17+
};

0 commit comments

Comments
 (0)