# 非本科生的 CS 留美時間成本估計

## 修大學部資工的課程

p.s. 我大學念商科的經驗，一堂課每週大約花 3–5小時 (一學期 16 週)，系定必修算 12 堂，大概花 750 小時在唸書？工科跟商科所需要投注的時間心力差距實在是太大了

--

--

# [Leetcode] Generate Parentheses

Consider the pair attribute as the requirement of designing DFS.

Description

Given `n` pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Examples

Example 1:

`Input: n = 3Output: ["((()))","(()())","(())()","()(())","()()()"]`

Example 2:

`Input: n = 1Output: ["()"]`

Solution

Use a container to store added parentheses and pass it across DFS. There are always 2 choises when forwarding this DFS, adding left or adding right. The backtracking is after going one path, remove the added paremtheses and then go another path. This is necessary because for 2 paths, the state should be the same before adding. While left can be freely added, the constraint is right can only be added when number of existing left in temporary < number of existing right.

`#        arr#      /     \# add "("    ")" (only when more "(" in arr)`
• code

--

--

--

--

# [Leetcode] Choose Suitable Algorithm for Problems

A short version from How to identify which Data Structure to use with additional observation.

• Common

Sorted array → Binary Search

Linked List O(1)→ Slow Fast Pointers

Top / Least K → Heap

Tree / Graph → DFS / BFS

Frequency / Duplicates → Hash

Max / Min Subarray / Subset → Sliding Window / Dynamic Programming

Local solution not altering computation of other elms → Greedy

Local solution altering computation of other elms (Permutations / Combinations / Subsets) → Backtracking

Need adding / retrieving elms with specific sequence → Stack / Queue

In place array modification → Two Pointers / View array as linked list and use index as next

--

--

# [Python] Snippets

Useful while forgetful codes

## Init a matrix of m rows & n cols

`[[val] * n for _ in range(m)]`

--

--

## PHIL

Jotting the ideas down in codes or articles