data_structure_python

@data-structure-python

Public team

Community (0)
No community contribution yet

Joined on Mar 12, 2020

  • https://leetcode.com/problems/find-k-closest-elements/ Naive def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]: L = sorted([(abs(elt - x), elt) for elt in arr], key=lambda tup: tup[0]) return sorted([tup[1] for tup in L[:k]]) Opti
     Like  Bookmark
  • Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Example: MinStack minStack = new MinStack();
     Like  Bookmark
  • Given a string containing just the characters $($, $)$, ${$, $}$, $[$ and $]$, determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1:
     Like  Bookmark
  • Solution 1 Time complexity: O(n³) Space complexity: O(n) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n = len(nums) if n < 3: return []
     Like  Bookmark
  • Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
     Like  Bookmark
  • Say you have an array for which the $i^{th}$ element is the price of a given stock on day $i$. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5
     Like  Bookmark
  • Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Note: Your returned answers (both index1 and index2) are not zero-based. You may assume that each input would have exactly one solution and you may not use the same element twice. Example:
     Like  Bookmark
  • Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer. Note: Please solve it without division and in O(n).
     Like  Bookmark
  • https://leetcode.com/problems/longest-consecutive-sequence/ Solution 1 O(n³) Outer loop: O(n) curr_num gets incremented n times (worse case) check curr_num + 1 in nums is O(n)
     Like  Bookmark
  • https://www.lintcode.com/problem/659/ class Solution: """ @param: strs: a list of strings @return: encodes a list of strings to a single string. """ def encode(self, strs): # write your code here res = ""
     Like  Bookmark
  • https://leetcode.com/problems/valid-sudoku/ class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: n = len(board) rows = {i:[] for i in range(n)} cols = {i:[] for i in range(n)} boxes = {i:[] for i in range(n)}
     Like  Bookmark
  • Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1
     Like  Bookmark
  • Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"]
     Like  Bookmark
  • https://leetcode.com/problems/search-insert-position/ class Solution: def searchInsert(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2
     Like  Bookmark
  • https://leetcode.com/problems/arranging-coins/ class Solution: def arrangeCoins(self, n: int) -> int: left, right = 0, n while left <= right: mid = (left + right) // 2
     Like  Bookmark
  • https://leetcode.com/problems/valid-perfect-square/ class Solution: def isPerfectSquare(self, num: int) -> bool: perfect_square = int(num ** (1/2)) return perfect_square * perfect_square == num class Solution: def isPerfectSquare(self, num: int) -> bool:
     Like  Bookmark
  • https://leetcode.com/problems/sum-root-to-leaf-numbers/ # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def sumNumbers(self, root: Optional[TreeNode]) -> int:
     Like  Bookmark
  • https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted array: [-10,-3,0,5,9],
     Like  Bookmark
  • https://leetcode.com/problems/count-good-nodes-in-binary-tree/ Preorder traversal while keeping track of maximum # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution:
     Like  Bookmark
  • Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. Example 1:: Input: Tree 1 Tree 2 1 2 / \ / \
     Like  Bookmark