-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
31 changed files
with
1,014 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
" |
49 changes: 49 additions & 0 deletions
49
construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
56 changes: 56 additions & 0 deletions
56
construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.