Intellij Idea is a popular Java IDE. But usually, a java project may contain different kinds of files as well. For example in my case, we were using proto files in our application.

So while searching a function name in the proto file, you might also get java files.

For this, we can use file masking.

To open finder, click command + shift + f

Select file masking option

and type *.proto

After that, you will only get proto files in the search.

Update

In earlier versions, just typing .proto would have worked. But now after the update, we need to write *.proto to get the same result.

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

Solution

class Solution {
public int majorityElement(int[] nums) {

int m = -1;

int i = 0;

for(int n : nums) {

if(i==0){
m = n;
i++;
} else if(n == m){
i++;
} else {
i — ;
}

}

return m;

}
}

Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its…

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

Solution

class Solution {
public int longestConsecutive(int[] nums) {

HashSet<Integer> set = new HashSet<>();

for(int n : nums){
set.add(n);
}

int sol = 0;

for(int n : nums) {
if(!set.contains(n-1)){

int temp = n;
int count=0;

while(set.contains(temp)) {
temp++;
count++;
}

sol = Math.max(sol, count);

}
}

return sol;

}
}

In 0–1 Knapsack problem, we are given a set of items, each with a weight and a value and we need to determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total…

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N…

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

Solution

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {

List<List<Integer>> sol = new ArrayList<>();

if (root == null) {
return sol;
}

Queue<TreeNode> q = new LinkedList<>();
q.offer(root);

while(!q.isEmpty()){

int levelSize = q.size();
List<Integer> level = new ArrayList<>();

while(levelSize → 0){
TreeNode t = q.poll();
level.add(t.val);

if(t.left != null){
q.offer(t.left);
}

if(t.right != null) {
q.offer(t.right);
}

}

sol.add(level);

}

return sol;
}
}

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 2

Example 2:

Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -1000 <= Node.val <= 1000

Solution:

class Solution {
public int minDepth(TreeNode root) {

if(root == null)
return 0;

if(root.left == null && root.right == null){
return 1;
}

int left = root.left != null ? minDepth(root.left) : Integer.MAX_VALUE;
int right = root.right != null ? minDepth(root.right) : Integer.MAX_VALUE;

return 1+Math.min(left,right);
}
}

Arpit Khurana

Came to read, Stayed to write.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store