Liam's Blog


  • Home

  • Archives

  • Tags

  • Categories

  • Photos

  • About

LeetCode #785 Is Graph Bipartite?

Posted on 2018-10-07 | Post modified 2018-10-07 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Breadth-first Search , Graph

Problem

Given an undirected graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn’t contain any element twice.

Examples

1
2
3
4
5
6
7
8
9
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
1
2
3
4
5
6
7
8
9
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
Read more »

Binary Tree Traversal

Posted on 2018-10-06 | Post modified 2018-10-07 | In Languages , Prolog , Python , Algorithms , Tree , Traversal

Binary Tree Traversals (Inorder, Preorder and Postorder)

An example binary tree

The inorder enumeration for the tree above is B D A G E C H F I

The preorder enumeration for the tree above is A B D C E G F H I

The postorder enumeration for the tree above is D B G E H I F C A

Read more »

Great Common Divisor

Posted on 2018-10-06 | Post modified 2018-10-07 | In Languages , Prolog

Write the greatest common divisor (GCD) program in Prolog. The greatest common divisor of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
% base case
gcd(0, X, X):-
X > 0, !.

gcd(X, Y, Z):-
X >= Y, !,
X1 is X-Y,
gcd(X1,Y,Z).
gcd(X, Y, Z):-
X1 is Y-X,
gcd(X1,X,Z).

gcd([H],H).
gcd([H1,H2|T], D) :-
gcd(H1, H2, D2),
gcd([D2|T], D).

% Testing it:
test1 :-
gcd([5,4,3,7], X),
write(X),
nl,
fail;
true.

?- test1.

LeetCode #121 Best Time to Buy and Sell Stock

Posted on 2018-10-06 | Post modified 2018-10-06 | In Languages , LeetCode , Python , Difficulties , Algorithms , Easy , Dynamic Programming , Greedy

Problem

Say you have an array for which the ith 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.

Examples

1
2
3
4
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
1
2
3
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Read more »

LeetCode #836 Rectangle Overlap

Posted on 2018-10-06 | Post modified 2018-10-06 | In LeetCode , Difficulties , Easy

Problem

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whether they overlap.

Examples

1
2
Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true
1
2
Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false
Read more »

Mathematical Models

Posted on 2018-10-06 | Post modified 2018-10-23 | In Data Science , Basic Concepts , Analysis Pipeline

The Data Science Analysis Pipeline

  • Ask an interesting question

    What is the scientific goal?

    What would you do if you had the data?

    What do you want to predict or estimate?

  • Get the data

    How were the data sampled?

    Which data are relevant?

    Are there privacy issues?

  • Explore the data

    Plot the data.

    Are there anomalies?

    Are there patterns?

  • Model the data

    Build a model.

    Fit the model.

    Validate the model.

  • Communicate and visualize the results

    What did we learn?

    Do the results make sense?

    Can we tell a story?

Modeling is the process of encapsulating information into a tool which can make forecases/predictions.

The key steps are building, fitting, and validating the model.

Read more »

Analysis of Algorithms Lecture 6

Posted on 2018-10-06 | Post modified 2018-10-06 | In Algorithms , Basic Concepts , Greedy
Read more »

Statistical Significance

Posted on 2018-10-05 | Post modified 2018-10-11 | In Data Science

Talking to Statisticians

Statisticians are primarily concerned with whether observations on data are significant.

Data miners are primarily concerned with whether their observations are interesting.

I have never had a satisfying conversation with a statistician, but…

Read more »

Well Said - Work with a partner

Posted on 2018-10-03 | Post modified 2018-10-03

Hello, My name is Liam, and I’d like to introduce xxx, or you can also call him ooo. He is from Spain. He can speak at least three languages: Spanish, English, of course, and German.

Read more »

Analysis of Algorithms Lecture 10

Posted on 2018-10-02 | Post modified 2018-10-06 | In Algorithms , Basic Concepts , Divide and Conquer
Read more »
1…678…18
Liam Wang

Liam Wang

Liam's Personal Blog

174 posts
133 categories
64 tags
© 2019 Liam Wang
Powered by Hexo
Theme - NexT.Muse