You are on page 1of 7

Name -awadhesh Kumar Maurya

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:

 Each row is sorted in non-decreasing order.


 The first integer of each row is greater than the last integer of the previous row.

Given an integer target, return true if target is in matrix or false otherwise.

You must write a solution in O(log(m * n)) time complexity.

Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13


Output: false

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:

 Integers in each row are sorted in ascending from left to right.


 Integers in each column are sorted in ascending from top to bottom.

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:

Input: nums = [10,9,2,5,3,7,101,18]


Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Example 2:

Input: nums = [0,1,0,3,2,3]


Output: 4

Example 3:

Input: nums = [7,7,7,7,7,7,7]


Output: 1

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;

}
}

You might also like