Skip to content

Commit 71cc97e

Browse files
committed
July challenges - Day 1 - 6
1 parent b73ee73 commit 71cc97e

File tree

6 files changed

+423
-0
lines changed

6 files changed

+423
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
/*
4+
* You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
5+
* Given n, find the total number of full staircase rows that can be formed.
6+
* n is a non-negative integer and fits within the range of a 32-bit signed integer.
7+
*
8+
* */
9+
10+
/*
11+
* n = 5
12+
13+
The coins can form the following rows:
14+
¤
15+
¤ ¤
16+
¤ ¤
17+
18+
Because the 3rd row is incomplete, we return 2.
19+
*
20+
* n = 8
21+
22+
The coins can form the following rows:
23+
¤
24+
¤ ¤
25+
¤ ¤ ¤
26+
¤ ¤
27+
28+
Because the 4th row is incomplete, we return 3.
29+
*
30+
* */
31+
32+
/*
33+
* Approach: Binary Search/Mathematical
34+
*
35+
*
36+
* Mathematical Approach:
37+
* @link: https://leetcode.com/articles/arranging-coins/#
38+
* Time Complexity: O(1)
39+
* Space Complexity: O(1)
40+
*
41+
* */
42+
43+
44+
object ArrangingCoins {
45+
46+
//Matematical Approach
47+
def arrangeCoinsMath(n: Int): Int = {
48+
return math.sqrt((2 * n.toLong + 0.25) - 0.5).toInt
49+
}
50+
51+
def main(args: Array[String]): Unit = {
52+
val n = 8
53+
val result = arrangeCoins(n)
54+
println(s"Result: ${result}, ${result == 3}")
55+
}
56+
57+
def arrangeCoins(n: Int): Int = {
58+
var current = n
59+
var result = 0
60+
while (current > 0 && current > result) {
61+
result += 1
62+
current -= result
63+
}
64+
return result
65+
}
66+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
import com.leetcode.scala.monthly2020.june.TreeNode
4+
5+
import scala.collection.mutable
6+
7+
8+
/*
9+
* Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
10+
*
11+
* */
12+
13+
/*
14+
* For example:
15+
Given binary tree [3,9,20,null,null,15,7],
16+
*
17+
* 3
18+
/ \
19+
9 20
20+
/ \
21+
15 7
22+
*
23+
* return its bottom-up level order traversal as:
24+
* [
25+
[15,7],
26+
[9,20],
27+
[3]
28+
]
29+
*
30+
* */
31+
32+
/*
33+
* Approach: BFS - Breadth First Search
34+
*
35+
* 1. We have to Build Queue of Queue[TreeNode]
36+
* 2. We will instantiate the Queue with Queue[root node]
37+
* 3. We will dequeue the outer queue first and then we will have queue[TreeNode].
38+
* 4. For each Queue in the outer queue we are instantiating an empty queue to store all the child nodes and an empty list to store all the Integer values from the TreeNode.
39+
* 5. We will dequeue the inner queue and have to add the integer value to the list and we have to add the child node to the inner queue.
40+
* 6. Once the currentElement becomes empty we will check if the new Queue which we created is empty. If it's nonEmpty we will add it to the outer Queue and the list to the result. Iteration will continue until the outer queue nonEmpty.
41+
* 7. Finally we will return the result.
42+
*
43+
* */
44+
45+
46+
object BinaryTreeLevelOrderTraversalII {
47+
48+
def main(args: Array[String]): Unit = {
49+
val tree: TreeNode = new TreeNode(3, new TreeNode(9, new TreeNode(10)), new TreeNode(20, new TreeNode(15), new TreeNode(7)))
50+
println(tree)
51+
println(levelOrderBottom(tree))
52+
}
53+
54+
def levelOrderBottom(root: TreeNode): List[List[Int]] = {
55+
var result: mutable.Stack[List[Int]] = mutable.Stack()
56+
if (root == null) return List()
57+
var queue: mutable.Queue[mutable.Queue[TreeNode]] = mutable.Queue()
58+
queue.enqueue(mutable.Queue(root))
59+
while (queue.nonEmpty) {
60+
val currentElement = queue.dequeue()
61+
var queue1: mutable.Queue[TreeNode] = mutable.Queue()
62+
var list: List[Int] = List()
63+
while (currentElement.nonEmpty) {
64+
val currentTree = currentElement.dequeue()
65+
val value = currentTree.value
66+
if (currentTree.left != null) queue1.enqueue(currentTree.left)
67+
if (currentTree.right != null) queue1.enqueue(currentTree.right)
68+
list = list :+ value
69+
}
70+
result.push(list)
71+
if (queue1.nonEmpty) queue.enqueue(queue1)
72+
}
73+
return result.toList
74+
}
75+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
/*
4+
* The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
5+
* Given two integers x and y, calculate the Hamming distance.
6+
*
7+
* */
8+
9+
/*
10+
* Input: x = 1, y = 4
11+
* Output: 2
12+
13+
Explanation:
14+
1 (0 0 0 1)
15+
4 (0 1 0 0)
16+
↑ ↑
17+
18+
The above arrows point to positions where the corresponding bits are different.
19+
*
20+
* */
21+
22+
/*
23+
* Approach: Bit Manuplation
24+
*
25+
* 1 ^ 4 will give 0101
26+
* we can count 1's from the binary number to get the bit count.
27+
*
28+
* */
29+
30+
31+
object HammingDistance {
32+
33+
//Using In-built Functions
34+
def hammingDistanceInBuilts(x: Int, y: Int): Int = {
35+
Integer.bitCount(x ^ y)
36+
}
37+
38+
def main(args: Array[String]): Unit = {
39+
val x = 1
40+
val y = 4
41+
val result = hammingDistance(x, y)
42+
println(s"Result: ${result}, ${result == 2}")
43+
}
44+
45+
//Counting from the binary string
46+
def hammingDistance(x: Int, y: Int): Int = {
47+
(x ^ y).toBinaryString.count(a => a == '1')
48+
}
49+
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
/*
4+
* Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
5+
* The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
6+
* You may assume the integer does not contain any leading zero, except the number 0 itself.
7+
*
8+
* */
9+
10+
/*
11+
* Input: [1,2,3]
12+
Output: [1,2,4]
13+
Explanation: The array represents the integer 123.
14+
*
15+
* Input: [4,3,2,1]
16+
Output: [4,3,2,2]
17+
Explanation: The array represents the integer 4321.
18+
*
19+
* */
20+
21+
/*
22+
* Approach: Brute-Force
23+
* Time Complexity: O(n), Best Case: O(1), If the last digit is less than 9
24+
*
25+
* 1. First we will check if the last digit value is less than 9. If yes we will add 1 to the last digit and we will return the result.
26+
* 2. If last digit is 9 then we have to iterate through the array in reverse order and we have to check if the current element is less than 9 if yes we will add 1 and we will return the result.
27+
* else: we need to change the current element to 0 and we have to add 1 to next element.
28+
* if the first element of the array is 9 then we have to concat a new Array(1) ++ (0,0) eg: Input == [9,9]
29+
*
30+
* */
31+
32+
object PlusOne {
33+
def main(args: Array[String]): Unit = {
34+
val nums = Array(8, 9)
35+
val nums1 = Array(9, 9)
36+
val result = plusOne(nums)
37+
val result1 = plusOne(nums1)
38+
println(s"Result: [${result.mkString(", ")}], ${result.mkString(",") == "9,0"}")
39+
println(s"Result: [${result1.mkString(", ")}], ${result1.mkString(",") == "1,0,0"}")
40+
}
41+
42+
def plusOne(digits: Array[Int]): Array[Int] = {
43+
if (digits.isEmpty) return digits
44+
var result = Array(digits: _*)
45+
if (result.last < 9) {
46+
result(result.length - 1) += 1
47+
} else {
48+
var i = result.length - 1
49+
while (i >= 0) {
50+
if (i != 0 && result(i) == 9) {
51+
result(i) = 0
52+
} else if (result.length != 1 && result(i) != 9) {
53+
result(i) += 1
54+
return result
55+
}
56+
if (i == 0 && digits(0) == 9) {
57+
result(0) = 0
58+
result = Array(1) ++ result
59+
}
60+
i -= 1
61+
}
62+
63+
}
64+
return result
65+
}
66+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
import scala.collection.mutable
4+
5+
/*
6+
* There are 8 prison cells in a row, and each cell is either occupied or vacant.
7+
* Each day, whether the cell is occupied or vacant changes according to the following rules:
8+
* If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
9+
* Otherwise, it becomes vacant.
10+
* (Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
11+
*
12+
* We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
13+
* Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
14+
*
15+
* */
16+
17+
/*
18+
* Input: cells = [0,1,0,1,1,0,0,1], N = 7
19+
Output: [0,0,1,1,0,0,0,0]
20+
Explanation:
21+
The following table summarizes the state of the prison on each day:
22+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
23+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
24+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
25+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
26+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
27+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
28+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
29+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
30+
*
31+
* Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
32+
Output: [0,0,1,1,1,1,1,0]
33+
*
34+
* Note:
35+
* cells.length == 8
36+
* cells[i] is in {0, 1}
37+
* 1 <= N <= 10^9
38+
*
39+
* */
40+
41+
42+
/*
43+
* Approach: Memoization
44+
*
45+
* We can implement this program using brute force but when ever the input increase it will take too much time to compute the result.
46+
* Inorder to overcome the above issue we need to store the result in the map and when ever we see the same key/cycle in the map
47+
* we will get the n value for the key and we need to compute and modify the n.
48+
*
49+
*
50+
* */
51+
52+
object PrisonCellsAfterNDays {
53+
def main(args: Array[String]): Unit = {
54+
val input = Array(1, 0, 0, 1, 0, 0, 1, 0)
55+
val n = 1000000000
56+
val result = prisonAfterNDays(input, n)
57+
println(s"Result: [${result.mkString(", ")}], ${result.mkString(",") == "0,0,1,1,1,1,1,0"}")
58+
}
59+
60+
def prisonAfterNDays(cells: Array[Int], N: Int): Array[Int] = {
61+
var n: Int = N
62+
var map: mutable.HashMap[String, Int] = mutable.HashMap()
63+
var currDay: Array[Int] = Array(cells: _*)
64+
while (n > 0) {
65+
var modified: Array[Int] = Array(currDay: _*)
66+
map += (currDay.mkString -> n)
67+
for (i <- cells.indices) {
68+
if ((i == 0 || i == cells.length - 1) && currDay(i) != 0) {
69+
modified(i) = 0
70+
} else if (i >= 1 && i <= cells.length - 2) {
71+
val prev = currDay(i - 1)
72+
val next = currDay(i + 1)
73+
if (prev == next) {
74+
modified(i) = 1
75+
} else {
76+
modified(i) = 0
77+
}
78+
}
79+
}
80+
currDay = Array(modified: _*)
81+
n -= 1
82+
if (map.contains(currDay.mkString)) {
83+
n = n % (map(currDay.mkString) - n)
84+
}
85+
}
86+
return currDay
87+
}
88+
}

0 commit comments

Comments
 (0)