Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.

54 changes: 54 additions & 0 deletions findDisappearedNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :

// Question https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
// Your code here along with comments explaining your approach
/**
* Time complexity : O(n) where n is number of elements in array
* Space complexity : O(n)
* Did this code successfully run on Leetcode : yes
*/
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
HashMap<Integer,Boolean> map = new HashMap<>();
// Iterate over array nums and store in map to show its presence by marking as true
for(int i=0;i<nums.length;++i){
map.put(nums[i],true);
}
List<Integer> result = new ArrayList<>();
for(int i=1;i<=nums.length;++i){
// if map does not contain any value from array it means it is not present in array
if(!map.containsKey(i)){
result.add(i);
}
}
return result;
}
}
/**
* Time complexity : O(n)
* Space complexity : O(1)
* Did this code successfully run on Leetcode : yes
*
*/
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for(int i=0;i<nums.length;++i){
// converting the values as negative by taking num[i]-1 as index value in same array
int idx = Math.abs(nums[i])-1;
if(nums[idx]>0){
nums[idx] = - nums[idx];
}
}
List<Integer> result = new ArrayList<>();
for(int i=0;i<nums.length;++i){
// if any number is not converted into negative it means those are not present
if(nums[i]>0){
result.add(i+1);
}
}
return result;
}
}
47 changes: 47 additions & 0 deletions gameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Question https://leetcode.com/problems/game-of-life/
/**
* Time complexity : O(n*m)
* Space complexity : O(1)
* Did this code successfully run on Leetcode : yes
*/
class Solution {
// 0->1
int[][] dirs;
public void gameOfLife(int[][] board) {
this.dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
int m = board.length;
int n = board[0].length;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
// get the count of live cells
int alive = countOfliveCells(board,i,j);

if(board[i][j]==0 && alive==3){
board[i][j] = 3; // dead will convert into alive
}else if(board[i][j]==1 && (alive<2 || alive>3)){
board[i][j] = 2; // alive will convert into dead
}
}
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(board[i][j]==2){
board[i][j] = 0;
}else if(board[i][j]==3){
board[i][j] = 1;
}
}
}
}
int countOfliveCells(int[][] board,int i,int j){
int count = 0;
for(int[] dir: dirs){
int nr = i+dir[0];
int nc = j+dir[1];
if((nr>=0 && nr<board.length) && (nc>=0 && nc<board[0].length)){
if(board[nr][nc]==1 || board[nr][nc]==2) count++;
}
}
return count;
}
}