Skip to content

Instantly share code, notes, and snippets.

View jatinsharrma's full-sized avatar

Jatin Sharma jatinsharrma

View GitHub Profile
@jatinsharrma
jatinsharrma / MoveToEnd.py
Last active April 23, 2020 07:34
Move all occurrences of given element to last
#--------------------------------------------------------------
#--------Move all occurrences of given element to last----------
#--------------------------------------------------------------
'''
Question : Move all occurrences of given element to the last and we don't care about other
element's order
We could sort the array and then move the element to the last but it will take O(nlogn) time
That is not the best solution to do this
@jatinsharrma
jatinsharrma / linkedlist.py
Last active November 25, 2022 18:01
Linked List : simple Linked List
#-------------------------------------------
#------------Simple Linked List-------------
#-------------------------------------------
# Node Class
class Node:
def __init__(self, data):
self.data = data
self.link = None
@jatinsharrma
jatinsharrma / minNoCoinsChange.py
Last active April 23, 2020 09:33
Minimum Number Of Coins To make Change
#-----------------------------------------------------
#--------Minimum Number Of Coins For Change-----------
#-----------------------------------------------------
'''
Logic
Suppose we are asked to make change of 24 and denomination given are [10,5,2,1]
Let total_coins = 0
@jatinsharrma
jatinsharrma / threeLargestNumbers.py
Created April 19, 2020 13:16
Find Three Largest Numbers
#---------------------------------------------------------
#---------Three Largest Numbers----------
#----------------------------------------------------------
# this function returns three largest numbers
# complexity O(2n) = O(n) where n is length of array
def threeLargestNumbers(numbers):
if len(numbers) < 4:
return numbers
largest = [float("-inf")]*3
@jatinsharrma
jatinsharrma / palindrome.py
Created April 18, 2020 22:01
Palindrome Check
#-------------------------------------------
#------------Palindrome---------------------
#-------------------------------------------
# this function check whether given string is palindrome or not
def palindrome(string):
length = len(string)
mid = length//2
for i in range(mid):
if string[i] != string[length -1 -i]:
@jatinsharrma
jatinsharrma / CaesarCipher.py
Last active May 20, 2021 16:05
Caesar Cipher : Python
#---------------------------------------
#-----------Caesar Cipher---------------
#---------------------------------------
# Ceaser Cipher class
class CaesarCipher:
# this method encrypt the plain text
@staticmethod
def encrypt(string,key):
result = ""
@jatinsharrma
jatinsharrma / MinMaxStack_list.py
Created April 17, 2020 20:35
MinMax Stack :List/Array implementation
#--------------------------------------------
#------------MinMax Stack--------------------
#--------------------------------------------
'''
This is similar to the normal stack with small modification.
MinMax Stack can return min and max value of the stack in O(1) time
Below code is the list/array implementation of stack
Fore linked list implementation in C : https://gist.github.com/jatinsharrma/47b0c3ab06856563b890e400338956da
@jatinsharrma
jatinsharrma / MinHeap_Array.py
Created April 16, 2020 20:45
Min Heap : Array Implementation
#-----------------------------------------------------------------
#-------------------------Min Heap--------------------------------
#-----------------------------------------------------------------
# Min Heap class
class MinHeap:
def __init__ (self, array):
self.heap = self.bulidHeap(array)
# This method take an array and build a heap from it
@jatinsharrma
jatinsharrma / maxNoAdjacentSubsetSum.py
Last active April 15, 2020 13:13
Finding maximum sum (Non Adjacent elements)
#------------------------------------------------------------
#----------Finding maximum sum (Non Adjacent)----------------
#------------------------------------------------------------
'''
Logic:
Suppose we have given [4,8,2,10,15,9,3,11]
@jatinsharrma
jatinsharrma / longestPalindromicSubstring.py
Created April 14, 2020 15:10
Longest Palindromic Substring
#--------- Longest Palindromic Substring------------------
'''
Logic:
we know that a palindrome can be of even or odd length
For example "aba" here b is center and in "abba" center is between 2 "b"s
In below logic we will be using this knowledge
Everytime we will make 2 assumptions