text
stringlengths
37
1.41M
__source__ = 'https://leetcode.com/problems/valid-parenthesis-string/description/' # Time: O() # Space: O() # # Description: Leetcode # 678. Valid Parenthesis String # # Given a string containing only three types of characters: '(', ')' and '*', # write a function to check whether this string is valid. # We define the...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/perfect-number/' # Time: O() # Space: O() # # Description: Leetcode # 507. Perfect Number # # We define the Perfect Number is a positive integer t # hat is equal to the sum of all its positive divisors except itself. # Now, given an integer n, write a function...
__source__ = 'https://leetcode.com/problems/prime-palindrome/' # Time: O(N) # Space: O(1) # # Description: Leetcode # 866. Prime Palindrome # # Find the smallest prime palindrome greater than or equal to N. # # Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1. # # For exam...
__source__ = 'https://leetcode.com/problems/global-and-local-inversions/' # Time: O() # Space: O() # # Description: Leetcode # 775. Global and Local Inversions # # We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. # # The number of (global) inversions is the number of i < j with 0 <= i < j ...
__source__ = 'https://leetcode.com/problems/remove-duplicates-from-sorted-list/' # https://github.com/kamyu104/LeetCode/blob/master/Python/remove-duplicates-from-sorted-list.py # Time: O(n) # Space: O(1) # LinkedList # # Description: Leetcode # 83. Remove Duplicates from Sorted List # # Given a sorted linked list, del...
__source__ = 'https://leetcode.com/problems/least-operators-to-express-number/solution/' # Time: (Logx target) # Space: (Log target) # # Description: Leetcode # 964. Least Operators to Express Number # # Given a single positive integer x, # we will write an expression of the form x (op1) x (op2) x (op3) x ... # where ...
__source__ = 'https://leetcode.com/problems/wiggle-sort-ii/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/wiggle-sort-ii.py # Time: O(nlogn) # Space: O(n) # # Description: Leetcode # 324. Wiggle Sort II # # Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3...
__source__ = 'https://leetcode.com/problems/surface-area-of-3d-shapes/' # Time: O() # Space: O() # # Description: Leetcode # 892. Surface Area of 3D Shapes # # On a N * N grid, we place some 1 * 1 * 1 cubes. # # Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). # # Return the t...
__source__ = 'https://leetcode.com/problems/exam-room/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 855. Exam Room # # In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1. # # When a student enters the room, # they must sit in the seat that maximizes the distance to the closest pe...
__source__ = 'https://leetcode.com/problems/asteroid-collision/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 735. Asteroid Collision # # We are given an array asteroids of integers representing asteroids in a row. # # For each asteroid, the absolute value represents its size, # and the sign represents its d...
__source__ = 'https://leetcode.com/problems/search-a-2d-matrix-ii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/search-a-2d-matrix-ii.py # Time: O(m + n) # Space: O(1) # # Description: Leetcode # 240. Search a 2D Matrix II # # Write an efficient algorithm that searches for a value in an m x n matrix. # T...
__source__ = 'https://leetcode.com/problems/linked-list-cycle/' # https://github.com/kamyu104/LeetCode/blob/master/Python/linked-list-cycle.py # Time: O(n) # Space: O(1) # Two Pointer # # Description: Leetcode # 141. Linked List Cycle # # Given a linked list, determine if it has a cycle in it. # # Follow up: # Can you...
__source__ = 'https://leetcode.com/problems/number-of-digit-one/' # https://github.com/kamyu104/LeetCode/blob/master/Python/number-of-digit-one.py # Time: O(logn) # Space: O(1) # # Description: Leetcode # 233. Number of Digit One # # Given an integer n, count the total number of digit 1 appearing # in all non-negative...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/implement-rand10-using-rand7/' # Time: O(1) average, but O(∞) worst case. # Space: O(1) # # Description: Leetcode # 470. Implement Rand10() Using Rand7() # # Given a function rand7 which generates a uniform random integer in the range 1 to 7, # write a functio...
__source__ = 'https://leetcode.com/problems/mirror-reflection/' # Time: O(logP) # Space: O(1) # # Description: Leetcode # 858. Mirror Reflection # # There is a special square room with mirrors on each of the four walls. # Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0...
__source__ = 'https://leetcode.com/problems/find-pivot-index/description/' # Time: O(N) # Space: O(1) # # Description: Leetcode # 724. Find Pivot Index # # Given an array of integers nums, write a method that returns the "pivot" index of this array. # # We define the pivot index as the index # where the sum of the num...
__source__ = 'https://leetcode.com/problems/minimum-area-rectangle-ii/' # Time: O(N^3) # Space: O(N) # # Description: Leetcode # 963. Minimum Area Rectangle II # # Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, # with sides not necessarily parallel to the x...
__author__ = 'July' # http://agiliq.com/blog/2013/09/understanding-threads-in-python/ #define a global variable from threading import Thread from threading import Lock some_var = 0 lock = Lock() class IncrementThreadRace(Thread): def run(self): #we want to read a global variable #and then increme...
__source__ = 'https://leetcode.com/problems/minimum-index-sum-of-two-lists/' # Time: O( n + m) # Space: O(n) # # Description: 599. Minimum Index Sum of Two Lists # # Suppose Andy and Doris want to choose a restaurant for dinner, # and they both have a list of favorite restaurants represented by strings. # # You need t...
__source__ = 'https://leetcode.com/problems/prefix-and-suffix-search/' # Time: O(NK^2 + QK) where N is the number of words, # K is the maximum length of a word, and Q is the number of queries. # Space: O(NK^2), the size of the trie. # # Description: Leetcode # 745. Prefix and Suffix Search # # Given many words, words[...
__source__ = 'https://leetcode.com/problems/set-mismatch/' # Time: O(n) # Space: O(1) # # Description: Leetcode # 645. Set Mismatch # # The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, # one of the numbers in the set got duplicated to another number in the set, # which resul...
__source__ = 'https://leetcode.com/problems/happy-number/' # https://github.com/kamyu104/LeetCode/blob/master/Python/happy-number.py # Time: O(k), where k is the steps to be happy number # Space: O(k) # # Description: Leetcode # 202. Happy Number # # Write an algorithm to determine if a number is "happy". # # A happy ...
__source__ = 'https://leetcode.com/problems/goat-latin/' # Time: O(N^2) # Space: O(N^2) # # Description: Leetcode # 824. Goat Latin # # A sentence S is given, composed of words separated by spaces. # Each word consists of lowercase and uppercase letters only. # # We would like to convert the sentence to "Goat Latin" (...
__source__ = 'https://leetcode.com/problems/increasing-triplet-subsequence/' # https://github.com/kamyu104/LeetCode/blob/master/Python/increasing-triplet-subsequence.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 334. Increasing Triplet Subsequence # # Given an unsorted array return whether an increasing # ...
__source__ = 'https://leetcode.com/problems/next-greater-element-ii/' # Time: O(n) stack + 2 pass # Space: O(n) # # Description: 503. Next Greater Element II # # Given a circular array (the next element of the last element is the first element of the array), # print the Next Greater Number for every element. The Next ...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/longest-word-in-dictionary/' # Time: O(m) sum of the length of words[i] # Space: O(m) the space used by our trie # # Description: Leetcode # 720. Longest Word in Dictionary # # Given a list of strings words representing an English Dictionary, # find the longes...
__source__ = 'https://leetcode.com/problems/delete-columns-to-make-sorted/' # Time: O(N) # Space: O(1) # # Description: Leetcode # 944. Delete Columns to Make Sorted # # We are given an array A of N lowercase letter strings, all of the same length. # # Now, we may choose any set of deletion indices, and for each strin...
__source__ = 'https://leetcode.com/problems/bitwise-and-of-numbers-range/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/bitwise-and-of-numbers-range.py # Time: O(1) # Space: O(1) # # Description: Leetcode # 201. Bitwise AND of Numbers Range # # Given a range [m, n] where 0 <= m <= n <= 2147483...
__source__ = 'https://leetcode.com/problems/falling-squares/description/' # Time: O(N*N^(1/2)) # Space: O(N) # Segment Tree # # Description: Leetcode # 699. Falling Squares # # On an infinite number line (x-axis), we drop given squares in the order they are given. # # The i-th square dropped (positions[i] = (left, sid...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/race-car/' # Time: O() # Space: O() # # Description: Leetcode # 818. Race Car # # Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.) # # Your car drives automatically according to a sequence of in...
__source__ = 'https://leetcode.com/problems/valid-perfect-square/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/valid-perfect-square.py # Time: O(logn) # Space: O(1) # # Description: Leetcode # 367. Valid Perfect Square # # Given a positive integer num, write a function # which returns True if...
__source__ = 'https://leetcode.com/problems/trapping-rain-water-ii/#/description' # https://github.com/kamyu104/LeetCode/blob/master/Python/trapping-rain-water-ii.py # Time: O(m * n * log(m + n)) ~ O(m * n * log(m * n)) # Space: O(m * n) # # Description: Leetcode # 407. Trapping Rain Water II # # Given an m x n matrix...
__source__ = 'https://leetcode.com/problems/minimum-height-trees/' # https://github.com/kamyu104/LeetCode/blob/master/Python/minimum-height-trees.py # Time: O(n) # Space: O(n) # # Description: Leetcode # 310. Minimum Height Trees # # For a undirected graph with tree characteristics, we can # choose any node as the roo...
__source__ = 'https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/' # Time: O(nlogn + n*x), Sorting takes O(nlogn) and isSubsequence takes O(x) # Space: O(logn) Sorting takes O(logn) space in average case. # # Description: 524. Longest Word in Dictionary through Deleting # # Given a string and a ...
__source__ = 'https://leetcode.com/problems/subarray-product-less-than-k/' # Time: O(N) # Space: O(1) # # Description: Leetcode # 713. Subarray Product Less Than K # # Your are given an array of positive integers nums. # # Count and print the number of (contiguous) subarrays # where the product of all the elements in ...
__source__ = 'https://leetcode.com/problems/range-sum-query-immutable/' # https://github.com/kamyu104/LeetCode/blob/master/Python/range-sum-query-immutable.py # Time: ctor: O(n), # update: O(logn), # query: O(logn) # Space: O(n) # # Description: Leetcode # 303. Range Sum Query - Immutable # # Given an...
__source__ = 'https://leetcode.com/problems/binary-tree-maximum-path-sum/#/description' # https://github.com/kamyu104/LeetCode/blob/master/Python/binary-tree-maximum-path-sum.py # Time: O(n) # Space: O(h), h is height of binary tree # Divide and Conquer # # Description: Leetcode # 124. Binary Tree Maximum Path Sum # #...
__source__ = 'https://leetcode.com/problems/number-of-music-playlists/' # Time: O() # Space: O() # DP # # Description: Leetcode # 920. Number of Music Playlists # # # our music player contains N different songs # and she wants to listen to L (not necessarily different) songs during your trip. # You create a playlist s...
__source__ = 'https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/' # https://github.com/kamyu104/LeetCode/blob/master/Python/minimum-number-of-arrows-to-burst-balloons.py # Time: O(nlogn) # Space: O(1) # # Description: 452. Minimum Number of Arrows to Burst Balloons # # There are a number of sphe...
__source__ = 'https://leetcode.com/problems/unique-paths-iii/' # Time: O(4^(R*C)), where R,C are the number of rows and columns in the grid. # Space: O(R*C) # # Description: Leetcode # 980. Unique Paths III # # On a 2-dimensional grid, there are 4 types of squares: # # 1 represents the starting square. There is exact...
__source__ = 'https://leetcode.com/problems/frog-jump/' # https://github.com/kamyu104/LeetCode/blob/master/Python/frog-jump.py # Time: O(n) ~ O(n^2) # Space: O(n) # # Description: Leetcode # 403. Frog Jump # # A frog is crossing a river. The river is divided into x units and # at each unit there may or may not exist a...
__source__ = 'https://leetcode.com/problems/4-keys-keyboard/description/' # Time: O() # Space: O() # # Description: Leetcode # 651. 4 Keys Keyboard # # Imagine you have a special keyboard with the following keys: # # Key 1: (A): Prints one 'A' on screen. # # Key 2: (Ctrl-A): Select the whole screen. # # Key 3: (Ctrl-C...
__source__ = 'https://leetcode.com/problems/split-array-into-consecutive-subsequences/' # Time: O(n) # Space: O(n) # # Description: Leetcode # 659. Split Array into Consecutive Subsequences # # You are given an integer array sorted in ascending order (may contain duplicates), # you need to split them into several subs...
__source__ = 'https://leetcode.com/problems/evaluate-reverse-polish-notation/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/evaluate-reverse-polish-notation.py # Time: O(n) # Space: O(n) # Stack # # Description: Leetcode # 150. Evaluate Reverse Polish Notation # # http://en.wikipedia.org/wiki/...
__source__ = 'https://leetcode.com/problems/maximum-length-of-pair-chain/' # Time: O(nlogn) # Space: O(1) # # Description: Leetcode # 646. Maximum Length of Pair Chain # # You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. # # Now, we define a pair (c, d) can fo...
__source__ = 'https://leetcode.com/problems/remove-comments/' # Time: O(N) N: length of source code # Space: O(N) # # Description: Leetcode # 722. Remove Comments # # Given a C++ program, remove comments from it. # The program source is an array where source[i] is the i-th line of the source code. # This represents th...
__source__ = 'https://leetcode.com/problems/squares-of-a-sorted-array/' # Time: O() # Space: O() # # Description: Leetcode # 977. Squares of a Sorted Array # # Given an array of integers A sorted in non-decreasing order, # return an array of the squares of each number, also in sorted non-decreasing order. # # Example ...
__source__ = 'https://leetcode.com/problems/largest-divisible-subset/' # https://github.com/kamyu104/LeetCode/blob/master/Python/largest-divisible-subset.py # Time: O(n^2) # Space: O(n) # # Description: Leetcode # 368. Largest Divisible Subset # # Given a set of distinct positive integers, # find the largest subset su...
__source__ = 'https://leetcode.com/problems/partition-array-into-disjoint-intervals/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 915. Partition Array into Disjoint Intervals # # Given an array A, partition it into two (contiguous) subarrays left and right so that: # # Every element in left is less than or ...
__source__ = 'https://leetcode.com/problems/detect-capital/' # Time: O() # Space: O() # # Description: 520. Detect Capital # # Given a word, you need to judge whether the usage of capitals in it is right or not. # # We define the usage of capitals in a word to be right when one of the following cases holds: # # All le...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/similar-string-groups/' # Time: O() # Space: O() # # Description: Leetcode # 839. Similar String Groups # # Two strings X and Y are similar if we can swap two letters (in different positions) of X, # so that it equals Y. # # For example, "tars" and "rats" are ...
__source__ = 'https://leetcode.com/problems/teemo-attacking/#/description' # Time: O(n) # Space: O(1) # # Description: 495. Teemo Attacking # # In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. # Now, given the Teemo's attacking ascending time series towards...
__source__ = 'https://leetcode.com/problems/nim-game/' # https://github.com/kamyu104/LeetCode/blob/master/Python/nim-game.py # Time: O(1) # Space: O(1) # # Description: # 292. Nim Game # # You are playing the following Nim Game with your friend: # There is a heap of stones on the table, each time one of # you take tur...
__source__ = 'https://leetcode.com/problems/student-attendance-record-ii/' # Time: O(n) # Space: O(n) # # Description: 552. Student Attendance Record II # # Given a positive integer n, return the number of all possible attendance records with length n, # which will be regarded as rewardable. The answer may be very lar...
__source__ = 'https://leetcode.com/problems/find-smallest-letter-greater-than-target/' # Time: O() # Space: O() # # Description: Leetcode # 744. Find Smallest Letter Greater Than Target # # Given a list of sorted characters letters containing only lowercase letters, # and given a target letter target, # find the small...
__source__ = 'https://leetcode.com/problems/string-without-aaa-or-bbb/' # Time: O() # Space: O() # # Description: Leetcode # 984. String Without AAA or BBB # # Given two integers A and B, return any string S such that: # # S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters; # The substrin...
__source__ = 'https://leetcode.com/problems/expression-add-operators/#/description' # https://github.com/kamyu104/LeetCode/blob/master/Python/expression-add-operators.py # Time: O(4^n) # Space: O(n) # # Description: Leetcode # 282. Expression Add Operators # # Given a string that contains only digits 0-9 # and a targe...
__source__ = 'https://leetcode.com/problems/palindrome-permutation/' # https://github.com/kamyu104/LeetCode/blob/master/Python/palindrome-permutation.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 266. Palindrome Permutation # # Given a string, determine if a permutation of the string could form a palindrom...
__author__ = 'July' # write a method to compute all permutation of a string # write a methof to compute all combinatino of a string import re class Solution: # no ordering def getPermutation(self, str): result = self.getPermutationRecu(str) for i in xrange(len(result)): result[i] = re...
__source__ = 'https://leetcode.com/problems/trim-a-binary-search-tree/description/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 669. Trim a Binary Search Tree # # Given a binary search tree and the lowest and highest boundaries as L and R, # trim the tree so that all its elements lies in [L, R] (R >= L). # ...
__source__ = 'https://leetcode.com/problems/judge-route-circle/description/' # Time: O() # Space: O() # # Description: Leetcode # 657. Judge Route Circle # # Initially, there is a Robot at position (0, 0). Given a sequence of its moves, # judge if this robot makes a circle, which means it moves back to the original pl...
__source__ = 'https://leetcode.com/problems/triangle/description/' # Time: O(m * n) # Space: O(n) # DP # # Description: Leetcode # 120. Triangle # # Given a triangle, find the minimum path sum from top to bottom. # Each step you may move to adjacent numbers on the row below. # # For example, given the following triang...
__source__ = 'https://leetcode.com/problems/search-a-2d-matrix/' # https://github.com/kamyu104/LeetCode/blob/master/Python/search-a-2d-matrix.py # Time: O(logm + logn) # Space: O(1) # Binary Search # # Description: Leetcode # 74. Search a 2D Matrix # # Write an efficient algorithm that searches for a value in an m x n...
__source__ = 'https://leetcode.com/problems/longest-palindrome/' # https://github.com/kamyu104/LeetCode/blob/master/Python/longest-palindrome.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 409. Longest Palindrome # # Given a string which consists of lowercase or uppercase letters, # find the length of the l...
__source__ = 'https://leetcode.com/problems/reshape-the-matrix/' # Time: O(n * m ) # Space: O(n * m) # # Description: Leetcode # 566. Reshape the Matrix # # In MATLAB, there is a very useful function called 'reshape', # which can reshape a matrix into a new one with different size but keep its original data. # # You'r...
__source__ = 'https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/remove-duplicates-from-sorted-list-ii.py # Time: O(n) # Space: O(1) # LinkedList # # Description: Leetcode # 82. Remove Duplicates from Sorted List II # # Given a sorted linked ...
__source__ = 'https://leetcode.com/problems/minimum-increment-to-make-array-unique/' # Time: O() # Space: O() # # Description: Leetcode # 945. Minimum Increment to Make Array Unique # # Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. # # Return the least number of moves to...
__source__ = 'https://leetcode.com/problems/house-robber/' # https://github.com/kamyu104/LeetCode/blob/master/Python/house-robber.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 198. House Robber # # You are a professional robber planning to rob houses along a street. # Each house has a certain amount of mon...
__source__ = 'https://leetcode.com/problems/minimum-moves-to-equal-array-elements/' # https://github.com/kamyu104/LeetCode/blob/master/Python/minimum-moves-to-equal-array-elements-ii.py # Time: O(nlogn) # Space: O(1) # # Description: 453. Minimum Moves to Equal Array Elements # # Given a non-empty integer array of siz...
__source__ = 'https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/' # https://github.com/kamyu104/LeetCode/blob/master/Python/binary-tree-zigzag-level-order-traversal.py # Time: O(n) # Space: O(n) # BFS # # Description: Leetcode # 103. Binary Tree Zigzag Level Order Traversal # # Given a binary tree...
__source__ = 'https://leetcode.com/problems/rearrange-string-k-distance-apart/' # https://github.com/kamyu104/LeetCode/blob/master/Python/rearrange-string-k-distance-apart.py # Time: O(n) # Space: O(n) # # Description: Leetcode # 358. Rearrange String k Distance Apart # # Given a non-empty string str and an integer k,...
__source__ = 'https://leetcode.com/problems/spiral-matrix-ii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/spiral-matrix-ii.py # Time: O(n^2) # Space: O(1) # # Description: Leetcode # 59. Spiral Matrix II # # Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order...
__source__ = 'https://leetcode.com/problems/delete-and-earn/' # Time: O(N + W) # Space: O(W) # # Description: Leetcode # 740. Delete and Earn # # Given an array nums of integers, you can perform operations on the array. # # In each operation, you pick any nums[i] and delete it to earn nums[i] points. # After, you must...
__source__ = 'https://leetcode.com/problems/boundary-of-binary-tree/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 545. Boundary of Binary Tree # # Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. # Boundary includes left boundary, leaves, and right bound...
__source__ = 'https://leetcode.com/problems/linked-list-cycle-ii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/linked-list-cycle-ii.py # Time: O(n) # Space: O(1) # Two Pointer # # Description: Leetcode # 142. Linked List Cycle II # # Given a linked list, return the node where the cycle begins. If there i...
__source__ = 'https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/convert-sorted-list-to-binary-search-tree.py # Time: O(n) # Space: O(logn) # Divide and Conquer # # Description: Leetcode # 109. Convert Sorted List to Binary Se...
__source__ = 'https://leetcode.com/problems/number-of-matching-subsequences/' # Time: O() # Space: O() # # Description: Leetcode # 792. Number of Matching Subsequences # # Given string S and a dictionary of words words, # find the number of words[i] that is a subsequence of S. # # Example : # Input: # S = "abcde" # wo...
__source__ = 'https://leetcode.com/problems/find-anagram-mappings/description/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 760. Find Anagram Mappings # # Given two lists A and B, and B is an anagram of A. # B is an anagram of A means B is made by randomizing the order of the elements in A. # # We want to f...
__source__ = 'https://leetcode.com/problems/backspace-string-compare/description/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 844. Backspace String Compare # # Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. # # Example 1: # #...
# coding=utf-8 __source__ = 'https://leetcode.com/problems/rotate-function/' # https://github.com/kamyu104/LeetCode/blob/master/Python/rotate-function.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 396. Rotate Function # # Given an array of integers A and let n to be its length. # # Assume Bk to be an array...
__source__ = 'https://leetcode.com/problems/shortest-path-visiting-all-nodes/' # Time: O(2^N * N) # Space: O(2^N * N) # # Description: Leetcode # 847. Shortest Path Visiting All Nodes # # An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. # # graph.length = N, and j != i is in the...
__source__ = 'https://leetcode.com/problems/sequence-reconstruction/' # https://github.com/kamyu104/LeetCode/blob/master/Python/sequence-reconstruction.py # Time: O(n * s), n is the size of org, s is the size of seqs # Space: O(n) # # Description: 444. Sequence Reconstruction # # Check whether the original sequence or...
__source__ = 'https://leetcode.com/problems/keys-and-rooms/' # Time: O(N + E), where NN is the number of rooms, and EE is the total number of keys. # Space: O(N) # # Description: Leetcode # 841. Keys and Rooms # # There are N rooms and you start in room 0. # Each room has a distinct number in 0, 1, 2, ..., N-1, # and ...
__source__ = 'https://leetcode.com/problems/fraction-to-recurring-decimal/' # https://github.com/kamyu104/LeetCode/blob/master/Python/fraction-to-recurring-decimal.py # Time: O(logn), where log n is the length of result strings # Space: O(1) # MATH # # Description: Leetcode # 166. Fraction to Recurring Decimal # # Giv...
__source__ = 'https://leetcode.com/problems/walking-robot-simulation/' # Time: O() # Space: O() # # Description: Leetcode # 874. Walking Robot Simulation # # A robot on an infinite grid starts at point (0, 0) and faces north. # The robot can receive one of three possible types of commands: # # -2: turn left 90 degrees...
__source__ = 'https://leetcode.com/problems/flip-string-to-monotone-increasing/' # Time: O(N) # Space: O(N) # # Description: Leetcode # 926. Flip String to Monotone Increasing # # A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), # followed by some number of '1's (als...
__source__ = 'https://leetcode.com/problems/integer-to-roman/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/integer-to-roman.py # Time: O(n) # Space: O(1) # Math # # Description: Leetcode # 12. Integer to Roman # # Given an integer, convert it to a roman numeral. # # Input is guaranteed to be ...
__source__ = 'https://leetcode.com/problems/string-compression/' # Time: O(N) # Space: O(1) # # Description: Leetcode # 443. String Compression # # Given an array of characters, compress it in-place. # # The length after compression must always be smaller than or equal to the original array. # # Every element of the a...
__source__ = 'https://leetcode.com/problems/perfect-rectangle/' # https://leetcode.com/problems/perfect-rectangle/#/description # Time: O(n) or o(nlogn) for sweep line # Space: O(n) set # # Description: Leetcode # 391. Perfect Rectangle # # Given N axis-aligned rectangles where N > 0, # determine if they all together ...
__source__ = 'https://leetcode.com/problems/add-and-search-word-data-structure-design/description/' # Time: O(min(n, h)), per operation # Space: O(min(n, h)) # # Description: Leetcode # 211. Add and Search Word - Data structure design # # Design a data structure that supports the following two operations: # # void add...
__source__ = 'https://leetcode.com/problems/maximum-vacation-days/' # Time: O(n^2 * k) # Space: O(n * k) # # Description: 568. Maximum Vacation Days # # LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. # But all work and no play makes Jack a dull boy, ...
__source__ = 'https://leetcode.com/problems/minimum-time-difference/' # Time: O() # Space: O() # # Description: 539. Minimum Time Difference # # Given a list of 24-hour clock time points in "Hour:Minutes" format, # find the minimum minutes difference between any two time points in the list. # # Example 1: # Input: ["2...
__source__ = 'https://leetcode.com/problems/search-in-rotated-sorted-array-ii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/search-in-rotated-sorted-array-ii.py # Time: O(logn) When there are duplicates, the worst case is O(n). # Space: O(1) # Binary Search # # Description: Leetcode # 34. Search for a Ra...
__source__ = 'https://leetcode.com/problems/shortest-word-distance-iii/' # https://github.com/kamyu104/LeetCode/blob/master/Python/shortest-word-distance-iii.py # Time: O(n) # Space: O(1) # # Description: Leetcode # 245. Shortest Word Distance III # # This is a follow up of Shortest Word Distance. The only difference ...
import numpy as np def make_vect(point1, point2): """makes a vector out of 2 points Args: point1: first point point2: second point Returns: vector of 2 points (tuple) """ return ((point2[0] - point1[0]), (point2[1] - point1[1])) def get_magnitude(vector): """gets th...
#!/usr/bin/env python def do_something_with_args(name, *args, show_args=False): print('Greetings, {}'.format(name)) if show_args and args: print('Here are the args:') for arg in args: print(arg) print('\n') def do_something_with_kwargs(name, show_args=False, **kwargs): ...
#!/usr/bin/env python """ A logging demo that shows most of all you'll ever need to know concerning Python and logging. """ import logging import sys logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) log_format = logging.Formatter( '%(asctime)s [%(levelname)s] [%(module)s.%(name)s.%(funcName)s...
#!/usr/bin/env python def subtract(num1, num2): return num1 - num2 func = { '-': subtract, } if __name__ == '__main__': diff = func['-'](6, 3) print(diff)
import pygame import TicTacToeBoard ### Values for drawing graphics BLACK = ( 0, 0, 0) WHITE = (250, 250, 250) RED = (250, 0, 0) BLUE = ( 0, 0, 250) WIDTH_0 = 0 # The width of one square WIDTH_1 = 200 WIDTH_2 = 400 # The width of the whole board WIDTH_3 = 600 # Size and thickness of noughts and cros...
def vatCalculate(totalprice): result = totalprice+(totalprice*7)/100 return result print(vatCalculate(int(input("Price>>"))))