This file contains 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 { | |
const int mod = (1e9)+7; // As the question expects. | |
int getMax(int length, vector<int> &cuts) { | |
sort(cuts.begin(), cuts.end()); // Sort so that we will easily get the thickness | |
// of each piece after cutting it. | |
int n = cuts.size(); | |
int maxCut = max(length-cuts[n-1], // Thickness of the last piece. | |
cuts[0]); // Thickness of the first piece. | |
for (int i=1; i<n; i++) { // For each cut - |
This file contains 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 { | |
unordered_map<char, int> frequency; // Stores how many times each sound has occured. | |
// Sounds are c, r, o, a, k. | |
/* | |
* At any time, for a sequence to be valid, number of 'c' must not be less than 'r'. | |
* Similarly, #'r' shouldn't less than #'o', and so on. | |
*/ | |
bool isStateValid() { | |
return (frequency['c'] >= frequency['r']) && | |
(frequency['r'] >= frequency['o']) && |
This file contains 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: | |
int maxScore(vector<int>& cardPoints, int k) { | |
int n = cardPoints.size(); | |
vector<int> cumulativeSumFront(n+1, 0); // For easier indexing, add an extra element. | |
vector<int> cumulativeSumBack(n+1, 0); // For easier indexing, add an extra element. | |
for (int i=0; i<n; i++) { | |
cumulativeSumFront[i+1] = cumulativeSumFront[i] + cardPoints[i]; | |
} |
This file contains 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 { | |
unordered_map<int, vector<int>> tree; | |
unordered_map<int, bool> visited; | |
vector<bool> hasApple; | |
/** Convert edge list into adjacency list */ | |
void createTreeStructure(vector<vector<int>>& edges) { | |
// Adjacency list representation. | |
for (auto e: edges) { | |
tree[e[0]].push_back(e[1]); // a --> b |
This file contains 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 BrowserHistory { | |
stack<string> history; | |
stack<string> future; | |
string currentWebsite; | |
void clearForwardStack() { | |
future = stack<string>(); | |
} | |
void goBackOneStep() { |
This file contains 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
bool comparator(const vector<int> &x, const vector<int> &y) { | |
return (x[0] > y[0]) || ((x[0] == y[0]) && (x[1] < y[1])); | |
} | |
class Solution { | |
public: | |
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) { | |
vector<vector<int>> answer; | |
sort(people.begin(), people.end(), comparator); | |
for (auto &x:people) { |
This file contains 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
vector<int> findDiagonalOrder(vector<vector<int>>& nums) { | |
vector<int> answer; | |
unordered_map<int, vector<int>> m; | |
int maxKey = 0; // maximum key inserted into the map i.e. max value of i+j indices. | |
for (int i=0; i<nums.size(); i++) { | |
for (int j=0; j<nums[i].size(); j++) { | |
m[i+j].push_back(nums[i][j]); // insert nums[i][j] in bucket (i+j). | |
maxKey = max(maxKey, i+j); // | |
} |
This file contains 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
int start = 0; // 1 | |
int end = 0; // 1 | |
while (end <= n-1) { // n times | |
if ( ... ) { // 1 | |
while (start <= end && nums[start] < nums[end]) { // ?? | |
... | |
start++; // 1 | |
} | |
} | |
end++; // 1 |
This file contains 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
int main() { | |
return 0; | |
} |
This file contains 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
int factorial(int n) { // T(n) | |
if (n <= 1) { // 1 | |
return 1; // 1 | |
} | |
int temp = factorial(n-1); // T(n-1) | |
return n*temp; // 1 | |
} |