Skip to content

Commit 656ff42

Browse files
authored
Added tasks 188, 190, 191, 192.
1 parent 2b5fcd5 commit 656ff42

File tree

12 files changed

+269
-0
lines changed

12 files changed

+269
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
368368

369369
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
370370
|-|-|-|-|-|-
371+
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
371372

372373
#### Day 3 Conditional Statements
373374

@@ -1057,6 +1058,8 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
10571058

10581059
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10591060
|-|-|-|-|-|-
1061+
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
1062+
| 0190 |[Rotate Array](src/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 198 | 81.82
10601063
| 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
10611064
| 0029 |[Divide Two Integers](src/main/kotlin/g0001_0100/s0029_divide_two_integers/Solution.kt)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 281 | 31.67
10621065

@@ -1381,11 +1384,13 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
13811384

13821385
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13831386
|-|-|-|-|-|-
1387+
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
13841388

13851389
#### Day 14 Bit Manipulation
13861390

13871391
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13881392
|-|-|-|-|-|-
1393+
| 0190 |[Rotate Array](src/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 198 | 81.82
13891394
| 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation | 344 | 83.63
13901395

13911396
### Algorithm II
@@ -1566,7 +1571,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15661571
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph | 252 | 95.41
15671572
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 194 | 92.89
15681573
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming | 156 | 92.24
1574+
| 0192 |[Word Frequency](src/main/kotlin/g0101_0200/s0192_word_frequency/script.sh)| Medium | Shell | 114 | 73.60
1575+
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Algorithm_I_Day_13_Bit_Manipulation, Programming_Skills_I_Day_2_Operator, Udemy_Bit_Manipulation | 237 | 68.44
1576+
| 0190 |[Rotate Array](src/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Bit_Manipulation | 198 | 81.82
15691577
| 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays | 483 | 86.95
1578+
| 0188 |[Best Time to Buy and Sell Stock IV](src/main/kotlin/g0101_0200/s0188_best_time_to_buy_and_sell_stock_iv/Solution.kt)| Hard | Array, Dynamic_Programming | 293 | 68.31
15701579
| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash, Data_Structure_II_Day_9_String, Udemy_Strings | 319 | 79.03
15711580
| 0185 |[Department Top Three Salaries](src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/script.sql)| Hard | Database | 757 | 87.06
15721581
| 0184 |[Department Highest Salary](src/main/kotlin/g0101_0200/s0184_department_highest_salary/script.sql)| Medium | Database | 637 | 76.01
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0188_best_time_to_buy_and_sell_stock_iv
2+
3+
// #Hard #Array #Dynamic_Programming #2022_10_18_Time_293_ms_(68.31%)_Space_34.9_MB_(84.15%)
4+
5+
class Solution {
6+
fun maxProfit(k: Int, prices: IntArray): Int {
7+
val n = prices.size
8+
val dp = IntArray(k + 1)
9+
val maxdp = IntArray(k + 1)
10+
for (i in 0..k) {
11+
maxdp[i] = Int.MIN_VALUE
12+
}
13+
for (i in 1..n) {
14+
maxdp[0] = Math.max(maxdp[0], dp[0] - prices[i - 1])
15+
for (j in k downTo 1) {
16+
maxdp[j] = Math.max(maxdp[j], dp[j] - prices[i - 1])
17+
dp[j] = Math.max(maxdp[j - 1] + prices[i - 1], dp[j])
18+
}
19+
}
20+
return dp[k]
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
188\. Best Time to Buy and Sell Stock IV
2+
3+
Hard
4+
5+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `k`.
6+
7+
Find the maximum profit you can achieve. You may complete at most `k` transactions.
8+
9+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
11+
**Example 1:**
12+
13+
**Input:** k = 2, prices = [2,4,1]
14+
15+
**Output:** 2
16+
17+
**Explanation:** Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
18+
19+
**Example 2:**
20+
21+
**Input:** k = 2, prices = [3,2,6,5,0,3]
22+
23+
**Output:** 7
24+
25+
**Explanation:** Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
26+
27+
**Constraints:**
28+
29+
* `1 <= k <= 100`
30+
* `1 <= prices.length <= 1000`
31+
* `0 <= prices[i] <= 1000`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0101_0200.s0190_reverse_bits
2+
3+
// #Easy #Top_Interview_Questions #Bit_Manipulation #Divide_and_Conquer
4+
// #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Bit_Manipulation
5+
// #2022_10_18_Time_198_ms_(81.82%)_Space_32.8_MB_(73.86%)
6+
7+
class Solution {
8+
// you need treat n as an unsigned value
9+
fun reverseBits(n: Int): Int {
10+
var n = n
11+
var ret = 0
12+
// because there are 32 bits in total
13+
for (i in 0..31) {
14+
ret = ret shl 1
15+
// If the bit is 1 we OR it with 1, ie add 1
16+
if (n and 1 > 0) {
17+
ret = ret or 1
18+
}
19+
n = n ushr 1
20+
}
21+
return ret
22+
}
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
189\. Rotate Array
2+
3+
Medium
4+
5+
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,4,5,6,7], k = 3
10+
11+
**Output:** [5,6,7,1,2,3,4]
12+
13+
**Explanation:**
14+
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
17+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
18+
19+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [-1,-100,3,99], k = 2
24+
25+
**Output:** [3,99,-1,-100]
26+
27+
**Explanation:**
28+
29+
rotate 1 steps to the right: [99,-1,-100,3]
30+
31+
rotate 2 steps to the right: [3,99,-1,-100]
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
37+
* <code>0 <= k <= 10<sup>5</sup></code>
38+
39+
**Follow up:**
40+
41+
* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
42+
* Could you do it in-place with `O(1)` extra space?
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g0101_0200.s0191_number_of_1_bits
2+
3+
// #Easy #Top_Interview_Questions #Bit_Manipulation #Algorithm_I_Day_13_Bit_Manipulation
4+
// #Programming_Skills_I_Day_2_Operator #Udemy_Bit_Manipulation
5+
// #2022_10_18_Time_237_ms_(68.44%)_Space_33.8_MB_(52.46%)
6+
7+
class Solution {
8+
fun hammingWeight(n: Int): Int {
9+
val str = Integer.toBinaryString(n).filter { it == '1' }
10+
return str.length
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
191\. Number of 1 Bits
2+
3+
Easy
4+
5+
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
6+
7+
**Note:**
8+
9+
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
10+
* In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 3**, the input represents the signed integer. `-3`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 00000000000000000000000000001011
15+
16+
**Output:** 3
17+
18+
**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 00000000000000000000000010000000
23+
24+
**Output:** 1
25+
26+
**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.
27+
28+
**Example 3:**
29+
30+
**Input:** n = 11111111111111111111111111111101
31+
32+
**Output:** 31
33+
34+
**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.
35+
36+
**Constraints:**
37+
38+
* The input must be a **binary string** of length `32`.
39+
40+
**Follow up:** If this function is called many times, how would you optimize it?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
192\. Word Frequency
2+
3+
Medium
4+
5+
Write a bash script to calculate the frequency of each word in a text file `words.txt`.
6+
7+
For simplicity sake, you may assume:
8+
9+
* `words.txt` contains only lowercase characters and space `' '` characters.
10+
* Each word must consist of lowercase characters only.
11+
* Words are separated by one or more whitespace characters.
12+
13+
**Example:**
14+
15+
Assume that `words.txt` has the following content:
16+
17+
the day is sunny the the
18+
the sunny is is
19+
20+
Your script should output the following, sorted by descending frequency:
21+
22+
the 4
23+
is 3
24+
sunny 2
25+
day 1
26+
27+
**Note:**
28+
29+
* Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
30+
* Could you write it in one-line using [Unix pipes](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html)?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Read from the file words.txt and output the word frequency list to stdout.
2+
# #Medium #Shell #2022_10_18_Time_114_ms_(73.60%)_Space_3.8_MB_(32.09%)sed -e 's/ /\n/g' words.txt |
3+
sed -e '/^$/d' | sort | uniq -c | sort -r | awk '{print $2" "$1}'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0101_0200.s0188_best_time_to_buy_and_sell_stock_iv
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxProfit() {
10+
assertThat(Solution().maxProfit(2, intArrayOf(2, 4, 1)), equalTo(2))
11+
}
12+
13+
@Test
14+
fun maxProfit2() {
15+
assertThat(Solution().maxProfit(2, intArrayOf(3, 2, 6, 5, 0, 3)), equalTo(7))
16+
}
17+
}

0 commit comments

Comments
 (0)