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<int> spiralOrder(vector<vector<int>> &matrix) | |
{ | |
vector<int> res; | |
if (!matrix.size()) | |
return res; | |
int rows = matrix.size(), cols = matrix[0].size(); |
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: | |
ListNode* reverseBetween(ListNode* head, int start, int end) { | |
if(!head || start == end) return head; | |
ListNode dummyHead(INT_MIN); | |
dummyHead.next = head; | |
auto* nodeBeforeReversedSublist = &dummyHead; | |
int pos = 1; |
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: | |
int islandPerimeter(vector<vector<int>>& grid) { | |
int perimeter = 0; | |
for(int i = 0; i < grid.size(); ++i) { | |
for(int j = 0; j < grid[0].size(); ++j) { | |
if(grid[i][j]) { | |
perimeter += (i == 0 || grid[i - 1][j] == 0) + (i == grid.size() - 1 || grid[i + 1][j] == 0) + (j == 0 || grid[i][j - 1] == 0) + (j == grid[0].size() - 1 || grid[i][j + 1] == 0); | |
} | |
} |
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: | |
int countLargestGroup(int n) | |
{ | |
// vector to maintain sizes of each of the groups | |
// note that max sum of digits can be for 9999 (9 + 9 + 9 + 9 = 36) | |
vector<int> count(37); | |
int max_size = 0; | |
int res = 0; |
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<int> plusOne(vector<int>& digits) { | |
digits.back()++; | |
for(int i = digits.size() - 1; i > 0 && digits[i] == 10; --i) { | |
digits[i] = 0; | |
digits[i - 1]++; | |
} | |
if(digits[0] == 10) { |
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: | |
int numSubseq(vector<int>& nums, int target) { | |
const int MOD = 1000000007; | |
vector<int> exponents(nums.size(), 1); | |
// calculate all the possible expoenents you might need and save them beforehand | |
for(int i = 1; i < exponents.size(); ++i) { | |
exponents[i] = (2 * exponents[i - 1]) % MOD; | |
} | |
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: | |
int arrangeCoins(int n) { | |
long n_long = (long) n; | |
long left = 1, right = n_long; | |
while(left <= right) { | |
long mid = left + (right - left) / 2; // (left + right) / 2 | |
if(mid * (mid + 1) / 2 <= n_long) { |
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: | |
bool exist(vector<vector<char>> &board, string word) | |
{ | |
for (int i = 0; i < board.size(); ++i) | |
{ | |
for (int j = 0; j < board[0].size(); ++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
class Solution { | |
public: | |
int minDistance(string word1, string word2) { | |
vector<vector<int>> dp(word2.size() + 1, vector<int> (word1.size() + 1)); | |
/* | |
* Going downwards in the first column (for "" as the first string), | |
* we're performing the insert operation | |
* And the number of inserts to turn an empty string to a | |
* string with length L is L |
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<int>> fourSum(vector<int> &nums, int target) | |
{ | |
vector<vector<int>> res; | |
if (nums.size() < 4) | |
{ | |
return res; | |
} |