Skip to content

Commit a057cfd

Browse files
committed
2 parents a36c7cc + 52b9dfd commit a057cfd

66 files changed

Lines changed: 1068 additions & 608 deletions

Some content is hidden

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

3sum/jiji-hoon96.ts

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +0,0 @@
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: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +0,0 @@
1-
function maxProfit(prices: number[]): number {
2-
if (prices.length <= 1) return 0;
3-
4-
let minPrice = prices[0];
5-
let maxProfit = 0;
6-
7-
for (let i = 1; i < prices.length; i++) {
8-
// 현재 가격이 최소가보다 낮으면 최소가 업데이트
9-
if (prices[i] < minPrice) {
10-
minPrice = prices[i];
11-
}
12-
// 현재 가격으로 팔았을 때의 이익 계산
13-
else {
14-
const currentProfit = prices[i] - minPrice;
15-
// 최대 이익 업데이트
16-
if (currentProfit > maxProfit) {
17-
maxProfit = currentProfit;
18-
}
19-
}
20-
}
21-
22-
return maxProfit;
23-
}

climbing-stairs/jiji-hoon96.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +0,0 @@
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-
};

combination-sum/jiji-hoon96.ts

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +0,0 @@
1-
/**
2-
*
3-
* @param candidates
4-
* @param target
5-
*
6-
* backtracking 알고리즘으로 문제 해결
7-
*
8-
*/
9-
10-
function combinationSum(candidates: number[], target: number): number[][] {
11-
const result : number[][] = [];
12-
if(candidates.length === 0){
13-
return result ;
14-
}
15-
16-
candidates.sort((a,b)=> a-b);
17-
18-
const validCandidates : number[] = candidates.filter(num => num <= target);
19-
20-
if(validCandidates.length ===0) {
21-
return result;
22-
}
23-
24-
const currentCombination : number[] = [];
25-
26-
function backtrack (startIndex : number, remainingTarget : number) :void {
27-
if(remainingTarget === 0){
28-
result.push([...currentCombination]);
29-
return;
30-
}
31-
32-
for(let i=startIndex; i<validCandidates.length; i++){
33-
const currentNum = validCandidates[i];
34-
35-
if(currentNum > remainingTarget) {
36-
break;
37-
}
38-
currentCombination.push(currentNum);
39-
40-
backtrack(i,remainingTarget - currentNum)
41-
42-
currentCombination.pop()
43-
44-
}
45-
}
46-
47-
backtrack(0, target);
48-
return result;
49-
};

contains-duplicate/Cyjin-jani.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const containsDuplicate = function (nums) {
2+
const data = new Set(nums);
3+
return data.size !== nums.length;
4+
};

contains-duplicate/SamTheKorean.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TC : O(n) n being a length of nums array and it iterates the nums array once
2+
# SC : O(n) n being a size of nums array
3+
class Solution:
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
seen = set()
6+
for num in nums:
7+
if num in seen:
8+
return True
9+
seen.add(num)
10+
return False

contains-duplicate/YOOHYOJEONG.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://leetcode.com/problems/contains-duplicate/
2+
3+
class Solution(object):
4+
def containsDuplicate(self, nums):
5+
if len(nums) > len(set(nums)):
6+
return True
7+
else:
8+
return False

contains-duplicate/devdays9.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
fun containsDuplicate(nums: IntArray): Boolean {
3+
val occurrences = HashSet<Int>(nums.size)
4+
return nums.any { !occurrences.add(it) }
5+
}
6+
}

contains-duplicate/gcount85.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Approach : De-duplicate using Set
2+
# Complexity: O(n)
3+
# Space complexity: O(n)
4+
5+
6+
class Solution:
7+
def containsDuplicate(self, nums: List[int]) -> bool:
8+
dedup = set(nums)
9+
return len(dedup) != len(nums)

contains-duplicate/hyeri0903.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
ans = False
4+
dic = dict()
5+
6+
for n in nums:
7+
if n in dic:
8+
return True
9+
else:
10+
dic[n] = 1
11+
return ans
12+
13+

0 commit comments

Comments
 (0)