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> 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) |
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
| 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 { |
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
| 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 |
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 { | |
| 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); |
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
| 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 |
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
| 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] |
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
| 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? |
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
| #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++) |
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
| //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); | |
| } |