본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 8.Dominator 안녕하세요 남갯입니다. public int solution(int[] A) { // write your code in Java SE 8 int[] B = A.clone(); Arrays.sort(A); int length = A.length; if(length == 0){ return -1; } int count = 0; //반이 넘는것이기 때문에 중앙은 항상 최고 값이다. int cadidate = A[length / 2]; for (int a : A) { if (a == cadidate) { count++; } } //반이상이 안넘으면 대표leader가 없다 if (count < (length / 2) + 1) { return -1; } //clone 한곳에서 찾는다. for (int i = 0; i..
[코딜리티] - Lesson 7.StoneWall 안녕하세요 남갯입니다. public static int solution(int[] H) { // write your code in Java SE 8 int result = 0; Stack stack = new Stack(); stack.push(H[0]); result ++; for (int i = 0; i stack.peek()) { stack.push(H[i]); result++; } else if (H[i] < stack.peek()) { while (!stack.empty() && H[i] < stack.peek()) { stack.pop(); } if(!stack.empty() && stack.peek() == H[i]){ }else{ res..
[코딜리티] - Lesson 7. Nesting 안녕하세요 남갯입니다. public int solution(String S) { // write your code in Java SE 8 Stack stack = new Stack(); char[] chars = S.toCharArray(); for (char c : chars) { if (c == '(') { stack.push(c); } else if ((stack.empty() && c == ')') || (!stack.empty() && stack.pop() != '(')) { return 0; } } if(!stack.empty()){ return 0; } return 1; } https://app.codility.com/demo/results/training82HTSE-VWZ/
[코딜리티] - Lesson 7. Fish 안녕하세요 남갯입니다. public class Fish { public static int solution(int[] A, int[] B) { // write your code in Java SE 8 int result = 0; int max = 0; Stack downFishStack = new Stack(); for (int i = 0; i < A.length; i++) { if (B[i] == 0) { //왼쪽으로 가는방향인데 오른쪽으로 가는 방향이 없을경우 if (downFishStack.isEmpty()) { result++; } //왼쪽으로 가는애인데 오른쪽으로 가는 방향이 있었을 경우 else { // 오른쪽방향의 max값보다 왼쪽방향의 값이 클경우 오른쪽 방향 값들은 모두 삭제 while ..
[코딜리티] - Lesson 7. Brackets 안녕하세요 남갯입니다. public static int solution(String S) { // write your code in Java SE 8 if (S.length() % 2 == 1) { return 0; } else if (S.isEmpty()) { return 1; } char[] array = S.toCharArray(); Stack stack = new Stack(); for (int i = 0; i < array.length; i++) { if (array[i] == '{' || array[i] == '[' || array[i] == '(') { stack.push(array[i]); } else if (array[i] == '}') { if (stack.empty() || stack..
[코딜리티] - Lesson 6. Triangle 안녕하세요 남갯입니다. 앞에 두수의 합이 뒤에꺼의 값보다 크면 OKint Max값까지의 범위여서 생각없이 더할경우 오버플로우가 나게된다.첫번째수가 0보다 크고 다음수가 맥스값이면 마지막이 맥스값이여도 크다. import java.util.Arrays; public class Triangle { public int solution(int[] A) { // write your code in Java SE 8 int result = 0; Arrays.sort(A); for (int i = 0; i 0 && A[i + 1] == Integer.MAX_VALUE) || (A[i]..
[코딜리티] - Lesson 5. CountDiv 안녕하세요 남갯입니다. public int solution(int A, int B, int K) { // write your code in Java SE 8 if (K == 1 || (A == B && B == K)){ return B - A + 1; } int result = 0; int mock = (A > K) ? (A / K) : 0; int namugi = A % K; if (namugi > 0 && B = K) ? ((B - (K * mock)) / K) : 0; if (namugi == 0) { result++; } return result; } https://app.codility.com/demo/results/training35D..
[코딜리티] - Lesson 6. Distinct 안녕하세요 남갯입니다. public class Distinct { public static int solution(int[] A) { // write your code in Java SE 8 HashSet set = new HashSet(); for (int a : A) { set.add(a); } return set.size(); } } https://app.codility.com/demo/results/training5H2N4H-GDA/