The 15th Week of ARTS:KthLargest Element in a Stream_703
Introduction
- Algorithm - Learning Algorithm
- Review - Learning English
- Tip - Learning Techniques
- Share - Learning Influence
Let's do it!!!
Algorithm
KthLargest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
- My Solution
package com.silence.arts.leetcode.stack;
import java.util.PriorityQueue;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author Silence<br>
* <b>Date:</b>2018-12-01 16:04<br>
* <b>Desc:</b>无<br>
*/
public class KthLargest_703 {
/**
* PriorityQueue is kind of MINI Heap structure
*/
private PriorityQueue<Integer> queue;
private int k;
public KthLargest_703(int k, int[] nums) {
this.queue = new PriorityQueue<>(k);
this.k = k;
for (int num : nums) {
add(num);
}
}
/**
* If the size < k that means we just need to add the element then maintain the Heap
* else if the peek() < val we need to poll() the head of Heap and add the element then maintain the Heap
* if the peek() > val we do nothing
* return the peek()
*
* @param val
* @return
*/
public int add(int val) {
if (queue.size() < k) {
queue.offer(val);
} else if (queue.peek() < val) {
queue.poll();
queue.offer(val);
}
return queue.peek();
}
}
Extra spaces keep ListNode
Review
I just got a developer job at Facebook. Here’s how I prepped for my interviews.
Main ideas
- Algorithm Interviews
- Architecture Design Interviews
- Behavioral Interviews
- Culture Fit
- Pair Programming
- Finding and Patching Bugs
- Testing Domain Knowledge
- Understanding Operating Systems
Resources
- Mock Interviews
- Algorithms
- Cracking the Code Interview, Book
- byte by byte, Website and YouTube
- CS50, YouTube
- Interview Cake, Website
- HackerRank, Website
- LeetCode, Website
- Operating Systems
- Architecture Design
- Behavioural
Tips
Java CAS(Compare And Swap)比较交换
- MySql数据库的 InnoDB 引擎的索引采用 B+树存储,之所以采用 B+树是为了减少磁盘的访问,提供查询效率
- 主键索引保存的是整行数据,普通索引保存的是主键,索引普通索引的查询比主键搜索的查询多一次树的搜索
Share
Sharing
One more thing
- Personal Medium Home Page: https://medium.com/@zhuxiang134
- Personal Website: https://zxsilence.cn/
标题:The 15th Week of ARTS:KthLargest Element in a Stream_703
作者:zhuSilence
地址:https://home.zxsilence.cn/articles/2018/12/01/1570340346710.html