Skip to content

Instantly share code, notes, and snippets.

View shauryagupta30's full-sized avatar
๐Ÿ˜„

Shaurya Gupta shauryagupta30

๐Ÿ˜„
View GitHub Profile
class Solution {
public:
vector<int> asteroidCollision(vector<int>& nums) {
stack<int> st;
vector<int> ans;
//iterating backwards
//why -> when a negative asteroid is moving in left dir, it can have impact on multiple right direction asteroid
for(int i=nums.size() - 1;i>=0;i--)
{
if(nums[i] > 0)
@shauryagupta30
shauryagupta30 / BST.java
Created July 18, 2021 21:09 — forked from philipjkim/BST.java
Finding sum of n elements after kth smallest element in BST
package com.foo;
import java.util.Stack;
/*
Find sum of n elements after kth smallest element in BST.
Tree is very large, you are not allowed to traverse the tree.
*/
public class BST {
Jump Game II
Hard
Given an array of non-negative integers nums, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
You can assume that you can always reach the last index.
There are 3 Approaches to the problem
class Solution {
void kadane(vector<int>nums,vector<int>&process)
{
int max_so_far = nums[0];
int max_curr = nums[0];
process.push_back(nums[0]);
for(int i=1;i<nums.size();i++)
{
max_curr = max(nums[i],max_curr+nums[i]);
max_so_far = max(max_so_far,max_curr);
Runtime: 1128 ms, faster than 5.10% of C++ online submissions for Maximum Subarray.
Memory Usage: 706.7 MB, less than 5.65% of C++ online submissions for Maximum Subarray.
Next challenges:
//CLRS Solution
class Solution {
int maxSubArrayCross(vector<int>nums,int low,int mid,int high)
{
int sum;
//find the max_sum for left
349. Intersection of Two Arrays
Easy
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
@shauryagupta30
shauryagupta30 / rotations.cpp
Created January 12, 2021 16:11
Array Rotations
189. Rotate Array
Medium
Given an array, rotate the array to the right by k steps, where k is non-negative.
Follow up:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
#include <iostream>
#include<vector>
using namespace std;
bool dfs(vector<vector<int>> adj,vector<vector<bool>> &visited,int N,int i,int j){
if(adj[i][j]==2) return true;
if(!visited[i][j]) visited[i][j] = true;
int row[]={1,0,-1,0};
int col[]={0,1,0,-1};
for(int k=0;k<4;k++)
@shauryagupta30
shauryagupta30 / bfs_dfs.cpp
Created June 25, 2020 14:05
consistent dfs and bfs c++ code
//bfs and dfs on graph
//bfs
#include <bits/stdc++.h>
using namespace std;
//add edge{
void addEdge(vector <int> adj[],int v,int u){
adj[u].push_back(v);
adh[v].push_back(u);
}