Skip to content

Commit

Permalink
Merge pull request #100 from TakhyunKim/feature/algorithm
Browse files Browse the repository at this point in the history
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ํƒญ, ํŽ˜์ด์ง€ ์ถ”๊ฐ€ ๋ฐ ํฌ์ŠคํŒ… ์ถ”๊ฐ€
  • Loading branch information
TakhyunKim committed Apr 10, 2024
2 parents 187ff23 + 1cfae8c commit 3833bcb
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 1 deletion.
67 changes: 67 additions & 0 deletions algorithms/10828-stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "๋ฐฑ์ค€ 10828-stack"
date: "2024-04-09"
tag: "stack"
---

## ๋ฌธ์ œ

> <a href="https://www.acmicpc.net/problem/10828" target="_blank">๋ฌธ์ œ ๋งํฌ</a>
์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ์Šคํƒ์„ ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋ช…๋ น์€ ์ด ๋‹ค์„ฏ ๊ฐ€์ง€์ด๋‹ค.

push X: ์ •์ˆ˜ X๋ฅผ ์Šคํƒ์— ๋„ฃ๋Š” ์—ฐ์‚ฐ์ด๋‹ค.<br />
pop: ์Šคํƒ์—์„œ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๋นผ๊ณ , ๊ทธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.<br />
size: ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. <br />
empty: ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.<br />
top: ์Šคํƒ์˜ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.<br />

## ํ’€์ด

```ts
const answers = [];
const [n, ...commands] = input;

function solution(n, commands) {
const stack = [];

const stackCommand = {
push: (value) => {
stack.push(value);
},
pop: () => {
return stack.length === 0 ? -1 : stack.pop();
},
size: () => {
return stack.length;
},
empty: () => {
return stack.length === 0 ? 1 : 0;
},
top: () => {
return stack.length === 0 ? -1 : stack.at(-1);
},
};

for (i = 0; i < n; i++) {
const [command, value] = commands[i].split(" ");
if (command === "push") {
stackCommand.push(parseInt(value));
} else {
answers.push(stackCommand[command]());
}
}

console.log(answers.join("\n"));
}
```

## ์„ค๋ช…

Stack ๊ตฌํ˜„์€ javascript Array method ๋ฅผ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.<br />
๊ตฌํ˜„ํ•ด์•ผํ•  push, pop, size, empty, top ์„ ๊ตฌํ˜„ ํ›„,<br />
for ๋ฌธ์„ ํ†ตํ•ด ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ˆœํšŒ, split ์„ ํ†ตํ•ด ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ๋ฅผ ํ†ตํ•ด ๊ฐ’๊ณผ stack command ๋ฅผ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.<br />
push ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ method ๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ณ„๋„๋กœ ์ €์žฅํ•˜๊ณ ,<br />
๋งˆ์ง€๋ง‰์—” ์˜ˆ์ œ ์ถœ๋ ฅ์— ๋งž๋„๋ก `join("\n")` ์„ ํ†ตํ•ด ์ค„๋ฐ”๊ฟˆ ํ›„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์„ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.<br />
50 changes: 50 additions & 0 deletions algorithms/9093-reverse-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "๋ฐฑ์ค€ 9093-๋‹จ์–ด ๋’ค์ง‘๊ธฐ"
date: "2024-04-10"
tag: "stack,๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ"
---

## ๋ฌธ์ œ

> <a href="https://www.acmicpc.net/problem/9093" target="_blank">๋ฌธ์ œ ๋งํฌ</a>
๋ฌธ์žฅ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹จ์–ด๋ฅผ ๋ชจ๋‘ ๋’ค์ง‘์–ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋‹จ์–ด์˜ ์ˆœ์„œ๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค. ๋‹จ์–ด๋Š” ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ฌธ์žฅ์ด ํ•˜๋‚˜ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 20, ๋ฌธ์žฅ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 1000์ด๋‹ค. ๋‹จ์–ด์™€ ๋‹จ์–ด ์‚ฌ์ด์—๋Š” ๊ณต๋ฐฑ์ด ํ•˜๋‚˜ ์žˆ๋‹ค.

## ํ“ฐ์ด

```ts
function solution(targetList) {
const answers = [];

for (i = 0; i < targetList.length; i++) {
const reversedList = [];
const target = targetList[i].split(" ");

for (j = 0; j < target.length; j++) {
const reversedTarget = target[j].split("").reverse().join("");
reversedList.push(reversedTarget);
}

answers.push(reversedList.join(" "));
}

console.log(answers.join("\n"));
}

solution(targetInput);
```

## ์„ค๋ช…

๋ฌธ์ œ ์œ ํ˜•์€ ์Šคํƒ์ด๋‚˜, ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ๋Š” split(String method), reverse(Array method) ์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ ํ•ด๋‹น method ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

> 1. for ๋ฌธ์„ ํ†ตํ•ด ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ ๋Œ€์ƒ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
> 2. ๋Œ€์ƒ ๋ฌธ์ž์—ด์€ ๋ฌธ์žฅ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋‚ด๋ถ€์— ๋’ค์ง‘๊ธฐํ•œ ๋ฌธ์ž์—ด์„ ๋ณด๊ด€ํ•  ๋ฐฐ์—ด์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.<br />(ex: 'I am happy today')
> 3. ๋Œ€์ƒ ๋ฌธ์ž์—ด์„ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ split ํ•˜์—ฌ ๋ฐฐ์—ดํ™”ํ•ฉ๋‹ˆ๋‹ค.
> 4. ๋ฐฐ์—ดํ™”ํ•œ ๋ฌธ์ž์—ด์„ for ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
> 5. ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌ๋œ ๋ฌธ์ž์—ด์„ reverse ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ split("")์œผ๋กœ ๋‹จ์–ด ๋ณ„๋กœ ๋ถ„๋ฆฌํ•œ ํ›„ reverse ํ–ˆ์œผ๋ฉฐ, ์ด ํ›„ join("")์„ ํ†ตํ•ด ๋’ค์ง‘์€ ๋ฌธ์ž์—ด์„ ํ•ฉ์นฉ๋‹ˆ๋‹ค.
> 6. ๋’ค์ง‘๊ธฐํ•œ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด์— ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค.
> 7. ์ฒ˜์Œ for ๋ฌธ์ด ๋๋‚  ์ง€์ ์—์„  ๋’ค์ง‘์€ ๋ฌธ์ž์—ด list ๋ฅผ ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค.
> 8. ์ด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
49 changes: 49 additions & 0 deletions app/algorithms/AlgorithmPost.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.list_wrapper {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}

.item_wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 8px;
padding-bottom: 0;
border-radius: 8px;
margin-bottom: 12px;
}

.item_wrapper:hover {
background-color: var(--box-bg-hovered-color);
box-shadow: 10px 20px 20px 0 var(--box-shadow-hovered-color);
transition: 0.5s ease;
}

.item_header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 6px;
}

.title {
font-size: 1.3rem;
font-weight: bold;
}

.date {
margin-left: 0.5rem;
font-size: 0.8rem;
font-weight: 400;
color: var(--description-text-color);
}

.tag_wrapper {
display: flex;
justify-content: left;
}
54 changes: 54 additions & 0 deletions app/algorithms/AlgorithmPostList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Link from "next/link";
import Tag from "@/common/components/PostList/PostItem/Tag";

import styles from "./AlgorithmPost.module.css";

import type { PostData } from "@/common/utils/posts";

interface AlgorithmPostListProps {
algorithmPostList: PostData[];
}

const AlgorithmPostList = ({ algorithmPostList }: AlgorithmPostListProps) => {
return (
<section className={styles.list_wrapper}>
{algorithmPostList.map((algorithmPostData) => (
<AlgorithmPostItem
key={algorithmPostData.id}
algorithmPostData={algorithmPostData}
href={`/algorithms/${algorithmPostData.id}`}
/>
))}
</section>
);
};

interface AlgorithmPostItemProps {
href: string;
algorithmPostData: PostData;
}

const AlgorithmPostItem = ({
href,
algorithmPostData,
}: AlgorithmPostItemProps) => {
const { title, date, tagList } = algorithmPostData;

return (
<article className={styles.item_wrapper}>
<Link href={href}>
<div className={styles.item_header}>
<div className={styles.title}>{title}</div>
<div className={styles.date}>{date}</div>
</div>
<div className={styles.tag_wrapper}>
{tagList.map((tag) => (
<Tag key={tag} tag={tag} />
))}
</div>
</Link>
</article>
);
};

export default AlgorithmPostList;
96 changes: 96 additions & 0 deletions app/algorithms/[id]/page.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.title {
margin-top: 0;
}

.date {
margin-bottom: 3rem;
font-size: 1rem;
font-weight: 400;
color: var(--description-text-color);
}

.html_wrapper {
line-height: 1.7rem;
color: var(--post-text-color);
}

.html_wrapper h1 {
margin: 3rem 0;
color: var(--post-h-tag-color);
}

.html_wrapper h2 {
margin-top: 3rem;
color: var(--post-h-tag-color);
}

.html_wrapper h3 {
margin-top: 2rem;
color: var(--post-h-tag-color);
}

.html_wrapper a {
color: var(--post-link-tag-color);
text-decoration: underline;
text-underline-position: under;
text-decoration-color: var(--post-link-tag-color);
text-decoration-thickness: 1px;
}

.html_wrapper strong {
color: var(--post-h-tag-color);
}

.html_wrapper blockquote {
font-weight: 500;
font-style: italic;
color: var(--post-block-quote-color);
border-left: 3px solid var(--post-block-quote-color);
margin: 2rem 0;
padding-left: 1em;
}

.html_wrapper li {
margin: 1rem 0;
}

.html_wrapper code {
color: var(--post-h-tag-color);
font-size: 1rem;
font-weight: bold;
font-family: oblique;
background: none;
}

.html_wrapper pre {
color: var(--post-h-tag-color);
font-size: 1rem;
line-height: 2rem;
font-family: inherit;
border: 1px solid var(--post-text-color);
border-radius: 10px;
overflow: hidden;
background-color: none;
}

.html_wrapper pre code {
color: #d1d5db;
font-size: 1rem;
font-weight: initial;
font-family: inherit;
background-color: #292c33;
}

@media (max-width: 768px) {
.thumbnail_wrapper {
position: relative;
width: 100%;
height: 15rem;
border-radius: 10px;
overflow: hidden;
}

.html_wrapper img {
width: 90%;
}
}
54 changes: 54 additions & 0 deletions app/algorithms/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getAllPostIds, getPostData } from "@/common/utils/posts";

import MDXRemote from "@/common/components/MDXRemote";
import TopScrollButton from "@/common/components/TopScrollButton";
import TableOfContents from "@/common/components/TableOfContents";

import styles from "./page.module.css";

import type { Metadata } from "next";
import type { Params } from "@/common/types/params";

const getPost = async (params: Params) => {
const postData = await getPostData({ postType: "algorithms", id: params.id });

return postData;
};

export const generateMetadata = async ({
params,
}: {
params: Params;
}): Promise<Metadata> => {
const { title, description, tagList } = await getPost(params);

return {
title,
description,
keywords: tagList,
};
};

export const generateStaticParams = async () => {
const allPostIds = getAllPostIds({ postType: "algorithms" });

return allPostIds;
};

const AlgorithmPost = async ({ params }: { params: Params }) => {
const { date, title, mdxSource, tableOfContents } = await getPost(params);

return (
<>
<h1 className={styles.title}>{title}</h1>
<div className={styles.date}>{date}</div>
<div className={styles.html_wrapper}>
<MDXRemote {...mdxSource} />
</div>
<TopScrollButton />
<TableOfContents tableOfContents={tableOfContents} />
</>
);
};

export default AlgorithmPost;
18 changes: 18 additions & 0 deletions app/algorithms/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getSortedPostsData } from "@/common/utils/posts";

import AlgorithmPostList from "./AlgorithmPostList";

import type { Metadata } from "next";

export const metadata: Metadata = {
title: "์•Œ๊ณ ๋ฆฌ์ฆ˜ ํฌ์ŠคํŒ…",
description: "๋ฌธ์ œ ํ’€์ด๋ฅผ ๊ธฐ๋กํ•œ ํฌ์ŠคํŒ…",
};

const Algorithms = () => {
const allAlgorithmsData = getSortedPostsData({ postType: "algorithms" });
console.log(allAlgorithmsData);
return <AlgorithmPostList algorithmPostList={allAlgorithmsData} />;
};

export default Algorithms;
Loading

0 comments on commit 3833bcb

Please sign in to comment.