Giter Club home page Giter Club logo

leetcode-21's Introduction

LeetCode

LeetCode solutions.

Always try to optimize time complexity O(n^2) -> O(nlogn) or O(n) in most cases.

Python Tips:

  1. Dictionary build-in method keys() will not return a list object. To get a list of keys, need try list(newdict.keys()). This will convert the dict_keys object to a list.
  2. Dictionary (Hash Table) should be considered in priority to deal with the duplicated information or the mapping relationship.
  3. For Linked List problem, using/creating a dummy head is usually needed.
  4. For Linked List problem, the middle element of a list can be found by using a slow and a fast pointers together.

Java Tips:

  1. The ASCII values of '`' = 96, 'a' = 97, 'z' = 122, '{' = 123. The full ASCII table can be found at: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

Data Structure Implementation:

  1. An implementation of Trie can be found in 720. Longest Word in Dictionary
  2. An implementation of Max Heap can be found in 692. Top K Frequent Words

Array Problems:

  1. Using 2 pointers.
  2. Or using Priority Queue (Heap) if some kinds of ordering is required.
  3. Regarding circular array, possible solution is: -- concatenate the array with its copy to get a new array with double length. But cost extra space. -- Or, use larger index range like 0 < i < 2n with mod operation i%n, here the n is the length of array.

List Problems:

  1. Using two pointers (slow pointer and fast pointer) to locate the middle element of the list.

Memoization:

  1. For a tree-like data structure, probably need a Hash table such as HashMap<TreeNode, Integer> to store the calculated values for Java implementation.

Sorting:

  1. If the number of data is not big and related to integer, could using Counting Sort for better Time Complexity.
  2. TreeMap is a choice when handle sorting of large number data.

Binary Search:

  1. Use 'index space' as searching space in 1-dimension data like array
  2. Use 'number range space' as searching space in 2-dimension data like matrix

Dynamic Programming:

  1. the Math.max() and Math.min() are often used in Java solutions
  2. a local extremum (optimal) value is always used
  3. a matrix dp[i][j] is used to store result in most cases

Bit Manipulation:
Use Column Operation

Interval Problem: Use TreeMap with its lowerKey() or floorKey() and higherKey() or ceilingKey()

Using HashMap to reduce Time Complexity:
0560. Subarray Sum Equals K

Sort Time Stamp with TreeMap:
0253. Meeting Rooms II
0731. My Calendar II
1094. Car Pooling

Boyer-Moore Majority Vote algorithm
0229. Majority Element II

Morris Traversal 0099. Recover Binary Search Tree

Good discussion and explanation:

  1. Approach the problem using the "trial and error" algorithm:
    https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/109082/Approach-the-problem-using-the-%22trial-and-error%22-algorithm
  2. Summary of solutions for problems "reducible" to LeetCode 378:
    https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/115819/Summary-of-solutions-for-problems-%22reducible%22-to-LeetCode-378

Heuristic Solutions:

  1. Longest Common Prefix
  2. Maximum Subarray
  3. Best Time to Buy and Sell Stock III
  4. Linked List Cycle
  5. Linked List Cycle II
  6. Intersection of Two Linked Lists
  7. Majority Element
  8. Palindrome Linked List
  9. Range Sum Query 2D - Immutable

leetcode-21's People

Contributors

wilwfy 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.