Skip to content

[jejufather] Week 1 #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a48c32e
add sample code for testing
Nov 24, 2024
f4ac433
add solution: contains duplicate
dusunax Dec 4, 2024
227415b
fix: add new line for lint rules
dusunax Dec 4, 2024
75e6d44
fix: change filename to GitHub username
dusunax Dec 4, 2024
be09081
add solution: valid-palindrome
dusunax Dec 4, 2024
9a395e9
add solution: top-k-frequent-elements
dusunax Dec 4, 2024
86658cb
fix: add new line for lint rules
dusunax Dec 4, 2024
395f249
fix: fix new line
dusunax Dec 4, 2024
e6c66d5
fix solution: use equality operator, remove regex
dusunax Dec 4, 2024
89739e6
fix solution: update comment
dusunax Dec 4, 2024
24792fe
add solution: longest-consecutive-sequence
dusunax Dec 6, 2024
3dff920
update solution: top-k-frequent-elements
dusunax Dec 6, 2024
4b036af
contains duplicate
JisooPyo Dec 7, 2024
7c19091
house robber
JisooPyo Dec 7, 2024
40c2fe9
longest consecutive sequence
JisooPyo Dec 7, 2024
920d92b
top k frequent elements
JisooPyo Dec 7, 2024
920c9a9
valid palindrome
JisooPyo Dec 7, 2024
aeb9221
Week1 solutions
5YoonCheol Dec 7, 2024
16ac10c
Week1 solutions
5YoonCheol Dec 7, 2024
055969d
add solution: house-robber
dusunax Dec 7, 2024
aa2e678
contains-duplicate
Dec 8, 2024
cc905f5
contains-duplicate
Dec 8, 2024
c3e92cf
Delete 3sum/csucom.py
csucom Dec 8, 2024
7ed1558
contains-duplicate
Dec 8, 2024
ecd1977
Merge branch 'main' of https://github.com/csucom/leetcode-study
Dec 8, 2024
85aa203
contains-duplicate
Dec 8, 2024
9a149b9
Merge pull request #643 from JisooPyo/main
JisooPyo Dec 9, 2024
622ff7a
Delete contains-duplicate/jejufather.cpp
csucom Dec 9, 2024
aef971c
contains-duplicate solution
csucom Dec 9, 2024
7165f94
Merge pull request #628 from dusunax/main
dusunax Dec 9, 2024
c8505f9
Merge pull request #645 from 5YoonCheol/main
5YoonCheol Dec 9, 2024
596e53a
chore: fix for integration filename
HC-kang Dec 9, 2024
d975b97
Merge pull request #658 from HC-kang/integration-hotfix
HC-kang Dec 9, 2024
1101bc1
add sample code for testing
Nov 24, 2024
2bb46c5
contains-duplicate
Dec 8, 2024
07dc830
contains-duplicate
Dec 8, 2024
1da888d
contains-duplicate
Dec 8, 2024
84c2e41
Delete 3sum/csucom.py
csucom Dec 8, 2024
2750e33
contains-duplicate
Dec 8, 2024
9e53fee
delete wrong file
Dec 9, 2024
b213d93
contains-duplicate solution
csucom Dec 9, 2024
3538957
Merge branch 'main' of https://github.com/csucom/leetcode-study
Dec 9, 2024
49876ae
contains-duplicate solution
Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
- name: Check filename rules
if: ${{ steps.pr-labels.outputs.has_maintenance != 'true' }}
run: |
files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | tr -d '"')
files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | tr -d '"')
pr_author="${{ github.event.pull_request.user.login }}"
success=true

Expand Down
16 changes: 16 additions & 0 deletions contains-duplicate/5YoonCheol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
/**
* 시간 복잡도: O(N)
* 공간 복잡도: O(N)
*/
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();

for (int num : nums) {
if (set.contains(num)) return true;
set.add(num);
}

return false;
}
}
38 changes: 38 additions & 0 deletions contains-duplicate/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

/**
* Leetcode
* 217. Contains Duplicate
* Easy
*/
class ContainsDuplicate {
/**
* Runtime: 17 ms(Beats: 80.99 %)
* Time Complexity: O(n)
* - 배열 순회
*
* Memory: 50.63 MB(Beats: 70.32 %)
* Space Complexity: O(n)
* - HashSet에 최악의 경우 배열 원소 모두 저장
*/
fun containsDuplicate(nums: IntArray): Boolean {
val set = hashSetOf<Int>()
for (i in nums) {
if (set.contains(i)) {
return true
}
set.add(i)
}
return false
}

@Test
fun test() {
containsDuplicate(intArrayOf(1, 2, 3, 1)) shouldBe true
containsDuplicate(intArrayOf(1, 2, 3, 4)) shouldBe false
containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)) shouldBe true
}
}
32 changes: 32 additions & 0 deletions contains-duplicate/csucom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <malloc.h>
#include <string.h>

bool containsDuplicate(int* nums, int numsSize) {
char* pflag = (char*)malloc(1000000001);
char* mflag = (char*)malloc(1000000001);
memset(pflag, 0, 1000000001);
memset(mflag, 0, 1000000001);
for (int i = 0; i < numsSize; ++i) {
if (nums[i] < 0) {
if (mflag[-nums[i]] == 1) {
free(pflag);
free(mflag);
return true;
}
mflag[-nums[i]] = 1;
}
else {
if (pflag[nums[i]] == 1) {
free(pflag);
free(mflag);
return true;
}
pflag[nums[i]] = 1;
}
}
free(pflag);
free(mflag);
return false;
}


23 changes: 23 additions & 0 deletions contains-duplicate/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
# Leetcode 217. Contains Duplicate

use set to store distinct elements 🗂️

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the list just once to convert it to a set.

### SC is O(n):
- creating a set to store the distinct elements of the list.
'''

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))

19 changes: 19 additions & 0 deletions house-robber/5YoonCheol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int rob(int[] nums) {
//배열 길이 0이면 털 수 있는 집이 없음.
if (nums.length == 0) return 0;
//배열 길이가 1이면 한 집만 털 수 있음.
if (nums.length == 1) return nums[0];

//동적 계획법으로 풀이
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

//배열 크기가 2이상일 경우 최대 금액의 범위 확장
for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
return dp[nums.length - 1];
}
}
74 changes: 74 additions & 0 deletions house-robber/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

/**
* Leetcode
* 198. House Robber
* Medium
*
* 사용된 알고리즘: Dynamic Programming
*
* i번째 집에서 얻을 수 있는 최대 금액은 다음 두 가지 중 큰 값입니다.
* - (i-2)번째 집까지의 최대 금액 + 현재 집의 금액
* - (i-1)번째 집까지의 최대 금액
*/
class HouseRobber {
/**
* Runtime: 0 ms(Beats: 100.00 %)
* Time Complexity: O(n)
*
* Memory: 34.65 MB(Beats: 40.50 %)
* Space Complexity: O(n)
*/
fun rob(nums: IntArray): Int {
if (nums.size == 1) {
return nums[0]
}
if (nums.size == 2) {
return max(nums[0], nums[1])
}
val dp = IntArray(nums.size)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for (i in 2 until nums.size) {
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
}
return dp[nums.size - 1]
}

/**
* 공간 복잡도를 개선
* Runtime: 0 ms(Beats: 100.00 %)
* Time Complexity: O(n)
*
* Memory: 34.95 MB(Beats: 36.98 %)
* Space Complexity: O(1)
*/
fun rob2(nums: IntArray): Int {
if (nums.size == 1) return nums[0]
if (nums.size == 2) return max(nums[0], nums[1])

var twoBack = nums[0]
var oneBack = max(nums[0], nums[1])
var current = oneBack

for (i in 2 until nums.size) {
current = max(twoBack + nums[i], oneBack)
twoBack = oneBack
oneBack = current
}

return current
}

@Test
fun test() {
rob(intArrayOf(1, 2, 3, 1)) shouldBe 4
rob(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
rob2(intArrayOf(1, 2, 3, 1)) shouldBe 4
rob2(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
}
}
44 changes: 44 additions & 0 deletions house-robber/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
# Leetcode 198. House Robber

use **dynamic programming** to solve this problem. (bottom-up approach) 🧩

choose bottom-up approach for less space complexity.

## DP relation

```
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
```

- **dp[i - 1]:** skip and take the value from the previous house
- **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the list just once to calculate the maximum money. = O(n)

### SC is O(n):
- using a list to store the maximum money at each house. = O(n)

'''

class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(2, len(nums)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])

return dp[-1]
34 changes: 34 additions & 0 deletions longest-consecutive-sequence/5YoonCheol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;

class Solution {
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0) return 0;

//모든 요소 HashSet 삽입
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}

int longest = 0;

// 시작지점 체크
for (int num : nums) {
//배열 요소보다 1 작은 수 가 없는 경우 새로운 시작 지점이 됨
if (!set.contains(num - 1)) {
int start = num;
int currentLength = 1;

// 1씩 증가시키면서 연속된 수의 개수 탐색
while (set.contains(start + 1)) {
start++;
currentLength++;
}
// 기존 longest와 현재 연속된 수를 비교
longest = Math.max(longest, currentLength);
}
}

return longest;
}
}
81 changes: 81 additions & 0 deletions longest-consecutive-sequence/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

/**
* Leetcode
* 128. Longest Consecutive Sequence
* Medium
*/
class LongestConsecutiveSequence {
/**
* Runtime: 58 ms(Beats: 79.06 %)
* Time Complexity: O(n)
* - while 루프의 총 반복 횟수는 n을 넘을 수 없다.
*
* Memory: 62.65 MB(Beats: 10.48 %)
* Space Complexity: O(n)
*/
fun longestConsecutive(nums: IntArray): Int {
val numsSet: MutableSet<Int> = nums.toHashSet()
val startSet: MutableSet<Int> = hashSetOf()

// 수열의 시작점이 될 수 있는 수를 찾는다.
for (num in numsSet) {
if (!numsSet.contains(num - 1)) {
startSet.add(num)
}
}
var answer = 0
for (start in startSet) {
// 수열의 시작점부터 몇 개 까지 numsSet에 있는지 확인한다.
var count = 0
var first = start
while (numsSet.contains(first)) {
first++
count++
}
// 최대 수열의 개수를 업데이트한다.
answer = max(answer, count)
}
return answer
}

/**
* 위 풀이에서 startSet을 제거하여 공간적으로 효율적인 풀이
* Runtime: 63 ms(Beats: 65.70 %)
* Time Complexity: O(n)
*
* Memory: 58.47 MB(Beats: 70.81 %)
* Space Complexity: O(n)
*/
fun longestConsecutive2(nums: IntArray): Int {
val numsSet = nums.toHashSet()
var maxLength = 0

for (num in numsSet) {
if (!numsSet.contains(num - 1)) {
var currentNum = num
var currentLength = 0

while (numsSet.contains(currentNum)) {
currentLength++
currentNum++
}
maxLength = max(maxLength, currentLength)
}
}
return maxLength
}

@Test
fun test() {
longestConsecutive(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
longestConsecutive(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9

longestConsecutive2(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
longestConsecutive2(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9
}
}
Loading
Loading