Skip to content

Commit cde832d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents f86c6e9 + 114d7cb commit cde832d

99 files changed

Lines changed: 2309 additions & 73 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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/ppxyn1.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
# idea: DP
2-
# I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP.
3-
# If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
2+
# Time Complexity : O(n)
3+
'''
4+
n = 4
5+
-------------------------------
6+
(dp[3] + 1step)
7+
1step + 1step + 1step + 1step
8+
1step + 2step + 1step
9+
2step + 1step + 1step
10+
11+
(dp[2] + 2step)
12+
1step + 1step + 2step
13+
2step + 2step
14+
15+
================================
16+
n = 5
17+
--------------------------------
18+
(dp[4] + 1step)
19+
1step + 1step + 1step + 1step + 1step
20+
1step + 2step + 1step + 1step
21+
1step + 1step + 2step + 1step
22+
2step + 1step + 1step + 1step
23+
2step + 2step + 1step
24+
25+
(dp[3] + 2step)
26+
1step + 2step + 2step
27+
2step + 1step + 2step
28+
1step + 1step + 1step + 2step
29+
'''
30+
431

532
class Solution:
633
def climbStairs(self, n: int) -> int:
@@ -9,7 +36,6 @@ def climbStairs(self, n: int) -> int:
936
dp = [0] * (n+1)
1037
dp[2], dp[3] = 2, 3
1138

12-
#for i in range(4, n): error when n=4
1339
for i in range(4, n+1):
1440
dp[i] = dp[i-1] + dp[i-2]
1541

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/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/Yu-Won.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/contains-duplicate/
3+
*
4+
* 요구사항:
5+
* nums: number[]를 Input 으로 받았을 때
6+
* 중복된 값이 있을 경우 true 없다면 false 를 반환
7+
*
8+
* 해시맵 이용
9+
* */
10+
11+
const containsDuplicate = (nums) => {
12+
const set = new Set();
13+
14+
for(const num of nums) {
15+
if(set.has(num)) {
16+
return true;
17+
}
18+
set.add(num);
19+
}
20+
return false;
21+
22+
}

contains-duplicate/acious.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
// 1 <= nums.length <= 10^5
3+
// Integer.MIN_VALUE(2,147,483,647) < -10^9 <= nums[i] <= 10^9 < Integer.MAX_VALUE(2,147,483,647)
4+
// HashSet에 아이템을 하나씩 넣으면서 이미 존재하는 아이템이 있으면 true, 없으면 false
5+
// 시간복잡도 : O(n), 공간복잡도 : O(n)
6+
fun containsDuplicate(nums: IntArray): Boolean {
7+
val set = HashSet<Int>()
8+
for (num in nums) {
9+
if (set.contains(num)) {
10+
return true
11+
}
12+
set.add(num)
13+
}
14+
return false
15+
}
16+
}

contains-duplicate/doh6077.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
# https://leetcode.com/problems/contains-duplicate/description/
12

2-
# set에 저장하면서 중복 여부 확인하기
33
class Solution:
4-
def containsDuplicate(self, nums: list[int]) -> bool:
5-
hashset = set()
6-
for i in nums:
7-
if i in hashset:
8-
return True
9-
hashset.add(i)
10-
return False
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
# Create a set to store unique numbers from nums
6+
nums_set = set(nums)
7+
return len(nums_set) != len(nums)

contains-duplicate/dongzoolee.ts

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

contains-duplicate/grapefruit13.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @description nums 배열에서 중복 숫자 확인
3+
* @param nums - 숫자 배열
4+
* @returns boolean - 중복 숫자 여부
5+
*/
6+
const containsDuplicate = (nums: number[]) => {
7+
const hasSeen = new Set<number>();
8+
9+
for (const num of nums) {
10+
if (hasSeen.has(num)) {
11+
return true;
12+
}
13+
hasSeen.add(num);
14+
}
15+
return false;
16+
};

0 commit comments

Comments
 (0)