Skip to content

Commit 8f38943

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 222658e + 8e0008e commit 8f38943

File tree

14 files changed

+216
-0
lines changed

14 files changed

+216
-0
lines changed

contains-duplicate/JisuuungKim.py

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/ayleeee.py

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

contains-duplicate/froggy1014.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Set을 사용한 중복값 제거 후 길이 비교
2+
function containsDuplicate(nums) {
3+
const numSet = new Set(nums);
4+
return numSet.size !== nums.length;
5+
}
6+
7+
console.log(containsDuplicate([1, 2, 3, 1])); // true
8+
console.log(containsDuplicate([1, 2, 3, 4])); // false
9+
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
HashSet<Integer> numSet = new HashSet<>();
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
9+
if (numSet.contains(nums[i])) {
10+
return true;
11+
}
12+
numSet.add(nums[i]);
13+
}
14+
15+
return false;
16+
}
17+
}

contains-duplicate/toychip.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public boolean containsDuplicate(int[] nums) {
6+
Set<Integer> answer = new HashSet<>();
7+
for (int num : nums) {
8+
if (!answer.contains(num)) {
9+
answer.add(num);
10+
} else {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}

house-robber/froggy1014.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function (nums) {
6+
if (nums.length === 0) return 0;
7+
if (nums.length === 1) return nums[0];
8+
9+
const dp = new Array(nums.length + 1);
10+
dp[0] = 0;
11+
dp[1] = nums[0];
12+
for (let i = 2; i < dp.length; i++) {
13+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
14+
}
15+
return dp[dp.length - 1];
16+
};
17+
18+
const nums = [2, 7, 9, 3, 1];
19+
20+
console.log(rob(nums));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
6+
// 시간복잡도: O(n)
7+
var longestConsecutive = function (nums) {
8+
let longest = 0;
9+
10+
let set = new Set(nums);
11+
12+
for (let num of nums) {
13+
if (set.has(num - 1)) {
14+
continue;
15+
}
16+
17+
let count = 1;
18+
let currentNum = num;
19+
20+
while (set.has(currentNum + 1)) {
21+
count++;
22+
currentNum++;
23+
}
24+
longest = Math.max(longest, count);
25+
}
26+
return longest;
27+
};
28+
29+
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); // 4
30+
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); // 9
31+
console.log(longestConsecutive([1, 0, 1, 2])); // 3

top-k-frequent-elements/ayleeee.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
dict_map = {}
4+
for a in nums:
5+
if a in dict_map:
6+
dict_map[a] += 1
7+
else:
8+
dict_map[a] = 1
9+
lst = sorted(dict_map.items(), key=lambda x: x[1], reverse=True) # 공백 수정
10+
return [item[0] for item in lst[0:k]]
11+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function (nums, k) {
7+
const map = new Map();
8+
for (let n = 0; n < nums.length; n++) {
9+
map.has(nums[n])
10+
? map.set(nums[n], map.get(nums[n]) + 1)
11+
: map.set(nums[n], 1);
12+
}
13+
14+
return Array.from(map.entries())
15+
.sort(([key1, value1], [key2, value2]) => value2 - value1)
16+
.slice(0, k)
17+
.map((v) => v[0]);
18+
};
19+
20+
console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2));

two-sum/JisuuungKim.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
d = {}
4+
for i in range(len(nums)):
5+
cur = nums[i]
6+
x = target - cur
7+
if x in d:
8+
return [i, d[x]]
9+
else:
10+
d[cur] = i
11+
12+
# 시간복잡도 O(n)

0 commit comments

Comments
 (0)