Skip to content

Commit cfb47e4

Browse files
committed
- 10주차 문제 풀이와 그외 못풀었던 문제들도 같이 커밋
1 parent 0e73afc commit cfb47e4

4 files changed

Lines changed: 210 additions & 0 deletions

File tree

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+
}

sum-of-two-integers/Geegong.java

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)