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: | |
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 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: | |
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: | |
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: | |
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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
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
// https://leetcode.com/problems/course-schedule-ii/ | |
class Solution | |
{ | |
/* | |
* finished[i] can take one of three values: | |
* -1 -> the node i has never been visited | |
* 0 -> the node i has been visited but is currently in process of completing pre-requisites | |
* 1 -> the node i has finished both the pre-requisites and the course | |
*/ | |
vector<int> res; |
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: | |
string addBinary(string a, string b) { | |
string res; | |
int i = a.size() - 1, j = b.size() - 1; | |
int sum, carry = 0; | |
while(i >= 0 || j >= 0) { | |
sum = carry; | |
if(i >= 0) sum += a[i--] - '0'; | |
if(j >= 0) sum += b[j--] - '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 minSetSize(vector<int>& arr) { | |
unordered_map<int, int> freq; | |
for(int i = 0; i < arr.size(); ++i) freq[arr[i]]++; | |
priority_queue<int> pq; | |
for(auto iter: freq) pq.push(iter.second); |