index
int64
repo_name
string
branch_name
string
path
string
content
string
import_graph
string
9,411
ESEGroup/Brasil
refs/heads/master
/app/models/agendamento.py
from django.db import models from app.models import Recurso, Usuario class Agendamento (models.Model): # no getters and setters please (http://dirtsimple.org/2004/12/python-is-not-java.html) usuario = models.ForeignKey(Usuario) recurso = models.ForeignKey(Recurso) inicio = models.DateTimeField() periodo = models.PositiveIntegerField() #seconds # state choices: ESTADO_CHOICES = ( ('Agendado', 'Agendado'), ('Cancelado', 'Cancelado'), ('Confirmado', 'Confirmado'), ) estado = models.CharField( max_length = 12, choices = ESTADO_CHOICES, default = 'Agendado', ) def __str__(self): return self.recurso.nome + ' - '+ str(self.inicio) class Meta: db_table = 'Agendamentos' app_label = 'app'
{"/app/models/__init__.py": ["/app/models/usuario.py", "/app/models/recurso.py", "/app/models/agendamento.py", "/app/models/cadastro.py", "/app/models/notificador.py", "/app/models/notificador_cadastro.py", "/app/models/notificador_agendamento.py", "/app/models/busca.py", "/app/models/cadastro_recurso.py", "/app/models/cadastro_usuario.py", "/app/models/cadastro_agendamento.py", "/app/models/gerenciador.py", "/app/models/settingsgroups.py"], "/app/models/busca.py": ["/app/models.py"], "/app/views.py": ["/app/models.py", "/app/permissions.py"], "/app/tests/teste_recurso.py": ["/app/models.py"], "/app/serializers.py": ["/app/models.py"], "/app/models/cadastro_agendamento.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/permissions.py": ["/app/models.py"], "/app/models/notificador_cadastro.py": ["/app/models.py"], "/app/models/cadastro_recurso.py": ["/app/models.py"], "/app/models/agendamento.py": ["/app/models.py"], "/app/models/cadastro_usuario.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/models/notificador_agendamento.py": ["/app/models.py"]}
9,412
ESEGroup/Brasil
refs/heads/master
/app/models/cadastro_usuario.py
from django.db import models from app.models import Cadastro, Usuario from django.contrib.auth.models import User, Group from .settingsgroups import SettingsUserGroups class CadastroUsuario(Cadastro): notificador = None #NotificadorCadastro usuario = Usuario() usuarioTemplate = Usuario() solicitante = Usuario() settingsUserGroups = SettingsUserGroups() def has_permission(self): if self.solicitante.groups.all()[0].pk == self.settingsUserGroups.AdminGroup: if Usuario.objects.get(user=self.solicitante).departamento != self.usuarioTemplate.departamento: return False #if self.usuario.departamento != self.usuarioTemplate.departamento: # return False return True def parser (self,json): #model : {"pk":"1","username":"anything", "password":"pass", "email":"somethind@mail.com", "first_name" : " ", #"last_name" : " ", "registro" : "5", "departamento" : ""} self.usuarioTemplate.usr = User() self.usuarioTemplate.usr.username = json['username'] if 'password' in json.keys(): self.usuarioTemplate.usr.set_password(json['password']) self.usuarioTemplate.usr.email = json['email'] self.usuarioTemplate.usr.first_name = json['first_name'] self.usuarioTemplate.usr.last_name = json['last_name'] if 'pk' in json.keys() and json['pk'] != '': self.usuarioTemplate.pk=int(json['pk']) self.usuario = Usuario.objects.get(pk=self.usuarioTemplate.pk) self.usuarioTemplate.registro = int(json['registro']) self.usuarioTemplate.departamento = json['departamento'] def cadastrar (self,group): self.usuario.usr = User.objects.create_user( username=self.usuarioTemplate.usr.username, email=self.usuarioTemplate.usr.email, first_name=self.usuarioTemplate.usr.first_name, last_name=self.usuarioTemplate.usr.last_name ) self.usuario.usr.groups.add(Group.objects.get(pk=group)) self.usuario = Usuario.objects.create( user=self.usuario.usr, registro=self.usuarioTemplate.registro, departamento=self.usuarioTemplate.departamento ) return self.usuario.pk #falta notificação def atualizar (self): self.usuario.user.username = self.usuarioTemplate.usr.username self.usuario.user.password = self.usuarioTemplate.usr.password self.usuario.user.email = self.usuarioTemplate.usr.email self.usuario.user.first_name = self.usuarioTemplate.usr.first_name self.usuario.user.last_name = self.usuarioTemplate.usr.last_name self.usuario.registro = self.usuarioTemplate.registro self.usuario.departamento = self.usuarioTemplate.departamento self.usuario.user.save() self.usuario.save() def deletar (self): self.usuario.user.is_active = False self.usuario.user.save() def notificar (self): return False class Meta: managed = False app_label = 'app'
{"/app/models/__init__.py": ["/app/models/usuario.py", "/app/models/recurso.py", "/app/models/agendamento.py", "/app/models/cadastro.py", "/app/models/notificador.py", "/app/models/notificador_cadastro.py", "/app/models/notificador_agendamento.py", "/app/models/busca.py", "/app/models/cadastro_recurso.py", "/app/models/cadastro_usuario.py", "/app/models/cadastro_agendamento.py", "/app/models/gerenciador.py", "/app/models/settingsgroups.py"], "/app/models/busca.py": ["/app/models.py"], "/app/views.py": ["/app/models.py", "/app/permissions.py"], "/app/tests/teste_recurso.py": ["/app/models.py"], "/app/serializers.py": ["/app/models.py"], "/app/models/cadastro_agendamento.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/permissions.py": ["/app/models.py"], "/app/models/notificador_cadastro.py": ["/app/models.py"], "/app/models/cadastro_recurso.py": ["/app/models.py"], "/app/models/agendamento.py": ["/app/models.py"], "/app/models/cadastro_usuario.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/models/notificador_agendamento.py": ["/app/models.py"]}
9,413
ESEGroup/Brasil
refs/heads/master
/app/models/notificador.py
from django.db import models class Notificador(models.Model): # no getters and setters please (http://dirtsimple.org/2004/12/python-is-not-java.html) remetente = "<EMAIL DO SISTEMA>" mensagem = "" assunto = "" tipo_mensagem = 0 def enviarMensagem (): res = None return res def construirMensagem (): return False class Meta: abstract = True managed = False app_label = 'app'
{"/app/models/__init__.py": ["/app/models/usuario.py", "/app/models/recurso.py", "/app/models/agendamento.py", "/app/models/cadastro.py", "/app/models/notificador.py", "/app/models/notificador_cadastro.py", "/app/models/notificador_agendamento.py", "/app/models/busca.py", "/app/models/cadastro_recurso.py", "/app/models/cadastro_usuario.py", "/app/models/cadastro_agendamento.py", "/app/models/gerenciador.py", "/app/models/settingsgroups.py"], "/app/models/busca.py": ["/app/models.py"], "/app/views.py": ["/app/models.py", "/app/permissions.py"], "/app/tests/teste_recurso.py": ["/app/models.py"], "/app/serializers.py": ["/app/models.py"], "/app/models/cadastro_agendamento.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/permissions.py": ["/app/models.py"], "/app/models/notificador_cadastro.py": ["/app/models.py"], "/app/models/cadastro_recurso.py": ["/app/models.py"], "/app/models/agendamento.py": ["/app/models.py"], "/app/models/cadastro_usuario.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/models/notificador_agendamento.py": ["/app/models.py"]}
9,414
ESEGroup/Brasil
refs/heads/master
/app/models/notificador_agendamento.py
from django.db import models from app.models import Notificador class NotificadorAgendamento (Notificador): agendamento = None #Agendamento def construirMensagem (): return False class Meta: managed = False app_label = 'app'
{"/app/models/__init__.py": ["/app/models/usuario.py", "/app/models/recurso.py", "/app/models/agendamento.py", "/app/models/cadastro.py", "/app/models/notificador.py", "/app/models/notificador_cadastro.py", "/app/models/notificador_agendamento.py", "/app/models/busca.py", "/app/models/cadastro_recurso.py", "/app/models/cadastro_usuario.py", "/app/models/cadastro_agendamento.py", "/app/models/gerenciador.py", "/app/models/settingsgroups.py"], "/app/models/busca.py": ["/app/models.py"], "/app/views.py": ["/app/models.py", "/app/permissions.py"], "/app/tests/teste_recurso.py": ["/app/models.py"], "/app/serializers.py": ["/app/models.py"], "/app/models/cadastro_agendamento.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/permissions.py": ["/app/models.py"], "/app/models/notificador_cadastro.py": ["/app/models.py"], "/app/models/cadastro_recurso.py": ["/app/models.py"], "/app/models/agendamento.py": ["/app/models.py"], "/app/models/cadastro_usuario.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/models/notificador_agendamento.py": ["/app/models.py"]}
9,415
ESEGroup/Brasil
refs/heads/master
/app/migrations/0005_auto_20161119_1727.py
# -*- coding: utf-8 -*- # Generated by Django 1.10.3 on 2016-11-19 19:27 from __future__ import unicode_literals from django.conf import settings from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('app', '0004_auto_20161115_1756'), ] operations = [ migrations.RemoveField( model_name='usuario', name='email', ), migrations.RemoveField( model_name='usuario', name='estado', ), migrations.RemoveField( model_name='usuario', name='nome', ), migrations.RemoveField( model_name='usuario', name='tipo_perfil', ), migrations.AddField( model_name='usuario', name='user', field=models.OneToOneField(default=0, on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL), ), ]
{"/app/models/__init__.py": ["/app/models/usuario.py", "/app/models/recurso.py", "/app/models/agendamento.py", "/app/models/cadastro.py", "/app/models/notificador.py", "/app/models/notificador_cadastro.py", "/app/models/notificador_agendamento.py", "/app/models/busca.py", "/app/models/cadastro_recurso.py", "/app/models/cadastro_usuario.py", "/app/models/cadastro_agendamento.py", "/app/models/gerenciador.py", "/app/models/settingsgroups.py"], "/app/models/busca.py": ["/app/models.py"], "/app/views.py": ["/app/models.py", "/app/permissions.py"], "/app/tests/teste_recurso.py": ["/app/models.py"], "/app/serializers.py": ["/app/models.py"], "/app/models/cadastro_agendamento.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/permissions.py": ["/app/models.py"], "/app/models/notificador_cadastro.py": ["/app/models.py"], "/app/models/cadastro_recurso.py": ["/app/models.py"], "/app/models/agendamento.py": ["/app/models.py"], "/app/models/cadastro_usuario.py": ["/app/models.py", "/app/models/settingsgroups.py"], "/app/models/notificador_agendamento.py": ["/app/models.py"]}
9,418
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/lights_controller.py
import logging import random import threading from time import sleep from lib.const import * log = logging.getLogger() job_statuses = dict() def animation_worker(led_strip, job, status, color, start, end): while job_statuses[job.name] == status: for x in range(0, 40): b = 1 - x*.02 led_strip.fillRGB(color[0] * b, color[1] * b, color[2] * b, start, end) sleep(0.02) for x in range(40, 0, -1): b = 1 - x*.02 led_strip.fillRGB(color[0] * b, color[1] * b, color[2] * b, start, end) sleep(0.02) class LightsController: def __init__(self, led_strip, build_jobs): self.animation_threads = dict() self.job_leds = dict() self.jobs = list() self.led_strip = led_strip index = 0 for job in build_jobs: self.jobs.append(job.name) self.job_leds[job.name] = job.led_coordinates(index) self.animation_threads[job.name] = None index = job.next_index(index) def update_build_status(self, job, status): start = self.__start(job.name) end = self.__end(job.name) job_statuses[job.name] = status # print 'update_build_status: ', job.name, status, start, end animation_thread = self.animation_threads[job.name] if (animation_thread != None and animation_thread.isAlive()): animation_thread.join() if (status == OFF): self.__off(start, end) elif (status == SUCCESS): self.__success(start, end) elif (status == FAILURE): self.__failure(start, end) elif (status == BUILDING_FROM_SUCCESS): self.__building_from_success(job, start, end) elif (status == BUILDING_FROM_FAILURE): self.__building_from_failure(job, start, end) elif (status == BUILDING_FROM_UNKNOWN): self.__building_from_unknown(job, start, end) else: self.__unknown(job, start, end) def off(self): self.led_strip.fillOff() def random(self): for job in self.jobs: rgb = self.__randomRgb() self.led_strip.fillRGB(rgb[0], rgb[1], rgb[2], self.__start(job), self.__end(job)) def error(self): self.__fill_strand(BLUE, 0, 0) def __off(self, start, end): self.__fill_strand(NONE, start, end) def __success(self, start, end): self.__fill_strand(GREEN, start, end) def __failure(self, start, end): self.__fill_strand(RED, start, end) def __building_from_success(self, job, start, end): self.__building(job, BUILDING_FROM_SUCCESS, GREEN, start, end) def __building_from_failure(self, job, start, end): self.__building(job, BUILDING_FROM_FAILURE, RED, start, end) def __building_from_unknown(self, job, start, end): self.__building(job, BUILDING_FROM_UNKNOWN, YELLOW, start, end) def __unknown(self, job, start, end): self.__fill_strand(YELLOW, start, end) def __fill_strand(self, color, start, end): semaphore = threading.BoundedSemaphore() semaphore.acquire() self.led_strip.fillRGB(color[0], color[1], color[2], start, end) semaphore.release() def __start(self, build_name): return self.job_leds[build_name][0] def __end(self, build_name): return self.job_leds[build_name][1] def __randomRgb(self): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def __building(self, job, status, color, start, end): self.animation_threads[job.name] = threading.Thread(target=animation_worker, args=(self.led_strip, job, status, color, start, end)) self.animation_threads[job.name].daemon = True self.animation_threads[job.name].start()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,419
jwolstenholme/pimp-my-ci
refs/heads/master
/lights_off.py
from lib.ledstrip import LEDStrip LEDStrip().fillOff()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,420
jwolstenholme/pimp-my-ci
refs/heads/master
/monitors/circleci_monitor.py
from lib.const import * status_dict = { 'fixed' : SUCCESS, 'success' : SUCCESS, 'failed' : FAILURE, 'building_from_fixed' : BUILDING_FROM_SUCCESS, 'building_from_success' : BUILDING_FROM_SUCCESS, 'building_from_failed' : BUILDING_FROM_FAILURE, 'building_from_unknown' : BUILDING_FROM_UNKNOWN, } class CircleciMonitor: def __init__(self, job_queues): self.job_queues = job_queues self.job_statuses = dict.fromkeys(self.job_queues.keys()) def process_build_for_job(self, build, job): current_status = self.get_status(build) if (self.job_statuses[job.name] != current_status): self.job_queues[job.name].put_nowait(current_status) self.job_statuses[job.name] = current_status def get_status(self, build): raw_status = build[0]['status'] if (raw_status == 'running'): previous_status = build[0]['previous']['status'] return status_dict.get('building_from_' + previous_status, BUILDING_FROM_UNKNOWN) else: return status_dict.get(raw_status, UNKNOWN) def error(self): print "ERROR in CircleciMonitor"
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,421
jwolstenholme/pimp-my-ci
refs/heads/master
/gui/main.py
from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.boxlayout import BoxLayout from kivy.graphics import Color, Rectangle from kivy.clock import Clock from kivy.clock import mainthread from pimp_my_ci import PimpMyCi import threading import time SIZE = 15 class CanvasApp(App): def on_stop(self): self.pimp_my_ci.stop() def build(self): threading.Thread(target=self.start).start() return self.create_screen() def start(self): self.pimp_my_ci = PimpMyCi(self) self.pimp_my_ci.start() def create_screen(self): self.root = BoxLayout(spacing=SIZE) for i in range(32): led = Led() led.create(i) self.root.add_widget(led) return self.root @mainthread def fillOff(self): self.fillRGB(0, 0, 0, 0, 32) @mainthread def fillRGB(self, r, g, b, start=0, end=0): for i in range(start, end): led = self.root.children[i] led.update(r, g, b) class Led(Widget): def create(self, index): with self.canvas: self.color = Color(0, 0, 0, 1, mode='rgba') self.rect = Rectangle(pos=(SIZE * 2 * index, 10), size=(SIZE, SIZE)) def update(self, r, g, b): self.color.rgb = [r/255, g/255, b/255, 1] if __name__ == '__main__': CanvasApp().run()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,422
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/config.py
class Config: total_number_leds = 32 polling_interval_secs = 3 platform = 'circleci' job_defaults = dict( num_leds = 32 ) # The build jobs to monitor - defaults are overridden jobs = [ dict( name = 'vix', url = 'https://circleci.com/api/v1/project/DiUS/vix?circle-token=8dd04ea68b5caf87a3be4ed050a97ae9d91ae4e9&limit=1&offset=0', success = 'Burns_Excellent', failure = 'Sheeeeeiiiit' ), ]
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,423
jwolstenholme/pimp-my-ci
refs/heads/master
/pollers/http_json_poller.py
from time import sleep import logging import sys import threading import urllib2 import yaml from lib.config import Config log = logging.getLogger() class HttpJsonPoller(threading.Thread): def __init__(self, build_jobs, build_monitor): threading.Thread.__init__(self) self.daemon = True self.build_monitor = build_monitor self.jobs = build_jobs def run(self): poll = True while poll: sleep(Config.polling_interval_secs) for job in self.jobs: try: req = self.create_request(job) req.add_header('Content-Type', 'application/json') response_body = urllib2.urlopen(req).read() self.build_monitor.process_build_for_job( yaml.load(response_body), job ) except: self.build_monitor.error() log.error( "Polling error: %s", sys.exc_info() ) def create_request(self, job): return urllib2.Request(job.url, headers={"Accept" : "application/json"})
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,424
jwolstenholme/pimp-my-ci
refs/heads/master
/pollers/travis_poller.py
import urllib2 from http_json_poller import HttpJsonPoller class TravisPoller(HttpJsonPoller): def create_request(self, job): return urllib2.Request(job.url, headers={"Accept" : "application/vnd.travis-ci.2+json"})
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,425
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/sounds_controller.py
from lib.const import * from sounds.player import Player class SoundsController: def __init__(self, job_names): self.sound_player = Player() self.play_sounds = dict.fromkeys(job_names, False) def update_build_status(self, job, status): if (not self.play_sounds[job.name]): self.play_sounds[job.name] = True return if (status == SUCCESS and job.success != None): if (job.success == '__RANDOM'): self.sound_player.play_random_success_sound() else: self.sound_player.play_success(job.success) elif (status == FAILURE and job.failure != None): if (job.failure == '__RANDOM'): self.sound_player.play_random_failure_sound() else: self.sound_player.play_failure(job.failure)
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,426
jwolstenholme/pimp-my-ci
refs/heads/master
/tests/lib/test_buildJob.py
from unittest import TestCase from lib.build_job import BuildJob class TestBuildJob(TestCase): def setUp(self): self.build_job = BuildJob(dict(name='Test Build Job', url='http://some/url')) def test_job_name_validation(self): self.assertRaises(ValueError, BuildJob, dict(name=None, url='http://some/url')) self.assertRaises(ValueError, BuildJob, dict(name='', url='http://some/url')) def test_num_leds_validation(self): self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=0, url='http://some/url')) self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=33, url='http://some/url')) def test_offset_validation(self): self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=2, offset=32, url='http://some/url')) self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=2, offset=-1, url='http://some/url')) def test_url_validation(self): self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=2, offset=32)) self.assertRaises(ValueError, BuildJob, dict(name='some', num_leds=2, offset=-1, url=None)) def test_default_build_job(self): self.assertEqual(self.build_job.name, 'Test Build Job') self.assertEqual(self.build_job.offset, 1) self.assertEqual(self.build_job.num_leds, 1) def test_led_addresses_for_first_index(self): self.assertEqual(self.build_job.led_addresses(0), [0]) def test_led_addresses_for_multiple_leds(self): build_job = BuildJob(dict(name='Some Job', num_leds=4, url='http://some/url')) self.assertEqual(build_job.led_addresses(0), [0, 1, 2, 3]) def test_remaining_leds_are_utilized_when_almost_exhausted(self): build_job = BuildJob(dict(name='Some Job', num_leds=4, url='http://some/url')) self.assertEqual(build_job.led_addresses(29), [29, 30, 31]) def test_next_index_with_padding(self): self.assertEqual(self.build_job.next_index(0), 2) build_job = BuildJob(dict(name='Some Job', num_leds=4, url='http://some/url')) self.assertEqual(build_job.next_index(0), 5) def test_next_index_with_large_padding(self): build_job = BuildJob(dict(name='Some Job', num_leds=4, offset=5, url='http://some/url')) self.assertEqual(build_job.next_index(0), 9) def test_next_index_upper_bounds(self): self.assertRaises(ArithmeticError, self.build_job.next_index, 31) def test_coordinates(self): build_job = BuildJob(dict(name='Some Job', num_leds=4, url='http://some/url')) self.assertEqual(build_job.led_coordinates(0), [0, 4]) self.assertEqual(build_job.led_coordinates(10), [10, 14]) self.assertEqual(build_job.led_coordinates(29), [29, 32])
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,427
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/build_job.py
import Queue from lib.config import Config # TODO rename all these to just 'job' class BuildJob: def __init__(self, dictionary): self.name = dictionary.get('name') self.url = dictionary.get('url') self.num_leds = dictionary.get('num_leds', 1) self.offset = dictionary.get('offset', 1) self.success = dictionary.get('success', '__RANDOM') self.failure = dictionary.get('failure', '__RANDOM') self.queue = Queue.Queue() if self.is_blank(self.name): raise ValueError('Name must not be blank') if self.is_blank(self.url): raise ValueError('URL must not be blank') if self.num_leds <= 0 or self.num_leds > Config.total_number_leds: raise ValueError('Invalid num_leds') if self.offset < 0 or self.offset >= Config.total_number_leds: raise ValueError('Invalid offset') #Returns the array of led indecies this job will occupy at the given index def led_addresses(self, index): return range(index, min(index + self.num_leds, Config.total_number_leds)) def led_coordinates(self, index): addresses = self.led_addresses(index) return [addresses[0], (addresses[-1] + 1)] def next_index(self, index): if index >= Config.total_number_leds - 1: raise ArithmeticError('No leds available') return self.num_leds + self.offset + index def is_blank(self, string): return string is None or string.strip() == ''
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,428
jwolstenholme/pimp-my-ci
refs/heads/master
/sounds/player.py
import os from random import randrange import glob import logging import subprocess import signal log = logging.getLogger() class Player: def play_random_start_sound(self): self.play_this_thing(self.randomly_choose_mp3_in_sub_directory("start_build")) def play_random_success_sound(self): self.play_this_thing(self.randomly_choose_mp3_in_sub_directory("success")) def play_random_failure_sound(self): self.play_this_thing(self.randomly_choose_mp3_in_sub_directory("failure")) def play_success(self, filename): self.play_this_thing(self.get_directory("success") + "{0}".format(filename) + ".mp3") def play_failure(self, filename): self.play_this_thing(self.get_directory("failure") + "{0}".format(filename) + ".mp3") def play_this_thing(self, filename): self.kill_off_any_currently_playing_sounds() log.info("playing {0}...".format(filename)) os.system("mpg321 {0} &".format(filename)) def kill_off_any_currently_playing_sounds(self): proc = subprocess.Popen(["pgrep", 'mpg321'], stdout=subprocess.PIPE) for pid in proc.stdout: log.info("killing off mpg321 process with PID {0}...".format(pid)) os.kill(int(pid), signal.SIGTERM) # there are other variants if this doesn't do the trick def randomly_choose_mp3_in_sub_directory(self, sub_directory): directory = self.get_directory(sub_directory) files = glob.glob("{0}*.mp3".format(directory)) return files[randrange(len(files))] def get_directory(self, sub_directory): return "{0}/sounds/{1}/".format(os.environ['RPI_HOME'], sub_directory)
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,429
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/const.py
SUCCESS = 0 BUILDING_FROM_SUCCESS = 1 FAILURE = 2 BUILDING_FROM_FAILURE = 3 UNKNOWN = 4 BUILDING_FROM_UNKNOWN = 5 OFF = 6 GREEN = (0, 255, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) BLUE = (0, 0, 255) NONE = (0, 0, 0)
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,430
jwolstenholme/pimp-my-ci
refs/heads/master
/monitors/jenkins_monitor.py
from lib.const import * status_dict = { 'aborted_anime' : BUILDING_FROM_UNKNOWN, 'blue' : SUCCESS, 'blue_anime' : BUILDING_FROM_SUCCESS, 'disabled_anime' : BUILDING_FROM_UNKNOWN, 'grey_anime' : BUILDING_FROM_UNKNOWN, 'notbuilt_anime' : BUILDING_FROM_UNKNOWN, 'red' : FAILURE, 'red_anime' : BUILDING_FROM_FAILURE, 'yellow_anime' : BUILDING_FROM_UNKNOWN } class JenkinsMonitor: def __init__(self, job_queues): self.job_queues = job_queues self.job_statuses = dict.fromkeys(self.job_queues.keys()) def process_build_for_job(self, build, job): job_statuses = self.__parse_build(build) differences = self.__filter_differences(self.job_statuses, job_statuses) for build, status in differences.iteritems(): self.job_queues[build].put_nowait(status) self.job_statuses = job_statuses def error(self): for build, status in self.job_statuses.iteritems(): self.job_queues[build].put_nowait(UNKNOWN) def __filter_differences(self, old_builds, new_builds): return dict(new_builds.viewitems() - old_builds.viewitems()) # return true for only the jobs we're interested in def __filter_build(self, build): return build['name'] in self.job_queues.keys() # map jenkins job entries to our jobs def __jenkins_to_rpi(self, job): return { job['name'] : status_dict.get(job['color'], UNKNOWN) } # returns dictionary of build_name to current status def __parse_build(self, build): jobs = filter( self.__filter_build, build['jobs'] ) updated_statuses = map( self.__jenkins_to_rpi, jobs ) return dict(map(dict.popitem, updated_statuses))
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,431
jwolstenholme/pimp-my-ci
refs/heads/master
/pimp_my_ci.py
# script expects RPI_HOME to be set as an env var import sys import os import logging import threading import traceback import Queue from time import sleep from threading import Thread from lib.config import Config from lib.ledstrip import LEDStrip from lib.build_jobs import BuildJobs from lib.lights_controller import LightsController from lib.sounds_controller import SoundsController logging.basicConfig( level=logging.INFO, filename="{0}/logs/pimpmyci.log".format(os.environ['RPI_HOME']), format="%(asctime)s <%(threadName)s>: %(message)s", datefmt='%m/%d/%Y %I:%M:%S %p', maxBytes=1024, backupCount=3) log = logging.getLogger() class PimpMyCi: running = True def __init__(self, led_strip): #jobs is an instance of BuildJobs jobs = BuildJobs.from_dictionaries(Config.platform, Config.job_defaults, Config.jobs) sounds_controller = SoundsController(jobs.names) lights_controller = LightsController(led_strip, jobs) lights_controller.off() controllers = list( (lights_controller, sounds_controller) ) jobs.start_polling(controllers) def start(self): while self.running: try: sleep(0.05) except KeyboardInterrupt: log.info('^C received, shutting down controller') lights_controller.off() sys.exit() except Exception as e: log.error("Unexpected error type: %s", type(e)) log.error("Unexpected error args: %s", e.args) lights_controller.error() def stop(self): self.running = False def main(): led_strip = LEDStrip(Config.total_number_leds) PimpMyCi(led_strip).start() if __name__ == '__main__': main()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,432
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/rgbcolour.py
class RGBColour: def __init__(self, r=0.0, g=0.0, b=0.0, bright=1.0): if(r > 255.0 or r < 0.0 or g > 255.0 or g < 0.0 or b > 255.0 or b < 0.0): raise ValueError('RGB values must be between 0 and 255') if(bright > 1.0 or bright < 0.0): raise ValueError('Brightness must be between 0.0 and 1.0') self.R = r * bright self.G = g * bright self.B = b * bright def __str__( self ): return "%d,%d,%d" % (self.R, self.G, self.B)
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,433
jwolstenholme/pimp-my-ci
refs/heads/master
/check_config.py
from lib.build_jobs import BuildJobs from lib.config import Config import urllib2 import json class Check: def __init__(self): self.build_jobs = BuildJobs.from_dictionaries(Config.platform, Config.job_defaults, Config.jobs) self.printGlobal() self.printJobs() self.printLedLayout() self.printTestResponse() def printGlobal(self): print "\n==== Global ====" print " Toal number of LEDs : ", Config.total_number_leds print " Polling interval : ", Config.polling_interval_secs, "seconds" print " Platform : ", Config.platform if (Config.platform == 'jenkins'): print " URL : ", Config.job_defaults['url'] def printJobs(self): print "\n==== Builds ====" index = 0 self.job_leds = dict() for job in self.build_jobs: print " ", job.name print "\tleds:".ljust(14), str(job.led_coordinates(index)) if (Config.platform == 'travis'): print "\turl:".ljust(14), job.url print "\tsuccess:".ljust(14), str(job.success) print "\tfailure:".ljust(14), job.failure for i in job.led_addresses(index): self.job_leds[i] = job.name index = job.next_index(index) def printLedLayout(self): print "\n==== LEDs ====" for i in range(Config.total_number_leds): print " [", str(i).rjust(2), "]", self.job_leds.get(i, " *") def printTestResponse(self): print "\n==== Test ====" try: if (Config.platform == 'jenkins'): self.printJenkinsResponse() if (Config.platform == 'travis'): self.printTravisResponses() except Exception as e: print "Error:", type(e), e.args, e def printJenkinsResponse(self): req = urllib2.Request(Config.job_defaults['url']) req.add_header('Content-Type', 'application/json') response_body = urllib2.urlopen(req).read() print json.dumps(json.loads(response_body), indent=4, separators=(',', ': ')) def printTravisResponses(self): for job in self.build_jobs: req = urllib2.Request(job.url) req.add_header('Content-Type', 'application/vnd.travis-ci.2+json') response_body = urllib2.urlopen(req).read() print json.dumps(json.loads(response_body), indent=4, separators=(',', ': ')) if __name__ == '__main__': Check()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,434
jwolstenholme/pimp-my-ci
refs/heads/master
/lib/build_jobs.py
import Queue from threading import Thread from time import sleep from lib.build_job import BuildJob from monitors.circleci_monitor import CircleciMonitor from monitors.jenkins_monitor import JenkinsMonitor from monitors.travis_monitor import TravisMonitor from pollers.http_json_poller import HttpJsonPoller from pollers.travis_poller import TravisPoller def worker(controllers, job, queue): while True: try: status = queue.get_nowait() for controller in controllers: controller.update_build_status(job, status) queue.task_done() except Queue.Empty: sleep(1) # to represent a collection of build jobs class BuildJobs(list): def __init__(self, defaults, job_dictionaries): # set up jobs for job_description in job_dictionaries: self.append( BuildJob(dict(defaults.items() + job_description.items())) ) # create queues & names to be used later self.queues = dict([ (job.name, job.queue) for job in self ]) self.names = [ job.name for job in self ] def create_threads(self, controllers): for job in self: t = Thread(target=worker, args=(controllers, job, job.queue, )) t.daemon = True t.start() @staticmethod def from_dictionaries(platform, defaults, job_dictionaries): if platform == 'jenkins': return JenkinsBuildJobs(defaults, job_dictionaries) elif platform == 'travis': return TravisBuildJobs(defaults, job_dictionaries) elif platform == 'circleci': return CircleciBuildJobs(defaults, job_dictionaries) else: raise ValueError('platform must be one of [circleci, jenkins, travis]') class JenkinsBuildJobs(BuildJobs): def start_polling(self, controllers): self.create_threads(controllers) monitor = JenkinsMonitor(self.queues) HttpJsonPoller(self, monitor).start() class TravisBuildJobs(BuildJobs): def start_polling(self, controllers): self.create_threads(controllers) monitor = TravisMonitor(self.queues) TravisPoller(self, monitor).start() class CircleciBuildJobs(BuildJobs): def start_polling(self, controllers): self.create_threads(controllers) monitor = CircleciMonitor(self.queues) HttpJsonPoller(self, monitor).start()
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,435
jwolstenholme/pimp-my-ci
refs/heads/master
/monitors/travis_monitor.py
from lib.const import * status_dict = { 'passed' : SUCCESS, 'failed' : FAILURE } class TravisMonitor: def __init__(self, job_queues): self.job_queues = job_queues self.job_statuses = dict.fromkeys(self.job_queues.keys()) def process_build_for_job(self, build, job): raw_status = build['repo']['last_build_state'] current_status = status_dict.get(raw_status, UNKNOWN) if (self.job_statuses[job.name] != current_status): self.job_queues[job.name].put_nowait(current_status) self.job_statuses[job.name] = current_status def error(self): print "ERROR in TravisMonitor"
{"/lib/lights_controller.py": ["/lib/const.py"], "/gui/main.py": ["/pimp_my_ci.py"], "/pollers/http_json_poller.py": ["/lib/config.py"], "/lib/sounds_controller.py": ["/lib/const.py", "/sounds/player.py"], "/tests/lib/test_buildJob.py": ["/lib/build_job.py"], "/lib/build_job.py": ["/lib/config.py"], "/monitors/jenkins_monitor.py": ["/lib/const.py"], "/pimp_my_ci.py": ["/lib/config.py", "/lib/build_jobs.py", "/lib/lights_controller.py", "/lib/sounds_controller.py"], "/lib/build_jobs.py": ["/lib/build_job.py", "/monitors/circleci_monitor.py", "/monitors/jenkins_monitor.py", "/monitors/travis_monitor.py", "/pollers/http_json_poller.py", "/pollers/travis_poller.py"]}
9,436
ijmazur/pwpflask
refs/heads/main
/flaskblog/users/utils.py
import os import secrets from PIL import Image from flask import url_for, current_app from flask_mail import Message from flaskblog import mail # random hex from secrets is a base for randomising name of picture, so it never hurts our db # we save the file with same extension as its uploaded (os - needed for that) # the filename.extension is split and we save the extension # we need to use _, as variable not used in our application (f_name) # we join the two in picture_fn = hexname + extension # picture_path = root app(package directory) and static/profile_pics and we add te picture filename we created # the picture cant be bigger than 125x125 def save_picture(form_picture): random_hex = secrets.token_hex(8) _, f_ext = os.path.splitext(form_picture.filename) picture_fn = random_hex + f_ext picture_path = os.path.join(current_app.root_path, 'static/profile_pics', picture_fn) output_size = (125, 125) i = Image.open(form_picture) i.thumbnail(output_size) i.save(picture_path) # to delete previous profile pic, while adding new one # prev_picture = os.path.join(app.root_path, 'static/profile_pics', current_user.image_file) # if os.path.exists(prev_picture) and os.path.basename(prev_picture) != 'default.jpg': # os.remove(prev_picture) return picture_fn # get_reset_token (main app models.py) # 1800 = 30minutes # into Serializer we pass in the secret_key # and we return token created with this serializer # we dump it with payload of userid, and we use our own self.id that the user resets # and we decode it with utf-8 # we send the email message from pwpumcsim@gmail.com with reset token def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', sender='noreply@demo.com', recipients=[user.email]) msg.body = f'''To reset your password, visit the following link: {url_for('users.reset_token', token=token, _external=True)} If you did not make this request, then simply ignore this email and no changes will be made. ''' mail.send(msg)
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,437
ijmazur/pwpflask
refs/heads/main
/flaskblog/weather/routes.py
import configparser import os import requests from flask import render_template, Blueprint, request weather = Blueprint('weather', __name__) # OWM_API = os.environ.get('OWM_API') # asking what city we are looking for @weather.route("/weather") def weather_dashboard(): return render_template('weather.html') # filtering through the JSON, getting what I need @weather.route("/weather-results", methods=['POST']) def get_results(): cityname = request.form['cityname'] api_key = get_api() data = what_weather(cityname, api_key) temp = "{0:.2f}".format(data["main"]["temp"]) feels_like = "{0:.2f}".format(data["main"]["feels_like"]) pressure = "{0:.2f}".format(data["main"]["pressure"]) humidity = "{0:.2f}".format(data["main"]["humidity"]) weathers = data["weather"][0]["main"] location = data["name"] lat = data["coord"]["lat"] lon = data["coord"]["lon"] country = data["sys"]["country"] return render_template('weather-results.html', temp=temp, feels_like=feels_like, pressure=pressure, humidity=humidity, weather=weathers, location=location, lat=lat, lon=lon, country=country) # getting the API key from config.ini def get_api(): config = configparser.ConfigParser() config.read('flaskblog/weather/config.ini') return config['openweathermap']['api'] # getting the JSON information def what_weather(cityname, api_key): api_url = f"http://api.openweathermap.org/data/2.5/weather?q={cityname}&units=metric&appid={api_key}" r = requests.get(api_url) return r.json()
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,438
ijmazur/pwpflask
refs/heads/main
/flaskblog/coins/routes.py
from flask import render_template, request, Blueprint from flaskblog.models import Post import os import coinmarketcapapi import json from bs4 import BeautifulSoup import requests from types import SimpleNamespace # coins = Blueprint('coins', __name__) CMP_API = os.environ.get('CMP_API') # cmc = coinmarketcapapi.CoinMarketCapAPI(CMP_API) cmc = requests.get('https://coinmarketcap.com/') soup = BeautifulSoup(cmc.content, 'html.parser') #TODO use coingecko to # doing nothing :( @coins.route("/btc") def btc(): return render_template('btc.html', title='BTC Info') # filtering through JSON :( @coins.route("/doge") def doge(): data = '{"DOGE": {"id": 74,"name": "Dogecoin","symbol": "DOGE","slug": "dogecoin","num_market_pairs": 376,"date_added": "2013-12-15T00:00:00.000Z","tags": ["mineable","pow","scrypt","medium-of-exchange","memes","payments"],"max_supply": "None","circulating_supply": 129952951776.40607,"total_supply": 129952951776.40607,"is_active": 1,"platform": "None","cmc_rank": 6,"is_fiat": 0,"last_updated": "2021-06-09T10:23:03.000Z","quote": {"EUR": {"price": 0.2648561967893428,"volume_24h": 2679960884.4213257,"percent_change_1h": -0.91804719,"percent_change_24h": -2.86532626,"percent_change_7d": -23.78394102,"percent_change_30d": -38.38258941,"percent_change_60d": 411.89322012,"percent_change_90d": 487.94902906,"market_cap": 34418844569.04778,"last_updated": "2021-06-09T10:23:17.000Z"}}}} ' x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d)) return render_template('doge.html', title='DOGE Info', value=x.DOGE) """ data = '{"BTC": {"id": 1,"name": "Bitcoin","symbol": "BTC","slug": "bitcoin","num_market_pairs": 9095,' \ '"date_added": "2013-04-28T00:00:00.000Z", "tags": ["mineable", "pow", "sha-256", "store-of-value", ' \ '"state-channels", "coinbase-ventures-portfolio", "three-arrows-capital-portfolio", ' \ '"polychain-capital-portfolio", "binance-labs-portfolio", "arrington-xrp-capital", ' \ '"blockchain-capital-portfolio", "boostvc-portfolio", "cms-holdings-portfolio", "dcg-portfolio", ' \ '"dragonfly-capital-portfolio", "electric-capital-portfolio", "fabric-ventures-portfolio", ' \ '"framework-ventures", "galaxy-digital-portfolio", "huobi-capital", "alameda-research-portfolio", ' \ '"a16z-portfolio", "1confirmation-portfolio", "winklevoss-capital", "usv-portfolio", ' \ '"placeholder-ventures-portfolio", "pantera-capital-portfolio", "multicoin-capital-portfolio", ' \ '"paradigm-xzy-screener"], "max_supply": 21000000, "circulating_supply": 18734943, "total_supply": ' \ '18734943, "is_active": 1, "platform": None, "cmc_rank": 1," "is_fiat": 0, "last_updated": ' \ '"2021-06-14T21:06:09.000Z", "quote": {"EUR": {"price": 33263.39097356716, "volume_24h": ' \ '40310312912.62971, "percent_change_1h": 0.99812804, "percent_change_24h": 2.44472236, ' \ '"percent_change_7d": 14".80111402, "percent_change_30d": -16.67105913, "percent_change_60d": ' \ '-36.62361734, "percent_change_90d": -28.52918414, "market_cap": 623187733876.4954, "last_updated": ' \ '"2021"-06-14T21:07:18.000Z"}}}} ' y = json.loads(data, object_hook=lambda d: SimpleNamespace(**d)) """
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,439
ijmazur/pwpflask
refs/heads/main
/flaskblog/posts/routes.py
from flask import render_template, url_for, flash, redirect, request, abort, Blueprint from flask_login import current_user, login_required from flaskblog import db from flaskblog.models import Post from flaskblog.posts.forms import PostForm posts = Blueprint('posts', __name__) # need to be logged in to create a post, if information inputted into the Form is correct we add and commit @posts.route("/post/new", methods=['GET', 'POST']) @login_required def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post') # if posts exists = id, otherwise 404, needed so we can delete and update posts @posts.route("/post/<int:post_id>") def post(post_id): post = Post.query.get_or_404(post_id) return render_template('post.html', title=post.title, post=post) # if post does not exist 404, otherwise proceed # if author is not the current logged in user = 403 # otherwise, if information put into PostForm is valid, we accept the changes and flash message @posts.route("/post/<int:post_id>/update", methods=['GET', 'POST']) @login_required def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Update Post', form=form, legend='Update Post') # if post doesnt exist 404, otherwise proceed # if author is not the current logged in user = 403 # otherwise delete post, commit the db, flash message @posts.route("/post/<int:post_id>/delete", methods=['POST']) @login_required def delete_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) db.session.delete(post) db.session.commit() flash('Your post has been deleted', 'success') return redirect(url_for('main.home'))
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,440
ijmazur/pwpflask
refs/heads/main
/flaskblog/users/forms.py
from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed from wtforms import StringField, PasswordField, SubmitField, BooleanField from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError from flask_login import current_user from flaskblog.models import User class RegistrationForm(FlaskForm): # input field for type string, receiving the name of the field and validator that data is required to make sure # its not empty + the length of the string between 2-20characters username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)]) # we check if its a valid email address and we do that by import Email from wtf.validators email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()]) # EqualTo - checking if we input the same password above confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Sign Up') # checking the db to see if the username and email already exists. if it does, we raise an error def validate_username(self, username): user = User.query.filter_by(username=username.data).first() if user: raise ValidationError('That username is taken. Please choose a different one') def validate_email(self, email): user = User.query.filter_by(email=email.data).first() if user: raise ValidationError('This email is already registered') class LoginForm(FlaskForm): #username = StringField('Username', # validators=[DataRequired(), Length(min=2, max=20)]) email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()]) remember = BooleanField('Remember Me') submit = SubmitField('Login') class UpdateAccountForm(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)]) email = StringField('Email', validators=[DataRequired(), Email()]) picture = FileField('Update Profile Picture', validators=[FileAllowed(['jpg', 'png'])]) submit = SubmitField('Update') def validate_username(self, username): if username.data != current_user.username: user = User.query.filter_by(username=username.data).first() if user: raise ValidationError('That username is taken. Please choose a different one') def validate_email(self, email): if email.data != current_user.email: user = User.query.filter_by(email=email.data).first() if user: raise ValidationError('This email is already registered') class RequestResetForm(FlaskForm): email = StringField('Email', validators=[DataRequired(), Email()]) submit = SubmitField('Request Password Reset') def validate_email(self, email): user = User.query.filter_by(email=email.data).first() if user is None: raise ValidationError('There is no account with that email, you must register first') class ResetPasswordForm(FlaskForm): password = PasswordField('Password', validators=[DataRequired()]) confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Reset Password')
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,441
ijmazur/pwpflask
refs/heads/main
/flaskblog/models.py
from datetime import datetime from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from flask import current_app from flaskblog import db, login_manager from flask_login import UserMixin @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) # importing from db.Model # unique ID = column(type(int), primary_key=True (unique ID for our user) # username = has to be a string of max 20, unique, and cant be nullable # same for email and password, image_file # posts attribute is = relationship to our Post model, backref = author, lazy = True # backref is similar to adding another column to the Post Column, we get attribute who created the post # lazy defines when sqlalchemy loads the data, True = it loads it in one go (we can get all posts created by # individual user class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.png') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) # 1800 = 30minutes # into Serializer we pass in the secret_key # and we return token created with this serializer # we dump it with payload of userid, and we use our own self.id that the user resets # and we decode it with utf-8 def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') # verification of the above token, we make a Serializer object with SECRET_KEY again # its in a try except block due to the possibility that token might be expired or invalid # we try to get user_id by loading token and we try to get user_id out of that (user_id comes through payload) # if we do get user_id without throwing exception, we return the user. # if this method does not use self, we need to @staticmethod - telling python not to expect self parameter as arg @staticmethod def verify_reset_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) # dunder method (magic method) - how our object is printed, whenever we print it out. def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')" # same as above # date_posted = DateTime and we default from datetime.utcnow (without ()) because it would mean we want default rn # we want to pass the function as argument and not the date right now # we need username who posted the post, we get a ForeignKey id of user who created the post # in the User model we are referencing the actual Post class # in the user.id ForeignKey we are referencing the tablename and columnname in the db class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
{"/flaskblog/coins/routes.py": ["/flaskblog/models.py"], "/flaskblog/posts/routes.py": ["/flaskblog/models.py"], "/flaskblog/users/forms.py": ["/flaskblog/models.py"]}
9,469
aymane081/python_algo
refs/heads/master
/arrays/increasing_triplet_subsequence.py
class Solution: def has_increasing_subsequence(self, nums): smallest, next_smallest = float('inf'), float('inf') for num in nums: # if num <= smallest: # smallest = num # elif num <= next_smallest: # next_smallest = num # else: # return True # A second way of doing the same smallest = min(smallest, num) if smallest < num: next_smallest = min(next_smallest, min) if next_smallest < num: return True return False
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,470
aymane081/python_algo
refs/heads/master
/arrays/dissapeared_numbers.py
class Solution(object): def dissapeared_numbers(self, numbers): if not numbers: return [] n = len(numbers) result = [i for i in range(1, n + 1)] for num in numbers: result[num - 1] = 0 self.delete_zeros(result) return result def delete_zeros(self, arr): insert_pos = 0 for num in arr: if num != 0: arr[insert_pos] = num insert_pos += 1 for _ in range(insert_pos, len(arr)): arr.pop() def dissapeared_numbers2(self, numbers): if not numbers: return [] for i, num in enumerate(numbers): val = abs(num) - 1 if (numbers[val] > 0): numbers[val] = - numbers[val] result = [] for i, num in enumerate(numbers): if num >= 0: result.append(i + 1) return result solution = Solution() numbers = [4, 3, 2, 7, 8, 2, 3, 1] print(solution.dissapeared_numbers2(numbers))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,471
aymane081/python_algo
refs/heads/master
/arrays/teemo_attacking.py
# 495 # time: O(n) # space: O(1) class Solution: def find_poisoned_duration(self, timeSeries, duration): result = 0 if not timeSeries: return result timeSeries.append(float('inf')) for i in range(1, len(timeSeries)): result += min(timeSeries[i] - timeSeries[i - 1], duration) return result # time: O(with of window * number of attacks) # space: O(1) class Solution2: def find_poisoned_duration(self, timeSeries, duration): result = 0 if not timeSeries: return result temp_poison = 0 for i in range(timeSeries[0], timeSeries[-1] + 1): if i in timeSeries: temp_poison = duration if temp_poison: result += 1 temp_poison -= 1 result += temp_poison return result solution = Solution() print(solution.find_poisoned_duration([1], 2)) print(solution.find_poisoned_duration([1], 2))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,472
aymane081/python_algo
refs/heads/master
/linkedList/linked_list_cycle.py
from utils.listNode import ListNode class Solution: def has_cycle(self, head): if not head or not head.next: return False slow, fast = head.next, head.next.next while fast and fast.next: if fast == slow: return True slow = slow.next fast = fast.next.next return False one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) six = ListNode(6) seven = ListNode(7) one.next = two two.next = three three.next = four four.next = five five.next = six six.next = three solution = Solution() print(solution.has_cycle(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,473
aymane081/python_algo
refs/heads/master
/dynamicProgramming/target_sum.py
from collections import defaultdict class Solution: def target_sum(self, nums, target): if not nums: return 0 sums = defaultdict(int) sums[0] = 1 running = nums[:] for i in range(len(running) - 2, -1, -1): running[i] += running[i + 1] for i, num in enumerate(nums): new_sums = defaultdict(int) for old_sum in sums: if target <= old_sum + running[i]: # if I can reach the target from this sum new_sums[num + old_sum] += sums[old_sum] if target >= old_sum - running[i]: new_sums[old_sum - num] += sums[old_sum] sums = new_sums return sums[target] nums = [1, 1, 1, 1, 1] target = 3 solution = Solution() print(solution.target_sum(nums, target))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,474
aymane081/python_algo
refs/heads/master
/graphs/surronded_regions.py
class Solution: def surrond(self, matrix): if not matrix or not matrix.rows_count or not matrix.cols_count: return matrix for row in range(matrix.rows_count): self.dfs(row, 0, matrix) self.dfs(row, matrix.cols_count - 1, matrix) for col in range(matrix.cols_count): self.dfs(0, col, matrix) self.dfs(matrix.rows_count - 1, col, matrix) for row in range(matrix.rows_count): for col in range(matrix.cols_count): if matrix[row][col] == 'O': matrix[row][col] = 'X' elif matrix[row][col] == '*': matrix[row][col] = 'O' def dfs(self, row, col, matrix): if not matrix.is_valid_cell(row, col): return if matrix[row][col] != 'O': return matrix[row][col] = '*' directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] for dr, dc in directions: self.dfs(row + dr, col + dc, matrix)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,475
aymane081/python_algo
refs/heads/master
/linkedList/reverse_linked_list.py
from utils.listNode import ListNode class Solution: def reverse(self, head): rev = None while head: rev, rev.next, head = head, rev, head.next return rev def reverse2(self, head): rev = None while head: next = head.next head.next = rev rev = head head = next return rev one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) six = ListNode(6) seven = ListNode(7) one.next = two two.next = three three.next = four four.next = five five.next = six six.next = seven print(one) solution = Solution() print(solution.reverse2(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,476
aymane081/python_algo
refs/heads/master
/arrays/maximum_product_subarray.py
class Solution: def get_maximum_product(self, nums): max_so_far = float('-inf') max_here, min_here = 1, 1 for num in nums: max_here, min_here = max(max_here * num, min_here * num, num), min(min_here * num, max_here * num, num) max_so_far = max(max_so_far, max_here) return max_so_far solution = Solution() nums = [2, 3, -2, -4] print(solution.get_maximum_product(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,477
aymane081/python_algo
refs/heads/master
/arrays/summary_ranges.py
class Solution: def get_summary_ranges(self, nums): result = [] for i, num in enumerate(nums): if not result or nums[i] > nums[i - 1] + 1: result.append(str(num)) else: start = result[-1].split(' -> ')[0] result[-1] = ' -> '.join([start, str(num)]) return result solution = Solution() nums = [0, 1, 2, 4, 5, 7] print(solution.get_summary_ranges(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,478
aymane081/python_algo
refs/heads/master
/strings/multiply_strings.py
class Solution(object): def multiply(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ result = [0] * (len(num1) + len(num2)) num1, num2 = num1[::-1], num2[::-1] for i in range(len(num1)): for j in range(len(num2)): product = int(num1[i]) * int(num2[j]) units = product % 10 tens = product // 10 result[i + j] += units if result[i + j] > 9: tens += result[i + j] // 10 result[i + j] %= 10 result[i + j + 1] += tens if result[i + j + 1] > 9: result[i + j + 2] += result[i + j + 1] // 10 result[i + j + 1] %= 10 # remove the trailing zeros from result while len(result) > 0 and result[-1] == 0: result.pop() return ''.join(map(str, result[::-1]))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,479
aymane081/python_algo
refs/heads/master
/dynamicProgramming/house_robber2.py
# 213 class Solution: def rob(self, houses): if not houses: return 0 # last house is not robbed rob_first = self.helper(houses, 0, len(houses) - 2) # first house is not robbed skip_first = self.helper(houses, 1, len(houses) - 1) return max(rob_first, skip_first) def helper(self, houses, start, end): if end == start: return houses[start] curr, prev = 0, 0 for i in range(start, end + 1): curr, prev = max(curr, houses[i] + prev), curr return curr
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,480
aymane081/python_algo
refs/heads/master
/strings/compare_versions.py
import unittest class Solution(object): def compare_versions(self, version1, version2): """ :type version1: str :type version2: str :rtype: int """ if not version1 or not version2: return None digits1 = list(map(int, version1.split('.'))) digits2 = list(map(int, version2.split('.'))) for i in range(max(len(digits1), len(digits2))): v1 = digits1[i] if i < len(digits1) else 0 v2 = digits2[i] if i < len(digits2) else 0 if v1 < v2: return -1 if v1 > v2: return 1 return 0 class Test(unittest.TestCase): data = [('1.2.3', '1.2.1', 1), ('1.2', '1.2.4', -1), ('1', '1.0.0', 0)] def test_compare_versions(self): solution = Solution() for test_data in self.data: actual = solution.compare_versions(test_data[0], test_data[1]) self.assertEqual(actual, test_data[2]) if __name__ == '__main__': unittest.main()
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,481
aymane081/python_algo
refs/heads/master
/arrays/max_consecutive_ones.py
class Solution(object): def max_consecutive_ones(self, numbers): if not numbers: return 0 longest, count = 0, 0 for num in numbers: if num > 0: count += 1 longest = max(longest, count) else: count = 0 return longest solution = Solution() numbers = [1, 1, 0, 1, 1, 1] print(solution.max_consecutive_ones(numbers))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,482
aymane081/python_algo
refs/heads/master
/dynamicProgramming/range_sum_query2.py
from utils.matrix import Matrix class RangeSumQuery: def __init__(self, matrix): """ :type matrix: Matrix :rtype: None """ matrix_sum = [[0 for _ in range(matrix.col_count + 1)] for _ in range(matrix.row_count + 1)] for row in range(1, matrix.row_count + 1): for col in range(1, matrix.col_count + 1): matrix_sum[row][col] = ( matrix[row - 1][col - 1] + matrix_sum[row][col - 1] + matrix_sum[row - 1][col] - matrix_sum[row - 1][col - 1] ) self.matrix_sum = Matrix(matrix_sum) print(self.matrix_sum) def get_range_sum(self, row1, col1, row2, col2): return ( self.matrix_sum[row2 + 1][col2 + 1] - self.matrix_sum[row1][col2 + 1] + self.matrix_sum[row1][col1] - self.matrix_sum[row2 + 1][col1] ) matrix = Matrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]) print("Matrix = {}".format(matrix)) rangeSumQuery = RangeSumQuery(matrix) print(rangeSumQuery.get_range_sum(2, 1, 4, 3)) print(rangeSumQuery.get_range_sum(1, 1, 2, 2)) print(rangeSumQuery.get_range_sum(1, 2, 2, 4))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,483
aymane081/python_algo
refs/heads/master
/linkedList/remove_duplicates.py
#83 from utils.listNode import ListNode class Solution: def remove_duplicates(self, head): if not head: return curr = head while curr and curr.next: if curr.value == curr.next.value: curr.next = curr.next.next else: curr = curr.next return head one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(1) one.next = four four.next = two two.next = three print(one) solution = Solution() print(solution.remove_duplicates(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,484
aymane081/python_algo
refs/heads/master
/binarySearch/min_circular_sorted_array.py
class Solution: def get_min(self, nums): if not nums: return None left, right = 0, len(nums) - 1 while left < right: if nums[left] <= nums[right]: # not rotated break mid = (left + right) // 2 if nums[mid] < nums[left]: # min must be on the left of mid or mid right = mid else: # min must be on the right of mid left = mid + 1 return nums[left] solution = Solution() nums = [7,0,1,2,3,4,5,6] # nums = [4,5,6, 7, 8, 1, 2, 3] print(solution.get_min(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,485
aymane081/python_algo
refs/heads/master
/linkedList/delete_nth_node_from_end.py
#19 from utils.listNode import ListNode class Solution: def delete_from_end(self, head, n): if not head: return front, back = head, head dummy = prev = ListNode(None) while n > 0: back = back.next n -= 1 while back: back = back.next prev.next = front prev = front front = front.next # front is the node I need to delete, and prev is right behind it prev.next = prev.next.next return dummy.next class Solution2: def delete_from_end(self, head, n): first, second = head, head for _ in range(n): first = first.next if not first: # n is the length of the linked list. the first element needs to be removed return head.next while first.next: first = first.next second = second.next # second is right before the nth element from the end second.next = second.next.next return head one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) one.next = two two.next = three three.next = four four.next = five print(one) solution = Solution() print(solution.delete_from_end(one, 2))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,486
aymane081/python_algo
refs/heads/master
/linkedList/odd_even_linked_list.py
# 328 from utils.listNode import ListNode class Solution: def odd_even_list(self, head): if not head: return None odd = head even_head, even = head.next, head.next while even and even.next: odd.next = even.next odd = odd.next even.next = odd.next even = even.next odd.next = even_head return head class Solution2: def odd_even_list(self, head): if not head: return None odd_head = odd_tail = ListNode(None) even_head = even_tail = ListNode(None) node = head count = 1 while node: if count == 1: odd_tail.next = node odd_tail = odd_tail.next else: even_tail.next = node even_tail = even_tail.next count = 1 - count node = node.next even_tail.next = None odd_tail.next = even_head.next return odd_head.next one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) one.next = two two.next = three three.next = four four.next = five print(one) solution = Solution() print(solution.odd_even_list(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,487
aymane081/python_algo
refs/heads/master
/arrays/remove_duplicate_sorted_array.py
class Solution(object): def remove_duplicate(self, arr): """ type arr: list rtype: int """ if not arr: return 0 j = 0 for i in range(1, len(arr)): if arr[i] != arr[j]: j += 1 arr[j] = arr[i] j += 1 for i in range(len(arr) - j): arr.pop() return j solution = Solution() arr = [1, 1, 2, 3, 4, 4] print(solution.remove_duplicate(arr)) print(arr)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,488
aymane081/python_algo
refs/heads/master
/arrays/find_all_duplicates.py
#442 class Solution: def get_duplicates(self, nums): if not nums: return None result = [] for num in nums: index = abs(num) - 1 if nums[index] < 0: result.append(abs(num)) else: nums[index] *= -1 return result solution = Solution() nums = [4, 3, 2, 7, 8, 2, 4, 1] print(solution.get_duplicates(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,489
aymane081/python_algo
refs/heads/master
/graphs/reconstruct_itenirary.py
from collections import defaultdict import unittest class Solution: def reconstruct_itinerary(self, flights): graph = self.build_graph(flights) path = [] self.dfs('JFK', graph, path, len(flights)) return path def dfs(self, node, graph, path, remaining): if node == 'X': return print('current node: {} - current path: {}'.format(node, path)) path.append(node) if remaining == 0: return True for i, nbr in enumerate(graph[node]): # remove nbr from the graph graph[node][i] = 'X' if self.dfs(nbr, graph, path, remaining - 1): return True graph[node][i] = nbr path.pop() return False def build_graph(self, flights): graph = defaultdict(list) for source, dest in flights: graph[source].append(dest) for nbrs in graph.values(): nbrs.sort() return graph class Solution3: def reconstruct_itinerary(self, flights): graph = self.build_graph(flights) path = [] def dfs(airport): path.append(airport) while graph[airport]: nbr = graph[airport].pop() dfs(nbr) dfs('JFK') return path def build_graph(self, flights): graph = defaultdict(list) for start, end in flights: graph[start].append(end) for node in graph: graph[node].sort(reverse=True) return graph class Test(unittest.TestCase): test_data = [[["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]] expected_results = [["JFK", "MUC", "LHR", "SFO", "SJC"], ["JFK","ATL","JFK","SFO","ATL","SFO"]] def test_reconstruct_itinerary(self): solution = Solution3() for i, data in enumerate(self.test_data): self.assertEqual(solution.reconstruct_itinerary(data), self.expected_results[i]) if __name__ == '__main__': unittest.main()
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,490
aymane081/python_algo
refs/heads/master
/trees/binary_tree_pruning.py
#814 from utils.treeNode import TreeNode class Solution: def prune(self, root): if not root: return root root.left, root.right = self.prune(root.left), self.prune(root.right) return root if root.value == 1 or root.left or root.right else None
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,491
aymane081/python_algo
refs/heads/master
/linkedList/linked_list_components.py
# 817 from utils.listNode import ListNode class Solution: # time: O(len of linked list) # space: O(len of G) def get_connected_count(self, head, G): count = 0 if not head: return count values_set = set(G) prev = ListNode(None) prev.next = head while prev.next: if prev.value not in values_set and prev.next.value in values_set: count += 1 prev = prev.next return count zero = ListNode(0) one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) zero.next = one one.next = two two.next = three three.next = four four.next = five print(zero) G = [0, 3, 1, 4] print(G) solution = Solution() print(solution.get_connected_count(zero, G))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,492
aymane081/python_algo
refs/heads/master
/trees/path_sum.py
from utils.treeNode import TreeNode class Solution: def has_path_sum(self, node, sum): if not node: return False sum -= node.value if not sum and not node.left and not node.right: return True return self.has_path_sum(node.left, sum) or self.has_path_sum(node.right, sum)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,493
aymane081/python_algo
refs/heads/master
/arrays/matrix_spiral2.py
class Solution: def generate_spiral(self, n): if n <= 0: raise Exception('n should be bigger than 0') # matrix = [[0] * n] * n # for some reason this does not work matrix = [[0 for _ in range(n)] for _ in range(n)] row, col = 0, 0 d_row, d_col = 0, 1 for i in range(n ** 2): matrix[row][col] = i + 1 next_row, next_col = row + d_row, col + d_col if self.is_out_of_border(next_row, next_col, n) or matrix[next_row][next_col] != 0: # the next cell is either out of border, or already processed. Change direction d_row, d_col = d_col, - d_row # move to the next cell row += d_row col += d_col return matrix def is_out_of_border(self, row, col, n): return row < 0 or row == n or col < 0 or col == n def print_matrix(self, matrix): for row in matrix: print(row) solution = Solution() solution.print_matrix(solution.generate_spiral(3))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,494
aymane081/python_algo
refs/heads/master
/strings/time_conversion.py
import unittest class Solution(object): def time_conversion(self, time_str): if not time_str: return None time_list = list(time_str) is_pm = time_list[-2].lower() == 'p' # handle the 12 AM case. It should be converted to 00 if not is_pm and time_str[:2] == '12': time_list[:2] = ['0', '0'] elif is_pm: hour = str(int(time_str[:2]) + 12) time_list[:2] = list(hour) return ''.join(map(str, time_list[:-2])) class Test(unittest.TestCase): test_data = [('03:22:22PM', '15:22:22'), ('12:22:22AM', '00:22:22')] def test_time_conversion(self): solution = Solution() for data in self.test_data: actual = solution.time_conversion(data[0]) self.assertEqual(actual, data[1]) if __name__ == '__main__': unittest.main()
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,495
aymane081/python_algo
refs/heads/master
/strings/ransom_note.py
import collections import unittest # time: O(M + N) - space: O(N) # def can_construct(ransom, magazine): # if not magazine: # return False # ransom_dict = dict() # for s in ransom: # if s not in ransom_dict: # ransom_dict[s] = 1 # else: # ransom_dict[s] += 1 # for char in magazine: # if char in ransom_dict: # if ransom_dict[char] > 1: # ransom_dict[char] -= 1 # else: # del ransom_dict[char] # return not ransom_dict # def can_construct(ransom, magazine): # return all(ransom.count(x) <= magazine.count(x) for x in set(ransom)) #time: O(M+N) - space: O(M+N) # each time a Counter is produced through an operation, any items with zero or negative counts are discarded class Solution(object): def can_construct(self, ransom, magazine): return not collections.Counter(ransom) - collections.Counter(magazine) class Test(unittest.TestCase): test_data = [('ab', 'aab', True), ('abb', 'aab', False)] def test_can_construct(self): solution = Solution() for data in self.test_data: actual = solution.can_construct(data[0], data[1]) self.assertIs(actual, data[2]) if __name__ == '__main__': unittest.main()
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,496
aymane081/python_algo
refs/heads/master
/binarySearch/find_right_interval.py
from utils.interval import Interval class Solution: def get_right_intervals(self, intervals): intervals = [(intervals[i], i) for i in range(len(intervals))] # In order to do binary search, the array needs to be sorted # We need to sort by the start because the intervals with a bigger start represent the pool of possibilities intervals.sort(key= lambda x: x[0].start) result = [-1 for _ in range(len(intervals))] for index, (interval, i) in enumerate(intervals): left, right = index + 1, len(intervals) # right = len(intervals) means that it is possible to get no right interval while left < right: mid = (left + right) // 2 midInterval = intervals[mid][0] if midInterval.start < interval.end: left = mid + 1 else: right = mid # left is the index of the right interval if left < len(intervals): result[i] = intervals[left][1] return result solution = Solution() interval1 = Interval(1, 4) interval2 = Interval(2, 3) interval3 = Interval(3, 4) intervals = [interval1, interval3, interval2] print("intervals = {}".format(intervals)) print(solution.get_right_intervals(intervals))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,497
aymane081/python_algo
refs/heads/master
/trees/maximum_binary_tree.py
#654 from utils.treeNode import TreeNode class Solution: def build_maximum_tree(self, nums): if not nums: return None return self.helper(nums, 0, len(nums) - 1) def helper(self, nums, start, end): if start > end: return None max_num, index = float('-inf'), -1 for i, num in enumerate(nums[start: end + 1]): if num > max_num: max_num, index = num, i + start root = TreeNode(max_num) root.left = self.helper(nums, start, index - 1) root.right = self.helper(nums, index + 1, end) return root nums = [3,2,1,6,0,5] solution = Solution() print(solution.build_maximum_tree(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,498
aymane081/python_algo
refs/heads/master
/utils/listNode.py
class ListNode: def __init__(self, value): self.value = value self.next = None # def __repr__(self): # return '(value = {}, next: {})'.format(self.value, self.next) def __repr__(self): nodes = [] while self: nodes.append(str(self.value)) self = self.next return ' -> '.join(nodes)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,499
aymane081/python_algo
refs/heads/master
/trees/diameter_binary_tree.py
# 543 from utils.treeNode import TreeNode # time: O(N) # space: O(N) class Solution: def dimater(self, root): self.result = 0 if not root: return self.result def depth(root): left_depth = 1 + depth(root.left) if root.left else 0 right_depth = 1 + depth(root.right) if root.right else 0 self.result = max(self.result, left_depth + right_depth) return max(left_depth, right_depth) depth(root) return self.result # time = O(N) # space = O(N) class Solution2: heights_map = dict() def dimater(self, root): if not root: return 0 return max( self.height(root.left) + self.height(root.right), self.dimater(root.left), self.dimater(root.right) ) def height(self, root): if not root: return 0 if root in self.heights_map: return self.heights_map[root] height = 1 + max(self.height(root.left), self.height(root.right)) self.heights_map[root] = height return height one = TreeNode(1) two = TreeNode(2) three = TreeNode(3) four = TreeNode(4) five = TreeNode(5) six = TreeNode(6) seven = TreeNode(7) one.left = two two.left = three three.left = four two.right = five five.right = six six.right = seven print(one) print('===============') solution = Solution() print(solution.dimater(one)) class SOlution: def get_diameter(self, root): depth_map = dict() if not root: return 0 def depth(root): if not root: return 0 if root in depth_map: return depth_map[root] result = 1 + max(depth(root.left), depth(root.right)) depth_map[root] = result return result return max( 1 + depth(root.left) + depth(root.right), self.get_diameter(root.left), self.get_diameter(root.right) )
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,500
aymane081/python_algo
refs/heads/master
/arrays/number_of_subarrays_bounded_maximum.py
# 795 class Solution: def subarray_count(self, nums, L, R): # dp is the number of subarrays ending with nums[i] result, dp = 0, 0 prev_invalid_index = -1 if not nums: return result for i, num in enumerate(nums): if num < L: result += dp elif num > R: dp = 0 prev_invalid_index = i else: dp = i - prev_invalid_index result += dp return result class Solution2: def subarray_count(self, nums, L, R): prev_invalid_index = -1 res = count = 0 for i, num in enumerate(nums): if num < L: res += count elif num > R: count = 0 prev_invalid_index = i else: count = i - prev_invalid_index res += count return res
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,501
aymane081/python_algo
refs/heads/master
/trees/delete_note_bst.py
class Solution: # time: O(N) worst case, O(height) average # space: O(N) worst case, O(height) average def delete_node(self, root, value): if not root: return None if root.value > value: root.left = self.delete_node(root.left, value) elif root.value < value: root.right = self.delete_node(root.right, value) else: if not root.left or not root.right: # one or no children root = root.left or root.right else: # has both children next_biggest = root.right while next_biggest.left: next_biggest = next_biggest.left root.value = next_biggest.value root.right = self.delete_node(root.right, root.value) return root
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,502
aymane081/python_algo
refs/heads/master
/linkedList/reorder_list.py
#143 from utils.listNode import ListNode class Solution: def reorder(self, head): if not head: return head fast, slow = head, head while fast and fast.next: fast = fast.next.next slow = slow.next rev, node = None, slow while node: rev, rev.next, node = node, rev, node.next first, second = head, rev while second.next: first.next, first = second, first.next second.next, second = first, second.next return head class Solution2: def reorder(self, head): list_map = dict() curr, i = head, 1 while curr: list_map[i] = curr curr = curr.next i += 1 left, right = 1, i - 1 node = head while left <= right: node.next = list_map[right] left += 1 if left <= right: node = node.next node.next = list_map[left] right -= 1 node = node.next node.next = None return head one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) one.next = two two.next = three three.next = four four.next = five print(one) solution = Solution() print(solution.reorder(one)) def reorder(head): if not head: return None slow = fast = head while fast and fast.next: fast = fast.next.next slow = slow.next rev = ListNode(None) while slow: rev, rev.next, slow = slow, rev, slow.next first, second = head, rev while second.next: first.next, first = second, first.next second.next, second = first, second.next
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,503
aymane081/python_algo
refs/heads/master
/trees/binary_tree_from_sorted_array.py
from utils.treeNode import TreeNode class Solution: # time: O(N) - space: O(N) def build_binary_tree(self, nums): if not nums: return None return self.convert(nums, 0, len(nums) - 1) def convert(self, nums, left, right): if left > right: return None mid = (left + right) // 2 left = self.convert(nums, left, mid - 1) right = self.convert(nums, mid + 1, right) root = TreeNode(nums[mid], left, right) return root nums = [1,2,3,4,5,6,7,8,9] solution = Solution() print(solution.build_binary_tree(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,504
aymane081/python_algo
refs/heads/master
/utils/matrix.py
class Matrix: def __init__(self, rows): self.rows = rows self.row_count = len(rows) self.col_count = len(rows[0]) def is_valid_cell(self, row, col): return ( row >= 0 and row < self.row_count and col >= 0 and col < self.col_count ) def __repr__(self): result = '' for row in self.rows: result += str(row) + '\n' return result def __getitem__(self, index): return self.rows[index] def __setitem__(self, index, value): self.rows[index] = value
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,505
aymane081/python_algo
refs/heads/master
/arrays/target_sum_amazon.py
class Solution: def sum_target(self, collection1, collection2, target): result = [] sum_dict = dict() for nums in [collection1, collection2]: for num in nums: remaining = target - num if remaining in sum_dict: result.append((remaining, num)) if sum_dict[remaining] == 1: del sum_dict[remaining] else: sum_dict[remaining] -= 1 else: sum_dict[num] = 1 if num not in sum_dict else sum_dict[num] + 1 return result collection1 = [4, 5, 2, 1, 1, 8] collection2 = [7, 1, 8, 8] solution = Solution() print(solution.sum_target(collection1, collection2, 9))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,506
aymane081/python_algo
refs/heads/master
/trees/invert_binary_tree.py
from utils.treeNode import TreeNode class Solution: def invert(self, node): if not node: return self.invert(node.left) self.invert(node.right) node.left, node.right = node.right, node.left node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node5 = TreeNode(5) node6 = TreeNode(6) node7 = TreeNode(7) node1.left = node2 node1.right = node3 node2.left = node4 node2.right = node5 node3.left = node6 node6.left = node7 print(node1) print('=================') solution = Solution() solution.invert(node1) print(node1)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,507
aymane081/python_algo
refs/heads/master
/strings/simplify_path.py
class Solution(object): def simplify_path(self, path): """ :type path: str :rtype: str """ directories = path.split('/') # get the directories from the path result = [] # stack to hold the result for dir in directories: if dir == '..' and result: # go up one level if possible result.pop() elif dir and dir != '.': # add the dir to the stack result.append(dir) # else ignore '' and '.' return '/' + result[-1] if result else '/' solution = Solution() print(solution.simplify_path('/a/b/c/./../'))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,508
aymane081/python_algo
refs/heads/master
/arrays/insert_delete_get_random.py
import random class RandomizedSet: def __init(self): self.mapping = {} self.items = [] def insert(self, value): if value not in self.mapping: self.items.append(value) self.mapping[value] = len(self.items) - 1 return True return False def remove(self, value): if value not in self.mapping: return False index = self.mapping[value] self.items[index] = self.items[-1] self.mapping[self.items[index]] = index self.items.pop() del self.mapping[value] return True def get_random(self): index = random.randint(0, len(self.items) - 1) return self.items[index] class RandomizedSet2: def __init__(self): self.mapping = {} self.items = [] def insert(self, value): if value not in self.mapping: self.items.append(value) self.mapping[value] = len(self.items) - 1 return True return False def remove(self, value): if value in self.mapping: index = self.mapping[value] self.items[index] = self.items[-1] self.mapping[self.items[-1]] = index self.items.pop() del self.mapping[value] return True return False def get_random(self): index = random.randint(0, len(self.items) - 1) return self.items[index]
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,509
aymane081/python_algo
refs/heads/master
/trees/print_binary_tree.py
# 655 #time: O(H * 2**H - 1) need to fill the result array # space: O(H * 2**H - 1) the number of elements in the result array from utils.treeNode import TreeNode class Solution: def print(self, root): if not root: return [] height = self.get_height(root) result = [["" for _ in range((2 ** height - 1))] for _ in range(height)] self.traverse(root, 0, (2 ** height) - 2, 0, result) return result # DFS traverse def traverse(self, root, start, end, level, result): if not root: return mid = (start + end) // 2 result[level][mid] = root.value self.traverse(root.left, start, mid - 1, level + 1, result) self.traverse(root.right, mid + 1, end, level + 1, result) def get_height(self, root): if not root: return 0 return 1 + max(self.get_height(root.left), self.get_height(root.right)) one = TreeNode(1) two = TreeNode(2) three = TreeNode(3) four = TreeNode(4) five = TreeNode(5) one.left = two two.left = three three.left = four one.right = five print(one) print('==============') solution = Solution() print(solution.print(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,510
aymane081/python_algo
refs/heads/master
/arrays/majority_element.py
class Solution(object): # this algorithm is called the Boyer-Moore majority voting algorithm # https://stackoverflow.com/questions/4325200/find-the-majority-element-in-array # the majority element appears more than all the other elements combined. Therefore, if we keep a # counter and change the major every time the counter is 0, eventually, major will be the major element def majority_element(self, numbers): counter, major = 0, None for num in numbers: if counter == 0: counter = 1 major = num elif num == major: counter += 1 else: counter -= 1 # major is guaranteed to be the major element if it exists. # otherwise, iterate over the array, and count the occurrences of major #return major counter = 0 for num in numbers: if num == major: counter += 1 if counter > len(numbers) / 2: return major return None numbers = [2, 3, 5, 3, 5, 6, 5, 5, 5] solution = Solution() print(solution.majority_element(numbers))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,511
aymane081/python_algo
refs/heads/master
/arrays/flipping_image.py
# 832 class Solution: def flip(self, matrix): for row in matrix: for i in range((len(row) + 1) // 2): row[i], row[-1 -i] = 1 - row[-1 -i], 1 - row[i] return matrix solution = Solution() matrix = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]] print(solution.flip(matrix))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,512
aymane081/python_algo
refs/heads/master
/linkedList/delete_node_linked_list.py
from utils.listNode import ListNode #237 class Solution: def delete_node(self, node): # node is not the tail => node.next exists node.value = node.next.value node.next = node.next.next
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,513
aymane081/python_algo
refs/heads/master
/dynamicProgramming/maximum_square.py
from utils.matrix import Matrix class Solution: # O(m * n * min(m, n)) time and O(1) space def get_max_square(self, matrix): """ :type matrix: Matrix :rtype: int """ max_area = 0 for row in range(1, matrix.row_count): for col in range(1, matrix.col_count): if matrix[row][col] == 0: continue max_length = matrix[row - 1][col - 1] length = 1 while length <= max_length: if matrix[row - length][col] == 0 or matrix[row][col - 1] == 0 or matrix[row - 1][col - 1] == 0: break length += 1 matrix[row][col] = length ** 2 max_area = max(max_area, matrix[row][col]) return max_area # dynamic programming: changing the matrix itself def get_max_square2(self, matrix): """ :type matrix: Matrix :rtype: int """ max_side = 0 if not matrix or not matrix.row_count or not matrix.col_count: return max_side for row in range(1, matrix.row_count): for col in range(1, matrix.col_count): if matrix[row][col] == 0: continue matrix[row][col] = 1 + min(matrix[row - 1][col], matrix[row][col - 1], matrix[row - 1][col - 1]) max_side = max(max_side, matrix[row][col]) return max_side ** 2 # dynamic programming: keeping an array of longest square sides def get_max_square3(self, matrix): """ :type matrix: Matrix :rtype: int """ max_side = 0 if not matrix or not matrix.row_count or not matrix.col_count: return max_side max_sides = [0 for _ in range(matrix.col_count)] for row in range(matrix.row_count): new_max_sides = [matrix[row][0]] + [0 for _ in range(1, matrix.col_count)] max_side = max(max_side, matrix[row][0]) for col in range(1, matrix.col_count): if matrix[row][col] == 0: continue new_max_sides[col] = 1 + min(new_max_sides[col - 1], max_sides[col], max_sides[col - 1]) max_side = max(max_side, new_max_sides[col]) max_sides = new_max_sides return max_side ** 2 matrix = Matrix([[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]]) print(matrix) print('===============') solution = Solution() print(solution.get_max_square3(matrix))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,514
aymane081/python_algo
refs/heads/master
/trees/binary_tree_paths.py
from utils.treeNode import TreeNode class Solution: def get_paths(self, node): result = [] if not node: return result self.helper([], node, result) return [" -> ".join(path) for path in result] def helper(self, prefix, node, result): if not node.left and not node.right: #reached a leaf result.append(prefix + [str(node.value)]) return if node.left: self.helper(prefix + [str(node.value)], node.left, result) if node.right: self.helper(prefix + [str(node.value)], node.right, result) node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node5 = TreeNode(5) node6 = TreeNode(6) node7 = TreeNode(7) node1.left = node2 node1.right = node3 node2.left = node4 node2.right = node5 node3.left = node6 node6.left = node7 print(node1) solution = Solution() print(solution.get_paths(node1))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,515
aymane081/python_algo
refs/heads/master
/dynamicProgramming/triangle.py
class Solution: def get_min_path(self, triangle): """ type triangle: List[List[int]] rtype: int """ if not triangle: return 0 for row in range(len(triangle) - 2, -1, -1): for col in range(row + 1): triangle[row][col] += min(triangle[row + 1][col], triangle[row + 1][col + 1]) return triangle[0][0] triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]] solution = Solution() print(solution.get_min_path(triangle))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,516
aymane081/python_algo
refs/heads/master
/strings/substring_without_repeating_characters.py
# Time: O(N) - Space: O(1): the length of the set/map is bounded by the number of the alphabet # set.clear() is O(1) class Solution(object): def longest_unique_substring(self, str): if not str: return 0 str_set = set() result = 0 for char in str: if char not in str_set: # Non repeated character str_set.add(char) result = max(result, len(str_set)) else: # Repeated character str_set.clear() str_set.add(char) # result = max(result, len(str_set)) return result # Sliding window technique # Maitain a sliding window, updating the start whenever we see a character repeated def longest_unique_substring2(self, s): """ :type str: str :rtype: int """ if not s: return 0 start = 0 # start index of the current window(substring) longest = 0 last_seen = {} # mapping from character to its last seen index for i, char in enumerate(s): if char in last_seen and last_seen[char] >= start: # start a new substring after the previous c start = last_seen[char] + 1 else: longest = max(longest, i - start + 1) last_seen[char] = i # update the last sightning index return longest
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,517
aymane081/python_algo
refs/heads/master
/arrays/word_search.py
from utils.matrix import Matrix class Solution: def exist(self, matrix, word): """ :type matrix: Matrix :type word: Str :rtype: boolean """ if not matrix.row_count or not matrix.col_count: return False for row in range(matrix.row_count): for col in range(matrix.col_count): if self.can_find(matrix, row, col, 0, word): return True return False def can_find(self, matrix, row, col, index, word): """ :type matrix: Matrix :type row: int :type col: int :type index: int :type word: str :rtype: boolean """ if index == len(word): return True if not matrix.is_valid_cell(row, col): return False if matrix[row][col] != word[index]: return False # in order to avoid using the same cell twice, we should mark it matrix[row][col]= '*' found = ( self.can_find(matrix, row + 1, col, index + 1, word) or self.can_find(matrix, row - 1, col, index + 1, word) or self.can_find(matrix, row, col + 1, index + 1, word) or self.can_find(matrix, row, col - 1, index + 1, word) ) if found: return True matrix[row][col] = word[index] return False matrix = Matrix([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']]) solution = Solution() print(matrix) print(solution.exist(matrix, 'ABCCED'))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,518
aymane081/python_algo
refs/heads/master
/dynamicProgramming/longest_increasing_sequence.py
from bisect import bisect class Solution: # Time: O(n log n) - Space: O(n) def get_longest_increasing_sequence(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 dp = [] for num in nums: index = bisect(dp, num) if index == len(dp): dp.append(num) else: # num is smaller than the current value at dp[index], therefore, num can be used to build a # longer increasing sequence dp[index] = num return len(dp) solution = Solution() nums = [5, 2, 3, 8, 1, 19, 7] print(solution.get_longest_increasing_sequence(nums))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,519
aymane081/python_algo
refs/heads/master
/arrays/remove_duplicate_sorted_array2.py
class Solution: def remove_duplicates(self, nums): if len(nums) <= 2: return len(nums) j = 1 for i in range(2, len(nums)): if nums[i] > nums[j - 1]: j += 1 nums[j] = nums[i] j += 1 for _ in range(j, len(nums)): nums.pop() return j solution = Solution() nums = [1,1,1,2,2,3] print(solution.remove_duplicates(nums)) print(nums)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,520
aymane081/python_algo
refs/heads/master
/graphs/topological_sort.py
class Solution: def topological_sort(self, graph): result = [] discovered = set() path = [] for node in graph: self.helper(node, result, discovered, path) return result.reverse() def helper(self, node, result, discovered, path): if node in discovered: return path.append(node) discovered.add(node) for nbr in node.neighbors: if nbr in path: raise Exception('Cyclic graph') self.helper(nbr, result, discovered, path) path.pop() result.append(node.key)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,521
aymane081/python_algo
refs/heads/master
/binarySearch/intersection_of_two_arrays.py
class Solution: def get_interesection(self, nums1, nums2): set1 = set(nums1) intersection = set() for num in nums2: if num in set1: intersection.add(num) return list(intersection)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,522
aymane081/python_algo
refs/heads/master
/arrays/container_with_most_water.py
class Solution(object): def max_area(self, heights): """ :type heights: List(int) :rtype: int """ if not heights: return 0 left = 0 right = len(heights) - 1 # calculate the area of the outer container max_area = (right - left) * min(heights[left], heights[right]) # start moving in-ward. # In order to get a bigger area, the min of both left and right borders need to be higher while left < right: if heights[left] < heights[right]: # increment left for the possibility of finding a larger area left += 1 else: right -= 1 max_area = max(max_area, (right - left) * min(heights[left], heights[right])) return max_area
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,523
aymane081/python_algo
refs/heads/master
/arrays/random_pick_index.py
import random from collections import defaultdict class Solution: # O(1) space and time in initialization. O(n) time and O(1) space when getting the rand index def __init__(self, nums): self.nums = nums def get_random_index(self, target): result, count = None, 0 for i, num in enumerate(self.nums): if num == target: if random.randint(0, count) == 0: result = i count += 1 return result #Solution 2: # O(N) space and time in initilization. O(1) space and time when picking random index # def __init__(self, nums): # self.nums = nums # self.mapping = defaultdict(list) # for i, num in enumerate(nums): # self.mapping[num].append(i) # def get_random_index(self, target): # return random.choice(self.mapping[target]) nums = [1, 3, 2, 2, 3, 3, 3, 4] solution = Solution(nums) print(solution.get_random_index(2))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,524
aymane081/python_algo
refs/heads/master
/strings/longestCommonPrefix.py
class Solution: def get_longest_common_prefix(self, words): if not words: return '' min_word_length = min(words, key=len) start, end = 0, len(min_word_length) - 1 while start <= end: mid = (start + end) // 2 if not self.is_common_prefix(mid, words): end = mid - 1 else: start = mid + 1 return words[0][:end + 1] def is_common_prefix(self, length, words): prefix = words[0][:length + 1] for word in words: if not word.startswith(prefix): return False return True list_strings = ['abu', 'abcd', 'abce', 'abcee'] solution = Solution() print(solution.get_longest_common_prefix(list_strings))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,525
aymane081/python_algo
refs/heads/master
/utils/interval.py
class Interval: def __init__(self, start, end): self.start = start self.end = end def __repr__(self): return "[{0}, {1}]".format(self.start, self.end)
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,526
aymane081/python_algo
refs/heads/master
/dynamicProgramming/word_break.py
class Solution: def word_break(self, s, wordDict): if not wordDict: return False if not s: return True can_make = [True] + [False for _ in range(len(s))] for i in range(1, len(s) + 1): for j in range(i - 1, -1, -1): if can_make[j] and s[j:i] in wordDict: can_make[i] = True break return can_make[-1]
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,527
aymane081/python_algo
refs/heads/master
/linkedList/merge_two_sorted_lists.py
from utils.listNode import ListNode class Solution: def merge(self, l1, l2): if not l1 or not l2: return l1 or l2 node1, node2 = l1, l2 head = None curr = None while node1 and node2: min_value = min(node1.value, node2.value) if min_value == node1.value: node1 = node1.next else: node2 = node2.next if not head: head = ListNode(min_value) curr = head else: curr.next = ListNode(min_value) curr = curr.next if node1: curr.next = node1 elif node2: curr.next = node2 return head # time: O(m + n) # space: O(1) class Solution2: def merge(self, l1, l2): prev = dummy = ListNode(None) while l1 and l2: if l1.value < l2.value: prev.next = l1 l1 = l1.next else: prev.next = l2 l2 = l2.next prev = prev.next prev.next = l1 or l2 return dummy.next one = ListNode(1) two = ListNode(2) four = ListNode(4) one.next = two two.next = four one_ = ListNode(1) three_ = ListNode(3) four_ = ListNode(4) one_.next = three_ three_.next = four_ print(one) print(one_) solution = Solution2() print(solution.merge(one, one_))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,528
aymane081/python_algo
refs/heads/master
/graphs/clone_graph.py
class Node: def __init__(self, label): self.label = label self.neighbors = [] class Solution: def clone_graph(self, node): if not node: return None cloned_start = Node(node.key) node_mapping = { node: cloned_start} queue = [node] while queue: node = queue.pop() cloned_node = node_mapping[node] for nbr in node.neighbors: if nbr not in node_mapping: cloned_nbr = Node(nbr.key) node_mapping[nbr] = cloned_nbr queue.append(nbr) else: cloned_nbr = node_mapping[nbr] cloned_node.neighbors.append(cloned_nbr) return cloned_start def clone_graph2(self, node): mapping = dict() return self.helper(node, mapping) def helper(self, node, mapping): if node in mapping: return mapping[node] cloned_node = Node(node.key) for nbr in node.neighbors: cloned_nbr = self.helper(nbr, mapping) cloned_node.neighbors.append(cloned_nbr) mapping[node] = cloned_node return cloned_node
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,529
aymane081/python_algo
refs/heads/master
/arrays/search_matrix.py
from utils.matrix import Matrix import unittest class Solution: def search(self, matrix, value): if value < matrix[0][0] or value > matrix[-1][-1]: return False row = self.get_row(matrix, value) return self.binary_search_row(matrix[row], value) def get_row(self, matrix, value): left, right = 0, matrix.row_count - 1 while left <= right: mid = (left + right) // 2 if value < matrix[mid][0]: right = mid - 1 elif value > matrix[mid][-1]: left = mid + 1 else: return mid return left def binary_search_row(self, nums, value): left, right = 0, len(nums) - 1 while left <= right: mid = (right + left) // 2 if value == nums[mid]: return True if value > nums[mid]: left = mid + 1 else: right = mid - 1 return False #time: O(col_count + row_count) - space: O(1) def search2(self, matrix, value): row, col = matrix.row_count - 1, 0 while row >= 0 and col < matrix.col_count: if matrix[row][col] == value: return True elif value < matrix[row][col]: row -= 1 else: col += 1 return False class Test(unittest.TestCase): matrix = Matrix([[1,3,5,7],[10,11,16,20],[23,30,34,50]]) test_data = [(3, True), (9, False), (0, False), (56, False), (30, True)] def test_search(self): solution = Solution() for data in self.test_data: actual = solution.search2(self.matrix, data[0]) self.assertEqual(actual, data[1]) if __name__ == '__main__': unittest.main()
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,530
aymane081/python_algo
refs/heads/master
/arrays/subarray_sum_equals_k.py
# 560 from collections import defaultdict # time: O(N) # space: O(N) class Solution: def subarray_sum(self, nums, k): result = 0 if not nums: return result sum_map = defaultdict(int) sum_map[0] = 1 curr_sum = 0 for num in nums: curr_sum += num result += sum_map[curr_sum - k] sum_map[curr_sum] += 1 return result nums = [1, 1, 1] solution = Solution() print(solution.subarray_sum(nums, 2))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,531
aymane081/python_algo
refs/heads/master
/graphs/evaluate_division.py
from collections import defaultdict class Solution: def solve_queries(self, equations, values, queries): graph = self.build_graph(equations, values) result = [] for query in queries: result.append(self.dfs(query[0], query[1], 1, graph, set())) return result def dfs(self, start, target, temp_result, graph, visited): if not start in graph or start in visited: return -1 if start == target: return temp_result visited.add(start) for nbr, division in graph[start].items(): result = self.dfs(nbr, target, temp_result * division, graph, visited) if result != -1: # found the target return result return -1 def build_graph(self, equations, values): graph = defaultdict(dict) for i, eq in enumerate(equations): graph[eq[0]][eq[1]] = values[i] graph[eq[1]][eq[0]] = 1 / values[i] return graph equations = [ ["a", "b"], ["b", "c"] ] values = [2.0, 3.0] queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ] solution = Solution() print(solution.solve_queries(equations, values, queries))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,532
aymane081/python_algo
refs/heads/master
/arrays/contains_duplicates.py
class Solution(object): def contains_duplicates(self, numbers): number_set = set(numbers) return len(numbers) != len(number_set) def contains_duplicates2(self, numbers): """ :type numbers: list :rtype : Boolean """ numbers.sort() for i in range(1, len(numbers)): if numbers[i] == numbers[i - 1]: return True return False numbers = [1, 2, 1, 4] solution = Solution() print(solution.contains_duplicates(numbers))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,533
aymane081/python_algo
refs/heads/master
/binarySearch/sqrt.py
class Solution: def sqrt(self, n): if n == 0: return 0 left, right = 1, n # while True: # mid = (left + right) // 2 # if mid * mid > n: # right = mid - 1 # else: # if (mid + 1) * (mid + 1) > n: # return mid # left = mid + 1 # use the while left <= right and return left - 1 when looking for the max value that satisfies a condition. because we do left = mid + 1, the last value of left will be our result + 1 while left <= right: mid = (left + right) // 2 if mid * mid > n: right = mid - 1 else: left = mid + 1 return left - 1 solution = Solution() print(solution.sqrt(24))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,534
aymane081/python_algo
refs/heads/master
/graphs/course_schedule.py
from collections import defaultdict class Solution: def course_schedule(self, classes): if not classes: return[] graph = self.build_graph(classes) result = [] path = [] discovered = set() for node in graph: self.topological_sort(node, graph, path, discovered, result) return result.reverse() def build_graph(self, classes): graph = defaultdict(set) for dep in classes: graph[dep[1]].add(dep[0]) return graph def topological_sort(self, node, graph, path, discovered, result): if node in discovered: return discovered.add(node) path.append(node) for nbr in graph[node]: if nbr in path: raise Exception('Cyclic dependency. Cannot finish all the courses') self.topological_sort(nbr, graph, path, discovered, result) result.append(result) path.pop() def can_finish(self, courses_count, prerequisites): " Returns True if can finish all the courses " nb_prerequisites = defaultdict(int) # mapping between each course and the number of its pre-requisites preq_map = defaultdict(list) # mapping between each course and the courses depending on it for after, before in prerequisites: nb_prerequisites[after] += 1 preq_map[before].append(after) # get the list of courses with no dependencies can_take = set(range(courses_count)) - set(nb_prerequisites.keys()) while can_take: course = can_take.pop() courses_count -= 1 for dep in preq_map[course]: nb_prerequisites[dep] -= 1 if nb_prerequisites[dep] == 0: can_take.add(dep) return courses_count == 0 def get_order(self, num_courses, prerequisites): nb_prerequisites = defaultdict(int) preq_list = defaultdict(list) result = [] for (after, before) in prerequisites: nb_prerequisites[after] += 1 preq_list[before].append(after) can_take = set(i for i in range(num_courses)) - set(nb_prerequisites.keys()) while can_take: course = can_take.pop() result.append(course) for dep in preq_list[course]: nb_prerequisites[dep] -= 1 if nb_prerequisites[dep] == 0: can_take.append(dep) return result if len(result) == num_courses else []
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,535
aymane081/python_algo
refs/heads/master
/arrays/reshape_matrix.py
# 566 # time: O(N * M) # space: O(N * M) class Solution: def reshape(self, nums, r, c): if not nums or len(nums) * len(nums[0]) != r * c: return nums rows, cols = len(nums), len(nums[0]) queue = [] for row in range(rows): for col in range(cols): queue.append(nums[row][col]) res, count =[], 0 for row in range(r): res.append([]) for col in range(c): res[-1].append(queue[count]) count += 1 return res # time: O(N * M) # space: O(N * M) class Solution2: def reshape(self, nums, r, c): if not nums or len(nums) * len(nums[0]) != r * c: return nums res = [[] * r] rows = cols = 0 for i in range(len(nums)): for j in range(len(nums[0])): res[rows].append(nums[i][j]) cols += 1 if cols == c: rows += 1 cols = 0 return res nums = [[1,2], [3,4]] solution = Solution2() print(solution.reshape(nums, 1, 4))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,536
aymane081/python_algo
refs/heads/master
/trees/maximum_width_binary_tree.py
#662 from utils.treeNode import TreeNode class Solution2: def max_width(self, root): queue = [(root, 0, 0)] curr_level, left, result = 0, 0, 0 for node, pos, level in queue: if node: queue.append((node.left, 2 * pos, level + 1)) queue.append((node.right, (2 * pos) + 1, level + 1)) if curr_level != level: curr_level = level left = pos result = max(result, pos - left + 1) return result class Solution: def max_width(self, root): if not root: return 0 level = [(root, 0)] left, right, result = float('inf'), 0, 1 while level: new_level = [] for node, pos in level: if node: new_level.append((node.left, 2 * pos)) new_level.append((node.right, (2 * pos) + 1)) left = min(left, pos) right = max(right, pos) result = max(result, right - left + 1) left, right = float('inf'), 0 level = new_level return result one = TreeNode(1) two = TreeNode(1) three = TreeNode(1) four = TreeNode(1) five = TreeNode(1) six = TreeNode(1) seven = TreeNode(1) one.left = two two.left = three three.left = four one.right = five five.right = six six.right = seven print(one) print('============') solution = Solution2() print(solution.max_width(one))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,537
aymane081/python_algo
refs/heads/master
/strings/reverse_words_in_string.py
class Solution(object): def reverse_words(self, string): if not string: return '' # words = string.split() # return ' '.join(words[::-1]) word_lists = [[]] for i, c in enumerate(string): if c != ' ': word_lists[-1].append(c) elif string[i] == ' ' and i < len(string) - 1 and string[i + 1] != ' ': word_lists.append([]) words = [''.join(word_list) for word_list in word_lists] return ' '.join(words[::-1]) class Solution2(object): # time: O(n) # space: O(n) because we transform the str to list, otherwise it is O(1) def reverse_words(self, s): string_list = list(s) self.reverse(string_list, 0, len(string_list) - 1) string_list.append(' ') start = 0 for i, c in enumerate(string_list): if string_list[i] == ' ': self.reverse(string_list, start, i - 1) start = i + 1 string_list.pop() return ''.join(string_list) def reverse(self, s, left, right): while left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 solution = Solution2() print(solution.reverse_words('The sky is blue'))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,538
aymane081/python_algo
refs/heads/master
/strings/anagram_groups.py
from collections import defaultdict # Gotcha 1: there is no sort for strings. # You have to convert the word to a list, then sort it using list.sort(), # then reconstruct the string using ''.join(sorted_list) # Gotcha 2: defaultdict is part of the collections library class Solution(object): # Time: O(n * k * log k) where n is the number of words, and k is the length of the longest word # Space: O(n * k) to hold the result - k * n is the total number of characters def group_anagrams(self, words): """ :type words: List[str] :rtype: List[List[str]] """ sorted_dict = defaultdict(list) for word in words: letter_list = list(word) # or [c for c in word] letter_list.sort() sorted_word = ''.join(letter_list) sorted_dict[sorted_word].append(word) return list(sorted_dict.values()) solution = Solution() print(solution.group_anagrams(['bat', 'car', 'atb', 'rca', 'aaa']))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}
9,539
aymane081/python_algo
refs/heads/master
/linkedList/intersection_of_two_linked_list.py
from utils.listNode import ListNode class Solution: def get_intersection(self, head1, head2): if not head1 or not head2: return None l1, l2 = self.get_length(head1), self.get_length(head2) node1, node2 = self.move_ahead(head1, l1 - l2), self.move_ahead(head2, l2 - l1) while node1 and node2: if node1 == node2: return node1 node1 = node1.next node2 = node2.next return None def get_length(self, head): count = 0 node = head while node: count += 1 node = node.next return count def move_ahead(self, head, l): if l <= 0: return head curr = head while l > 0: curr = curr.next l -= 1 return curr class Solution2: def get_intersection(self, head1, head2): if not head1 or not head2: return None savedA, savedB = head1, head2 while head1 != head2: head1 = savedB if not head1 else head1.next head2 = savedA if not head2 else head2.next return head1 one = ListNode(1) two = ListNode(2) three = ListNode(3) four = ListNode(4) five = ListNode(5) six = ListNode(6) seven = ListNode(7) one.next = two two.next = three three.next = four four.next = five six.next = seven seven.next = two print(one) print(six) solution = Solution() print(solution.get_intersection(one, six))
{"/linkedList/linked_list_cycle.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/range_sum_query2.py": ["/utils/matrix.py"], "/linkedList/remove_duplicates.py": ["/utils/listNode.py"], "/linkedList/delete_nth_node_from_end.py": ["/utils/listNode.py"], "/linkedList/odd_even_linked_list.py": ["/utils/listNode.py"], "/trees/binary_tree_pruning.py": ["/utils/treeNode.py"], "/linkedList/linked_list_components.py": ["/utils/listNode.py"], "/trees/path_sum.py": ["/utils/treeNode.py"], "/binarySearch/find_right_interval.py": ["/utils/interval.py"], "/trees/maximum_binary_tree.py": ["/utils/treeNode.py"], "/trees/diameter_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/reorder_list.py": ["/utils/listNode.py"], "/trees/binary_tree_from_sorted_array.py": ["/utils/treeNode.py"], "/trees/invert_binary_tree.py": ["/utils/treeNode.py"], "/trees/print_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/delete_node_linked_list.py": ["/utils/listNode.py"], "/dynamicProgramming/maximum_square.py": ["/utils/matrix.py"], "/trees/binary_tree_paths.py": ["/utils/treeNode.py"], "/arrays/word_search.py": ["/utils/matrix.py"], "/linkedList/merge_two_sorted_lists.py": ["/utils/listNode.py"], "/arrays/search_matrix.py": ["/utils/matrix.py"], "/trees/maximum_width_binary_tree.py": ["/utils/treeNode.py"], "/linkedList/intersection_of_two_linked_list.py": ["/utils/listNode.py"], "/trees/substree_of_another_tree.py": ["/utils/treeNode.py"], "/linkedList/split_linked_list_in_parts.py": ["/utils/listNode.py"], "/trees/lowest_common_ancestor.py": ["/utils/treeNode.py"], "/graphs/number_of_islands.py": ["/utils/matrix.py"], "/trees/most_frequent_substree_sum.py": ["/utils/treeNode.py"], "/linkedList/rotate_list.py": ["/utils/listNode.py"], "/linkedList/reverse_linked_list2.py": ["/utils/listNode.py"], "/linkedList/partition_list.py": ["/utils/listNode.py"], "/trees/same_tree.py": ["/utils/treeNode.py"], "/trees/next_right_pointer.py": ["/utils/treeNode.py"], "/trees/path_sum3.py": ["/utils/treeNode.py"], "/trees/is_balanced.py": ["/utils/treeNode.py"], "/trees/unique_bst2.py": ["/utils/treeNode.py"], "/trees/binary_tree_level_order_traversal.py": ["/utils/treeNode.py"], "/trees/minimum_depth.py": ["/utils/treeNode.py"], "/trees/sum_root_to_leaf_numbers.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/binary_tree_side_view.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/arrays/merge_intervals.py": ["/utils/interval.py"], "/arrays/min_path_sum.py": ["/utils/matrix.py"], "/trees/binary_tree_inorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/preorder_traversal.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_largest_value_in_each_tree_row.py": ["/utils/treeNode.py"], "/graphs/dfs.py": ["/utils/graph.py"], "/trees/trim_binary_search_tree.py": ["/utils/treeNode.py"], "/linkedList/remove_duplicates2.py": ["/utils/listNode.py"], "/arrays/diagonal_traverse.py": ["/utils/matrix.py"], "/trees/symetric_tree.py": ["/utils/treeNode.py"], "/utils/treeUtils.py": ["/utils/treeNode.py"], "/linkedList/remove_linked_list_elements.py": ["/utils/listNode.py"], "/trees/second_minimum_binary_tree.py": ["/utils/treeNode.py"], "/graphs/pacific_atlantic.py": ["/utils/matrix.py"], "/trees/binary_tree_zigzag_level_order.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/linkedList/swap_nodes_in_pair.py": ["/utils/listNode.py"], "/trees/add_one_row_to_tree.py": ["/utils/treeNode.py"], "/trees/tree_from_preorder_inorder.py": ["/utils/treeUtils.py"], "/linkedList/add_two_numbers.py": ["/utils/listNode.py"], "/trees/bst_iterator.py": ["/utils/treeNode.py"], "/trees/path_sum2.py": ["/utils/treeNode.py"], "/linkedList/insertion_sort_list.py": ["/utils/listNode.py"], "/trees/flatten_binary_tree.py": ["/utils/treeNode.py", "/utils/treeUtils.py"], "/trees/find_bottom_left_value.py": ["/utils/treeNode.py"], "/trees/tree_from_postorder_inorder.py": ["/utils/treeNode.py"], "/linkedList/palindrome_linked_list.py": ["/utils/listNode.py"], "/trees/left_leaves_sum.py": ["/utils/treeNode.py"]}