Skip to content

Commit 08a8463

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 0ea55a8 + 1d40150 commit 08a8463

File tree

18 files changed

+360
-0
lines changed

18 files changed

+360
-0
lines changed
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
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+
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
set๋ฅผ ํ†ตํ•ด ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ํ•œ ํ›„ ๊ธฐ์กด nums์™€ ๊ธธ์ด ๋น„๊ต
3+
4+
nums์˜ ๊ธธ์ด N
5+
6+
TC : O(N)
7+
set๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์ „์ฒด ์ˆœํšŒํ•˜๋ฉฐ N ์‹œ๊ฐ„ ์†Œ๋ชจ
8+
9+
SC : O(N)
10+
set ๋งŒ๋“ค ๋•Œ N์˜ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
11+
*/
12+
#include <vector>
13+
#include <unordered_set>
14+
using namespace std;
15+
16+
class Solution {
17+
public:
18+
bool containsDuplicate(vector<int>& nums) {
19+
unordered_set<int> us(nums.begin(), nums.end());
20+
if (nums.size() == us.size())
21+
return false;
22+
else
23+
return true;
24+
}
25+
};
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));

โ€Žhouse-robber/sungjinwi.cppโ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
ํ’€์ด :
3+
ํ˜„์žฌ ์ง‘์˜ ์ˆœ์„œ๋ฅผ n์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ „ cur์—๋Š” n - 1 ์ง‘๊นŒ์ง€ ์ตœ๋Œ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ˆ, prv์—๋Š” n - 2์ง‘๊นŒ์ง€ ์ตœ๋Œ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ˆ์ด ์ €์žฅ
4+
cur๋ฅผ max(n - 2๊นŒ์ง€ ๋ˆ + n์˜ ๋ˆ, n - 1๊นŒ์ง€์˜ ๋ˆ)๋กœ n๊นŒ์ง€ ์˜ค๋ฉด์„œ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ˆ์œผ๋กœ ์—…๋ฐ์ดํŠธ,
5+
prv๋Š” n - 1๊นŒ์ง€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ˆ์œผ๋กœ ์—…๋ฐ์ดํŠธ
6+
7+
nums์˜ ๊ฐฏ์ˆ˜ : N
8+
9+
TC : O(N)
10+
11+
SC : O(1)
12+
๋ฐฐ์—ด์—†์ด ์ƒ์ˆ˜ ๋ณ€์ˆ˜ 3๊ฐœ๋งŒ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉ
13+
*/
14+
15+
#include <vector>
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
int rob(vector<int>& nums) {
21+
int prv = 0, cur = 0;
22+
int tmp;
23+
for (auto& num : nums)
24+
{
25+
tmp = cur;
26+
cur = max(prv + num, cur);
27+
prv = tmp;
28+
}
29+
return cur;
30+
}
31+
};
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
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
ํ’€์ด :
3+
ํ•ด์‹œํ…Œ์ด๋ธ”์— nums๋ฅผ ๋‹ด์€ ๋’ค num - 1์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” num์— ๋Œ€ํ•ด์„œ๋งŒ (์ค‘๊ฐ„์ ์—์„œ๋Š” ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด)
4+
๊ธธ์ด๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ๋‚˜๊ฐ€๋ฉฐ ์—ฐ์†๋œ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  cur์˜ max๊ฐ’ ans๋ฅผ ๊ตฌํ•œ๋‹ค
5+
6+
nums์˜ ๊ฐฏ์ˆ˜ N
7+
TC : O(N)
8+
์ด์ค‘ for๋ฌธ์ด์ง€๋งŒ ๋‚ด๋ถ€ for๋ฌธ์€ num - 1์ด ์—†์„๋•Œ๋งŒ ์—ฐ์†๋œ ํ•ด์‹œํ…Œ์ด๋ธ” ๋‚ด๋ถ€ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(N)
9+
10+
SC : O(N)
11+
ํ•ด์‹œํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ๋Š” N์— ๋น„๋ก€
12+
*/
13+
14+
#include <vector>
15+
#include <unordered_set>
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
int longestConsecutive(vector<int>& nums) {
21+
int cur;
22+
int ans = 0;
23+
unordered_set<int> us(nums.begin(), nums.end());
24+
25+
for (auto& num : us)
26+
{
27+
if (us.find(num - 1) == us.end())
28+
{
29+
cur = 1;
30+
for(int i = 1; us.find(num + i) != us.end(); i++)
31+
cur++;
32+
ans = max(ans, cur);
33+
}
34+
}
35+
return ans;
36+
}
37+
};

0 commit comments

Comments
ย (0)