Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
eunhwa99 authored Dec 15, 2024
2 parents 7d30552 + 1230e6b commit afaf83e
Show file tree
Hide file tree
Showing 63 changed files with 1,743 additions and 0 deletions.
17 changes: 17 additions & 0 deletions contains-duplicate/HerrineKim.js
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; // ๋ชจ๋“  ์š”์†Œ๊ฐ€ ๊ณ ์œ 
};

11 changes: 11 additions & 0 deletions contains-duplicate/HodaeSsi.py
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

23 changes: 23 additions & 0 deletions contains-duplicate/Jay-Mo-99.py
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


10 changes: 10 additions & 0 deletions contains-duplicate/Real-Reason.kt
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
}
}
36 changes: 36 additions & 0 deletions contains-duplicate/YeomChaeeun.ts
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;

};
60 changes: 60 additions & 0 deletions contains-duplicate/aa601.c
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);
}



31 changes: 31 additions & 0 deletions contains-duplicate/donghyeon95.java
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;
}
}

10 changes: 10 additions & 0 deletions contains-duplicate/higeuni.js
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
};

15 changes: 15 additions & 0 deletions contains-duplicate/imsosleepy.java
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;
}
}
18 changes: 18 additions & 0 deletions contains-duplicate/jungsiroo.py
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



35 changes: 35 additions & 0 deletions contains-duplicate/kdh-92.java
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;
}
}
3 changes: 3 additions & 0 deletions contains-duplicate/o-ddong.py
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))
14 changes: 14 additions & 0 deletions contains-duplicate/oyeong011.cpp
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;
}
};
16 changes: 16 additions & 0 deletions contains-duplicate/river20s.java
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;
}
}

25 changes: 25 additions & 0 deletions house-robber/HerrineKim.js
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;
};

15 changes: 15 additions & 0 deletions house-robber/HodaeSsi.py
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)

37 changes: 37 additions & 0 deletions house-robber/Jay-Mo-99.py
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)

Loading

0 comments on commit afaf83e

Please sign in to comment.