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
# Problem here -> https://leetcode.com/problems/trapping-rain-water/ | |
height = [0,1,0,2,1,0,1,3,2,1,2,1] # Elevation map | |
lm, rm = 0, 0 | |
left, right = 0, len(height)-1 | |
water = 0 | |
while left<right: | |
if height[left]>lm: lm = height[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
# Problem statement is here -> https://www.geeksforgeeks.org/minimum-number-platforms-required-railwaybus-station/ | |
# Assuming the schedule is already sorted! | |
arrival = [900, 940, 950, 1100, 1500, 1800] | |
departure = [910, 1200, 1120, 1130, 1900, 2000] | |
# Uncomment these two lines if schedules are not already sorted | |
#arrival.sort() | |
#departure.sort() |
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
a = [2] | |
b = [-10,3] | |
a_pointer = len(a)-1 | |
b_pointer = len(b)-1 | |
for _ in range(len(b)): a.append(0) | |
l_pointer = len(a)-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
# Done using Kadane's Algorithm | |
nums = [-2, -3, 4, -1, -2, 1, 5, -3] # Change array here | |
max_so_far,max_ending_here = 0,0 | |
beg,end=0,0 | |
for x in range(0,len(nums)): | |
max_ending_here += nums[x] | |
if max_ending_here > max_so_far: | |
max_so_far = max_ending_here |
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
nums = [3,4,-7,3,1,3,1,-4,-2,-2] # Change the array here | |
k = 0 # Change the sum value here | |
hashmap = {0:[-1]} | |
cumsum = 0 | |
def printsubarray(indexarray, index): | |
for x in indexarray: | |
print(nums[x+1:index+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
nums = [1,2,2,0,1,0,1,1,2,0] | |
low,mid,high=0,0,len(nums)-1 | |
while mid<high: | |
if nums[mid]==0: | |
nums[mid],nums[low] = nums[low],nums[mid] | |
low+=1 | |
mid+=1 | |
elif nums[mid]==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
nums = [0, 0, 1, 0, 1, 1, 0, 1, 0, 0] # change the array here | |
i,j=0,len(nums)-1 | |
zeroes, ones = 0,0 | |
flag=0 | |
while i<=j: | |
if nums[i]==0: | |
zeroes+=1 | |
else: | |
ones+=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
presum = {0:1} | |
cumsum, res = 0,0 | |
for x in nums: | |
cumsum += x | |
if cumsum-k in presum: | |
res += presum[cumsum-k] | |
if cumsum in presum: | |
presum[cumsum]+=1 | |
else: |
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
const mongoose = require("mongoose") | |
mongoose.connect("mongodb://127.0.0.1:27017/task-manager-api", { // establishing DB connection | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
useCreateIndex: true | |
}) | |
const User = mongoose.model('User', { | |
name: { |
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
// Callback | |
const doWorkCallback = (callback) => { | |
setTimeout(() => { | |
//callback(true) | |
callback(false) | |
},2000) | |
} |