Skip to content

Commit a019353

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 021c2bc + 81d4e12 commit a019353

File tree

31 files changed

+1014
-0
lines changed

31 files changed

+1014
-0
lines changed

โ€Ž.github/workflows/integration.yaml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,82 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v4
1111
- uses: fernandrone/linelint@0.0.6
12+
13+
label-lang:
14+
runs-on: ubuntu-latest
15+
continue-on-error: true
16+
17+
permissions:
18+
contents: write
19+
pull-requests: write
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Node.js
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: '20'
29+
30+
- name: Create package.json
31+
run: echo '{}' > package.json
32+
33+
- name: Install dependencies
34+
run: npm install @octokit/rest node-fetch
35+
36+
- name: Detect languages and add labels
37+
env:
38+
GITHUB_TOKEN: ${{ github.token }}
39+
PR_NUM: ${{ github.event.number }}
40+
run: |
41+
node --input-type=module -e "
42+
import { Octokit } from '@octokit/rest';
43+
import path from 'path';
44+
import fetch from 'node-fetch';
45+
46+
const octokit = new Octokit({
47+
auth: process.env.GITHUB_TOKEN,
48+
request: { fetch }
49+
});
50+
51+
const extensionsToLanguages = {
52+
js: 'js',
53+
ts: 'ts',
54+
py: 'py',
55+
java: 'java',
56+
kt: 'kotlin',
57+
cpp: 'c++',
58+
go: 'go',
59+
exs: 'elixir',
60+
swift: 'swift'
61+
// ํ•„์š”ํ•œ ๋‹ค๋ฅธ ํ™•์žฅ์ž์™€ ์–ธ์–ด ๋งคํ•‘ ์ถ”๊ฐ€
62+
};
63+
64+
async function run() {
65+
const { data: files } = await octokit.pulls.listFiles({
66+
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
67+
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
68+
pull_number: process.env.PR_NUM,
69+
});
70+
71+
const languages = new Set();
72+
files.forEach(file => {
73+
const ext = path.extname(file.filename).slice(1);
74+
if (extensionsToLanguages[ext]) {
75+
languages.add(extensionsToLanguages[ext]);
76+
}
77+
});
78+
79+
if (languages.size > 0) {
80+
await octokit.issues.addLabels({
81+
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
82+
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
83+
issue_number: process.env.PR_NUM,
84+
labels: Array.from(languages),
85+
});
86+
}
87+
}
88+
89+
run();
90+
"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* For the number of given nodes N,
3+
*
4+
* Time complexity: O(N)
5+
*
6+
* Space complexity: O(N) at worst
7+
*/
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
23+
unordered_map<int, int> inorder_index_map;
24+
stack<TreeNode*> tree_stack;
25+
26+
for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i;
27+
28+
TreeNode* root = new TreeNode(preorder[0]);
29+
tree_stack.push(root);
30+
31+
for (int i = 1; i < preorder.size(); i++) {
32+
TreeNode* curr = new TreeNode(preorder[i]);
33+
34+
if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) {
35+
tree_stack.top()->left = curr;
36+
} else {
37+
TreeNode* parent;
38+
while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) {
39+
parent = tree_stack.top();
40+
tree_stack.pop();
41+
}
42+
parent->right = curr;
43+
}
44+
tree_stack.push(curr);
45+
}
46+
47+
return root;
48+
}
49+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.equals.shouldBeEqual
4+
import org.junit.jupiter.api.Test
5+
6+
class `construct-binary-tree-from-preorder-and-inorder-traversal` {
7+
8+
/**
9+
* preorder : ํ˜„์žฌ(๋ถ€๋ชจ) ๋…ธ๋“œ๋ถ€ํ„ฐ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
10+
* inorder : ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ๋ถ€ํ„ฐ ๋ถ€๋ชจ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ
11+
*/
12+
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
13+
val inorderIndices = inorder.withIndex().associate { it.value to it.index }
14+
return traversal(preorder, inorder, inorderIndices)
15+
}
16+
17+
/**
18+
* preorder์—์„œ ์กฐํšŒํ•œ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’์€ inorder์˜ ์ค‘๊ฐ„์— ์œ„์น˜ํ•œ๋‹ค.
19+
* ๊ทธ ์ค‘๊ฐ„ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
20+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
21+
*/
22+
private fun traversal(
23+
preorder: IntArray, inorder: IntArray, inorderIndices: Map<Int, Int>,
24+
preStart: Int = 0, inStart: Int = 0, inEnd: Int = inorder.size - 1
25+
): TreeNode? {
26+
if (preStart > preorder.size - 1 || inStart > inEnd) {
27+
return null
28+
}
29+
val value = preorder[preStart]
30+
val rootIndexInInorder = inorderIndices[value]!!
31+
32+
return TreeNode(value).apply {
33+
this.left = traversal(
34+
preorder, inorder, inorderIndices,
35+
preStart + 1, inStart, rootIndexInInorder - 1
36+
)
37+
this.right = traversal(
38+
preorder, inorder, inorderIndices,
39+
preStart + rootIndexInInorder - inStart + 1, rootIndexInInorder + 1, inEnd
40+
)
41+
}
42+
}
43+
44+
@Test
45+
fun `์ „์œ„ ์ˆœํšŒ, ์ค‘์œ„ ์ˆœํšŒ ์ˆœ์„œ์˜ ์ •์ˆ˜ ๋ฐฐ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
46+
val actual = buildTree(intArrayOf(3,9,20,15,7), intArrayOf(9,3,15,20,7))!!
47+
val expect = TreeNode.of(3,9,20,null,null,15,7)!!
48+
49+
actual shouldBeEqual expect
50+
51+
val actual1 = buildTree(intArrayOf(3,9,8,10,20,15,7), intArrayOf(8,9,10,3,15,20,7))!!
52+
val expect1 = TreeNode.of(3,9,20,8,10,15,7)!!
53+
54+
actual1 shouldBeEqual expect1
55+
}
56+
}

โ€Žcontains-duplicate/0-chan.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* time complexity : O(n)
3+
* space complexity : O(n)
4+
*/
5+
function containsDuplicate(nums: number[]): boolean {
6+
const hasDuplicate = new Set(nums).size !== nums.length;
7+
return hasDuplicate;
8+
};

โ€Žcontains-duplicate/GUMUNYEONG.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
const setObj = new Set(nums);
7+
8+
const diff = !(nums.length === setObj.size);
9+
10+
return diff;
11+
};
12+
13+
// TC: O(n)
14+
// SC: O(n)

โ€Žcontains-duplicate/codyman0.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
https://leetcode.com/problems/contains-duplicate/
3+
"""
4+
5+
# Time complexity : O(n)
6+
7+
8+
class Solution:
9+
def containsDuplicate(self, nums: List[int]) -> bool:
10+
sortedArray = sorted(nums)
11+
for i in range(len(sortedArray)):
12+
if i == len(sortedArray) - 1:
13+
return False
14+
if sortedArray[i] == sortedArray[i + 1] :
15+
return True
16+
return False

โ€Žcontains-duplicate/f-exuan21.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// time : O(n)
2+
// space : O(n)
3+
4+
class Solution {
5+
public boolean containsDuplicate(int[] nums) {
6+
Set<Integer> set = new HashSet<>();
7+
for(int i : nums) {
8+
if(set.contains(i)) {
9+
return true;
10+
}
11+
set.add(i);
12+
}
13+
return false;
14+
}
15+
}
16+

โ€Žcontains-duplicate/hajunyoo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
# Time complexity: O(n)
3+
def containsDuplicate(self, nums: List[int]) -> bool:
4+
string_len = len(nums)
5+
set_len = len(set(nums))
6+
7+
return string_len != set_len

โ€Žcontains-duplicate/whewchews.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
const dict: Set<number> = new Set();
3+
4+
// O(n)
5+
for (let i = 0; i <= nums.length; i++) {
6+
const n = nums[i];
7+
8+
// O(1)
9+
if (dict.has(n)) return true;
10+
// O(1)
11+
dict.add(n);
12+
}
13+
14+
return false;
15+
}
16+
17+
// TC: O(N)
18+
// SC: O(N)

โ€Žcounting-bits/flynn.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Time complexity: O(N)
3+
*
4+
* Space complexity: O(1)
5+
*/
6+
7+
class Solution {
8+
public:
9+
vector<int> countBits(int n) {
10+
vector<int> res;
11+
res.push_back(0);
12+
13+
int i = 1, j = 1;
14+
while (i <= n) {
15+
res.push_back(res[i - j] + 1);
16+
i++;
17+
if (i == j * 2) j *= 2;
18+
}
19+
20+
return res;
21+
}
22+
};

โ€Žcounting-bits/jdalma.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `counting-bits` {
7+
8+
fun countBits(n: Int): IntArray {
9+
return usingDPAndLeastSignificantBit(n)
10+
}
11+
12+
// 1. ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋งŒํผ ์ˆœํšŒํ•˜๋ฉฐ bit ์นด์šดํŠธ
13+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * log(n)), ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
14+
private fun usingBinary(n: Int): IntArray {
15+
fun binary(n: Int): Int {
16+
var calc = n
17+
var count = 0
18+
while(calc > 0) {
19+
if (calc % 2 != 0) {
20+
count++
21+
}
22+
calc /= 2
23+
}
24+
return count
25+
}
26+
27+
return (0 .. n).map { binary(it) }.toIntArray()
28+
}
29+
30+
// 2. MSB, ์ฆ‰ ์ตœ์ƒ์œ„ ๋น„ํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹ญ์ง„์ˆ˜๊ฐ€ ๋‘ ๋ฐฐ๊ฐ€ ๋ ๋•Œ๋งˆ๋‹ค MSB๋ฅผ ๊ฐฑ์‹ ํ•˜์—ฌ ์ด์ „์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™œ์šฉ
31+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
32+
private fun usingDPAndMostSignificantBit(n: Int): IntArray {
33+
val dp = IntArray(n + 1)
34+
var msb = 1
35+
36+
for (index in 1 .. n) {
37+
if (index == msb shl 1) {
38+
msb = index
39+
}
40+
dp[index] = 1 + dp[index - msb]
41+
}
42+
43+
return dp
44+
}
45+
46+
// 3. ์ตœํ•˜์œ„ ๋น„ํŠธ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์žฌํ™œ์šฉํ•œ๋‹ค. (์ตœํ•˜์œ„ ๋น„ํŠธ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ) + (ํ˜„์žฌ ์‹ญ์ง„์ˆ˜์˜ ์ตœํ•˜์œ„๋น„ํŠธ)
47+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
48+
private fun usingDPAndLeastSignificantBit(n: Int): IntArray {
49+
val dp = IntArray(n + 1)
50+
for (index in 1 .. n) {
51+
dp[index] = dp[index shr 1] + (index and 1)
52+
}
53+
54+
return dp
55+
}
56+
57+
@Test
58+
fun `์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ฐ i(0 ~ i)์— ๋Œ€ํ•ด ์ด์ง„ ํ‘œํ˜„์—์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
59+
countBits(2) shouldBe intArrayOf(0,1,1)
60+
countBits(5) shouldBe intArrayOf(0,1,1,2,1,2)
61+
}
62+
}

0 commit comments

Comments
ย (0)