Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions container-with-most-water/tedkimdev.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 좌우 포인터를 이동시키며 최적의 영역을 찾는 두 포인터 패턴을 사용하여 효율적으로 문제를 해결합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(n)
// SC: O(1)
impl Solution {
pub fn max_area(heights: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, heights.len() - 1);
let mut res = 0i32;
while left <= right {
res = res.max(((right - left) as i32) * heights[left].min(heights[right]));

if heights[right] < heights[left] {
right -= 1;
} else {
left += 1;
}
}

res
}
}
56 changes: 56 additions & 0 deletions design-add-and-search-words-data-structure/tedkimdev.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie, Backtracking
  • 설명: 이 코드는 Trie 자료구조를 활용하여 단어 검색을 효율적으로 수행하며, '.' 와일드카드 문자에 대해 DFS 기반 백트래킹을 사용합니다. 따라서 Trie와 Backtracking 패턴이 적용됩니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
type WordDictionary struct {
root *TrieNode
}

func Constructor() WordDictionary {
return WordDictionary{root: NewTrieNode()}
}

// TC: O(n)
// SC: O(t + n)
// t = total number of TrieNodes created so far
func (this *WordDictionary) AddWord(word string) {
cur := this.root
for _, c := range word {
if cur.Children[c-'a'] == nil {
cur.Children[c-'a'] = NewTrieNode()
}
cur = cur.Children[c-'a']
}
cur.IsWord = true
}

// TC: O(n)
func (this *WordDictionary) Search(word string) bool {
return this.dfs(word, 0, this.root)
}

func (this *WordDictionary) dfs(word string, index int, root *TrieNode) bool {
cur := root
for i := index; i < len(word); i++ {
c := word[i]
if c == '.' {
for _, child := range cur.Children {
if child != nil && this.dfs(word, i+1, child) {
return true
}
}
return false
} else {
if cur.Children[c-'a'] == nil {
return false
}
cur = cur.Children[c-'a']
}
}
return cur.IsWord
}

type TrieNode struct {
Children [26]*TrieNode
IsWord bool
}

func NewTrieNode() *TrieNode {
return &TrieNode{}
}
32 changes: 32 additions & 0 deletions longest-increasing-subsequence/tedkimdev.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 최장 증가 부분 수열의 길이를 찾기 위해 DP 배열을 사용하여 이전 결과를 저장하며 계산합니다. 부분 문제를 해결하고 이를 활용하는 전형적인 DP 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// TC: O(n ^ 2)
// SC: O(n)
func lengthOfLIS(nums []int) int {
dp := make([]int, len(nums))

for i := range dp {
dp[i] = 1
}

for i := len(nums) - 2; i >= 0; i-- {
for j := i + 1; j < len(nums); j++ {
if nums[i] < nums[j] {
if dp[i] < 1+dp[j] {
dp[i] = 1 + dp[j]
}
}
}
}

maxLength := 1
for _, length := range dp {
maxLength = max(maxLength, length)
}
return maxLength
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
38 changes: 38 additions & 0 deletions spiral-matrix/tedkimdev.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window
  • 설명: 이 코드는 행과 열의 범위를 조절하며 겹치지 않게 원형으로 순회하는 방식으로, 슬라이딩 윈도우와 유사한 패턴을 사용합니다. 각 방향별로 범위를 좁혀가며 탐색하는 구조입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// TC: O(m * n)
// SC: O(m * n)
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result: Vec<i32> = Vec::new();

let mut left = 0i32;
let mut right = matrix[0].len() as i32;
let mut top = 0i32;
let mut bottom = matrix.len() as i32;

while left < right && top < bottom {
for i in left..right {
result.push(matrix[top as usize][i as usize]);
}
top += 1;
for i in top..bottom {
result.push(matrix[i as usize][(right - 1) as usize]);
}
right -= 1;

if !(left < right && top < bottom) {
break;
}

for i in (left..right).rev() {
result.push(matrix[(bottom - 1) as usize][i as usize]);
}
bottom -= 1;
for i in (top..bottom).rev() {
result.push(matrix[i as usize][left as usize]);
}
left += 1;
}

result
}
}
33 changes: 33 additions & 0 deletions valid-parentheses/tedkimdev.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack / Monotonic Stack
  • 설명: 이 코드는 괄호의 유효성을 검사하기 위해 스택을 활용하며, 열린 괄호를 스택에 넣고 닫힌 괄호와 비교하는 방식으로 문제를 해결합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// TC: O(n)
// SC: O(n)
impl Solution {
pub fn is_valid(s: String) -> bool {
let mut stack = Vec::new();

for c in s.chars() {
match c {
'{' | '(' | '[' => {
stack.push(c);
},
'}' => {
if Some('{') != stack.pop() {
return false;
}
},
')' => {
if Some('(') != stack.pop() {
return false;
}
},
']' => {
if Some('[') != stack.pop() {
return false;
}
},
_ => {},
};
}

stack.is_empty()
}
}
Loading