Skip to content

Added solution for week03 problems #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions week03/13-Roman-to-Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int romanToInt(string str) {

unordered_map<char, int> roman;
roman['I'] = 1;
roman['V'] = 5;
roman['X'] = 10;
roman['L'] = 50;
roman['C'] = 100;
roman['D'] = 500;
roman['M'] = 1000;

int res = 0, i;

for ( i = 0 ; i < str.length() - 1; i++){
if(roman[str[i]] < roman[str[i+1]]){
res -= roman[str[i]];
}
else{
res += roman[str[i]];
}
}
res += roman[str[i]];

return res;
}
};
43 changes: 43 additions & 0 deletions week03/146-LRU-Cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class LRUCache {
public:
int max_cap;
list<int>lru;
unordered_map<int, int> kv;
unordered_map<int, list<int>::iterator> key_address;

LRUCache(int capacity) {
max_cap = capacity;
}

int get(int key) {
if(kv.count(key) == 0) {
return -1;
}
updateLRU(key);
return kv[key];
}

void put(int key, int value) {
if(kv.size() == max_cap && kv.count(key) == 0){
evict();
}
updateLRU(key);
kv[key] = value;

}

void updateLRU(int key){
if(kv.count(key)) {
lru.erase(key_address[key]);
}
lru.push_front(key);
key_address[key] = lru.begin();

}

void evict() {
kv.erase(lru.back());
key_address.erase(lru.back());
lru.pop_back();
}
};
36 changes: 36 additions & 0 deletions week03/205-Isomorphic-String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

// Way - 01
class Solution {
public:
bool isIsomorphic(string s, string t, int iteration_count = 0) {
unordered_map<char, char> from;

for(int i = 0; i < s.size(); i++){
if (from.count(s[i]) && from[s[i]] != t[i] ) {
return false;
}
from[s[i]] = t[i];
}
if (iteration_count == 0) return isIsomorphic(t, s, 1);
return true;
}
};

// Way - 02

class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> from, to;

for(int i = 0; i < s.size(); i++){
if ((from.count(s[i]) && from[s[i]] != t[i] ) || (to.count(t[i]) && to[t[i]] != s[i] )) {
return false;
}
from[s[i]] = t[i];
to[t[i]] = s[i];
}
return true;
}
};

31 changes: 31 additions & 0 deletions week03/2103-Detect-Square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class DetectSquares {
public:
map<pair<int, int>, int> points;
DetectSquares() {
points.clear();
}

void add(vector<int> point) {
int x = point[0];
int y = point[1];
points[{x, y}]++;
}

int count(vector<int> qpoint) {
int total = 0;

for( auto point : points) {
int x1 = point.first.first;
int y1 = point.first.second;
int x4 = qpoint[0];
int y4 = qpoint[1];

if((x1 == x4) || (abs(x4 - x1) != abs(y4 - y1))) continue;

if(points.count({x1, y4}) && points.count({x4, y1})){
total += (points[{x1, y4}] * points[{x4, y1}] * point.second);
}
}
return total;
}
};