package org.udanax.eddieyoon.codeguru; public class KthLargest { public static void main(String[] args) { int[] x = new int[] { 3, 6, 92, 34, 1, 35, 62, 13, 12, 24, 53 }; System.out.println(getKthLargest(x, 3)); } private static int getKthLargest(int[] x, int k) { int low = 0; int high = x.length - 1; while (true) { int pivot = (low + high) / 2; int newPiv = partition(x, low, high, pivot); if (newPiv == k) { return x[newPiv]; } else if (newPiv < k) { low = newPiv + 1; } else { high = newPiv - 1; } } } private static int partition(int[] x, int left, int right, int pivot) { int pivValue = x[pivot]; swap(x, pivot, right); int storePos = left; for (int i = left; i < right; i++) { if (x[i] < pivValue) { swap(x, i, storePos); storePos++; } } swap(x, storePos, right); return storePos; } private static void swap(int[] x, int a, int b) { int temp = x[a]; x[a] = x[b]; x[b] = temp; } }
Finding Kth largest element in Array
See Selection Algorithm first: http://en.wikipedia.org/wiki/Selection_algorithm
Subscribe to:
Post Comments (Atom)
-
Opening the black box of Deep Neural Networks via Information - https://arxiv.org/pdf/1703.00810.pdf 지금까지 딥 러닝은 어떻게 동작하는지 이해할 수 없다고 믿어져왔다...
-
음성 인공지능 분야에서 스타트업이 생각해볼 수 있는 전략은 아마 다음과 같이 3가지 정도가 있을 것이다: 독자적 Vertical 음성 인공지능 Application 구축 기 음성 플랫폼을 활용한 B2B2C 형태의 비지니스 구축 기 음성 플랫폼...
-
As mentioned ago, I've been forming up the Hamburg project with Hyunsik Choi. Let's see more detail in the diagram of computing met...
No comments:
Post a Comment