Skip to content

Instantly share code, notes, and snippets.

View priyankvex's full-sized avatar
💭
Chilling 🍺

Priyank Verma priyankvex

💭
Chilling 🍺
View GitHub Profile
@priyankvex
priyankvex / josephus_problem.py
Created September 5, 2019 14:48
Josephus Problem
class Solution(object):
def solve(self, n, k):
if n == 1:
return 1
else:
return (self.solve(n - 1, k) + k - 1) % n + 1
@priyankvex
priyankvex / combination_sum.py
Created September 2, 2019 15:39
Combination Sum
class Solution(object):
combinations = set()
def solve(self, arr, n):
self.helper(0, [], 0, n, arr)
return list(self.combinations)
def helper(self, start_index, current_comb, current_sum, sum, arr):
@priyankvex
priyankvex / anagram_substring.py
Created August 29, 2019 16:02
Find the anagram substring
from collections import Counter, defaultdict
class Solution(object):
def solve(self, s1, s2):
s1 = list(s1)
s2 = list(s2)
@priyankvex
priyankvex / number_of_substrings_starting_with_a_vowel.py
Created August 26, 2019 15:30
Number of substrings starting with a vowel
class Solution(object):
def solve(self, s):
vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
ans = 0
for i, c in enumerate(s):
if c in vowels:
@priyankvex
priyankvex / valid_ip_addresses.py
Created August 26, 2019 14:39
Valid IP addresses possible
class Solution(object):
count = []
def solve(self, s):
self.helper(s, 0, 0, 0, [])
addresses = []
for a in self.count:
prev = None
add = None
@priyankvex
priyankvex / majority_element.py
Created August 16, 2019 10:44
Find the majority element
class Solution(object):
def solution(self, a):
count = 0
ele = None
for n in a:
if ele is None:
count = 1
ele = n
@priyankvex
priyankvex / minimum_candies.py
Created August 16, 2019 10:18
Minimum number of candies needed for distribution
class Solution(object):
def solve(self, a):
candies = [1 for i in a]
n = len(a)
# first pass
for i in range(1, n):
if a[i] > a[i-1]:
candies[i] = candies[i-1] + 1
@priyankvex
priyankvex / minimum_bulbs.py
Created August 16, 2019 09:54
Minimum numbers of toggles to turn all the light bulbs on
class Solution(object):
def solve(self, a):
is_flip_on = False
flips = 0
for n in a:
if not is_flip_on and n == 1:
continue
elif is_flip_on and n == 0:
@priyankvex
priyankvex / minimum_swaps.py
Created August 8, 2019 16:27
Minimum swaps needs to sort the array
class Solution(object):
def solution(self, a):
seen = {}
cycles = []
for i, n in enumerate(a):
@priyankvex
priyankvex / edit_distance.py
Created July 18, 2019 16:18
Edit Distance
"""
https://scammingthecodinginterview.com
Week 7: Dynamic Programming
Problem: 1
"""
class Solution(object):
def solve(self, a, b):
n = len(a)
m = len(b)