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
26 changes: 26 additions & 0 deletions FindDisappearedNumbers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// In this solution we need find all disappeared numbers. What we do is have two for loops. In the first pass we mark the indices negative if they are present in the array. In the second pass we read the numbers that are not negative and add it to our liste by incrementing the index with one.
// Time complexity is O(n) and space complexity is O(1)

class Solution {
fun findDisappearedNumbers(nums: IntArray): List<Int> {
val list = mutableListOf<Int>()
val n = nums.size

for (i in 0 ..n-1) {
var idx = Math.abs(nums[i]) - 1
if (nums[idx] > 0) {
nums[idx] *= -1
}
}

for (i in 0 ..n-1) {
if (nums[i] > 0) {
list.add(i + 1)
} else {
nums[i] *= -1
}
}
return list
}
}

62 changes: 62 additions & 0 deletions GameOfLife.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This problem expects us to return a matrix where by flipping certain 1s and 0s. What we do is for the first pass we use the consitions in th e problem flip the elements to 2 or 3 which are placeholders
// to indicate that they were originally 1 or 0 respectively. In the second pass we convert all 2s to 0s and all 3s to 1s. We also use a private function to count the alive elements in all directions.
// Time complexity is O(m*n) and space complexity is O(1)

class Solution {
fun gameOfLife(board: Array<IntArray>): Unit {

//updating 1-> 0 as 2
//updating 0->1 as 3
val m = board.size
val n = board[0].size

for(i in 0..m-1) {
for (j in 0..n-1) {
val count = countAliveCells(board, i, j)
if (board[i][j] == 1 && (count < 2 || count > 3)) {
board[i][j] = 2
}
if (board[i][j] == 0 && count == 3) {
board[i][j] = 3
}
}
}
for(i in 0..m-1) {
for (j in 0..n-1) {
if(board[i][j] == 2) {
board[i][j] = 0
}
if(board[i][j] == 3) {
board[i][j] = 1
}
}
}
}

private fun countAliveCells(board: Array<IntArray>, i: Int, j: Int) : Int {
var count = 0

val dirs = arrayOf(
intArrayOf(0, 1),
intArrayOf(0, -1),
intArrayOf(-1, 0),
intArrayOf(1, 0),
intArrayOf(-1, -1),
intArrayOf(-1, 1),
intArrayOf(1, -1),
intArrayOf(1, 1)
)


for (dir in dirs) {
val newRow = i + dir[0]
val newCol = j + dir[1]

if (newRow >= 0 && newCol >= 0 && newRow < board.size && newCol < board[0].size &&
(board[newRow][newCol] == 1 || board[newRow][newCol] == 2)) {
count++
}
}
return count
}
}