Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. Solution: public class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { TreeSet<Long> ts = new TreeSet<>(); for(int i=0; i<nums.length; i++){ Long floor = ts.floor((long)nums[i]); Long
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. Solution: public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> set = new HashSet<>(); for(int i=0; i<nums.length; i++){ if(!set.add(nums[i])) return true; if(set.size()==k+1) set.remove(nums[i-k]); } return false; } }
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. public class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int n : nums) if(!set.add(n)) return true; return false; } }
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] word = “ABCCED”, -> returns true, word = “SEE”, -> returns true, word = “ABCB”, -> returns false.
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. For example, Consider the following matrix: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ] Given target = 5, return true.
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. For example, Consider the following matrix: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] Given target = 3, return true.
Given two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] Solution: public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> ans = new ArrayList<>(); combine(ans, n, k, 0, new ArrayList<Integer>()); return ans; } public void combine(List<List<Integer>> ans, int
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Solution: can be much much simpler public class Solution { public void setZeroes(int[][] matrix) { boolean left=false, right=false, up=false, down=false; for(int i=0; i<matrix.length; i++){ if(matrix[i][0]==0) left=true; if(matrix[i][matrix[0].length-1]==0) right=true; } for(int i=0; i<matrix[0].length; i++){ if(matrix[0][i]==0) up=true; if(matrix[matrix.length-1][i]==0) down=true; } for(int i=0; i<matrix.length; i++){ for(int j=0; j<matrix[0].length; j++){ if(matrix[i][j]==0)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Solution: public class Solution { public int minPathSum(int[][] grid) { int[][] dp = new int[grid.length+1][grid[0].length+1]; for(int i=0; i<=grid.length; i++){ for(int j=0; j<=grid[0].length; j++){ if(i==0 || j==0) dp[i][j]=Integer.MAX_VALUE;