Skip to content

Commit 98b575e

Browse files
committed
[TimeComplexity] Codility-TapeEquilibrium
1 parent 156e365 commit 98b575e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
| 04 | | [Baekjoon-2748 피보나치 수 2](./src/TimeComplexity/P2748) | DP |
6868
| 05 | | [Baekjoon-1806 부분합](./src/TimeComplexity/P1806) | 2-pointer |
6969
| 06 | | [Codility-Lesson3 FrogJmp](./src/TimeComplexity/FrogJmp) | |
70-
| 06 | | [Codility-Lesson3 PermMissingElem](./src/TimeComplexity/PermMissingElem) | |
70+
| 07 | | [Codility-Lesson3 PermMissingElem](./src/TimeComplexity/PermMissingElem) | |
71+
| 08 | | [Codility-Lesson3 TapeEquilibrium](./src/TimeComplexity/TapeEquilibrium) | |
7172

7273
## Data Structure
7374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package TimeComplexity.TapeEquilibrium;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Solution sol = new Solution();
6+
System.out.println(sol.solution(new int[]{1, 2, 3}));
7+
System.out.println(sol.solution(new int[]{3, 1, 2, 4, 3}));
8+
}
9+
}
10+
11+
class Solution {
12+
13+
static int N;
14+
static int[] toRight, toLeft;
15+
16+
public int solution(int[] A) {
17+
N = A.length;
18+
toRight = new int[N];
19+
toLeft = new int[N];
20+
21+
toRight[0] = A[0];
22+
toLeft[N-1] = A[N-1];
23+
for (int i = 1; i < N; i++) {
24+
toRight[i] = toRight[i-1] + A[i];
25+
toLeft[N-i-1] = toLeft[N-i] + A[N-i-1];
26+
}
27+
28+
int ans = Integer.MAX_VALUE;
29+
for (int p = 1; p < A.length; p++) {
30+
ans = Math.min(ans, Math.abs(toRight[p-1] - toLeft[p]));
31+
}
32+
33+
return ans;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [Codility - Lesson 3 Time Complexity] TapeEquilibrium
2+
3+
> Detected time complexity : **O(N)**
4+
5+
![image](https://user-images.githubusercontent.com/22045163/101920230-e7bc0600-3c0e-11eb-8d04-7d38cc34f1b7.png)
6+
![image](https://user-images.githubusercontent.com/22045163/101920286-f6a2b880-3c0e-11eb-9354-efcd279939f8.png)

0 commit comments

Comments
 (0)