Skip to content

Week6 Solution #473

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

Closed
wants to merge 11 commits into from
Closed
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
37 changes: 37 additions & 0 deletions coin-change/yeonguchoe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
private Integer[] memo;

public int helper(int[] coins, int remain) {
if (remain < 0) { // 0 이하로 가는 경우 -1로 지정
return -1;
}
if (remain == 0) { // 처음 값은 0으로 설정
return 0;
}

if (memo[remain] != null) {
return memo[remain];
}

int minNumCoin = Integer.MAX_VALUE; // 세개 중에 최소값을 고르는 로직

for (int coin : coins) {
int numCoin = helper(coins, remain - coin);
if (numCoin != -1) { // 도달 가능한 경우
minNumCoin = Math.min(minNumCoin, numCoin + 1);
}
}
if (minNumCoin < Integer.MAX_VALUE) { // 도달 가능한 경우
memo[remain] = minNumCoin;
} else {
memo[remain] = -1;
}

return memo[remain];
}

public int coinChange(int[] coins, int amount) {
memo = new Integer[amount + 1];
return helper(coins, amount);
}
}
30 changes: 30 additions & 0 deletions combination-sum/yeonguchoe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
// Time complexity: O(candidate^(target/smallest candidate))
// Space complexity: O(target/smallest candidate)

List<List<Integer>> result = new ArrayList<>();

public void backtracking(int remainingAmount, List<Integer> combination, int startPoint, int[] candidateList,
List<List<Integer>> resultList) {
if (remainingAmount == 0) {
resultList.add(new ArrayList<>(combination));
return;
}

if (remainingAmount < 0) {
return;
}

for (int i = startPoint; i < candidateList.length; i++) {
combination.add(candidateList[i]);
backtracking(remainingAmount - candidateList[i], combination, i, candidateList, resultList);
combination.remove(combination.size() - 1);
}
}

public List<List<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<Integer> initialCombination = new ArrayList<>();
backtracking(target, initialCombination, 0, candidates, result);
return result;
}
}
22 changes: 22 additions & 0 deletions container-with-most-water/yeonguchoe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

public class Solution {
public int MaxArea(int[] height) {
int currentMax = int.MinValue;
int leftPtr = 0;
int rightPtr = height.Length - 1;

while (leftPtr < rightPtr) {
int currentAmount = (rightPtr - leftPtr) * Math.Min(height[leftPtr], height[rightPtr]);
currentMax = Math.Max(currentMax, currentAmount);

if (height[leftPtr] < height[rightPtr]) {
leftPtr++;
} else {
rightPtr--;
}
}

return currentMax;
}
}
67 changes: 67 additions & 0 deletions design-add-and-search-words-data-structure/yeonguchoe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
public class LetterNode
{
public LetterNode[] Alphabet { get; } = new LetterNode[26]; // Initialize to null
public bool IsEnd { get; set; }
}

public class WordDictionary
{
private LetterNode root;

public WordDictionary()
{
root = new LetterNode();
}

public void AddWord(string word)
{
LetterNode currentNode = root;
foreach (char c in word)
{
int index = c - 'a';
if (currentNode.Alphabet[index] == null)
{
currentNode.Alphabet[index] = new LetterNode();
}
currentNode = currentNode.Alphabet[index];
}
currentNode.IsEnd = true;
}

public bool Search(string word)
{
return Dfs(word, root);
}

private bool Dfs(string target, LetterNode currentRoot)
{
if (target.Length == 0)
{
return currentRoot.IsEnd;
}

char firstLetter = target[0];
string remainingTarget = target.Substring(1);

if (firstLetter == '.')
{
for (int i = 0; i < 26; i++)
{
if (currentRoot.Alphabet[i] != null && Dfs(remainingTarget, currentRoot.Alphabet[i]))
{
return true;
}
}
}
else
{
int index = firstLetter - 'a';
if (currentRoot.Alphabet[index] != null)
{
return Dfs(remainingTarget, currentRoot.Alphabet[index]);
}
}

return false;
}
}
22 changes: 22 additions & 0 deletions longest-increasing-subsequence/yeonguchoe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
public int LengthOfLIS(int[] nums) {
if (nums.Length == 0) return 0;

int[] dp = new int[nums.Length];
for (int i = 0; i < nums.Length; i++) {
dp[i] = 1;
}

int maxLength = 1;
for (int i = 1; i < nums.Length; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.Max(dp[i], dp[j] + 1);
}
}
maxLength = Math.max(maxLength, dp[i]);
}

return maxLength;
}
}
20 changes: 20 additions & 0 deletions maximum-product-subarray/yeonguchoe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
// Time complexity: O(n)
// Space complexity: O(1)
public:
int maxProduct(vector<int>& nums) {

int result = nums[0];
int previous_max = 1;
int previous_min = 1;

for (int num : nums) {
int temp = previous_max;
previous_max = max({num, previous_max * num, previous_min * num});
previous_min = min({num, temp * num, previous_min * num});
result = max(result, previous_max);
}

return result;
}
};
24 changes: 24 additions & 0 deletions product-of-array-except-self/yeonguchoe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
// Time complexity: O(n)
// Space complexity: O(n)
public int[] productExceptSelf(int[] nums) {
int[] leftMultiplier = new int[nums.length];
int[] rightMultiplier = new int[nums.length];

leftMultiplier[0] = 1;
for (int i = 0; i < nums.length - 1; i++) {
leftMultiplier[i + 1] = nums[i] * leftMultiplier[i];
}

rightMultiplier[nums.length - 1] = 1;
for (int i = nums.length - 1; i > 0; i--) {
rightMultiplier[i - 1] = nums[i] * rightMultiplier[i];
}

int[] result = new int[nums.length];
for (int i = 0; i < result.length; i++) {
result[i] = leftMultiplier[i] * rightMultiplier[i];
}
return result;
}
}
48 changes: 48 additions & 0 deletions spiral-matrix/yeonguchoe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;

public class Solution {
public IList<int> SpiralOrder(int[][] matrix) {
List<int> result = new List<int>();
int row = matrix.Length;
if (row == 0) return result;
int column = matrix[0].Length;

int upRail = 0;
int downRail = row - 1;
int leftRail = 0;
int rightRail = column - 1;

while (result.Count < row * column) {
// L->R
for (int c = leftRail; c <= rightRail; c++) {
result.Add(matrix[upRail][c]);
}
// T->B
for (int r = upRail + 1; r <= downRail; r++) {
result.Add(matrix[r][rightRail]);
}

// R->L
if (upRail != downRail) {
for (int c = rightRail - 1; c >= leftRail; c--) {
result.Add(matrix[downRail][c]);
}
}

// B->T
if (leftRail != rightRail) {
for (int r = downRail - 1; r > upRail; r--) {
result.Add(matrix[r][leftRail]);
}
}

leftRail += 1;
rightRail -= 1;
upRail += 1;
downRail -= 1;
}

return result;
}
}
24 changes: 24 additions & 0 deletions valid-parentheses/yeonguchoe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
public bool IsValid(string s) {
Stack<char> parentheses = new Stack<char>();

Dictionary<char, char> pair = new Dictionary<char, char> {
{ ')', '(' },
{ '}', '{' },
{ ']', '[' }
};

foreach (char c in s) {
if (c == '(' || c == '{' || c == '[') {
parentheses.Push(c);
}
else if (c == ')' || c == '}' || c == ']') {
if (parentheses.Count == 0 || parentheses.Peek() != pair[c]) {
return false;
}
parentheses.Pop();
}
}

return parentheses.Count == 0;
}
59 changes: 59 additions & 0 deletions word-search/yeonguchoe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
public:
bool backtrack(vector<vector<char>>& board, const string& word,
int current_row, int current_column, int index) {
// 이전 iteration까지 backtrack을 통과한 경우
// index가 단어의 길이와 같아지는 경우
if (index == word.size()) {
return true;
}

// 탐색중인 cell이 board를 넘어선 경우
if (current_row < 0 || current_row >= board.size() ||
current_column < 0 || current_column >= board[0].size()) {
return false;
}

// 탐색중인 cell에 있는 문자가 단어의 문자와 다른 경우
if (board[current_row][current_column] != word[index]) {
return false;
}

// 탐색중인 cell의 문자를 글자가 아닌것으로 지정하여, 방문 표시
char temp = board[current_row][current_column];
board[current_row][current_column] = '#';

// 탐색중인 cell의 주변을 backtrack으로 탐색
bool found =
backtrack(board, word, current_row - 1, current_column,
index + 1) or
backtrack(board, word, current_row, current_column - 1,
index + 1) or
backtrack(board, word, current_row + 1, current_column,
index + 1) or
backtrack(board, word, current_row, current_column + 1, index + 1);

// 탐색중인 cell의 값을 원래대로 돌려 놓음
board[current_row][current_column] = temp;

// 만약에 찾아진 경우, true를 위로 올림
// 만약에 탐색 도중에 2가지 false를 반환하는 케이스에 걸렸으면 false를
// 위로 올림
return found;
}

bool exist(vector<vector<char>>& board, string word) {

// 시작점을 설정
for (int row = 0; row < board.size(); ++row) {
for (int col = 0; col < board[0].size(); ++col) {
// 만약에 backtrack 함수가 true인 경우를 찾았으면, true 반환
if (backtrack(board, word, row, col, 0)) {
return true;
}
}
}
// true인 경우를 한번도 찾지 못했다면, false 반환
return false;
}
};