Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New content: kickstart 2022 sol #20

Merged
merged 32 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d5063fa
try to import new theme
koneko096 Jun 26, 2020
4f7ddef
update copyright
koneko096 Jun 26, 2020
5de443c
restructuring for new theme
koneko096 Jun 26, 2020
039074f
change ci pipeline commands
koneko096 Jun 26, 2020
7fb238d
add new content
koneko096 Jun 26, 2020
221f6c2
enable gh actions
koneko096 Aug 11, 2021
9f85cb9
build page
koneko096 Aug 11, 2021
e8c39d2
downgrade hugo
koneko096 Aug 11, 2021
549c6ed
npm install
koneko096 Aug 11, 2021
4e56a32
deploy to staging
koneko096 Aug 11, 2021
59b76e1
deploy to live
koneko096 Aug 11, 2021
f9df07b
wrong branch name
koneko096 Aug 11, 2021
f6930c2
update copyright
koneko096 Aug 11, 2021
efdb35e
Merge branch 'source' into staging
koneko096 Aug 11, 2021
982143b
create new content
koneko096 Aug 25, 2021
acf9790
add links
koneko096 Aug 25, 2021
cc1a851
switch theme
koneko096 Aug 29, 2021
8093709
fix indentation
koneko096 Aug 29, 2021
4bb1b74
fix indentation
koneko096 Aug 29, 2021
08320ea
fix indentation
koneko096 Aug 29, 2021
431bca3
update staging config
koneko096 Aug 29, 2021
c50cafb
update content structure
koneko096 Aug 29, 2021
d0950be
update content structure
koneko096 Aug 29, 2021
f53944f
update content structure
koneko096 Aug 29, 2021
5efddc9
update content structure
koneko096 Aug 29, 2021
401c30a
cleanup
koneko096 Aug 29, 2021
853a2c6
update config prod
koneko096 Aug 29, 2021
0d9214d
Merge branch 'source' into staging
koneko096 Aug 29, 2021
f81d8ad
fix: change broken link + update copyright
koneko096 Jan 21, 2022
4333504
Merge branch 'source' into staging
koneko096 Jan 21, 2022
8989646
content: kickstart 2022 sol
koneko096 Apr 29, 2022
4284d35
content: update wording
koneko096 Apr 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
public/
config.toml
config.toml
.DS_store
2 changes: 1 addition & 1 deletion config.toml.source
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
baseURL = "https://koneko096.github.io"
title = "Postmortem"
author = "Afrizal Fikri"
copyright = "Copyright © 2021 - Afrizal Fikri"
copyright = "Copyright © 2022 - Afrizal Fikri"
canonifyurls = true
theme = "paper"
languageCode = "en-us"
Expand Down
2 changes: 1 addition & 1 deletion config.toml.staging
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
baseURL = "https://koneko096.github.io/staging"
title = "Postmortem"
author = "Afrizal Fikri"
copyright = "Copyright © 2021 - Afrizal Fikri"
copyright = "Copyright © 2022 - Afrizal Fikri"
canonifyurls = true
theme = "paper"
languageCode = "en-us"
Expand Down
2 changes: 1 addition & 1 deletion content/posts/atari-pong.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ There are few core component of reinforment learning: **environment state**, **a

{{< figure src="https://cdn-images-1.medium.com/max/1600/1*HvoLc50Dpq1ESKuejhICHg.png" title="RL loopback" >}}

Main goal of RL is to determine the best policy in order to maximize the *expected rewards*. We can imagine a policy as a path need to be taken to reach the goal. The longer the path taken, the least score can be obtained. If we for example, take the "dangerous" path which contains trap, we get the penalty and reducing the final score too. To achieve the best policy, sometimes we need to leave the most promising policy to explore another uncertain policy. This problem known as [**exploration vs exploitation dilemma**](http://www.cs.cmu.edu/~rsalakhu/10703/Lecture_Exploration.pdf).
Main goal of RL is to determine the best policy in order to maximize the *expected rewards*. We can imagine a policy as a path need to be taken to reach the goal. The longer the path taken, the least score can be obtained. If we for example, take the "dangerous" path which contains trap, we get the penalty and reducing the final score too. To achieve the best policy, sometimes we need to leave the most promising policy to explore another uncertain policy. This problem known as [**exploration vs exploitation dilemma**](https://towardsdatascience.com/intuition-exploration-vs-exploitation-c645a1d37c7a).

Using value function, we could determine whether our policy is good enough or need to be improved. Basically, there are 2 main method for policy improvement, by using **policy update** and **value update**. Value based method perform update by choosing maximum value available for each state. Maximum value computed by taking all possible action and take the most promising ones. Value based method does not care about certain policy during learning phase. On the other hand, policy based certainly does care about policy. This method changing policy step by step until value obtained maximized. **In short, value based is good for minimal state space, and policy based is good for minimal actions**. For further explanation, I suggest you to read my references on the bottom of this page. My post here will focus only on my experiment about Atari Pong games.

Expand Down
41 changes: 41 additions & 0 deletions content/posts/kickstart-2022.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
+++
categories = ["cp"]
tags = ["algorithm", "interview", "competition"]
date = "2022-04-29"
title = "Kick Start 2022 Round B: Hamiltonian Tour Solution"
type = "posts"
+++

It's been years since I wrote about algorithms and coding contest. This blog actually was not intended for that purpose because I thought I decided to move on from CP. But when I tried joining Kickstart few days ago, I stumbled upon this unique problem.

Even though this is the least solved problem in this round, the problem itself actually not that hard. No esoteric techniques needed (like greedy, DnC, or DP), and even no complex data structure used in for this problem. I can argue that it is an example of good coding interview problem (sure, I will talk more about coding interview in another post).

## Problem Description

Kick Start is one of Google annual coding tests beside Code Jam and Hash Code. But this contest is a bit different. The prizes for the top contestants are job opportunity at Google, not money.

Complete description can be viewed [here](https://codingcompetitions.withgoogle.com/kickstart/round/00000000008caa74/0000000000acf318). Given a N x M terrain with obstacles, each cell consists of 2x2 minicells. You start from top left corner (1,1). You have to find a way to visit each minicell *exactly once* and come back to initial corner, if any. You can only move into four true neighbors (no diagonal move).

{{< figure src="https://codejam.googleapis.com/dashboard/get_file/AQj_6U32_0J5YBAoNnQ3fGCopyxBqak78IdIxW1-INgCDqKp65LFlrrPgjKzBFwegZ6aVOcOwnqLdOM/case2.png" >}}

Google has released the official analysis. But I found it too difficult to understand. So, here I try to simplifying things for you guys with more intuitive way.

## Preorder Traversal

First, consider us a binary sorted tree (BST). Each node may have at most two childs, one for each side --left and right sorted in a way that left side is always less than the right side. When we want to output all elements in sorted manner increasingly, we perform what we call preorder traversal; in short `left-parent-right`. If we following the path of traversal, we can notice that each edge will be passed exactly twice, forward move and return move.

{{< figure src="/img/bst.png" >}}

## Minicell

What's the relation of preorder traversal with this problem? The trick is in the minicell. We knew that each cell will be splitted into 2x2 minicells. Consider a path between two junctions as below. The path will then looks like a two way road. We can reduce the junction as BST node and paths between them as BST edges. Hence, the fact that each edge will be passed twice, relates with the fact that all minicells in paths will be passed exactly once.

{{< figure src="/img/two-way-road.png" >}}

Then how about the nodes? Remember that our cells (and minicells in this matters) can connect with neighbors in four directions. For a fully connected cell, we can imagine them like a star with four inlets/outlets. In contrast, for a cell completely surrounded with obstacles, we can make a circle inside the "block". Anything in between will follows the same rule just like a roundabout. Inlet/outlet for valid neighbors, a bypass for a blocked side. This way, all 2x2 minicells will get visited. We just need to make sure the direction is consistent. In this articles, I choose counterclockwise direction following the problem examples.

{{< figure src="/img/roundabout.png" >}}

## Combining Altogether

Finally we can transform our tree into grids with nodes and edges building block above in mind. The rest is building the tree using DFS traversal as usual following counterclockwise direction. It's guaranteed for all cells that connected, we can built a complete path. So, we need to do DFS from (1,1) corner and keep the count of visited cells. If visited cells less than total of all the cells, it means there're unreachable cells and just output `IMPOSSIBLE`.
Binary file added static/img/bst.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/roundabout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/two-way-road.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.