Skip to content

Commit 20c0de5

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 6543850 + 559dfdb commit 20c0de5

File tree

10 files changed

+280
-0
lines changed

10 files changed

+280
-0
lines changed

โ€Ž.github/workflows/integration.yamlโ€Ž

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,77 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v4
1111
- uses: fernandrone/[email protected]
12+
13+
label-lang:
14+
runs-on: ubuntu-latest
15+
continue-on-error: true
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: '20'
24+
25+
- name: Create package.json
26+
run: echo '{}' > package.json
27+
28+
- name: Install dependencies
29+
run: npm install @octokit/rest node-fetch
30+
31+
- name: Detect languages and add labels
32+
env:
33+
GITHUB_TOKEN: ${{ github.token }}
34+
PR_NUM: ${{ github.event.number }}
35+
run: |
36+
node --input-type=module -e "
37+
import { Octokit } from '@octokit/rest';
38+
import path from 'path';
39+
import fetch from 'node-fetch';
40+
41+
const octokit = new Octokit({
42+
auth: process.env.GITHUB_TOKEN,
43+
request: { fetch }
44+
});
45+
46+
const extensionsToLanguages = {
47+
js: 'js',
48+
ts: 'ts',
49+
py: 'py',
50+
java: 'java',
51+
kt: 'kotlin',
52+
cpp: 'c++',
53+
go: 'go',
54+
exs: 'elixir',
55+
swift: 'swift'
56+
// ํ•„์š”ํ•œ ๋‹ค๋ฅธ ํ™•์žฅ์ž์™€ ์–ธ์–ด ๋งคํ•‘ ์ถ”๊ฐ€
57+
};
58+
59+
async function run() {
60+
const { data: files } = await octokit.pulls.listFiles({
61+
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
62+
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
63+
pull_number: process.env.PR_NUM,
64+
});
65+
66+
const languages = new Set();
67+
files.forEach(file => {
68+
const ext = path.extname(file.filename).slice(1);
69+
if (extensionsToLanguages[ext]) {
70+
languages.add(extensionsToLanguages[ext]);
71+
}
72+
});
73+
74+
if (languages.size > 0) {
75+
await octokit.issues.addLabels({
76+
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
77+
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
78+
issue_number: process.env.PR_NUM,
79+
labels: Array.from(languages),
80+
});
81+
}
82+
}
83+
84+
run().catch(err => console.error(err));
85+
"
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+
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
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
private int count = 0;
19+
private TreeNode resultNode = null;
20+
21+
public int kthSmallest(TreeNode root, int k) {
22+
searchNode(root, k);
23+
return resultNode.val;
24+
}
25+
26+
public void searchNode(TreeNode node, int k) {
27+
28+
if(resultNode != null) return;
29+
30+
if(node.left != null) {
31+
searchNode(node.left, k);
32+
}
33+
34+
count++;
35+
36+
if(count == k) {
37+
resultNode = node;
38+
return;
39+
}
40+
41+
if(node.right != null) {
42+
searchNode(node.right, k);
43+
}
44+
}
45+
}
46+
47+
// n : ๋…ธ๋“œ ๊ฐœ์ˆ˜
48+
// time : O(n) ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผํ•จ
49+
// space : O(n) ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•œ ์ชฝ์œผ๋กœ ๋…ธ๋“œ๊ฐ€ ์น˜์šฐ์ณ์ ธ ์žˆ์Œ
50+
// -> ์žฌ๊ท€ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ ์Šคํƒ์— ์Œ“์ž„ -> ํ•œ ์ชฝ์œผ๋กœ ์ ๋ ค ์žˆ์œผ๋ฉด ํŠธ๋ฆฌ์˜ ๋†’์ด๊ฐ€ n์ด ๋จ (ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๋†’์ด๊ฐ€ ์Šคํƒ์˜ ์ตœ๋Œ€ ๊นŠ์ด)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
from collections import defaultdict
8+
9+
10+
class Solution:
11+
# Time complexity: O(n)
12+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
13+
self.count = 0
14+
self.result = 0
15+
16+
def dfs(node):
17+
if not node:
18+
return
19+
20+
dfs(node.left)
21+
22+
self.count += 1
23+
if self.count == k:
24+
self.result = node.val
25+
return
26+
27+
dfs(node.right)
28+
29+
dfs(root)
30+
return self.result
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// time : O(1)
2+
// space : O(1)
3+
4+
class Solution {
5+
public int hammingWeight(int n) {
6+
int count = 0;
7+
8+
while(n != 0) {
9+
if((n&1) == 1) count++;
10+
n = n >> 1;
11+
}
12+
13+
return count;
14+
}
15+
}
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
# Time complexity: O(log n)
3+
def hammingWeight(self, n: int) -> int:
4+
cnt = 0
5+
while n > 0:
6+
if n % 2 == 1:
7+
cnt += 1
8+
n = n // 2
9+
return cnt
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# Time complexity: O(n^2) = O(n) * O(n)
3+
def countSubstrings(self, s: str) -> int:
4+
self.count = 0
5+
n = len(s)
6+
7+
def two_pointer_expand(left, right):
8+
while left >= 0 and right < n and s[left] == s[right]:
9+
self.count += 1
10+
left -= 1
11+
right += 1
12+
13+
for i in range(0, n):
14+
# 1, 3, 5 ...
15+
two_pointer_expand(i, i)
16+
# 2, 4, 6 ...
17+
two_pointer_expand(i, i + 1)
18+
19+
return self.count
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
class Solution {
3+
public int[] topKFrequent(int[] nums, int k) {
4+
Map<Integer, Integer> map = new HashMap<>();
5+
for(int num : nums) {
6+
map.put(num, map.getOrDefault(num, 0) + 1);
7+
}
8+
9+
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(
10+
(a, b) -> Integer.compare(b.getValue(), a.getValue())
11+
);
12+
13+
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
14+
queue.offer(entry);
15+
}
16+
17+
int[] res = new int[k];
18+
19+
for(int i = 0; i < k; i++) {
20+
res[i] = queue.poll().getKey();
21+
}
22+
23+
return res;
24+
}
25+
}
26+
27+
// n : nums์˜ ๊ธธ์ด
28+
// m : nums์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜
29+
30+
// time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm)
31+
// ์ตœ์•…์˜ ๊ฒฝ์šฐ, nums ๊ฐ€ ๋‹ค unique ํ•˜๊ธฐ ๋•Œ๋ฌธ์— n == m == k ๊ฐ€ ๋จ
32+
// ๋”ฐ๋ผ์„œ, O(n*logn)
33+
34+
// space : O(m) + O(m) + O(k) = O(m + k)
35+
// ์ตœ์•…์˜ ๊ฒฝ์šฐ n == m == k ๊ฐ€ ๋จ
36+
// ๋”ฐ๋ผ์„œ, O(n)
37+
38+
39+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
# Time complexity: O(nlogn) -> O(n) + O(nlogn) + O(k)
7+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
8+
counter_dict = defaultdict(int)
9+
10+
for n in nums:
11+
counter_dict[n] += 1
12+
13+
count_list = []
14+
for key, val in counter_dict.items():
15+
count_list.append((key, val))
16+
17+
count_list.sort(key=lambda x: x[1], reverse=True)
18+
answer = [a for a, b in count_list[:k]]
19+
20+
return answer

0 commit comments

Comments
ย (0)