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 / 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 / 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 / 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 / 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 / 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 / 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 / ArrangingCoins.cpp
Created July 1, 2020 09:28
Arranging Coins (LeetCode) | Binary Search Algorithm Explanation
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) {
@JyotinderSingh
JyotinderSingh / WordSearch.cpp
Created June 30, 2020 09:38
Word Search (LeetCode) | O(1) Space Algorithm Explanation
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)
@JyotinderSingh
JyotinderSingh / EditDistance.cpp
Created June 28, 2020 13:24
Edit Distance Algorithm Explanation | C++
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
@JyotinderSingh
JyotinderSingh / 4Sum.cpp
Created June 26, 2020 11:55
4Sum (LeetCode) | Algorithm Explanation
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
vector<vector<int>> res;
if (nums.size() < 4)
{
return res;
}