Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunjae95 committed Aug 23, 2024
2 parents 021c2bc + 81d4e12 commit e1b8436
Show file tree
Hide file tree
Showing 31 changed files with 1,014 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,82 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: fernandrone/[email protected]

label-lang:
runs-on: ubuntu-latest
continue-on-error: true

permissions:
contents: write
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Create package.json
run: echo '{}' > package.json

- name: Install dependencies
run: npm install @octokit/rest node-fetch

- name: Detect languages and add labels
env:
GITHUB_TOKEN: ${{ github.token }}
PR_NUM: ${{ github.event.number }}
run: |
node --input-type=module -e "
import { Octokit } from '@octokit/rest';
import path from 'path';
import fetch from 'node-fetch';
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
request: { fetch }
});
const extensionsToLanguages = {
js: 'js',
ts: 'ts',
py: 'py',
java: 'java',
kt: 'kotlin',
cpp: 'c++',
go: 'go',
exs: 'elixir',
swift: 'swift'
// ํ•„์š”ํ•œ ๋‹ค๋ฅธ ํ™•์žฅ์ž์™€ ์–ธ์–ด ๋งคํ•‘ ์ถ”๊ฐ€
};
async function run() {
const { data: files } = await octokit.pulls.listFiles({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
pull_number: process.env.PR_NUM,
});
const languages = new Set();
files.forEach(file => {
const ext = path.extname(file.filename).slice(1);
if (extensionsToLanguages[ext]) {
languages.add(extensionsToLanguages[ext]);
}
});
if (languages.size > 0) {
await octokit.issues.addLabels({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
issue_number: process.env.PR_NUM,
labels: Array.from(languages),
});
}
}
run();
"
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* For the number of given nodes N,
*
* Time complexity: O(N)
*
* Space complexity: O(N) at worst
*/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
unordered_map<int, int> inorder_index_map;
stack<TreeNode*> tree_stack;

for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i;

TreeNode* root = new TreeNode(preorder[0]);
tree_stack.push(root);

for (int i = 1; i < preorder.size(); i++) {
TreeNode* curr = new TreeNode(preorder[i]);

if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) {
tree_stack.top()->left = curr;
} else {
TreeNode* parent;
while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) {
parent = tree_stack.top();
tree_stack.pop();
}
parent->right = curr;
}
tree_stack.push(curr);
}

return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package leetcode_study

import io.kotest.matchers.equals.shouldBeEqual
import org.junit.jupiter.api.Test

class `construct-binary-tree-from-preorder-and-inorder-traversal` {

/**
* preorder : ํ˜„์žฌ(๋ถ€๋ชจ) ๋…ธ๋“œ๋ถ€ํ„ฐ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
* inorder : ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ๋ถ€ํ„ฐ ๋ถ€๋ชจ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
*/
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
val inorderIndices = inorder.withIndex().associate { it.value to it.index }
return traversal(preorder, inorder, inorderIndices)
}

/**
* preorder์—์„œ ์กฐํšŒํ•œ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’์€ inorder์˜ ์ค‘๊ฐ„์— ์œ„์น˜ํ•œ๋‹ค.
* ๊ทธ ์ค‘๊ฐ„ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
*/
private fun traversal(
preorder: IntArray, inorder: IntArray, inorderIndices: Map<Int, Int>,
preStart: Int = 0, inStart: Int = 0, inEnd: Int = inorder.size - 1
): TreeNode? {
if (preStart > preorder.size - 1 || inStart > inEnd) {
return null
}
val value = preorder[preStart]
val rootIndexInInorder = inorderIndices[value]!!

return TreeNode(value).apply {
this.left = traversal(
preorder, inorder, inorderIndices,
preStart + 1, inStart, rootIndexInInorder - 1
)
this.right = traversal(
preorder, inorder, inorderIndices,
preStart + rootIndexInInorder - inStart + 1, rootIndexInInorder + 1, inEnd
)
}
}

@Test
fun `์ „์œ„ ์ˆœํšŒ, ์ค‘์œ„ ์ˆœํšŒ ์ˆœ์„œ์˜ ์ •์ˆ˜ ๋ฐฐ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
val actual = buildTree(intArrayOf(3,9,20,15,7), intArrayOf(9,3,15,20,7))!!
val expect = TreeNode.of(3,9,20,null,null,15,7)!!

actual shouldBeEqual expect

val actual1 = buildTree(intArrayOf(3,9,8,10,20,15,7), intArrayOf(8,9,10,3,15,20,7))!!
val expect1 = TreeNode.of(3,9,20,8,10,15,7)!!

actual1 shouldBeEqual expect1
}
}
8 changes: 8 additions & 0 deletions contains-duplicate/0-chan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* time complexity : O(n)
* space complexity : O(n)
*/
function containsDuplicate(nums: number[]): boolean {
const hasDuplicate = new Set(nums).size !== nums.length;
return hasDuplicate;
};
14 changes: 14 additions & 0 deletions contains-duplicate/GUMUNYEONG.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
const setObj = new Set(nums);

const diff = !(nums.length === setObj.size);

return diff;
};

// TC: O(n)
// SC: O(n)
16 changes: 16 additions & 0 deletions contains-duplicate/codyman0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
https://leetcode.com/problems/contains-duplicate/
"""

# Time complexity : O(n)


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
sortedArray = sorted(nums)
for i in range(len(sortedArray)):
if i == len(sortedArray) - 1:
return False
if sortedArray[i] == sortedArray[i + 1] :
return True
return False
16 changes: 16 additions & 0 deletions contains-duplicate/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// time : O(n)
// space : O(n)

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int i : nums) {
if(set.contains(i)) {
return true;
}
set.add(i);
}
return false;
}
}

7 changes: 7 additions & 0 deletions contains-duplicate/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
# Time complexity: O(n)
def containsDuplicate(self, nums: List[int]) -> bool:
string_len = len(nums)
set_len = len(set(nums))

return string_len != set_len
18 changes: 18 additions & 0 deletions contains-duplicate/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function containsDuplicate(nums: number[]): boolean {
const dict: Set<number> = new Set();

// O(n)
for (let i = 0; i <= nums.length; i++) {
const n = nums[i];

// O(1)
if (dict.has(n)) return true;
// O(1)
dict.add(n);
}

return false;
}

// TC: O(N)
// SC: O(N)
22 changes: 22 additions & 0 deletions counting-bits/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Time complexity: O(N)
*
* Space complexity: O(1)
*/

class Solution {
public:
vector<int> countBits(int n) {
vector<int> res;
res.push_back(0);

int i = 1, j = 1;
while (i <= n) {
res.push_back(res[i - j] + 1);
i++;
if (i == j * 2) j *= 2;
}

return res;
}
};
62 changes: 62 additions & 0 deletions counting-bits/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class `counting-bits` {

fun countBits(n: Int): IntArray {
return usingDPAndLeastSignificantBit(n)
}

// 1. ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋งŒํผ ์ˆœํšŒํ•˜๋ฉฐ bit ์นด์šดํŠธ
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * log(n)), ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
private fun usingBinary(n: Int): IntArray {
fun binary(n: Int): Int {
var calc = n
var count = 0
while(calc > 0) {
if (calc % 2 != 0) {
count++
}
calc /= 2
}
return count
}

return (0 .. n).map { binary(it) }.toIntArray()
}

// 2. MSB, ์ฆ‰ ์ตœ์ƒ์œ„ ๋น„ํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹ญ์ง„์ˆ˜๊ฐ€ ๋‘ ๋ฐฐ๊ฐ€ ๋ ๋•Œ๋งˆ๋‹ค MSB๋ฅผ ๊ฐฑ์‹ ํ•˜์—ฌ ์ด์ „์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™œ์šฉ
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
private fun usingDPAndMostSignificantBit(n: Int): IntArray {
val dp = IntArray(n + 1)
var msb = 1

for (index in 1 .. n) {
if (index == msb shl 1) {
msb = index
}
dp[index] = 1 + dp[index - msb]
}

return dp
}

// 3. ์ตœํ•˜์œ„ ๋น„ํŠธ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์žฌํ™œ์šฉํ•œ๋‹ค. (์ตœํ•˜์œ„ ๋น„ํŠธ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ) + (ํ˜„์žฌ ์‹ญ์ง„์ˆ˜์˜ ์ตœํ•˜์œ„๋น„ํŠธ)
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
private fun usingDPAndLeastSignificantBit(n: Int): IntArray {
val dp = IntArray(n + 1)
for (index in 1 .. n) {
dp[index] = dp[index shr 1] + (index and 1)
}

return dp
}

@Test
fun `์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ฐ i(0 ~ i)์— ๋Œ€ํ•ด ์ด์ง„ ํ‘œํ˜„์—์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
countBits(2) shouldBe intArrayOf(0,1,1)
countBits(5) shouldBe intArrayOf(0,1,1,2,1,2)
}
}
Loading

0 comments on commit e1b8436

Please sign in to comment.