Skip to content

Commit

Permalink
week5 mission partially done
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-jonghoonpark committed May 29, 2024
1 parent e0508f0 commit 7d01251
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
29 changes: 29 additions & 0 deletions encode-and-decode-strings/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- 문제
- 유료 : https://leetcode.com/problems/encode-and-decode-strings/
- 무료 : https://www.lintcode.com/problem/659/
- time complexity : O(n)
- space complexity : O(n \* m), m은 각 문자열 길이의 평균
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/05/29/leetcode-271

```java
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str.replace("%", "%25").replace(",", "%2C")).append(",");
}
return sb.length() > 0 ? sb.toString() : "";
}

public List<String> decode(String str) {
List<String> decodedList = new ArrayList<>();
if (str.length() > 0) {
int commaIndex = str.indexOf(",");
while (commaIndex > -1) {
decodedList.add(str.substring(0, commaIndex).replace("%2C", ",").replace("%25", "%"));
str = str.substring(commaIndex + 1);
commaIndex = str.indexOf(",");
}
}
return decodedList;
}
```
28 changes: 28 additions & 0 deletions longest-consecutive-sequence/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- 문제 : https://leetcode.com/problems/longest-consecutive-sequence/
- time complexity : O(n)
- space complexity : O(n)
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/05/28/leetcode-128

```java
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();

for(int num : nums) {
set.add(num);
}

int max = 0;
for(int num : set) {
if (!set.contains(num - 1)) {
int current = 1;
while (set.contains(num + 1)) {
current++;
num++;
}
max = Math.max(current, max);
}
}

return max;
}
```
75 changes: 75 additions & 0 deletions product-of-array-except-self/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
- 문제 : https://leetcode.com/problems/product-of-array-except-self/
- time complexity : O(n)
- space complexity : O(n)
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/05/08/leetcode-238

## case 나눠서 풀기

```java
public int[] productExceptSelf(int[] nums) {
int[] products = new int[nums.length];
int result = 1;
int zeroCount = 0;

int p = 0;
while (p < nums.length) {
if (nums[p] != 0) {
result *= nums[p];
} else {
zeroCount++;
if (zeroCount >= 2) {
Arrays.fill(products, 0);
return products;
}
}
p++;
}

if (zeroCount == 1) {
p = 0;
while (p < nums.length) {
if (nums[p] == 0) {
products[p] = result;
}
p++;
}
} else {
p = 0;
while (p < nums.length) {
products[p] = result / nums[p];
p++;
}
}

return products;
}
```

## 재밋는 방법으로 풀기 (prefixProd)

```java
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];

int[] prefixProd = new int[nums.length];
int[] suffixProd = new int[nums.length];

prefixProd[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
prefixProd[i] = prefixProd[i-1] * nums[i];
}

suffixProd[nums.length - 1] = nums[nums.length - 1];
for (int i = nums.length - 2; i > -1; i--) {
suffixProd[i] = suffixProd[i + 1] * nums[i];
}

result[0] = suffixProd[1];
result[nums.length - 1] = prefixProd[nums.length - 2];
for (int i = 1; i < nums.length - 1; i++) {
result[i] = prefixProd[i - 1] * suffixProd[i + 1];
}

return result;
}
```

0 comments on commit 7d01251

Please sign in to comment.