Skip to content

Instantly share code, notes, and snippets.

View manojnaidu619's full-sized avatar
🎯
Focusing

Manoj Kumar manojnaidu619

🎯
Focusing
  • Bangalore,India
View GitHub Profile
@manojnaidu619
manojnaidu619 / trapping_rain_water.py
Last active June 18, 2020 11:00
Trapping rain water problem in O(n) time and O(1) space.
# 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]
@manojnaidu619
manojnaidu619 / min_platforms.py
Last active June 11, 2020 05:07
Minimum number of platforms needed
# 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()
@manojnaidu619
manojnaidu619 / sorted_arrays_merge.py
Last active June 10, 2020 06:15
Merge two sorted arrays. (using constant space)
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
@manojnaidu619
manojnaidu619 / max_subarray.py
Created April 11, 2020 11:36
Maximum subarray (Kadane's Algorithm) in python
# 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
@manojnaidu619
manojnaidu619 / all_subarrays_with_sum_k.py
Last active April 11, 2020 08:01
Print all subarrays whose sum equals k in python
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])
@manojnaidu619
manojnaidu619 / dutch_national_flag.py
Created April 11, 2020 03:10
Dutch national flag problem (sorting an array of 0s,1s,2s)
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:
@manojnaidu619
manojnaidu619 / sort_binary_array.py
Created April 9, 2020 14:45
sort binary array in linear time
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
@manojnaidu619
manojnaidu619 / subarray_sum_equals_k.py
Created April 9, 2020 14:04
Subarray sum equals k in python
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:
@manojnaidu619
manojnaidu619 / mongoose.js
Last active April 4, 2020 07:17
Mongoose boilerplate
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: {
@manojnaidu619
manojnaidu619 / PromisesVsCallbacks.js
Created April 4, 2020 06:36
Promises vs Callbacks in JS
// Callback
const doWorkCallback = (callback) => {
setTimeout(() => {
//callback(true)
callback(false)
},2000)
}