Skip to content

Commit f871faa

Browse files
committed
Initial commit
1 parent 9228a7f commit f871faa

9 files changed

+226
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//log(n)
2+
class Solution {
3+
public:
4+
bool isMajorityElement(vector<int>& nums, int target)
5+
{
6+
if(nums[nums.size()/2]!=target)
7+
return false;
8+
int j=nums.size()-1,i=0,mid,start;
9+
while(i<j) //simulate lower_bound
10+
{
11+
mid=(i+j)>>1;
12+
if(nums[mid]>=target)
13+
j=mid;
14+
else
15+
i=mid+1;
16+
}
17+
start=i;
18+
i=start,j=nums.size()-1;
19+
while(i<j) //Simulate upper_bound
20+
{
21+
mid=(i+j)>>1;
22+
if(nums[mid]<=target)
23+
i=mid+1;
24+
else
25+
j=mid;
26+
}
27+
return (i==nums.size()-1?1:0)+i-start>nums.size()/2;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int minSwaps(vector<int>& data)
4+
{
5+
int total1s=0,start=0,count=0,result=INT_MAX;
6+
for(int &i:data)
7+
total1s+=i;
8+
for(int i=0;i<data.size();i++)
9+
{
10+
count+=data[i]==1;
11+
if(i-start+1>total1s)
12+
count-=data[start++]==1;
13+
result=min(result,total1s-count);
14+
}
15+
return result;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*Cases to consider "ab"->"ba" and "abc...z"-> "z,y,x....a"
2+
Key is that we always need a temporary character to transform other characters
3+
to their mapping which is not in the string */
4+
class Solution {
5+
public:
6+
bool canConvert(string str1, string str2)
7+
{
8+
if(str1==str2) //Corner case
9+
return true;
10+
vector<char> map(26,'-');
11+
for(int i=0;i<str1.size();i++)
12+
{
13+
if(map[str1[i]-'a']!='-'&&map[str1[i]-'a']!=str2[i])//Already mapped but different character is always false
14+
return false;
15+
map[str1[i]-'a']=str2[i];
16+
}
17+
return unordered_set(str2.begin(),str2.end())<26;//Check if there is atleast 1 character that is not included and this can be used as temp to swap
18+
}
19+
};

1165-Single-Row Keyboard.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int calculateTime(string keyboard, string word)
4+
{
5+
vector<int> keyIndices(26,-1);
6+
for(int i=0;i<keyboard.length();i++)
7+
keyIndices[keyboard[i]-'a']=i;
8+
int start=0,result=0;
9+
for(char &c:word)
10+
result+=abs(start-keyIndices[c-'a']),start=keyIndices[c-'a'];
11+
return result;
12+
}
13+
};

1166-Design File System.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class FileSystem {
2+
public:
3+
FileSystem()
4+
{
5+
paths[""]=-1;
6+
}
7+
8+
bool create(string path, int value)
9+
{
10+
if(paths.count(path.substr(0,path.find_last_of('/'))))
11+
{
12+
paths[path]=value;
13+
return true;
14+
}
15+
return false;
16+
}
17+
18+
int get(string path)
19+
{
20+
it=paths.find(path);
21+
if(it!=paths.end())
22+
return it->second;
23+
return -1;
24+
}
25+
private:
26+
unordered_map<string,int> paths;
27+
unordered_map<string,int>::iterator it;
28+
string temp;
29+
};
30+
31+
/**
32+
* Your FileSystem object will be instantiated and called as such:
33+
* FileSystem* obj = new FileSystem();
34+
* bool param_1 = obj->create(path,value);
35+
* int param_2 = obj->get(path);
36+
*/
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int connectSticks(vector<int>& sticks)
4+
{
5+
int result=0,first,second;
6+
priority_queue<int,vector<int>,greater<int>> pq(sticks.begin(),sticks.end());
7+
while(pq.size()>1)
8+
{
9+
first=pq.top(),pq.pop();
10+
second=pq.top(),pq.pop();
11+
result+=first+second;
12+
pq.push(first+second);
13+
}
14+
return result;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Kruskal's MST, treat house 0(imaginary) which is the first well, and pipes 0 to each house is the well dig cost
2+
class Solution {
3+
public:
4+
vector<int> parent,Rank;
5+
int Find(int x)
6+
{
7+
return parent[x]==x?x:parent[x]=Find(parent[x]);
8+
}
9+
void Union(int x,int y)
10+
{
11+
int xset=Find(x),yset=Find(y);
12+
if(xset!=yset)
13+
{
14+
Rank[xset]<Rank[yset]?parent[xset]=yset:parent[yset]=xset;
15+
if(Rank[xset]==Rank[yset])
16+
Rank[xset]++;
17+
}
18+
}
19+
int minCostToSupplyWater(int n, vector<int>& wells, vector<vector<int>>& pipes)
20+
{
21+
Rank.resize(n+1,0),parent.resize(n+1);
22+
for(int i=0;i<parent.size();i++)
23+
parent[i]=i;
24+
for(int i=0;i<wells.size();i++)
25+
pipes.push_back({0,i+1,wells[i]});
26+
sort(pipes.begin(),pipes.end(),[](vector<int>&l,vector<int>&r){return l[2]<r[2];});
27+
int result=0;
28+
for(vector<int>&v:pipes)
29+
if(Find(v[0])!=Find(v[1]))
30+
{
31+
result+=v[2];
32+
Union(v[0],v[1]);
33+
}
34+
return result;
35+
}
36+
};
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class MedianFinder {
2+
public:
3+
/** initialize your data structure here. */
4+
MedianFinder() {
5+
}
6+
7+
void addNum(int num)
8+
{
9+
sorted.insert(num);
10+
if(sorted.size()==1)
11+
mid_1=sorted.begin();
12+
else if(num<*mid_1)
13+
{
14+
if(sorted.size()%2==0)
15+
mid_1--;
16+
}
17+
else if(sorted.size()&1)
18+
mid_1++;
19+
}
20+
21+
double findMedian()
22+
{
23+
return sorted.size()&1?*mid_1:((double)*(++(mid_2=mid_1))+*mid_1)/2;
24+
}
25+
private:
26+
multiset<int> sorted;
27+
multiset<int>::iterator mid_1,mid_2;
28+
};
29+
30+
/**
31+
* Your MedianFinder object will be instantiated and called as such:
32+
* MedianFinder* obj = new MedianFinder();
33+
* obj->addNum(num);
34+
* double param_2 = obj->findMedian();
35+
*/
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MedianFinder {
2+
public:
3+
/** initialize your data structure here. */
4+
priority_queue<int> left;
5+
priority_queue<int,vector<int>,greater<int>> right;
6+
void addNum(int num)
7+
{
8+
left.push(num);
9+
right.push(left.top()); //Balance eg, 1,2,3 (put to 3 right and get 2 put it to left)
10+
left.pop();
11+
if(left.size()<right.size())
12+
left.push(right.top()),right.pop();
13+
}
14+
double findMedian()
15+
{
16+
return (left.size()+right.size())&1?left.top():((double)left.top()+right.top())/2;
17+
}
18+
};
19+
20+
/**
21+
* Your MedianFinder object will be instantiated and called as such:
22+
* MedianFinder obj = new MedianFinder();
23+
* obj.addNum(num);
24+
* double param_2 = obj.findMedian();
25+
*/

0 commit comments

Comments
 (0)