# Problem Solving

在現實生活中,我們需要藉由撰寫程式來完成特定任務,而所謂的演算法(Algorithms)可以視為完成特定任務的一系列步驟或過程。那麼該怎麼提昇我們解決問題的能力呢?這裡提出了兩個主要的建議:

  • 設計一套針對問題求解的計畫
  • 熟悉常見問題的求解套路

關於面對問題時的求解計畫,可以參考 George Polya 所撰寫的 《How To Solve It》 一書。根據該書的內容可以總結出以下策略:

  1. Understand the Problem
  2. Explore Concrete Examples
  3. Break it down
  4. Solve and Simplify
  5. Look Back and Refactor

# Understand the Problem

  • Can I restate the problem in my own words?
  • What are the inputs that go into the problem?
  • What are the outputs that should come from the solution to the problem?
  • Can the outputs be determined from the inputs? In other words, do I have enough information to solve the problem? (You may not be ablue to answer this question until you set about solving the problem. That's Okay; it's still worth considering the question at this early stage.)
  • How should I label the important pieces of data that are a part of the problem?

# Explore Examples

  • Start with simple examples
  • Progress to more complex examples
  • Explore Examples with Empty Inputs
  • Explore Examples with Invalid Inputs

# Break it Down

# Solve and Simplify

# Look Back and Refactor

# Frequency Counter

This pattern uses objects or sets to collect values/frequencies of values. This can oftern avoid the need for nested loops or O(n2)O(n^2) operations with arrays or strings.

# Multiple Pointers

# Sliding Window

# Divide And Conquer

# Greedy

# Backtracking

Last Updated: 12/15/2020, 10:27:30 PM