Skip to content

Commit 090f8fa

Browse files
authored
Merge pull request #2278 from geegong/main
[geegong] WEEK 10 solutions
2 parents 46fe430 + cfb47e4 commit 090f8fa

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
public class Geegong {
2+
3+
/**
4+
* two pointer ๋ฅผ ์‚ฌ์šฉ, ์ฒ˜์Œ์—๋Š” height ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์„œ ๋†’์€ ์ˆœ์˜ค๋ฅด ์ตœ๋Œ€ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š”๊ฑด๊ฐ€ ์‹ถ์—ˆ์ง€๋งŒ ๊ทธ๋Ÿด ํ•„์š”๋Š” ์—†์—ˆ์Œ
5+
* (์–ด์ฐจํ”ผ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋Œ์•„์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์†ŒํŒ…์˜ ์˜๋ฏธ๊ฐ€ ์—†์Œ, ๊ทธ๋ฆฌ๊ณ  NLogN ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ƒ๊ฒจ์„œ ๋” ์ข‹์ง€ ์•Š์Œ)
6+
* time complexity : O(N)
7+
* space complexity : O(N)
8+
*
9+
* @param height
10+
* @return
11+
*/
12+
public int maxArea(int[] height) {
13+
14+
15+
int leftIdx=0;
16+
int rightIdx = height.length - 1;
17+
int maxVolume = 0;
18+
19+
while(leftIdx < rightIdx) {
20+
21+
int leftHeight = height[leftIdx];
22+
int rightHeight = height[rightIdx];
23+
int gap = rightIdx - leftIdx;
24+
int currentVolume = gap * Math.min(leftHeight, rightHeight);
25+
maxVolume = Math.max(currentVolume, maxVolume);
26+
27+
if (leftHeight > rightHeight) {
28+
rightIdx--;
29+
} else {
30+
leftIdx++;
31+
}
32+
33+
}
34+
35+
return maxVolume;
36+
37+
38+
39+
40+
41+
42+
43+
44+
////////// ์•„๋ž˜ ๋ถ€๋ถ„์€ ์˜ˆ์ „ ๊ธฐ์ˆ˜์—์„œ ํ’€์—ˆ๋˜ ๋ฐฉ๋ฒ•
45+
// int leftIndex = 0;
46+
// int rightIndex = height.length - 1;
47+
//
48+
// int maxAmount = 0;
49+
// int currentAmount = 0;
50+
//
51+
// while(leftIndex != rightIndex && leftIndex < rightIndex) {
52+
// // ๋ฉด์ ์„ ๋จผ์ € ๊ตฌํ•ด๋ณธ๋‹ค.
53+
// int minHeight = Math.min(height[leftIndex], height[rightIndex]);
54+
// currentAmount = minHeight * (rightIndex - leftIndex);
55+
//
56+
// maxAmount = Math.max(currentAmount, maxAmount);
57+
// // ์–ด๋А ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ผ์ง€ ๊ฒฐ์ •
58+
// /**
59+
// * case 1. ๋‹จ์ˆœํžˆ ์ „์ฒด๋ฅผ ๋ชจ๋‘ ํ›‘์–ด๋ฒ„๋ฆฌ๋ฉด Time limit exceeded ๋ฐœ์ƒ
60+
// */
61+
//// if (leftIndex < rightIndex - 1) {
62+
//// rightIndex--;
63+
//// } else if (leftIndex == rightIndex - 1) {
64+
//// rightIndex = height.length - 1;
65+
//// leftIndex++;
66+
//// }
67+
//
68+
// /**
69+
// * case 2. ํฌ์ธํ„ฐ๊ฐ€ ๋Œ๋ฉด์„œ ๋†’์€ height๋งŒ ๊ณ ๋ คํ•ด์„œ ํฌ์ธํ„ฐ๊ฐ€ ์›€์ง์ผ ๋•Œ์—๋Š” Time limit exceeded ๋ฐœ์ƒ X
70+
// */
71+
// if (height[leftIndex] < height[rightIndex]) {
72+
// leftIndex++;
73+
// } else if (height[leftIndex] > height[rightIndex]) {
74+
// rightIndex--;
75+
// } else {
76+
// rightIndex--;
77+
// leftIndex++;
78+
// }
79+
// }
80+
//
81+
// return maxAmount;
82+
}
83+
}
84+

โ€Žjump-game/Geegong.javaโ€Ž

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class Geegong {
2+
3+
/**
4+
* greedy algorithms ์‚ฌ์šฉ
5+
*
6+
* time complexity : O(N)
7+
* space complexity : O(N)
8+
* @param nums
9+
* @return
10+
*/
11+
public boolean canJump(int[] nums) {
12+
// greedy algorithm
13+
int farthest = 0;
14+
int lastIdx = nums.length - 1;
15+
for (int idx=0; idx<nums.length; idx++) {
16+
// idx๊ฐ€ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค๋„ ๋„˜์—ˆ๋‹ค๋ฉด ๊ฐ€๋ง์ด ์—†๋Š”๊ฒƒ
17+
if (idx > farthest) return false;
18+
int currentFarthest = idx + nums[idx];
19+
farthest = Math.max(farthest, currentFarthest);
20+
21+
if (farthest >= lastIdx) return true;
22+
}
23+
24+
return true;
25+
}
26+
27+
28+
// ์•„๋ž˜ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๋ฉด TLE ๋ฐœ์ƒ
29+
// public boolean canJump(int[] nums) {
30+
// return recursion(0, nums);
31+
// }
32+
//
33+
// public boolean recursion(int idx, int[] nums) {
34+
// int totalLength = nums.length;
35+
// if (idx == totalLength - 1) {
36+
// return true;
37+
// }
38+
//
39+
// if (idx >= totalLength) {
40+
// return false;
41+
// }
42+
//
43+
// int currVal = nums[idx];
44+
// while(currVal > 0) {
45+
// boolean result = recursion(idx + currVal, nums);
46+
// if (result) {
47+
// return true;
48+
// }
49+
//
50+
// currVal--;
51+
// }
52+
//
53+
// return false;
54+
// }
55+
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Geegong {
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* public class ListNode {
6+
* int val;
7+
* ListNode next;
8+
* ListNode() {}
9+
* ListNode(int val) { this.val = val; }
10+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
11+
* }
12+
*/
13+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
14+
15+
ListNode root = new ListNode(Integer.MIN_VALUE, list1);
16+
ListNode curr1 = list1;
17+
ListNode curr2 = list2;
18+
19+
while (curr1 != null && curr2 != null) {
20+
int val1 = curr1.val;
21+
int val2 = curr2.val;
22+
23+
if (val1 <= val2) {
24+
ListNode temp = curr1.next;
25+
curr1.next = curr2;
26+
curr1 = temp;
27+
} else if (val1 > val2) {
28+
ListNode temp = curr2.next;
29+
curr2.next = curr1;
30+
curr2 = temp;
31+
}
32+
}
33+
34+
return root.next;
35+
}
36+
37+
public static class ListNode {
38+
int val;
39+
ListNode next;
40+
ListNode() {}
41+
ListNode(int val) { this.val = val; }
42+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
43+
}
44+
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Geegong {
2+
/**
3+
* XOR, AND ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐ
4+
* XOR : carry ํ™•์ธํ•˜์—ฌ 1๋น„ํŠธ์”ฉ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ (<<)
5+
* AND : carry ์—†์ด ๋”ํ•˜๊ธฐ ๊ฒฐ๊ณผ๊ฐ’๋งŒ ํ™•์ธ
6+
* time complexity : O(N)
7+
* space complexity : O(N)
8+
* @param a
9+
* @param b
10+
* @return
11+
*/
12+
public int getSum(int a, int b) {
13+
int a_ = a;
14+
int b_ = b;
15+
16+
while (a_ != 0) {
17+
int temp = a_;
18+
a_ = (a_ & b_) << 1;
19+
b_ = (temp ^ b_);
20+
}
21+
22+
return b_;
23+
}
24+
25+
}

0 commit comments

Comments
ย (0)