Skip to content

Commit c7800fe

Browse files
add: week 1 problem solution
1 parent e0a1922 commit c7800fe

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
3+
public boolean containsDuplicate(int[] nums) {
4+
Map<Integer, Boolean> dupMap = new HashMap<>();
5+
6+
for(int i = 0; i < nums.length; i++) {
7+
if(dupMap.containsKey(nums[i])) {
8+
return true;
9+
}
10+
11+
dupMap.put(nums[i], true);
12+
}
13+
14+
return false;
15+
}
16+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
// TODO: DP ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋ณด๊ธฐ
6+
class Solution {
7+
public Map<Integer, Integer> robMap = new HashMap<>();
8+
public int rob(int[] nums) {
9+
return dfs(nums, 0);
10+
}
11+
12+
public int dfs(int[] nums, int index) {
13+
if(nums.length == 0) {
14+
return 0;
15+
}
16+
17+
if(index >= nums.length) {
18+
return 0;
19+
}
20+
21+
// ์ด๋ฏธ ํ„ธ์—ˆ๋˜ ์ง‘์ด๋ผ๋ฉด, ํ•ด
22+
if(robMap.containsKey(index)) {
23+
return robMap.get(index);
24+
}
25+
26+
// ์ด๋ฒˆ ์ง‘์„ ํ„ธ๊ฒŒ๋˜๋Š” ๊ฒฝ์šฐ
27+
int robThis = nums[index] + dfs(nums, index + 2);
28+
29+
// ์ด๋ฒˆ ์ง‘์„ ํ„ธ์ง€์•Š๊ณ  ๊ฑด๋„ˆ๋›ฐ๋Š” ๊ฒฝ์šฐ,.
30+
int skipThis = dfs(nums, index + 1);
31+
32+
robMap.put(index, Math.max(robThis, skipThis));
33+
34+
return robMap.get(index);
35+
}
36+
}
37+
38+
// TODO: ๋น„ํšจ์œจ์ ์œผ๋กœ ์ž‘์„ฑํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ๋„์‹ํ™” ํ•ด์„œ ๊ทธ๋ ค๋ณด๊ธฐ.
39+
// NOTE: dfs๋ฅผ ์‚ฌ์šฉํ•œ ์™„์ „ํƒ์ƒ‰
40+
// ํƒ์ƒ‰ ๋ฐฉ์‹์ด ๋งค์šฐ ๋น„ํšจ์œจ์ ์ด๋ผ, ์ •๋‹ต์€ ๋งž์ถ”์ง€๋งŒ N์ด ์ปค์ง€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ
41+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(2^n) + alpha(์ค‘๋ณตํƒ์ƒ‰)
42+
class WrongSolution {
43+
public boolean[] visit;
44+
public int mx = -987654321;
45+
public int curSum = 0;
46+
47+
public int rob(int[] nums) {
48+
if(nums.length == 1) {
49+
return nums[0];
50+
}
51+
52+
visit = new boolean[nums.length];
53+
dfs(nums, 0);
54+
dfs(nums, 1);
55+
56+
return mx;
57+
}
58+
59+
public void dfs(int[] arr, int idx) {
60+
int len = arr.length;
61+
int prevIdx = idx - 1;
62+
int nextIdx = idx + 1;
63+
64+
65+
if(idx == 0) {
66+
if(visit[idx]) return;
67+
} else {
68+
if(idx >= len || visit[idx] || visit[prevIdx]) {
69+
return;
70+
}
71+
}
72+
73+
visit[idx] = true;
74+
curSum += arr[idx];
75+
mx = Math.max(mx, curSum);
76+
77+
for(int i = idx; i < len; i++) {
78+
dfs(arr, i);
79+
}
80+
81+
visit[idx] = false;
82+
curSum -= arr[idx];
83+
}
84+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Arrays;
2+
import java.util.*;
3+
4+
class Solution {
5+
public int longestConsecutive(int[] nums) {
6+
int curSeq = 1;
7+
int maxSeq = -987654321;
8+
9+
if(nums.length == 0) {
10+
return 0;
11+
}
12+
13+
Arrays.sort(nums);
14+
15+
int cur = nums[0];
16+
for(int i = 1; i < nums.length; i++) {
17+
if(cur == nums[i]) {
18+
continue;
19+
}
20+
21+
if(cur < nums[i] && Math.abs(nums[i] - cur) == 1) {
22+
curSeq++;
23+
cur = nums[i];
24+
continue;
25+
}
26+
27+
// NOTE: ์ˆ˜์—ด์˜ ์กฐ๊ฑด์ด ๊นจ์กŒ์„ ๋•Œ
28+
maxSeq = Math.max(maxSeq, curSeq);
29+
curSeq = 1;
30+
cur = nums[i];
31+
}
32+
33+
return Math.max(maxSeq, curSeq);
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(3n) -> O(n)
3+
public int[] productExceptSelf(int[] nums) {
4+
5+
int zeroCount = 0;
6+
int[] result = new int[nums.length];
7+
int productExceptZero = 1;
8+
int zeroIdx = 0;
9+
10+
for(int i = 0; i < nums.length; i++) {
11+
if(nums[i] == 0) {
12+
zeroCount++;
13+
}
14+
}
15+
16+
// NOTE: 0์ด ๋‘๊ฐœ ์ด์ƒ์ผ ๋•Œ, ๋ชจ๋“  ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ 0;
17+
if(zeroCount >= 2) {
18+
return result;
19+
}
20+
21+
// NOTE: 0์ด 1๊ฐœ์ผ ๋•Œ, 0์ธ index๋งŒ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ๊ณฑ
22+
if(zeroCount == 1) {
23+
for(int i = 0; i < nums.length; i++) {
24+
if(nums[i] == 0) {
25+
zeroIdx = i;
26+
continue;
27+
}
28+
productExceptZero *= nums[i];
29+
}
30+
31+
result[zeroIdx] = productExceptZero;
32+
return result;
33+
}
34+
35+
// NOTE: 0์ด ์—†์„ ๋•Œ ๋ชจ๋“ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๋’ค idx๋ฅผ ๋‚˜๋ˆ„๊ธฐ.
36+
for(int i = 0; i < nums.length; i++) {
37+
productExceptZero *= nums[i];
38+
}
39+
40+
for(int i = 0; i < nums.length; i++) {
41+
int copy = productExceptZero;
42+
result[i] = copy / nums[i];
43+
}
44+
45+
return result;
46+
}
47+
}

0 commit comments

Comments
ย (0)