Skip to content

Commit 51ad997

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 47bff47 + b00545f commit 51ad997

158 files changed

Lines changed: 3207 additions & 660 deletions

File tree

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-
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
"""
4+
지금까지 본 가격 중 가장 작은 값을 계속 저장하고
5+
현재 가격에서 그 최소값을 뺀 값으로 최대 이익을 갱신합니다.
6+
한 번 순회하면서 최대 profit을 찾는 방식입니다
7+
"""
8+
min_price = float('inf')
9+
max_profit = 0
10+
11+
for price in prices:
12+
min_price = min(min_price, price)
13+
max_profit = max(max_profit, price - min_price)
14+
15+
return max_profit
16+

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/01-binary.js

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

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/OstenHun.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Problem:
3+
Given an integer array nums,
4+
return true if any value appears at least twice in the array,
5+
and return false if every element is distinct.
6+
7+
Input: nums = [1,2,3,1]
8+
Output: true
9+
10+
Constraints:
11+
1 <= nums.length <= 10^5
12+
-10^9 <= nums[i] <= 10^9
13+
*/
14+
15+
// 처음 생각한 코드
16+
/*
17+
#include <iostream>
18+
#include <vector>
19+
using namespace std;
20+
21+
class Solution {
22+
public:
23+
bool containsDuplicate(vector<int>& nums) {
24+
for (int i = 0; i < nums.size(); i++) {
25+
for (int j = i+1; j < nums.size(); j++) {
26+
if (nums[i]==nums[j]) {
27+
return true;
28+
}
29+
}
30+
}
31+
32+
return false;
33+
}
34+
};
35+
36+
int main() {
37+
vector<int> arr;
38+
int sz;
39+
cin >> sz;
40+
for (int i = 0; i < sz; i++) {
41+
int nm;
42+
cin >> nm;
43+
arr.push_back(nm);
44+
}
45+
46+
47+
Solution s;\
48+
if (s.containsDuplicate(arr))
49+
cout << "true";
50+
else
51+
cout << "false";
52+
53+
return 0;
54+
}
55+
*/
56+
57+
// O(n^2) 시간 복잡도를 개선해보자
58+
/*
59+
set 을 이용해서 중복이 있다면 배열의 길이가 줄어들테니
60+
이를 통해 중복을 판단할 수 있다고 생각했다.
61+
*/
62+
/*
63+
#include <iostream>
64+
#include <vector>
65+
#include <set>
66+
#include <algorithm>
67+
using namespace std;
68+
69+
class Solution {
70+
public:
71+
bool containsDuplicate(vector<int>& nums) {
72+
// set과 유사하게 만들기 위해서 사용
73+
sort(nums.begin(), nums.end());
74+
for (size_t i = 1; i < nums.size(); i++) {
75+
if (nums[i] == nums[i-1]) return true;
76+
}
77+
return false;
78+
}
79+
};
80+
81+
82+
int main() {
83+
size_t sz;
84+
cin >> sz;
85+
86+
vector<int> arr;
87+
for (int i = 0; i < sz; i++) {
88+
int num;
89+
cin >> num;
90+
arr.push_back(num);
91+
}
92+
93+
Solution sol;
94+
if (sol.containsDuplicate(arr)) {
95+
cout << "true";
96+
} else {
97+
cout << "false";
98+
}
99+
}
100+
*/
101+
102+
// 이보다 더 좋은 방법이 있다고 gpt의 리뷰가 있었다.
103+
/*
104+
unordered_set<int> 를 이용한다.
105+
해시 테이블 자료구조를 이용하여 구현 되어 있는 것.
106+
정렬은 못하지만 탐색하는데에 O(1) 이라는 장점이 있어 이를 활용한다.
107+
-> 빠른 탐색 굿
108+
*/
109+
110+
111+
#include <iostream>
112+
#include <unordered_set>
113+
#include <vector>
114+
using namespace std;
115+
116+
class Solution {
117+
public:
118+
bool containsDuplicate(vector<int>& nums) {
119+
unordered_set<int> copy_set;
120+
for (int x : nums) {
121+
if (copy_set.count(x)) return true;
122+
copy_set.insert(x);
123+
}
124+
return false;
125+
}
126+
};
127+
128+
int main() {
129+
int n;
130+
cin >> n;
131+
132+
vector<int> nums;
133+
nums.reserve(n);
134+
135+
for (int i = 0; i < n; i++) {
136+
int x;
137+
cin >> x;
138+
nums.push_back(x);
139+
}
140+
141+
Solution sol;
142+
cout << (sol.containsDuplicate(nums) ? "true" : "false");
143+
144+
return 0;
145+
}

contains-duplicate/TreeStone94.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
if len(set(nums)) == len(nums):
4+
return False
5+
else:
6+
return True

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

0 commit comments

Comments
 (0)