Skip to content

Commit 2c4b6b5

Browse files
committed
Day 18 - 22 Except Longest Duplicate Substring and Dungeon Game
1 parent 590a0a6 commit 2c4b6b5

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
import scala.util.control.Breaks
4+
5+
/*
6+
* Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
7+
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have
8+
at least h citations each, and the other N − h papers have no more than h citations each."
9+
*
10+
* */
11+
12+
/*
13+
* Input: citations = [0,1,3,5,6]
14+
Output: 3
15+
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
16+
received 0, 1, 3, 5, 6 citations respectively.
17+
Since the researcher has 3 papers with at least 3 citations each and the remaining
18+
two with no more than 3 citations each, her h-index is 3.
19+
*
20+
* */
21+
22+
/*
23+
* Approach:
24+
* 1. We have to iterate through the list in reverse order [If list is not sorted we have to sort the list in ascending order first].
25+
* 2. Will check if the current element is greater than or equal to count. If not we will continue the iteration.
26+
* 3. If not we have to break the loop and return the result.
27+
*
28+
* */
29+
30+
31+
object HIndex2 {
32+
33+
def main(args: Array[String]): Unit = {
34+
val citations = Array(0, 1, 3, 5, 6)
35+
val result = hIndex(citations)
36+
println(s"Result: ${result}, ${result == 3}")
37+
}
38+
39+
def hIndex(citations: Array[Int]): Int = {
40+
val length = citations.length
41+
var result = 0
42+
var tail = length - 1
43+
val breaks = new Breaks
44+
breaks.breakable {
45+
while (tail >= 0) {
46+
var count = length - tail
47+
if (citations(tail) >= count) {
48+
result = count
49+
tail -= 1
50+
} else {
51+
breaks.break()
52+
}
53+
}
54+
}
55+
return result
56+
}
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
/*
4+
* The set [1,2,3,...,n] contains a total of n! unique permutations.
5+
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
6+
7+
"123"
8+
"132"
9+
"213"
10+
"231"
11+
"312"
12+
"321"
13+
14+
Given n and k, return the kth permutation sequence.
15+
* Note:
16+
* Given n will be between 1 and 9 inclusive.
17+
* Given k will be between 1 and n! inclusive.
18+
* */
19+
20+
21+
/*
22+
* Input: n = 3, k = 3
23+
Output: "213"
24+
*
25+
* Input: n = 4, k = 9
26+
Output: "2314"
27+
* */
28+
29+
30+
/*
31+
* Approach - 1: [This method will be slow because we are calculating all the possible permutations]
32+
* 1. First we are creating a Range out of n.
33+
* 2. We use permutations method under Range object to get the permutations.
34+
* 3. We are dropping all the element upto k-1.
35+
* 4. Take next element from the Range.
36+
* 5. Convert list of integers to string using mkString method.
37+
* 6. return the string.
38+
*
39+
* */
40+
41+
42+
object PermutationSequence {
43+
44+
def main(args: Array[String]): Unit = {
45+
val result = getPermutation(3, 3)
46+
println(s"Result: ${result}, ${result == "213"}")
47+
val result1 = getPermutation(4, 4)
48+
println(s"Result: ${result1}, ${result1 == "1342"}")
49+
}
50+
51+
def getPermutation(n: Int, k: Int): String = {
52+
if (n < 0 || n > 9) return ""
53+
var result = ""
54+
Range(1, n + 1).permutations.drop(k - 1).next().mkString
55+
}
56+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
import scala.collection.mutable
4+
5+
/* Given a non-empty array of integers, every element appears three times except for one,
6+
which appears exactly once. Find that single one.
7+
*
8+
* */
9+
10+
/*
11+
* Note:
12+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
13+
* */
14+
15+
/*
16+
* Input: [2,2,3,2]
17+
Output: 3
18+
*
19+
* Input: [0,1,0,1,0,1,99]
20+
Output: 99
21+
* */
22+
23+
/*
24+
* Approach: Bit Manupulation Using XOR
25+
*
26+
* 0 ^ 2 = 2
27+
* 2 ^ 2 = 0
28+
*
29+
* 1. Declaring 3 variables ones,two and threes.
30+
* 2. Iterating through the array.
31+
*
32+
* Iteration:
33+
*
34+
* 1 -> (2,0,0)
35+
* 2 -> (0,2,0)
36+
* 3 -> (1,0,2)
37+
* 4 -> (3,0,0)
38+
*
39+
*
40+
* */
41+
42+
43+
object SingleNumber2 {
44+
def main(args: Array[String]): Unit = {
45+
val nums = Array(2, 2, 3, 2)
46+
val nums1 = Array(0, 1, 0, 1, 0, 1, 95)
47+
println(singleNumberBestApproach(nums))
48+
}
49+
50+
//Using Bit Manupulation
51+
def singleNumberBestApproach(nums: Array[Int]): Int = {
52+
var ones = 0
53+
var twos = 0
54+
var threes = 0
55+
for (num <- nums) {
56+
twos |= ones & num
57+
ones ^= num
58+
threes = ones & twos
59+
ones &= ~threes
60+
twos &= ~threes
61+
}
62+
ones
63+
}
64+
65+
//Using HashMap - Extra *Memory
66+
def singleNumber(nums: Array[Int]): Int = {
67+
var map: mutable.HashMap[Int, Int] = mutable.HashMap()
68+
for (num <- nums) {
69+
if (map.contains(num)) {
70+
val sum = map(num) + 1
71+
map += (num -> sum)
72+
} else {
73+
map += (num -> 1)
74+
}
75+
}
76+
for (item <- map) {
77+
if (item._2 == 1) return item._1
78+
}
79+
return -1
80+
}
81+
82+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
import scala.collection.mutable
4+
5+
/*
6+
* Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
7+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
8+
*
9+
* */
10+
11+
/*
12+
* Before:
13+
* X X X X
14+
X O O X
15+
X X O X
16+
X O X X
17+
*
18+
* After:
19+
* X X X X
20+
X X X X
21+
X X X X
22+
X O X X
23+
*
24+
* Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'.
25+
* Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'.
26+
* Two cells are connected if they are adjacent cells connected horizontally or vertically.
27+
* */
28+
29+
/*
30+
* Approach: Breadth First Search or Depth First Search.
31+
*
32+
* 1. We have to go through the border of the board and check if we have any O's then we have to replace that O's with new character '*' also we have see if that 'O' has adjacent 'O' and we have to replace them with '*'.
33+
* 2. After replacing all the O's in the border and their adjacent O's connected to the border. Once again we have to go through the each element of the array and we have replace the remaining O's with 'X' and '*' with 'O'.
34+
*
35+
* */
36+
37+
38+
object SurroundedRegions {
39+
def main(args: Array[String]): Unit = {
40+
// var board = Array(Array('x', 'x', 'x', 'x'), Array('x', 'o', 'o', 'x'), Array('x', 'x', 'o', 'x'), Array('x', 'o', 'x', 'x'))
41+
// var board = Array(Array('o', 'x', 'x', 'o', 'x'), Array('x', 'o', 'o', 'x', 'o'), Array('x', 'o', 'x', 'o', 'x'), Array('o', 'x', 'o', 'o', 'o'), Array('x', 'x', 'o', 'x', 'o'))
42+
var board = Array(Array('x', 'o', 'x', 'o', 'x', 'o'), Array('o', 'x', 'o', 'x', 'o', 'x'), Array('x', 'o', 'x', 'o', 'x', 'o'), Array('o', 'x', 'o', 'x', 'o', 'x'))
43+
// var board = Array(Array('x', 'o', 'x'), Array('x', 'o', 'x'), Array('x', 'o', 'x'))
44+
println("BEFORE")
45+
board.foreach(a => println(a.mkString))
46+
solve(board)
47+
println("AFTER")
48+
board.foreach(a => println(a.mkString))
49+
}
50+
51+
def solve(board: Array[Array[Char]]): Unit = {
52+
if (board.length == 0 || board(0).length == 0) return
53+
val rowLength = board.length
54+
val columnLength = board(0).length
55+
for (i <- board.indices) {
56+
if (board(i)(0).toUpper == 'O') depthFirstSearch(i, 0, board)
57+
if (board(i)(columnLength - 1).toUpper == 'O') depthFirstSearch(i, columnLength - 1, board)
58+
}
59+
60+
for (j <- board(0).indices) {
61+
if (board(0)(j).toUpper == 'O') depthFirstSearch(0, j, board)
62+
if (board(rowLength - 1)(j).toUpper == 'O') depthFirstSearch(rowLength - 1, j, board)
63+
}
64+
65+
for (i <- board.indices) {
66+
for (j <- board(0).indices) {
67+
if (board(i)(j).toUpper == 'O') {
68+
board(i)(j) = 'X'
69+
} else if (board(i)(j) == '*') {
70+
board(i)(j) = 'O'
71+
}
72+
}
73+
}
74+
}
75+
76+
def depthFirstSearch(i: Int, j: Int, board: Array[Array[Char]]): Unit = {
77+
if (i < 0 || i > board.length - 1 || j < 0 || j > board(i).length - 1) return
78+
if (board(i)(j).toUpper == 'O') board(i)(j) = '*'
79+
if (i > 0 && board(i - 1)(j).toUpper == 'O') depthFirstSearch(i - 1, j, board)
80+
if (i < board.length - 1 && board(i + 1)(j).toUpper == 'O') depthFirstSearch(i + 1, j, board)
81+
if (j > 0 && board(i)(j - 1).toUpper == 'O') depthFirstSearch(i, j - 1, board)
82+
if (j < board(0).length - 1 && board(i)(j + 1).toUpper == 'O') depthFirstSearch(i, j + 1, board)
83+
}
84+
85+
86+
def solveBFS(board: Array[Array[Char]]): Unit = {
87+
if (board.length == 0 || board(0).length == 0) return
88+
var queue: mutable.Queue[(Int, Int)] = mutable.Queue()
89+
for (i <- board.indices) {
90+
if (board(i)(0) == 'O') BFS(i, 0, board)
91+
if (board(i)(board(0).length - 1) == 'O') BFS(i, board(0).length - 1, board)
92+
}
93+
94+
for (j <- board(0).indices) {
95+
if (board(0)(j) == 'O') BFS(0, j, board)
96+
if (board(board.length - 1)(j) == 'O') BFS(board.length - 1, j, board)
97+
}
98+
99+
for (i <- board.indices) {
100+
for (j <- board(0).indices) {
101+
if (board(i)(j) == '*') board(i)(j) = 'O' else if (board(i)(j) == 'O') board(i)(j) = 'X'
102+
}
103+
}
104+
105+
def BFS(i: Int, j: Int, array: Array[Array[Char]]): Unit = {
106+
if (i < 0 || i > array.length - 1 || j < 0 || j > array(0).length - 1) return
107+
if (array(i)(j) == 'O') queue.enqueue((i, j))
108+
while (queue.nonEmpty) {
109+
val position = queue.dequeue()
110+
array(position._1)(position._2) = '*'
111+
if (position._1 > 0 && array(position._1 - 1)(position._2) == 'O' && !queue.contains((position._1 - 1, position._2))) queue.enqueue((position._1 - 1, position._2))
112+
if (position._1 < array.length - 1 && array(position._1 + 1)(position._2) == 'O' && !queue.contains((position._1 + 1, position._2))) queue.enqueue((position._1 + 1, position._2))
113+
if (position._2 > 0 && array(position._1)(position._2 - 1) == 'O' && !queue.contains((position._1, position._2 - 1))) queue.enqueue((position._1, position._2 - 1))
114+
if (position._2 < array(0).length - 1 && array(position._1)(position._2 + 1) == 'O' && !queue.contains((position._1, position._2 + 1))) queue.enqueue((position._1, position._2 + 1))
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)