Skip to content

Commit d96744f

Browse files
authored
Merge pull request #1774 from suhyenim/5th/week02
[suhyenim] WEEK02 solutions
2 parents 1450d8d + 6672f99 commit d96744f

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed

โ€Žclimbing-stairs/suhyenim.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* [5th/week02] 70. Climbing Stairs
2+
3+
1. ๋ฌธ์ œ ์š”์•ฝ
4+
๋งํฌ: https://leetcode.com/problems/climbing-stairs/description/
5+
ํ•œ ๋ฒˆ์— 1์นธ์ด๋‚˜ 2์นธ์„ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ, n์นธ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋Š”?
6+
7+
2. ๋ฌธ์ œ ํ’€์ด
8+
ํ’€์ด1: 1, 2์นธ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋Š” ์ด๋ฏธ ์•Œ๊ธฐ ๋•Œ๋ฌธ์—, "n์นธ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜ = n-2์นธ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜ + n-1์นธ์„ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜"๋ฅผ ๋ฐ˜๋ณต
9+
์„ฑ๊ณต: Time: 0 ms (100%), Space: 40.4 MB (50.46%)
10+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
11+
12+
class Solution {
13+
public int climbStairs(int n) {
14+
Map<Integer, Integer> dp = new HashMap<>();
15+
dp.put(1, 1);
16+
dp.put(2, 2);
17+
for (int i = 3; i <= n; i++){
18+
dp.put(i, dp.get(i - 2) + dp.get(i - 1));
19+
}
20+
return dp.get(n);
21+
}
22+
}
23+
24+
ํ’€์ด2: ํ’€์ด1๊ณผ ๋…ผ๋ฆฌ๋Š” ๊ฐ™์ง€๋งŒ, HashMap์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ์œผ๋กœ์จ ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋‚ฎ์ถค
25+
์„ฑ๊ณต: Time: 0 ms (100%), Space: 40.1 MB (97.08%)
26+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
27+
28+
class Solution {
29+
public int climbStairs(int n) {
30+
if (n < 3){
31+
return n;
32+
}
33+
int n1 = 1, n2 = 2;
34+
for (int i = 0; i < n - 2; i++){
35+
int n3 = n1 + n2;
36+
n1 = n2;
37+
n2 = n3;
38+
}
39+
return n2;
40+
}
41+
}
42+
43+
ํ’€์ด3: ํ’€์ด1์€ ๋ฐ˜๋ณต๋ฌธ(bottom-up) DP์ด๊ณ , ํ’€์ด3์€ ์žฌ๊ท€+๋ฉ”๋ชจ์ด์ œ์ด์…˜(top-down) DP์ž„
44+
์„ฑ๊ณต: Time: 0 ms (100%), Space: 40.5 MB (50.46%)
45+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
46+
47+
class Solution {
48+
private Map<Integer, Integer> memo = new HashMap<>();
49+
50+
public int climbStairs(int n) {
51+
if (memo.containsKey(n)) {
52+
return memo.get(n);
53+
}
54+
55+
if (n < 3) {
56+
return n;
57+
}
58+
59+
int result = climbStairs(n - 1) + climbStairs(n - 2);
60+
memo.put(n, result);
61+
return result;
62+
}
63+
}
64+
65+
3. TIL
66+
์žฌ๊ท€์—๋‹ค๊ฐ€ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ๋”ํ•˜๋ฉด
67+
=> ํฐ ๋ฌธ์ œ๋Š” ์žฌ๊ท€์ ์œผ๋กœ ์ชผ๊ฐœ์„œ ํ’€๋˜, ์ด๋ฏธ ๊ณ„์‚ฐํ•œ ์ž‘์€ ๋ฌธ์ œ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฉ”๋ชจ(map)์— ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ๊บผ๋‚ด ์“ธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
68+
=> ์ค‘๋ณต ๊ณ„์‚ฐ์„ ๋ฐฉ์ง€ํ•จ์œผ๋กœ์จ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์•„๋‚„ ์ˆ˜ ์žˆ๋‹ค.
69+
70+
*/
71+
72+
class Solution {
73+
public int climbStairs(int n) {
74+
Map<Integer, Integer> dp = new HashMap<>();
75+
dp.put(1, 1);
76+
dp.put(2, 2);
77+
for (int i = 3; i <= n; i++){
78+
dp.put(i, dp.get(i - 2) + dp.get(i - 1));
79+
}
80+
return dp.get(n);
81+
}
82+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* [5th/week02] 238. Product of Array Except Self
2+
3+
1. ๋ฌธ์ œ ์š”์•ฝ
4+
๋งํฌ: https://leetcode.com/problems/product-of-array-except-self/description/
5+
๊ฐ ์ธ๋ฑ์Šค i์— ๋Œ€ํ•ด, num[i]๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’๋“ค์„ ๊ณฑํ•ด์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
6+
(์ฃผ์˜: ๋‚˜๋ˆ„๊ธฐ ์•ˆ๋จ, ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n) ๋‚ด๋กœ ๋™์ž‘ํ•ด์•ผ ํ•จ)
7+
8+
2. ๋ฌธ์ œ ํ’€์ด
9+
ํ’€์ด1: ์ธ๋ฑ์Šค i๋ฅผ ๊ธฐ์ ์œผ๋กœ, ์ด์ „ ๊ฐ’๋“ค์˜ ๋ˆ„์ ๊ณฑ ๋ฐฐ์—ด & ์ดํ›„ ๊ฐ’๋“ค์˜ ๋ˆ„์ ๊ณฑ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  -> ๊ฐ ์ธ๋ฑ์Šค i์— ๋Œ€ํ•ด, ๋‘ ๋ฐฐ์—ด ๊ฐ’ ๊ณฑํ•˜๊ธฐ
10+
์„ฑ๊ณต: Time: 2 ms (87.36%), Space: 56.5 MB (10.9%)
11+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
12+
13+
class Solution {
14+
public int[] productExceptSelf(int[] nums) {
15+
int n = nums.length;
16+
int[] before = new int[n];
17+
int[] after = new int[n];
18+
int[] products = new int[n];
19+
20+
before[0] = 1;
21+
for (int i = 0; i < n - 1; i++) {
22+
before[i + 1] = before[i] * nums[i];
23+
}
24+
25+
after[n - 1] = 1;
26+
for (int i = n - 1; i > 0; i--) {
27+
after[i - 1] = after[i] * nums[i];
28+
}
29+
30+
for (int i = 0; i < n; i++) {
31+
products[i] = before[i] * after[i];
32+
}
33+
34+
return products;
35+
}
36+
}
37+
38+
ํ’€์ด2: ํ’€์ด1๊ณผ ๋…ผ๋ฆฌ๋Š” ๋™์ผํ•˜์ง€๋งŒ, ๋ˆ„์ ๊ณฑ์„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ์ €์žฅํ•ด๋‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ €์žฅํ•˜๋„๋ก ๋ณ€๊ฒฝ
39+
์„ฑ๊ณต: Time: 3 ms (19.55%), Space: 55.4 MB (61.74%)
40+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: ๊ฒฐ๊ณผ ๋ฐฐ์—ด ์ œ์™ธํ•˜๋ฉด O(1)
41+
42+
class Solution {
43+
public int[] productExceptSelf(int[] nums) {
44+
int n = nums.length;
45+
int[] products = new int[n];
46+
Arrays.fill(products, 1);
47+
48+
int leftProduct = 1;
49+
for (int i = 0; i < n - 1; i++) {
50+
leftProduct *= nums[i];
51+
products[i + 1] *= leftProduct;
52+
}
53+
54+
int rightProduct = 1;
55+
for (int i = n - 1; i > 0; i--) {
56+
rightProduct *= nums[i];
57+
products[i - 1] *= rightProduct;
58+
}
59+
60+
return products;
61+
}
62+
}
63+
64+
3. TIL
65+
๋™์  ๊ณ„ํš๋ฒ•(DP)์ด๋ž€?
66+
ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ณ , ๊ทธ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์˜ ํ•ด๋ฅผ ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€(๋ฉ”๋ชจ์ด์ œ์ด์…˜/ํ…Œ์ด๋ธ”), ํ•„์š”ํ•  ๋•Œ ์žฌ์‚ฌ์šฉํ•ด์„œ ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค.
67+
68+
*/
69+
70+
class Solution {
71+
public int[] productExceptSelf(int[] nums) {
72+
int n = nums.length;
73+
int[] before = new int[n];
74+
int[] after = new int[n];
75+
int[] products = new int[n];
76+
77+
before[0] = 1;
78+
for (int i = 0; i < n - 1; i++) {
79+
before[i + 1] = before[i] * nums[i];
80+
}
81+
82+
after[n - 1] = 1;
83+
for (int i = n - 1; i > 0; i--) {
84+
after[i - 1] = after[i] * nums[i];
85+
}
86+
87+
for (int i = 0; i < n; i++) {
88+
products[i] = before[i] * after[i];
89+
}
90+
91+
return products;
92+
}
93+
}

โ€Žvalid-anagram/suhyenim.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* [5th/week02] 242. Valid Anagram
2+
3+
1. ๋ฌธ์ œ ์š”์•ฝ
4+
๋งํฌ: https://leetcode.com/problems/valid-anagram/description/
5+
๋ฌธ์ž์—ด t๊ฐ€ ๋ฌธ์ž์—ด s์˜ anagram์ด๋ฉด true ๋ฐ˜ํ™˜
6+
7+
2. ๋ฌธ์ œ ํ’€์ด
8+
์ œ์ถœ1: ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ์ง€ ์šฐ์„  ์ฒดํฌํ•˜๊ณ  -> ๊ฐ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„, ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅด๋ฉด false ๋ฐ˜ํ™˜
9+
์„ฑ๊ณต: Time: 3 ms (91.78%), Space: 44.6 MB (43.28%)
10+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(nlogn), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
11+
12+
class Solution {
13+
public boolean isAnagram(String s, String t) {
14+
if (s.length() != t.length()){
15+
return false;
16+
}
17+
char[] ss = s.toCharArray();
18+
char[] tt = t.toCharArray();
19+
Arrays.sort(ss);
20+
Arrays.sort(tt);
21+
for (int i = 0; i < s.length(); i++){
22+
if (ss[i] != tt[i]){
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
}
29+
30+
ํ’€์ด2: ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ์ง€ ์šฐ์„  ์ฒดํฌํ•˜๊ณ  -> s๋ฅผ {๋ฌธ์ž:ํšŸ์ˆ˜} ๊ตฌ์กฐ์˜ HashMap์œผ๋กœ ๋งŒ๋“  ํ›„ -> t์˜ ๋ฌธ์ž๋“ค๊ณผ ๋น„๊ตํ•ด์„œ ๊ฐ™์œผ๋ฉด {ํ‚ค:๋ฐธ๋ฅ˜}์Œ ์‚ญ์ œ -> ์ตœ์ข…์ ์œผ๋กœ {ํ‚ค:๋ฐธ๋ฅ˜}์Œ์ด 0๊ฐœ๋ฉด true ๋ฐ˜ํ™˜
31+
์„ฑ๊ณต: Time: 15 ms (25.19%), Space: 45 MB (10.95%)
32+
=> ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
33+
34+
class Solution {
35+
public boolean isAnagram(String s, String t) {
36+
if (s.length() != t.length()) {
37+
return false;
38+
}
39+
40+
Map<Character, Integer> count = new HashMap<>();
41+
for (char c : s.toCharArray()) {
42+
count.put(c, count.getOrDefault(c, 0) + 1);
43+
}
44+
45+
for (char c : t.toCharArray()) {
46+
if (!count.containsKey(c)) {
47+
return false;
48+
}
49+
count.put(c, count.get(c) - 1);
50+
if (count.get(c) == 0) {
51+
count.remove(c);
52+
}
53+
}
54+
55+
return count.isEmpty();
56+
}
57+
}
58+
59+
3. TIL
60+
Map์—๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค. ex) getOrDefault(), containsKey(), put(), get(), remove() ๋“ฑ
61+
getOrDefault() ๋‚ด๋ถ€ ๊ตฌํ˜„์—๋Š” containsKey()๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.
62+
63+
*/
64+
65+
class Solution {
66+
public boolean isAnagram(String s, String t) {
67+
if (s.length() != t.length()){
68+
return false;
69+
}
70+
char[] ss = s.toCharArray();
71+
char[] tt = t.toCharArray();
72+
Arrays.sort(ss);
73+
Arrays.sort(tt);
74+
for (int i = 0; i < s.length(); i++){
75+
if (ss[i] != tt[i]){
76+
return false;
77+
}
78+
}
79+
return true;
80+
}
81+
}

0 commit comments

Comments
ย (0)