This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 != ')': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |