Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for ch in s:
if ch in ['(', '{', '[']:
stack.append(ch)
elif len(stack):
ch2 = stack.pop()
if ch2 == '(' and ch != ')':
@primaryobjects
primaryobjects / hungryStudents.ts
Last active February 7, 2022 02:57
Number of students unable to eat lunch, solved using in-place array. https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/
//
// Solution: Using a pointer to indicate the left-most point in the array for active students,
// go through each student and check if anyone will eat the current sandwich.
// If someone eats it, wrap their place in line with the student from the front of the queue who has not yet eaten.
// Move the left index to the next student, effectively removing that student from further rounds.
// Continue until either no more hungry students (left == students.length) or we wrapped around twice.
//
// Space: O(n)
// Time: O(n*2) => O(n)
@primaryobjects
primaryobjects / hasCycle.js
Last active February 8, 2022 16:34
Detect a cycle in a directed graph using javascript. Using white black gray algorithm. https://jsfiddle.net/57pz9s64/
// has cycle
// Write a function, hasCycle, that takes in an object representing the adjacency list of a directed graph. The function should return a boolean indicating whether or not the graph contains a cycle.
const search = graph => {
const visited = new Set(); // Black
const nodes = Object.keys(graph);
for (let node of Object.keys(graph)) {
if (hasCycle(graph, node, visited)) {
return true;
@primaryobjects
primaryobjects / islandCount.js
Created February 5, 2022 21:07
Count the number of islands in a graph. Using depth first search in javascript.
// island count
// Write a function, islandCount, that takes in a grid containing Ws and Ls. W represents water and L represents land. The function should return the number of islands on the grid. An island is a vertically or horizontally connected region of land.
// typs, try using depth first search.
const grid = [
['W', 'L', 'W', 'W', 'W'],
['W', 'L', 'W', 'W', 'W'],
['W', 'W', 'W', 'L', 'W'],
['W', 'W', 'L', 'L', 'W'],
@primaryobjects
primaryobjects / sumtree.js
Created February 4, 2022 02:49
Find the sum of all nodes in a tree.
// tree sum
// Write a function, treeSum, that takes in the root of a binary tree that contains number values. The function should return the total sum of all values in the tree.
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
};
@primaryobjects
primaryobjects / allpaths.js
Created February 4, 2022 02:48
Find all tree paths in a binary tree.
// All tree paths.
// Write a function, allTreePaths, that takes in the root of a binary tree. The function should return a 2-Dimensional array where each subarray represents a root-to-leaf path in the tree.
// The order within an individual path must start at the root and end at the leaf, but the relative order among paths in the outer array does not matter.
// You may assume that the input tree is non-empty.
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
// Naive solution, just swap element with correct position. O(n*n)
function minimumSwaps(arr) {
let result = 0;
if (arr && arr.length) {
let sorted = [];
sorted = sorted.concat(arr);
sorted.sort((a, b) => a - b);
for (let i=0; i<arr.length; i++) {
@primaryobjects
primaryobjects / lastWordLength.js
Created January 11, 2022 02:17
Find the length of the last word in a sentence. https://leetcode.com/problems/length-of-last-word
var lengthOfLastWord = function(s) {
let length = 0;
let lastLength = 0;
if (s) {
for (i=0; i<s.length; i++) {
if (s[i] === ' ') {
if (length > 0) {
lastLength = length;
length = 0;
@primaryobjects
primaryobjects / grover.py
Created November 7, 2021 20:13
Grover's Algorithm implemented in Qiskit using a simple oracle for searching bit strings. Quantum computing.
from qiskit import QuantumCircuit, qiskit, Aer
from qiskit.circuit.classicalregister import ClassicalRegister
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.classicalfunction import ClassicalFunction
def oracle(n):
# Define the logical circuit for the oracle.
prog = '''def oracle_func(x1: Int1, x2: Int1, x3: Int1) -> Int1:
return (not x1 and not x2 and not x3) or (x1 and x2 and not x3)'''
@primaryobjects
primaryobjects / swapn.py
Created August 6, 2021 00:30
Swap n bits in a quantum circuit with Qiskit.
def swap(self, bits):
"""
Swap n qubit values (right-most with left-most).
100000 => 000001
111000 => 000111
100010 => 010001
"""
n = len(bits)
swap = QuantumCircuit(n)