Skip to content

Commit a1e3845

Browse files
Merge branch 'DaleStudy:main' into main
2 parents f01effa + fd9ab2b commit a1e3845

File tree

281 files changed

+7621
-495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+7621
-495
lines changed

โ€Ž3sum/jiji-hoon96.tsโ€Ž

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*
5+
* ํ’€์ด 1
6+
*
7+
* ์ด๋ ‡๊ฒŒ ํ‘ธ๋‹ˆ๊นŒ ์‹œ๊ฐ„๋ณต์žก๋„ O(n^3) / ๊ณต๊ฐ„๋ณต์žก๋„ O(n) ๋ผ์„œ ๋ณต์žกํ•œ ์˜ˆ์‹œ์—๋Š” time limit ์ด ๋ฐœ์ƒํ•จ
8+
* ๊ฐœ์„ ํ•ด๋ณด์ž..
9+
*
10+
* function threeSum(nums: number[]): number[][] {
11+
* nums.sort((a, b) => a - b);
12+
* let result = []
13+
* for (let i= 0; i<nums.length; i++){
14+
* for(let j= i+1 ; j <nums.length; j++){
15+
* for (let k = j+1; k<nums.length; k++){
16+
* if(nums[i]+nums[j]+nums[k]===0){
17+
* result.push([nums[i], nums[j], nums[k]]);
18+
* }
19+
* }
20+
* }
21+
* }
22+
*
23+
* return Array.from(
24+
* new Set(result.map(item => JSON.stringify(item))),
25+
* str => JSON.parse(str)
26+
* );
27+
* }
28+
*
29+
* ํ’€์ด 2
30+
*
31+
* ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด๋ณด์ž.
32+
* ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ๋˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2) / ๊ณต๊ฐ„๋ณต์žก๋„ O(1) ์ด๋‹ค.
33+
* ์‹œ๊ณต๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ค„๊ธดํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ํฐ ์ˆซ์ž๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์„ฑ๋Šฅ์ด ๋งค์šฐ ์ข‹๋‹ค!
34+
*/
35+
36+
37+
function threeSum(nums: number[]): number[][] {
38+
let result : number[][] = []
39+
nums.sort((a, b) => a - b);
40+
const n = nums.length;
41+
42+
for(let first = 0; first<n-2; first++){
43+
// ์ฒซ๋ฒˆ์งธ๊ฐ€ ์–‘์ˆ˜๋ฉด 0์ด ๋  ์ˆ˜ ์—†์Œ
44+
if(nums[first] > 0) break;
45+
46+
//์ค‘๋ณต๋œ ์ˆ˜๋Š” ๊ฑด๋„ˆ๋œ€
47+
if(first > 0 && nums[first]===nums[first-1]) continue;
48+
49+
let left = first + 1;
50+
let right = n-1;
51+
52+
while(left < right){
53+
const sum = nums[first] +nums[left] + nums[right];
54+
55+
if(sum < 0){
56+
left ++
57+
}else if(sum > 0){
58+
right --;
59+
}else{
60+
result.push([nums[first],nums[left],nums[right]]);
61+
// left, left+1 ์ด ๊ฐ™์„ ๋•Œ ์ค‘๋ณต๋œ ์ˆ˜๋Š” ๊ฑด๋„ˆ๋œ€
62+
while(left < right && nums[left] === nums[left+1]) left++;
63+
// right, right+1 ์ด ๊ฐ™์„ ๋•Œ ์ค‘๋ณต๋œ ์ˆ˜๋Š” ๊ฑด๋„ˆ๋œ€
64+
while(left < right && nums[right] === nums[right-1]) right--;
65+
left++;
66+
right--;
67+
}
68+
}
69+
}
70+
return result;
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
for (let i = 0; i < prices.length; i++) {
5+
if (prices[i] < minPrice) {
6+
minPrice = prices[i];
7+
}
8+
9+
if (prices[i] - minPrice > maxProfit) {
10+
maxProfit = prices[i] - minPrice;
11+
}
12+
}
13+
return maxProfit;
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param n
3+
*
4+
* dp, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์‚ฌ์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
5+
*
6+
* ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€๊ฐ™์ด ์Šฌ๋ผ์ด๋”ฉ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
7+
*
8+
* function climbStairs(n: number): number {
9+
* if (n <= 2) return n;
10+
*
11+
* let first = 1; // 1๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ• ์ˆ˜
12+
* let second = 2; // 2๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ• ์ˆ˜
13+
*
14+
* for (let i = 3; i <= n; i++) {
15+
* let current = first + second;
16+
* first = second;
17+
* second = current;
18+
* }
19+
*
20+
* return second;
21+
* }
22+
*/
23+
24+
25+
function climbStairs(n: number): number {
26+
if(n <= 2) return n;
27+
28+
let dp: number[] = new Array(n+1);
29+
dp[1] = 1;
30+
dp[2] = 2;
31+
32+
for(let i=3;i<=n;i++){
33+
dp[i] = dp[i-1] +dp[i-2];
34+
}
35+
36+
return dp[n]
37+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* [Problem]: [217] Contains Duplicate
3+
* (https://leetcode.com/problems/contains-duplicate/description/)
4+
*/
5+
6+
function containsDuplicate(nums: number[]): boolean {
7+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
8+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
9+
const doubleLoopFunc = (nums: number[]) => {
10+
let isDuplicated = false;
11+
for (let i = 0; i < nums.length; i++) {
12+
for (let j = i + 1; j < nums.length; j++) {
13+
if (nums[i] === nums[j]) isDuplicated = true;
14+
}
15+
}
16+
return isDuplicated;
17+
};
18+
19+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
20+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
21+
const setFunc = (nums: number[]) => {
22+
const numsSet = new Set<number>(nums);
23+
24+
return nums.length !== numsSet.size;
25+
};
26+
27+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
28+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
29+
const mapFunc = (nums: number[]) => {
30+
const numsMap = new Map<number, boolean>();
31+
32+
for (const num of nums) {
33+
if (numsMap.get(num)) return true;
34+
numsMap.set(num, true);
35+
}
36+
37+
return false;
38+
};
39+
40+
return mapFunc(nums);
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
์‹œ๊ฐ„๋ณต์žก๋„: O(n) - new Set(nums)์—์„œ ๋ฐฐ์—ด ์š”์†Œ ์ˆœํšŒํ•˜๋ฉฐ Set ์ƒ์„ฑ O(n) + ๊ธธ์ด ๋น„๊ต O(1)
3+
- Set ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ค‘๋ณต๋œ ๊ฐ’์„ ์ž๋™์œผ๋กœ ์ œ๊ฑฐ
4+
*/
5+
6+
/**
7+
* @param {number[]} nums
8+
* @return {boolean}
9+
*/
10+
var containsDuplicate = function (nums) {
11+
// Set์œผ๋กœ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ๊ธฐ์กด ๋ฐฐ์—ด๊ณผ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋‹ค๋ฅด๋ฉด ์ค‘๋ณต์ด ์ œ๊ฑฐ๋œ๊ฑฐ์ž„
12+
const numsSet = new Set(nums);
13+
return nums.length !== numsSet.size;
14+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Approach 3: HashSet (Using TypeScript Set)
2+
// โณ Time Complexity: O(n)
3+
// ๐Ÿ’พ Space Complexity: O(n)
4+
5+
function containsDuplicate(nums: number[]): boolean {
6+
7+
const seen = new Set<number>();
8+
9+
for (const num of nums) {
10+
if (seen.has(num)) {
11+
return true;
12+
}
13+
seen.add(num);
14+
}
15+
16+
return false;
17+
18+
};
19+
20+
21+
// Approach 2: Sorting + Scan
22+
// โณ Time Complexity: O(n * log(n)) โŒ (Faster than O(n^2), but still not optimal)
23+
// ๐Ÿ’พ Space Complexity: O(1)
24+
25+
// function containsDuplicate(nums: number[]): boolean {
26+
27+
// nums.sort();
28+
29+
// for (let i = 0; i < nums.length - 1; i++) {
30+
// if (nums[i] === nums[i + 1]) {
31+
// return true;
32+
// }
33+
// }
34+
35+
// return false;
36+
37+
// };
38+
39+
40+
// Approach 1: Brute Force (O(n^2))
41+
// ๐Ÿšจโณ TLE (Time Limit Exceeded)!
42+
// โณ Time Complexity: O(n^2) โŒ (Inefficient for large inputs)
43+
// ๐Ÿ’พ Space Complexity: O(1) โœ… (Great, no extra memory used!)
44+
45+
// function containsDuplicate(nums: number[]): boolean {
46+
47+
// for (let i = 0; i < nums.length; i++) {
48+
// for (let j = i + 1; j < nums.length; j++) {
49+
// if (nums[i] === nums[j]) {
50+
// return true;
51+
// }
52+
// }
53+
// }
54+
55+
// return false;
56+
57+
// };
58+
59+
60+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ์ค‘๋ณต์ œ๊ฑฐ ํ›„ ๊ธธ์ด ํ™•์ธ ๋ฌธ์ œ
2+
# ์‹œ๊ฐ„๋ณต์žก๋„ ๋ฐ ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
3+
class Solution:
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
return len(nums) != len(set(nums))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
count = {}
4+
5+
for i in nums:
6+
if i in count:
7+
return True
8+
else:
9+
count[i] = 1
10+
11+
return False

โ€Žcontains-duplicate/KwonNayeon.pyโ€Ž

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
"""
2-
Title: 217. Contains Duplicate
3-
Link: https://leetcode.com/problems/contains-duplicate/
2+
Problem: 217. Contains Duplicate
43
5-
Summary:
6-
- ์ฃผ์–ด์ง„ ๋ฐฐ์—ด `nums`์—์„œ ์–ด๋–ค ๊ฐ’์ด ํ•œ ๋ฒˆ ์ด์ƒ ๋“ฑ์žฅํ•˜๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ’์ด ์œ ์ผํ•œ ๊ฒฝ์šฐ์—๋Š” False๋ฅผ ๋ฐ˜ํ™˜ํ•จ
7-
- Input: `nums = [1,2,3,1]`
8-
- Output: `True`
4+
Constraints:
5+
- 1 <= nums.length <= 10^5
6+
- -10^9 <= nums[i] <= 10^9
97
10-
Conditions:
11-
- ์ค‘๋ณต์ด ์žˆ์œผ๋ฉด: ๋ฐฐ์—ด์—์„œ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ๊ฐ’์ด ๋‘ ๋ฒˆ ์ด์ƒ ๋“ฑ์žฅํ•˜๋ฉด `True` ๋ฐ˜ํ™˜
12-
- ์ค‘๋ณต์ด ์—†์œผ๋ฉด: ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ’์ด ์œ ์ผํ•˜๋ฉด `False` ๋ฐ˜ํ™˜
13-
"""
8+
Time Complexity: O(n)
9+
- ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•จ
10+
- ์ง‘ํ•ฉ์—์„œ ๊ฒ€์ƒ‰๊ณผ ์ถ”๊ฐ€ ์—ฐ์‚ฐ์€ ํ‰๊ท ์ ์œผ๋กœ O(1)
11+
- ์ด n๊ฐœ ์š”์†Œ์— ๋Œ€ํ•ด ๊ฐ๊ฐ O(1) ์—ฐ์‚ฐ ์ˆ˜ํ–‰
1412
15-
"""
16-
First Try
17-
Time Complexity:
18-
- O(n) * O(n) = O(n^2): `for` ๋ฃจํ”„์—์„œ `nums.count(i)`๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฏ€๋กœ, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(n^2)`
19-
"""
20-
class Solution:
21-
def containsDuplicate(self, nums: List[int]) -> bool:
22-
for i in nums:
23-
if nums.count(i) > 1:
24-
return True
25-
return False
26-
27-
"""
28-
Second Try (set๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด๋ฏธ ๋ณธ ์š”์†Œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ถ”์ ํ•˜๋Š” ๋ฐฉ๋ฒ•)
29-
Time Complexity:
30-
- O(n): `for` ๋ฃจํ”„์—์„œ ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด `in` ์—ฐ์‚ฐ๊ณผ `add` ์—ฐ์‚ฐ์ด ์ƒ์ˆ˜ ์‹œ๊ฐ„ O(1)์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
13+
Space Complexity: O(n)
14+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ง‘ํ•ฉ์— ์ €์žฅ
15+
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์ด ์ž…๋ ฅ ๋ฐฐ์—ด ํฌ๊ธฐ์— ๋น„๋ก€ํ•จ
3116
"""
3217
class Solution:
3318
def containsDuplicate(self, nums: List[int]) -> bool:
3419
seen = set()
20+
3521
for i in nums:
3622
if i in seen:
3723
return True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool containsDuplicate(vector<int>& nums) {
4+
unordered_map<int, int> map;
5+
6+
for(int i = 0; i < nums.size(); i++){
7+
map[nums[i]]++;
8+
9+
if(map[nums[i]] == 2)
10+
return true;
11+
}
12+
13+
return false;
14+
}
15+
};

0 commit comments

Comments
ย (0)