Find ith largest array in an array

Question:

  Given an integer array, find out the second largest item.

Analysis:

  Sort the array (ascending) first. The second largest will be the last but one? Too young, too simple, sometimes naive. It will not work if there are duplicated times. E.g. [2,5,8,3,8,7].

  We can use two indicators, li (largest item index, set to 0 initially) and slii (second largest item index, set to -1 initially). Traverse the array, starting from the second item in the array, let lii store the largest item‘s index, slii store the second largest one‘s. It can complete in O(n).

Code:

   public static int findSecondLargest(final int[] array) {
      if (array != null && array.length >= 2) {
         int lii = 0;
         int slii = -1;

         for (int i = 1; i < array.length; i++) {
            if (array[i] > array[lii]) {
               slii = lii;
               lii = i;
            } else if (array[i] == array[lii]) {
               continue;
            } else {
               slii = (slii == -1 || array[slii] < array[i]) ? i : slii;
            }

         return slii;
      }

      return -1;
   }

  So far so good.

Question:

  Given an integer array, find out the third largest item.

Analysis:

  WTH? Use three indicators, lii, slii, tlii (third largest item index).

public static int findThirdLargest(final int[] array) {
      if (array != null && array.length >= 3) {
         int lii = 0;
         int slii = -1;
         int tlii = -1;

         for (int i = 1; i < array.length; i++) {
            if (array[i] > array[lii]) {
               tlii = slii;
               slii = lii;
               lii = i;
            } else if (array[i] == array[lii]) {
               continue;
            } else {
               if (slii == -1) {
                  slii = i;
               } else {
                  if (array[i] > array[slii]) {
                     tlii = slii;
                     slii = i;
                  } else if (array[i] == array[slii]) {
                     continue;
                  } else {
                     tlii = (tlii == -1 || array[tlii] < array[i]) ? i : tlii;
                  }
               }
            }
         }

         return tlii;
      }

      return -1;
   }

  It smells. I‘m kind of repeating myself.

Question:

  Given an integer array, find out the ith largest item.

Analysis:

  WTF? Use i indicators? Write i methods? Don‘t repeat yourself (DRY). To find out the ith largest item, we use an assistant array indicators.

public static int findithLargest(final int ith, final int[] array) {
      if (array == null || ith < 1 || array.length < ith) {
         return -1;
      }

      int[] inidcators = new int[ith];
      for (int i = 1; i < ith; i++) {
         inidcators[i] = -1;
      }

      for (int i = 1; i < array.length; i++) {
         updateIndices(i, array, 0, inidcators);
      }

      return inidcators[ith - 1];
   }

   private static void updateIndices(final int cur, final int[] array, final int ithlii, int[] inidcators) {
      if (ithlii < inidcators.length) {
         if (inidcators[ithlii] == -1) {
            inidcators[ithlii] = cur;
         } else if (array[cur] > array[inidcators[ithlii]]) {
            for (int i = inidcators.length - 1; i > ithlii; i--) {
               inidcators[i] = inidcators[i - 1];
            }
            inidcators[ithlii] = cur;
         } else if (array[cur] == array[inidcators[ithlii]]) {
            return;
         } else {
            updateIndices(cur, array, ithlii + 1, inidcators);
         }
      }
   }
时间: 2024-07-29 20:20:43

Find ith largest array in an array的相关文章

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

[email&#160;protected] [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums

Leetcode题解(3):L215/Kth Largest Element in an Array

L215: Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may as

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

[215-Kth Largest Element in an Array(数组中第K大的数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth d

堆排序 &amp;&amp; Kth Largest Element in an Array

堆排序 堆节点的访问 通常堆是通过一维数组来实现的.在数组起始位置为0的情形中: 父节点i的左子节点在位置(2*i+1); 父节点i的右子节点在位置(2*i+2); 子节点i的父节点在位置floor((i-1)/2); 堆的操作 在堆的数据结构中,堆中的最大值总是位于根节点(在优先队列中使用堆的话堆中的最小值位于根节点).堆中定义以下几种操作: 最大堆调整(Max_Heapify):将堆的末端子节点作调整,使得子节点永远小于父节点 创建最大堆(Build_Max_Heap):将堆所有数据重新排序

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k

leetcode_215题——Kth Largest Element in an Array(堆排序)

Kth Largest Element in an Array Total Accepted: 13165 Total Submissions: 48240My Submissions Question Solution Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct elemen