Skip to content

Instantly share code, notes, and snippets.

View JyotinderSingh's full-sized avatar
:octocat:
Building and breaking things

Jyotinder Singh JyotinderSingh

:octocat:
Building and breaking things
View GitHub Profile
@JyotinderSingh
JyotinderSingh / number-of-subsequences-that-satisfy-the-given-sum-condition.cpp
Created July 3, 2020 11:47
Number of Subsequences That Satisfy The Given Sum Condition (LeetCode) | Solution
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;
}
@JyotinderSingh
JyotinderSingh / PlusOne.cpp
Created July 6, 2020 08:19
Plus One (LeetCode) | Interview Question Explanation
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) {
@JyotinderSingh
JyotinderSingh / CountLargestGroup.cpp
Created July 7, 2020 05:56
Count Largest Group (LeetCode) | Interview Question Explanation
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;
@JyotinderSingh
JyotinderSingh / IslandPerimeter.cpp
Created July 7, 2020 13:48
Island Perimeter (LeetCode) | Algorithm Explanation
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);
}
}
@JyotinderSingh
JyotinderSingh / ReverseLinkedList-II.cpp
Created July 11, 2020 08:50
Reverse Sublist in a Linked List (Reverse Linked List - II on LeetCode) | Algorithm Explanation
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;
@JyotinderSingh
JyotinderSingh / SpiralMatrix.cpp
Created July 14, 2020 09:03
Spiral Matrix | Interview Question Explanation
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();
@JyotinderSingh
JyotinderSingh / RotateList.cpp
Created July 17, 2020 07:32
Rotate List (LeetCode) | Interview Question Explanation
/**
* 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) {}
* };
*/
@JyotinderSingh
JyotinderSingh / CourseSchedule2.cpp
Created July 18, 2020 08:02
Course Schedule II (LeetCode) | Topological Sort DFS Explanation
// 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;
@JyotinderSingh
JyotinderSingh / AddBinary.cpp
Created July 19, 2020 08:09
Add Binary (LeetCode) | Interview Question Explanation
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';
@JyotinderSingh
JyotinderSingh / ReduceArraySizeToTheHalf.cpp
Created July 22, 2020 09:02
Reduce Array Size to the Half (LeetCode) | Interview Question Explained
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);