Professional Documents
Culture Documents
Awadhesh Maurya Kiet Dsa Training Day 10
Awadhesh Maurya Kiet Dsa Training Day 10
Roll no 2100290100039
Dsa training day 10
Question no 1
74. Search a 2D Matrix
Solved
Medium
Topics
Companies
You are given an m x n integer matrix matrix with the following two properties:
Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 100
-104 <= matrix[i][j], target <= 10 4
Code
class Solution {
public boolean searchMatrix(int[][]matrix,int target) {
if (matrix.length==0) return false;
int m=matrix.length;
int n=matrix[0].length;
int l=0;
int r=m*n;
while(l<r){
int mid=(l+r)/2;
int i=mid/n;
int j=mid %n;
if(matrix[i][j]==target)
return true;
if(matrix[i][j]<target) l=mid+1;
else r=mid;
}
return false;
}
}
Question no 2
240. Search a 2D Matrix II
Attempted
Medium
Topics
Companies
Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This
matrix has the following properties:
Example 1:
Input: 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]], target = 5
Output: true
Example 2:
Input: 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]], target = 20
Output: false
Constraints:
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-109 <= matrix[i][j] <= 10 9
All the integers in each row are sorted in ascending order.
All the integers in each column are sorted in ascending order.
-109 <= target <= 10 9
Code
class Solution {
public boolean searchMatrix(int[][]matrix,int target) {
if (matrix.length==0)
return false;
int m=matrix.length;
int n=matrix[0].length;
int l = 0;
int r=m*n;
while(l<r){
}
return false;
}
}
Question no 3
300. Longest Increasing Subsequence
Medium
Topics
Companies
Given an integer array nums, return the length of the longest strictly increasing
subsequence
.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 2500
-104 <= nums[i] <= 104
Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
class Solution {
public int lengthOfLIS(int[] nums) {
int []dp=new int[nums.length];
Arrays.fill(dp,1);
int size=nums.length;
for(int i=1;i<size;i++){
for(int j=0;j<i;j++){
if(nums[i]>nums[j])
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
int max=0;
for(Integer ele:dp)
max=(max>ele)?ele:max;
return max;
}
}