-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
63 changed files
with
1,743 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function (nums) { | ||
const seen = new Set(); | ||
for (let num of nums) { | ||
if (seen.has(num)) { | ||
return true; // ์ค๋ณต ๋ฐ๊ฒฌ | ||
} | ||
seen.add(num); | ||
} | ||
return false; // ๋ชจ๋ ์์๊ฐ ๊ณ ์ | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
dict = {} | ||
for num in nums: | ||
dict[num] = dict.get(num, 0) + 1 | ||
if dict[num] > 1: | ||
return True | ||
return False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
๏ปฟclass Solution(object): | ||
def containsDuplicate(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: bool | ||
""" | ||
#ํด์ | ||
#sets๋ ๋ณต์ ์์๋ฅผ ํ์ฉํ์ง ์๋๋ค(sets don't allow duplicate elements.) | ||
#๋ง์ฝ set์ ๊ธฐ๋ฐ๋ list๊ฐ ๊ธฐ์กด nums์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ค๋ฉด duplicate element๊ฐ ์์๋ค๋ ๋ป์ด๋ค. | ||
#If the length of the set created from nums is different from the original list(nums), It means there are duplicates. | ||
|
||
#Big O | ||
#N: ์ฃผ์ด์ง ๋ฐฐ์ด nums์ ๊ธธ์ด(Length of the input list nums) | ||
|
||
#Time Complexity: O(N) | ||
#- set์ nums์ ๊ธธ์ด n์ ๊ธฐ๋ฐํ์ฌ ์์ฑ๋๋ค(Creating a set from nums): O(N) | ||
#- ์์ฑ๋ list์ ๊ธฐ์กด nums์์ ๋น๊ต๋ ์์(Comparing the lengths between created list and original list) : O(1) | ||
|
||
#Space Complexity: O(N) | ||
#-set์ nums์ ๊ธธ์ด์ ์ํด ์์ฑ๋๋ฏ๋ก n์ ์ํฅ๋ฐ์(The set requires extra space depends on the size of nums) : O(N) | ||
return len(list(set(nums))) != len(nums) #set๋ list์ ๊ธฐ์กด nums์ len์ด ์ผ์นํ์ง ์๋๋ค๋ฉด true(duplicate), ์๋๋ฉด false | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package leetcode_study | ||
|
||
class SolutionContainsDuplicate { | ||
fun containsDuplicate(nums: IntArray): Boolean { | ||
val size = nums.size | ||
val numsToSet = nums.toSet() | ||
|
||
return size != numsToSet.size | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function containsDuplicate(nums: number[]): boolean { | ||
|
||
// ์ ๊ทผ (1) - ์๊ฐ ๋ณต์ก๋๊ฐ ๋งค์ฐ ์ปค์ ์คํจ | ||
// const uniqueArr = nums.filter((item, index) => { return nums.indexOf(item) === index }) | ||
// console.log(uniqueArr) | ||
// | ||
// return nums.length !== uniqueArr.length; | ||
|
||
// ์ ๊ทผ (2) - ์ ์์ ๊ฐ์ ๋น๊ต ============= | ||
// if(nums.length === 1) | ||
// return false; | ||
// | ||
// // ์ ๋ ฌ | ||
// nums.sort() | ||
// | ||
// // ์ ์์ ๊ฐ์ ๋น๊ต | ||
// for(let i = 0; i < nums.length; i++){ | ||
// console.log(nums[i], nums[i+1]) | ||
// if(nums[i] === nums[i+1]){ | ||
// return true; | ||
// } | ||
// } | ||
// return false; | ||
|
||
// ์ ๊ทผ (3) - obj๋ฅผ ์ด์ฉ ================ | ||
let obj={} | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if(obj[nums[i]]) { | ||
return true; | ||
} | ||
obj[nums[i]] = 1; | ||
} | ||
return false; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
|
||
void merge(int *nums, int left, int mid, int right) | ||
{ | ||
int i; | ||
int j; | ||
int k; | ||
int leftArr[mid - left + 1]; | ||
int rightArr[right - mid]; | ||
|
||
i = -1; | ||
while (++i < mid - left + 1) // ์ผ์ชฝ ๋ถํ ๋ ๋ถ๋ถ ๋ฃ๊ธฐ | ||
leftArr[i] = nums[left + i]; | ||
i = -1; | ||
while (++i < right - mid) // ์ค๋ฅธ์ชฝ ๋ถํ ๋ ๋ถ๋ถ ๋ฃ๊ธฐ | ||
rightArr[i] = nums[mid + i + 1]; | ||
i = 0; | ||
j = 0; | ||
k = left; // **** nums๋ฐฐ์ด์ธ๋ฑ์ค => left๋ถํฐ ์์ | ||
// ๋๋์ด์ง ๋ฐฐ์ด๋ผ๋ฆฌ ๋น๊ตํด์ nums๋ฐฐ์ด ์ฌ๋ฐฐ์น | ||
while ((i < mid - left + 1) && (j < right - mid)) { | ||
if (leftArr[i] <= rightArr[j]) | ||
nums[k] = leftArr[i++]; | ||
else | ||
nums[k] = rightArr[j++]; | ||
++k; | ||
} | ||
while (i < mid - left + 1) // left๋ฐฐ์ด ๋จ์์์ผ๋ฉด ๋ง์ ์ฝ์ | ||
nums[k++] = leftArr[i++]; | ||
while (j < right - mid) // right๋ฐฐ์ด ๋จ์์์ผ๋ฉด ๋ง์ ์ฝ์ | ||
nums[k++] = rightArr[j++]; | ||
} | ||
|
||
void mergeSort(int *nums, int left, int right) { | ||
int mid; | ||
|
||
if (left < right) | ||
{ | ||
mid = (left + right) / 2; | ||
mergeSort(nums, left, mid); // ์ผ์ชฝ๋ถํ | ||
mergeSort(nums, mid + 1, right); // ์ค๋ฅธ์ชฝ๋ถํ | ||
merge(nums, left, mid, right); | ||
} | ||
} | ||
|
||
bool containsDuplicate(int* nums, int numsSize) { | ||
int i; | ||
|
||
mergeSort(nums, 0, numsSize - 1); | ||
i = -1; | ||
while (++i + 1 < numsSize) { | ||
if (nums[i] == nums[i + 1]) | ||
return (true); | ||
} | ||
return (false); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
/* | ||
* -- ํ์ด -- | ||
* nums๋ฅผ ์ํํ๋ฉด์ HashSet์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ ์ค๋ณต๋์๋ ์ง ํ์ธํ๋ค. | ||
* | ||
* -- ์๊ฐ ๋ณต์ก๋ -- | ||
* ๊ธธ์ด N์ธ nums๋ฅผ ์ํํ๋๋ฐ ๋ํ ์๊ฐ ๋ณต์ก๋ => O(N) | ||
* hashSet์ add์ ๋ํ ์๊ฐ ๋ณต์ก๋ => O(1) | ||
* ์ ์ฒด ์๊ฐ ๋ณต์ก๋ O(1)*O(N) =O(n) | ||
* ------------------------------------------ | ||
* | ||
* -- ๊ณต๊ฐ ๋ณต์ก๋ -- | ||
* ๊ธธ์ด N์ธ nums๋ฅผ ๋ฃ์ Hashset์ด ์์ด์ผ ํ๊ธฐ์ O(N) | ||
* ------------------------------------------- | ||
*/ | ||
|
||
// ์ค๋ณต์ ํ์ธํ ์ ์๋ set ์ ์ธ | ||
HashSet<Integer> hashSet = new HashSet<>(); | ||
|
||
for (int num: nums) { | ||
// set์ ์๋ค๋ฉด | ||
if (!hashSet.add(num)) return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// ํ์ด | ||
// Set์ผ๋ก ์ค๋ณต ์ ๊ฑฐ ํ nums์ ๊ธธ์ด ๋น๊ต | ||
|
||
// TC : O(N) | ||
// SC : O(N) | ||
|
||
var containsDuplicate = function(nums) { | ||
return new Set(nums).size !== nums.length | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ์ค๋ณต์ ๊ฑฐ๋ฅผ ์ํด set์ ์ ๊ทน์ ์ผ๋ก ํ์ฉํด์ผํ ๋ฏ... | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> numSet = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (numSet.contains(num)) { | ||
return true; | ||
} | ||
numSet.add(num); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
# Slow - tc : O(n) / sc : O(1) | ||
|
||
"""return len(set(nums)) != len(nums)""" | ||
|
||
# Fast - tc : O(n) / sc : O(n) | ||
check = set() | ||
|
||
for num in nums: | ||
if num in check: | ||
return True | ||
check.add(num) | ||
|
||
return False | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
/** | ||
* Constraints | ||
* - 1 <= nums[] <= 10^5 | ||
* - -10^9 <= nums[i] <= 10^9 | ||
* | ||
* Output | ||
* - true : ์ค๋ณต ๊ฐ ์กด์ฌ | ||
* - false : ๋ชจ๋ ๊ฐ์ด ์ ์ผ | ||
*/ | ||
|
||
// ํด๊ฒฐ๋ฒ 1 (HashMap ๋ฐฉ์ - HashSet ์ ์ฌ) | ||
// ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ ๋ณต์ก๋ : O(N) | ||
Map<Integer, Integer> countMap = new HashMap<>(); | ||
|
||
for (int num: nums) { | ||
int count = countMap.getOrDefault(num, 0) + 1; | ||
if (count > 1) return true; | ||
countMap.put(num, count); | ||
} | ||
|
||
return false; | ||
|
||
// ํด๊ฒฐ๋ฒ 2 (์ ๋ ฌ) | ||
// ์๊ฐ ๋ณต์ก๋ : O(N log N), ๊ณต๊ฐ ๋ณต์ก๋ : O(1) | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length - 1; i++) { | ||
if (nums[i] == nums[i + 1]) return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
return len(nums) != len(set(nums)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
unordered_map<int, int> mp; | ||
for(int a : nums){ | ||
if(++mp[a] >= 2){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.HashSet; | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> set = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (!set.add(num)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
const n = nums.length; | ||
if (n === 0) return 0; | ||
if (n === 1) return nums[0]; | ||
|
||
// DP ๋ฐฐ์ด ์ด๊ธฐํ | ||
let prev2 = 0; // dp[i-2] | ||
let prev1 = 0; // dp[i-1] | ||
|
||
// ์ต๋ ์์ต ๊ณ์ฐ | ||
for (let num of nums) { | ||
const current = Math.max(prev1, prev2 + num); | ||
prev2 = prev1; | ||
prev1 = current; | ||
} | ||
|
||
return prev1; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
dp = [0] * len(nums) | ||
|
||
for i in range(len(nums)-1, -1, -1): | ||
dpMax = 0 | ||
for j in range(i + 2, len(nums)): | ||
dpMax = max(dpMax, dp[j]) | ||
dp[i] = nums[i] + dpMax | ||
|
||
return max(dp) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
๏ปฟ# --- ํด์ --- | ||
#๋งค๊ฐ๋ณ์ nums ๋ฅผ ์ํํ๋ฉด์ nums[i] ์ผ๋์ ์ต๋ ๋์ ๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ค | ||
#prev1์ ํ์ฌ๊น์ง์ ์ต๊ณ ๋์ ๊ธ์ก, prev2๋ ์ด์ ์ง๊น์ง์ ์ต๊ณ ๋์ ๊ธ์ก์ด๋ค. | ||
#ํ์ฌ ์ง nums[i] ๋ฅผ ๋๋์ง ํ๋ ค๋ฉด, ์ด์ ์ง๊น์ง์ ์ต๊ณ ๊ธ์ก(prev2) + ํ์ฌ ์ง(nums[i])์ด๋ค. | ||
#ํ์ฌ ์ง nums[i]๋ฅผ ๋๋์ง์ ์ ์ธํ๋ ค๋ฉด ํ์ฌ๊น์ง์ ์ต๊ณ ๊ธ์(prev1) ์ด๋ค. | ||
#loop ๋น ์ ๋์ ์ต๋๊ฐ์ ์ ํํ์ฌ current๋ณ์์ updateํด์ค๋ค. | ||
|
||
# --- Big O | ||
#N: ๋งค๊ฐ๋ณ์ nums์ ๊ธธ์ด๊ฐ N์ด๋ค. | ||
|
||
# Time Complexity: O(N) | ||
#- for loop ๋ nums[0] ๋ถํฐ nums[len(nums)]๋งํผ ์ํ: O(N) | ||
|
||
# Space Complexity: O(1) | ||
#-current,prev1,prev2 ๋ nums์ ๋ฌด๊ดํ ์์ ๋ฉ๋ก๋ฆฌ ํ ๋น: O(1) | ||
|
||
|
||
class Solution(object): | ||
def rob(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
#prev1: ํ์ฌ ์ง๊น์ง์ ์ต๊ณ ๊ธ์ก | ||
#prev2: ์ด์ด์ ์ง๊น์ง์ ์ต๊ณ ๊ธ์ก | ||
prev1,prev2=0,0 | ||
|
||
for num in nums: | ||
#current๋ prev1๊ณผ prev2+num ์ค ํฐ ๊ฐ์ update | ||
current = max(prev1,prev2+num) | ||
prev2 = prev1 #current์ ๋ฐ์ดํธ ์ดํ prev1(ํ์ฌ ์ต๊ณ ๊ธ์ก) ์ด prev2(์ด์ด์ง ์ง๊น์ง ์ต๊ณ ๊ธ์ก)๊ฐ๋๋ค | ||
prev1= current #prev1์ ํ num๊น์ง ๊ณ ๋ ค๋ current์ ๊ฐ์ด๋ค. (ํ์ฌ ์ต๊ณ ๊ธ์ก์ก) | ||
return prev1 | ||
nums = [2,7,9,3,1] | ||
solution = Solution() | ||
solution.rob(nums) | ||
|
Oops, something went wrong.