mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day 2 and 3
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
class Solution{
|
||||
public:
|
||||
int maxArea(vector<int> &height){
|
||||
int maxArea = 0;
|
||||
int left = 0;
|
||||
int right = height.size()-1;
|
||||
while(left<right){
|
||||
maxArea = max(maxArea, (right-left)*min(height[left], height[left]));
|
||||
if(height[left]<height[right])
|
||||
left++;
|
||||
else
|
||||
right--;;
|
||||
}
|
||||
return maxArea;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
My initial intuition was to find the largest element and expand on either side of it to find the next farthest and largest element. But as it turns out, answer doesnt depend on largest element at all.
|
||||
|
||||
The update part for left and right depend on the fact that there is a "possibility" that we will get a bigger area with the taller height, it is important to assert that there cannot be a bigger area in between the two points that contains the lower pointer's point. This fact is what allows us to "ignore" any other combination with the lower point and shift the lower pointer closer to the middle.
|
||||
@@ -0,0 +1,4 @@
|
||||
>for(const auto& [value,symbol] : valueSymbols)
|
||||
|
||||
here [value,symbol] in cpp is called structured binding that allows you to unpack the elements of a compound object (such as a std::pair, std::tuple, or struct) into separate, named variables.
|
||||
Each element of valueSymbols is a pair<int, string> representing a Roman numeral value and its symbol. The syntax for (const auto& [value, symbol] : valueSymbols) unpackseach pair so that value refers to the numeric part and symbol refers to the Roman string
|
||||
@@ -0,0 +1,17 @@
|
||||
class Solution{
|
||||
public:
|
||||
int romanToInt(string s){
|
||||
unordered_map<char,int> convert = {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
|
||||
int result = 0;
|
||||
for(int i=0;i<s.size();++i){
|
||||
int curr = convert[s[i]];
|
||||
int next = ((i+1) < s.size())? convert[s[i+1]] : 0;
|
||||
if(curr<next){
|
||||
result-=curr;
|
||||
}else{
|
||||
result+=curr;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
class Solution{
|
||||
public:
|
||||
string LongestPalindrome(string s){
|
||||
for(int length= s.size();i>0;i--){
|
||||
for(int start = 0;start<=s.size()-length;start++){
|
||||
if(check(s,start,start+length))
|
||||
return s.substr(start,length);
|
||||
}
|
||||
}
|
||||
}
|
||||
private: bool check(string s, int i, int j){
|
||||
int left = i;
|
||||
int right = j-1;
|
||||
while(left<right){
|
||||
if(s[left]!=S[right])
|
||||
return false;
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
class Solution{
|
||||
public:
|
||||
string LongestPalindrome(string s){
|
||||
if(s.empty()) return "";
|
||||
string t = "#";
|
||||
for(char c : s){
|
||||
t+=c;
|
||||
t+="#";
|
||||
}
|
||||
int n = t.size();
|
||||
vector<int> p(n,0);
|
||||
int C =0;
|
||||
int R =0;
|
||||
|
||||
for(int i=0;i<n;i++){
|
||||
int mirror = 2*C-i;
|
||||
|
||||
if(i<R){
|
||||
p[i] = min(R-i, p[mirror]);
|
||||
}
|
||||
while(i+p[i]+1<n && i-p[i]-1>=0 && t[i+p[i]+1] == t[i-p[i]-1]){
|
||||
p[i]++;
|
||||
}
|
||||
if(i+p[i]>R){
|
||||
C = i;
|
||||
R = i+p[i];
|
||||
}
|
||||
|
||||
}
|
||||
int maxLen = 0;
|
||||
int centerIndex = 0;
|
||||
for(int i =0;i<n;i++){
|
||||
if(p[i]>maxLen){
|
||||
maxLen = p[i];
|
||||
centerIndex = i;
|
||||
}
|
||||
}
|
||||
int start = (centerIndex-maxLen)/2;
|
||||
return s.substr(start, maxLen);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
We can use 2D array instead of vector of vectors but prefer vector<vector<char>> when
|
||||
It has to grow dynamically
|
||||
Different rows have different lengths
|
||||
To save memory
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
class Solution{
|
||||
public:
|
||||
string convert(string s,int numrows){
|
||||
if(numrows == 0|| numsrows>=s.length())
|
||||
return s;
|
||||
vector<vector<char>> rows(numrows);
|
||||
int index = 0, direc = 1;
|
||||
|
||||
for(char c: s){
|
||||
rows[index].push_back(c);
|
||||
if(index == 0)
|
||||
direc =1;
|
||||
else if(index == numrows -1)
|
||||
direc = -1;
|
||||
index+=direc;
|
||||
}
|
||||
string res;
|
||||
for(const auto &row : rows)
|
||||
for(char c : row)
|
||||
res+=c;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
class Solution {
|
||||
public:
|
||||
int reverse(int x) {
|
||||
int rev =0;
|
||||
while(x!=0){
|
||||
int digit = x%10;
|
||||
x/=10;
|
||||
if(rev>INT_MAX/10 || rev==INT_MAX/10 && digit > 7){
|
||||
return 0;
|
||||
}
|
||||
if(rev<INT_MIN/10 || rev==INT_MIN/10 && digit < -8){
|
||||
return 0;
|
||||
}
|
||||
rev = rev*10 + digit;
|
||||
|
||||
}
|
||||
return rev;
|
||||
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
INT_MAX, INT_MIN are called integer limit macros or more generally type limit constants.(compile-time constants)
|
||||
defined in #include <climits>
|
||||
|
||||
Modern Alternative:
|
||||
|
||||
#include <limits>
|
||||
|
||||
numeric_limits<int>::max();
|
||||
numeric_limits<int>::min();
|
||||
|
||||
int maxInt = std::numeric_limits<int>::max();
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
class Solution {
|
||||
public:
|
||||
int myAtoi(string s) {
|
||||
if(s.empty()) return 0;
|
||||
|
||||
const long long MAX_INT = INT_MAX;
|
||||
const long long MIN_INT = INT_MIN;
|
||||
|
||||
int n = s.length();
|
||||
int i = 0;
|
||||
while(i<n && s[i]==' ')
|
||||
i++;
|
||||
if(i==n)
|
||||
return 0;
|
||||
int sign = 1;
|
||||
if(s[i]=='+')
|
||||
i++;
|
||||
else if(s[i]== '-'){
|
||||
sign = -1;
|
||||
i++;
|
||||
}
|
||||
|
||||
long long res= 0;
|
||||
while(i<n && isdigit(s[i])){
|
||||
int digit = s[i] - '0';
|
||||
res = res*10 + digit;
|
||||
|
||||
if(sign*res >= MAX_INT)
|
||||
return MAX_INT;
|
||||
if(sign*res <= MIN_INT)
|
||||
return MIN_INT;
|
||||
i++;
|
||||
}
|
||||
return sign*res;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -9,6 +9,6 @@ class Solution{
|
||||
rev = (rev*10) + (x%10);
|
||||
x/=10;
|
||||
}
|
||||
return ( x==rev || x = rev/10);
|
||||
return ( x==rev || x == rev/10);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user