Giter Club home page Giter Club logo

leetcode-46's Introduction

Leetcode fun

One question a day to ensure a sharp mind.

Grading Criteria

  • L0: straight forward question
  • L1: variance of template
  • L2: need to think for a while / complex implementation
  • L3: need aha moment / unexpected algorithm

Time Complexity Analysis

Row: input size(IS), column: time complexity(TC)

IS&TC O($2^n$) O($n^4$) O($n^3$) O($n^2$) O(nlogn) O(n) O(logn) O(1)
1-10
10-50
50-100
100-500
500-$10^3$
$10^3$-$10^4$
$10^4$-$10^5$ ?
$10^5$-$10^6$
$10^6$-$10^9$
TC Algorithm
O($2^n$) DFS-combination($2^n$), DFS-permutation(n!),
O($n^4$) DP
O($n^3$) DP, Floyd-Warshall
O($n^2$) DP
O(nlogn) Sorting, Heap, divide&conquer, Dijkstra-heap, QuickSort
O(n) DP, DFS-tree(V), BFS(V+E), TopologicalSorting(V+E), BucketSort(N+K), MonotonicStack()
O(logn) BinarySearch, BinaryIndexTree
O(1) Math

Python Basic

Operations

  1. [a if cond1 else cond2 for a in A]: if-else statement in for loop
  2. A[::i]: jump i-steps in a list, e.g., odd index elements A[::2] and even index elements A[1::2]
  3. A.insert(0, n): insert item to list by a specific index
  4. arr.sort(key=lambda x: (cond1, cond2, ..)): sort array by multiple conditions. Condition can be len(x), x #720
  5. sorted(list, key=functools.cmp_to_key(lambda x, y: int(y+x)-int(x+y))): custom compare function to a list

String

  1. str.startswith(s): returns True if the string starts with the specified value, otherwise False

Set

  1. s.add(x): add value x to set s. Time: O(1)
  2. s1.update(s2): add set s2 to set s1. Time: O(len(s2))

Hash Table

  1. Counter.most_common(num): return a list contains tuple.
  2. del Counter[key]: delete an item. or Counter[key]=0.
  3. cntA+cntB: add two counters together.
  4. cntA-cntB: subtract (keeping only positive counts).
  5. cntA&cntB: find intersection of two counters. min(cntA, cntB)
  6. cntA|cntB: find union of two counters. max(cntA, cntB)

Heap

  1. heapq.heappush(heap, item): Push the value item onto the heap, maintaining the heap invariant.
  2. heapq.heappop(heap): Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised. To access the smallest item without popping it, use heap[0].
  3. heapq.heapify(x): Transform list x into a heap, in-place, in linear time.
  4. heapq.nlargest/nsmallest(n, iterable[, key]): Return a list with the n largest elements from the dataset defined by iterable.

Reference

  1. 用什么语言刷题?C++/Java/Python横向大比较
  2. Leetcode 101: A Leetcode Grinding Guide(C++ Version)
  3. Algorithms for Competitive Programming
  4. 古城算法 slides(google drive)
  5. 输入数据规模和时间复杂度的关系

leetcode-46's People

Contributors

824zzy avatar sssssea7 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.