Skip to content

Commit 4169a25

Browse files
committed
Added July day 25 and 26 problem
1 parent 978ccb1 commit 4169a25

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
import scala.annotation.tailrec
4+
5+
/*
6+
* Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
7+
*
8+
* */
9+
10+
/*
11+
* Input: 38
12+
Output: 2
13+
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
14+
Since 2 has only one digit, return it.
15+
*
16+
* Follow up:
17+
Could you do it without any loop/recursion in O(1) runtime?
18+
*
19+
* */
20+
21+
/*
22+
* Approach: Recursion/Iterative/Mathematical
23+
*
24+
*
25+
* Mathematical approach will be O(1) Time complexity -- Digital Root
26+
*
27+
* For complete explanation visit.
28+
* @link: https://leetcode.com/articles/add-digits/#
29+
*
30+
* */
31+
32+
object AddDigits {
33+
34+
@tailrec
35+
def addDigitsRec(num: Int): Int = {
36+
if (num >= 0 && num <= 9) return num
37+
var N = num
38+
var sum = 0
39+
do {
40+
sum += N % 10
41+
N /= 10
42+
} while (N != 0)
43+
return addDigitsRec(sum)
44+
}
45+
46+
def main(args: Array[String]): Unit = {
47+
val testCases = Array(0, 9, 1234, 27)
48+
val actualResult = testCases.map(a => addDigits(a))
49+
val expectedResult = Array(0, 9, 1, 9)
50+
println(s"Result: ${actualResult.mkString(" ")}, ${actualResult sameElements expectedResult}")
51+
}
52+
53+
def addDigits(num: Int): Int = {
54+
if (num == 0) return 0
55+
else if (num % 9 == 0) return 9
56+
else return num % 9
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.leetcode.scala.monthly2020.july
2+
3+
/*
4+
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5+
* (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
6+
* Find the minimum element.
7+
* The array may contain duplicates.
8+
*
9+
* */
10+
11+
/*
12+
* Approach: Linear/Binary Search
13+
*
14+
* Binary Search will the best approach.
15+
*
16+
* */
17+
18+
object FindMinimuminRotatedSortedArrayII {
19+
def main(args: Array[String]): Unit = {
20+
val nums = Array(4, 5, 6, 6, 1, 2)
21+
val nums1 = Array(1, 2, 3, 4, 5)
22+
var result = findMin(nums)
23+
var result1 = findMin(nums1)
24+
println(s"Result: ${result}, ${result == 1}")
25+
println(s"Result: ${result1}, ${result1 == 1}")
26+
}
27+
28+
//Binary Search
29+
def findMin(nums: Array[Int]): Int = {
30+
var from = 0
31+
var to = nums.length - 1
32+
while (from < to) {
33+
val mid = from + (to - from) / 2
34+
nums(mid) compareTo nums(to) match {
35+
case -1 => to = mid
36+
case 0 => to -= 1
37+
case _ => from = mid + 1
38+
}
39+
}
40+
nums(from)
41+
}
42+
43+
//Linear Search
44+
def findMinLinear(nums: Array[Int]): Int = {
45+
if (nums.isEmpty) return 0
46+
var min = nums.head
47+
var result = nums.head
48+
for (i <- 1 until nums.length) {
49+
if (nums(i) >= result) {
50+
result = nums(i)
51+
} else {
52+
return nums(i)
53+
}
54+
min = min min result
55+
}
56+
min
57+
}
58+
59+
}

0 commit comments

Comments
 (0)