Skip to content

Commit f7fcc1f

Browse files
authored
Added tasks 371, 372, 373, 374.
1 parent e527f2f commit f7fcc1f

File tree

14 files changed

+447
-0
lines changed

14 files changed

+447
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
10291029
| 0191 |[Number of 1 Bits](src.save/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
10301030
| 0190 |[Reverse Bits](src.save/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 198 | 81.82
10311031
| 0338 |[Counting Bits](src/main/kotlin/g0301_0400/s0338_counting_bits/Solution.kt)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation | 186 | 99.26
1032+
| 0371 |[Sum of Two Integers](src/main/kotlin/g0301_0400/s0371_sum_of_two_integers/Solution.kt)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 129 | 95.45
10321033
| 0029 |[Divide Two Integers](src.save/main/kotlin/g0001_0100/s0029_divide_two_integers/Solution.kt)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 281 | 31.67
10331034

10341035
#### Udemy Design
@@ -1522,6 +1523,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
15221523

15231524
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15241525
|-|-|-|-|-|-
1526+
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/GuessGame.kt)| |||
15251527

15261528
#### Day 2
15271529

@@ -1606,6 +1608,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16061608
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
16071609
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
16081610
| 0378 |[Kth Smallest Element in a Sorted Matrix](src/main/kotlin/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/Solution.kt)| Medium | Top_Interview_Questions, Array, Sorting, Binary_Search, Matrix, Heap_Priority_Queue | 522 | 59.78
1611+
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/GuessGame.kt)| |||
1612+
| 0373 |[Find K Pairs with Smallest Sums](src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/Solution.kt)| Medium | Array, Heap_Priority_Queue | 1809 | 80.95
1613+
| 0372 |[Super Pow](src/main/kotlin/g0301_0400/s0372_super_pow/Solution.kt)| Medium | Math, Divide_and_Conquer | 196 | 100.00
1614+
| 0371 |[Sum of Two Integers](src/main/kotlin/g0301_0400/s0371_sum_of_two_integers/Solution.kt)| Medium | Top_Interview_Questions, Math, Bit_Manipulation, Udemy_Bit_Manipulation | 129 | 95.45
16091615
| 0368 |[Largest Divisible Subset](src/main/kotlin/g0301_0400/s0368_largest_divisible_subset/Solution.kt)| Medium | Array, Dynamic_Programming, Math, Sorting | 412 | 73.33
16101616
| 0367 |[Valid Perfect Square](src/main/kotlin/g0301_0400/s0367_valid_perfect_square/Solution.kt)| Easy | Math, Binary_Search, Binary_Search_I_Day_3 | 137 | 94.55
16111617
| 0365 |[Water and Jug Problem](src/main/kotlin/g0301_0400/s0365_water_and_jug_problem/Solution.kt)| Medium | Math, Depth_First_Search, Breadth_First_Search, Graph_Theory_I_Day_11_Breadth_First_Search | 130 | 100.00
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0301_0400.s0371_sum_of_two_integers
2+
3+
// #Medium #Top_Interview_Questions #Math #Bit_Manipulation #Udemy_Bit_Manipulation
4+
// #2022_11_22_Time_129_ms_(95.45%)_Space_32.9_MB_(90.91%)
5+
6+
class Solution {
7+
fun getSum(a: Int, b: Int): Int {
8+
var a = a
9+
var b = b
10+
var ans = 0
11+
var memo = 0
12+
var exp = 0
13+
var count = 0
14+
while (count < 32) {
15+
val val1 = a and 1
16+
val val2 = b and 1
17+
var `val` = sum(val1, val2, memo)
18+
memo = `val` shr 1
19+
`val` = `val` and 1
20+
a = a shr 1
21+
b = b shr 1
22+
ans = ans or (`val` shl exp)
23+
exp = plusOne(exp)
24+
count = plusOne(count)
25+
}
26+
return ans
27+
}
28+
29+
private fun sum(val1: Int, val2: Int, val3: Int): Int {
30+
var count = 0
31+
if (val1 == 1) {
32+
count = plusOne(count)
33+
}
34+
if (val2 == 1) {
35+
count = plusOne(count)
36+
}
37+
if (val3 == 1) {
38+
count = plusOne(count)
39+
}
40+
return count
41+
}
42+
43+
private fun plusOne(`val`: Int): Int {
44+
return -`val`.inv()
45+
}
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
371\. Sum of Two Integers
2+
3+
Medium
4+
5+
Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
6+
7+
**Example 1:**
8+
9+
**Input:** a = 1, b = 2
10+
11+
**Output:** 3
12+
13+
**Example 2:**
14+
15+
**Input:** a = 2, b = 3
16+
17+
**Output:** 5
18+
19+
**Constraints:**
20+
21+
* `-1000 <= a, b <= 1000`
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package g0301_0400.s0372_super_pow
2+
3+
// #Medium #Math #Divide_and_Conquer #2022_11_22_Time_196_ms_(100.00%)_Space_36.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun superPow(a: Int, b: IntArray): Int {
7+
val phi = phi(MOD)
8+
val arrMod = arrMod(b, phi)
9+
return if (isGreaterOrEqual(b, phi)) {
10+
// Cycle has started
11+
// cycle starts at phi with length phi
12+
exp(a % MOD, phi + arrMod)
13+
} else exp(a % MOD, arrMod)
14+
}
15+
16+
private fun phi(n: Int): Int {
17+
var n = n
18+
var result = n.toDouble()
19+
var p = 2
20+
while (p * p <= n) {
21+
if (n % p > 0) {
22+
p++
23+
continue
24+
}
25+
while (n % p == 0) {
26+
n /= p
27+
}
28+
result *= 1.0 - 1.0 / p
29+
p++
30+
}
31+
if (n > 1) {
32+
// if starting n was also prime (so it was greater than sqrt(n))
33+
result *= 1.0 - 1.0 / n
34+
}
35+
return result.toInt()
36+
}
37+
38+
// Returns true if number in array is greater than integer named phi
39+
private fun isGreaterOrEqual(b: IntArray, phi: Int): Boolean {
40+
var cur = 0
41+
for (j in b) {
42+
cur = cur * 10 + j
43+
if (cur >= phi) {
44+
return true
45+
}
46+
}
47+
return false
48+
}
49+
50+
// Returns number in array mod phi
51+
private fun arrMod(b: IntArray, phi: Int): Int {
52+
var res = 0
53+
for (j in b) {
54+
res = (res * 10 + j) % phi
55+
}
56+
return res
57+
}
58+
59+
// Binary exponentiation
60+
private fun exp(a: Int, b: Int): Int {
61+
var a = a
62+
var b = b
63+
var y = 1
64+
while (b > 0) {
65+
if (b % 2 == 1) {
66+
y = y * a % MOD
67+
}
68+
a = a * a % MOD
69+
b /= 2
70+
}
71+
return y
72+
}
73+
74+
companion object {
75+
private const val MOD = 1337
76+
}
77+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
372\. Super Pow
2+
3+
Medium
4+
5+
Your task is to calculate <code>a<sup>b</sup></code> mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
6+
7+
**Example 1:**
8+
9+
**Input:** a = 2, b = [3]
10+
11+
**Output:** 8
12+
13+
**Example 2:**
14+
15+
**Input:** a = 2, b = [1,0]
16+
17+
**Output:** 1024
18+
19+
**Example 3:**
20+
21+
**Input:** a = 1, b = [4,3,3,8,5,2]
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* <code>1 <= a <= 2<sup>31</sup> - 1</code>
28+
* `1 <= b.length <= 2000`
29+
* `0 <= b[i] <= 9`
30+
* `b` does not contain leading zeros.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0301_0400.s0373_find_k_pairs_with_smallest_sums
2+
3+
import java.util.PriorityQueue
4+
import kotlin.collections.ArrayList
5+
6+
// #Medium #Array #Heap_Priority_Queue #2022_11_22_Time_1809_ms_(80.95%)_Space_119.1_MB_(66.67%)
7+
8+
class Solution {
9+
private class Node(index: Int, num1: Int, num2: Int) {
10+
var sum: Long
11+
var al: MutableList<Int>
12+
var index: Int
13+
14+
init {
15+
sum = num1.toLong() + num2.toLong()
16+
al = ArrayList()
17+
al.add(num1)
18+
al.add(num2)
19+
this.index = index
20+
}
21+
}
22+
23+
fun kSmallestPairs(nums1: IntArray, nums2: IntArray, k: Int): List<List<Int>> {
24+
val queue = PriorityQueue { a: Node, b: Node -> if (a.sum < b.sum) -1 else 1 }
25+
val res: MutableList<List<Int>> = ArrayList()
26+
run {
27+
var i = 0
28+
while (i < nums1.size && i < k) {
29+
queue.add(Node(0, nums1[i], nums2[0]))
30+
i++
31+
}
32+
}
33+
var i = 1
34+
while (i <= k && !queue.isEmpty()) {
35+
val cur = queue.poll()
36+
res.add(cur.al)
37+
val next = cur.index
38+
val lastNum1 = cur.al[0]
39+
if (next + 1 < nums2.size) {
40+
queue.add(Node(next + 1, lastNum1, nums2[next + 1]))
41+
}
42+
i++
43+
}
44+
return res
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
373\. Find K Pairs with Smallest Sums
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
6+
7+
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
8+
9+
Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> _with the smallest sums_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,7,11], nums2 = [2,4,6], k = 3
14+
15+
**Output:** [[1,2],[1,4],[1,6]]
16+
17+
**Explanation:** The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1,1,2], nums2 = [1,2,3], k = 2
22+
23+
**Output:** [[1,1],[1,1]]
24+
25+
**Explanation:** The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [1,2], nums2 = [3], k = 3
30+
31+
**Output:** [[1,3],[2,3]]
32+
33+
**Explanation:** All possible pairs are returned from the sequence: [1,3],[2,3]
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
39+
* `nums1` and `nums2` both are sorted in **ascending order**.
40+
* <code>1 <= k <= 10<sup>4</sup></code>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package g0301_0400.s0374_guess_number_higher_or_lower
2+
3+
open class GuessGame {
4+
fun guess(number: Int): Int {
5+
return Integer.compare(7, number)
6+
}
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0301_0400.s0374_guess_number_higher_or_lower
2+
3+
// #Easy #Binary_Search #Interactive #Binary_Search_I_Day_1
4+
// #2022_11_22_Time_134_ms_(94.19%)_Space_32.6_MB_(98.60%)
5+
6+
/*
7+
* The API guess is defined in the parent class.
8+
* @param num your guess
9+
* @return -1 if num is higher than the picked number
10+
* 1 if num is lower than the picked number
11+
* otherwise return 0
12+
* fun guess(num:Int):Int {}
13+
*/
14+
15+
class Solution : GuessGame() {
16+
fun guessNumber(n: Int): Int {
17+
var start = 0
18+
var end = n
19+
while (start <= end) {
20+
val mid = start + (end - start) / 2
21+
if (guess(mid) == 0) {
22+
return mid
23+
} else if (guess(mid) == -1) {
24+
end = mid - 1
25+
} else {
26+
start = mid + 1
27+
}
28+
}
29+
return -1
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
374\. Guess Number Higher or Lower
2+
3+
Easy
4+
5+
We are playing the Guess Game. The game is as follows:
6+
7+
I pick a number from `1` to `n`. You have to guess which number I picked.
8+
9+
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
10+
11+
You call a pre-defined API `int guess(int num)`, which returns three possible results:
12+
13+
* `-1`: Your guess is higher than the number I picked (i.e. `num > pick`).
14+
* `1`: Your guess is lower than the number I picked (i.e. `num < pick`).
15+
* `0`: your guess is equal to the number I picked (i.e. `num == pick`).
16+
17+
Return _the number that I picked_.
18+
19+
**Example 1:**
20+
21+
**Input:** n = 10, pick = 6
22+
23+
**Output:** 6
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1, pick = 1
28+
29+
**Output:** 1
30+
31+
**Example 3:**
32+
33+
**Input:** n = 2, pick = 1
34+
35+
**Output:** 1
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
40+
* `1 <= pick <= n`

0 commit comments

Comments
 (0)