This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LRUCache{ | |
public: | |
LRUCache(int capacity) { | |
total = capacity; | |
current = 0; | |
} | |
int get(int key) { | |
auto it = m.find(key); | |
if (it == m.end()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <sstream> | |
#include <vector> | |
#include <set> | |
#include <list> | |
#include <map> | |
#include <thread> | |
#include <algorithm> | |
#include <stdio.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<string> wordBreak(string s, unordered_set<string> &dict) { | |
vector<bool> dp(s.length() + 1); | |
map<int, vector<int> > trace; | |
dp[0] = true; | |
for (int b = 0; b < s.length(); ++b) { | |
for (int e = b + 1; dp[b] && e <= s.length(); ++e) { | |
if (dict.count(s.substr(b, e - b))) { | |
dp[e] = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { | |
vector<vector<string> > result; | |
unordered_set<string> visited; | |
unordered_map<string, vector<string> > traces; | |
unordered_set<string> curr; | |
curr.insert(start); | |
dict.insert(end); | |
visited.insert(start); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { //O(n^2) but use set to remove duplicate result | |
public: | |
vector<vector<int> > fourSum(vector<int> &num, int target) { | |
// Note: The Solution object is instantiated only once and is reused by each test case. | |
sort(num.begin(), num.end()); | |
map<int, vector<pair<int, int> > > cache; | |
for (int i = 0; i < num.size(); ++i) { | |
for (int j = i + 1; j < num.size(); ++j) { | |
cache[ num[i] + num[j] ].push_back(pair<int, int>(i, j)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define pp pair<int, char> | |
int main() { | |
int m, weight; | |
cin >> m; | |
int ans = INT_MAX; | |
char ansc; | |
map<char, int> distance; | |
map<char, map<char, int> > graph; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Error!! | |
void helper(const vector<int> &vec, int pos, pair<int, int> &curr) { | |
for (int i = pos; i < vec.size(); ++i) { | |
if (curr.second == 0 || curr.first == vec[i] ) { | |
curr = make_pair(vec[i], curr.second + 1); | |
} else { | |
--curr.second; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. | |
class Solution { | |
public: | |
string simplifyPath(string path) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
vector<string> vec; | |
int pos = path.find('/'); | |
while (pos != string::npos && pos < path.length()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { | |
vector<vector<string> > result; | |
unordered_set<string> visited; | |
unordered_map<string, vector<string> > traces; | |
unordered_set<string> curr; | |
curr.insert(start); | |
dict.insert(end); | |
visited.insert(start); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <sstream> | |
#include <vector> | |
#include <set> | |
#include <map> | |
#include <algorithm> | |
#include <stdio.h> | |
#include <math.h> |
NewerOlder