Skip to content

Commit 1998b2d

Browse files
authored
Added tasks 564, 565, 566, 567
1 parent 3159d01 commit 1998b2d

File tree

13 files changed

+348
-0
lines changed

13 files changed

+348
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
8989

9090
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9191
|-|-|-|-|-|-
92+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix/Solution.kt)| Easy | Array, Matrix, Simulation | 239 | 99.05
9293

9394
#### Day 8 String
9495

@@ -849,6 +850,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
849850

850851
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
851852
|-|-|-|-|-|-
853+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix/Solution.kt)| Easy | Array, Matrix, Simulation | 239 | 99.05
852854
| 0118 |[Pascal's Triangle](src.save/main/kotlin/g0101_0200/s0118_pascals_triangle/Solution.kt)| Easy | Top_Interview_Questions, Array, Dynamic_Programming | 277 | 33.22
853855

854856
#### Day 5 Array
@@ -1112,6 +1114,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
11121114
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11131115
|-|-|-|-|-|-
11141116
| 0003 |[Longest Substring Without Repeating Characters](src.save/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09
1117+
| 0567 |[Permutation in String](src/main/kotlin/g0501_0600/s0567_permutation_in_string/Solution.kt)| Medium | String, Hash_Table, Two_Pointers, Sliding_Window | 169 | 100.00
11151118

11161119
#### Day 7 Breadth First Search Depth First Search
11171120

@@ -1655,6 +1658,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16551658
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16561659
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16571660
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1661+
| 0567 |[Permutation in String](src/main/kotlin/g0501_0600/s0567_permutation_in_string/Solution.kt)| Medium | String, Hash_Table, Two_Pointers, Sliding_Window, Algorithm_I_Day_6_Sliding_Window | 169 | 100.00
1662+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix/Solution.kt)| Easy | Array, Matrix, Simulation, Data_Structure_I_Day_4_Array, Programming_Skills_I_Day_7_Array | 239 | 99.05
1663+
| 0565 |[Array Nesting](src/main/kotlin/g0501_0600/s0565_array_nesting/Solution.kt)| Medium | Array, Depth_First_Search | 553 | 100.00
1664+
| 0564 |[Find the Closest Palindrome](src/main/kotlin/g0501_0600/s0564_find_the_closest_palindrome/Solution.kt)| Hard | String, Math | 179 | 100.00
16581665
| 0563 |[Binary Tree Tilt](src/main/kotlin/g0501_0600/s0563_binary_tree_tilt/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 197 | 100.00
16591666
| 0561 |[Array Partition](src/main/kotlin/g0501_0600/s0561_array_partition_i/Solution.kt)| Easy | Array, Sorting, Greedy, Counting_Sort | 337 | 90.48
16601667
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g0501_0600.s0564_find_the_closest_palindrome
2+
3+
// #Hard #String #Math #2023_01_21_Time_179_ms_(100.00%)_Space_33.7_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun nearestPalindromic(n: String): String {
8+
if (n.length == 1) {
9+
return (n.toInt() - 1).toString()
10+
}
11+
val num = n.toLong()
12+
val offset = Math.pow(10.0, (n.length / 2).toDouble()).toInt()
13+
val first =
14+
if (isPalindrome(n)) palindromeGenerator(num + offset, n.length) else palindromeGenerator(num, n.length)
15+
val second = if (first < num) palindromeGenerator(num + offset, n.length) else palindromeGenerator(
16+
num - offset,
17+
n.length
18+
)
19+
if (first + second == 2 * num) {
20+
return if (first < second) first.toString() else second.toString()
21+
}
22+
return if (Math.abs(num - first) > Math.abs(num - second)) second.toString() else first.toString()
23+
}
24+
25+
private fun palindromeGenerator(num: Long, length: Int): Long {
26+
var num = num
27+
if (num < 10) {
28+
return 9
29+
}
30+
val numOfDigits = num.toString().length
31+
if (numOfDigits > length) {
32+
return Math.pow(10.0, (numOfDigits - 1).toDouble()).toLong() + 1
33+
} else if (numOfDigits < length) {
34+
return Math.pow(10.0, numOfDigits.toDouble()).toLong() - 1
35+
}
36+
num = num - num % Math.pow(10.0, (numOfDigits / 2).toDouble()).toLong()
37+
var temp = num
38+
for (j in 0 until numOfDigits / 2) {
39+
val digit = Math.pow(10.0, (numOfDigits - j - 1).toDouble()).toLong()
40+
num += (temp / digit * Math.pow(10.0, j.toDouble())).toInt().toLong()
41+
temp = temp % digit
42+
}
43+
return num
44+
}
45+
46+
private fun isPalindrome(str: String): Boolean {
47+
for (i in 0 until str.length / 2) {
48+
if (str[i] != str[str.length - 1 - i]) {
49+
return false
50+
}
51+
}
52+
return true
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
564\. Find the Closest Palindrome
2+
3+
Hard
4+
5+
Given a string `n` representing an integer, return _the closest integer (not including itself), which is a palindrome_. If there is a tie, return _**the smaller one**_.
6+
7+
The closest is defined as the absolute difference minimized between two integers.
8+
9+
**Example 1:**
10+
11+
**Input:** n = "123"
12+
13+
**Output:** "121"
14+
15+
**Example 2:**
16+
17+
**Input:** n = "1"
18+
19+
**Output:** "0"
20+
21+
**Explanation:** 0 and 2 are the closest palindromes but we return the smallest which is 0.
22+
23+
**Constraints:**
24+
25+
* `1 <= n.length <= 18`
26+
* `n` consists of only digits.
27+
* `n` does not have leading zeros.
28+
* `n` is representing an integer in the range <code>[1, 10<sup>18</sup> - 1]</code>.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0501_0600.s0565_array_nesting
2+
3+
// #Medium #Array #Depth_First_Search #2023_01_21_Time_553_ms_(100.00%)_Space_52.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun arrayNesting(nums: IntArray): Int {
7+
var index: Int
8+
var value: Int
9+
var maxLen = 0
10+
var len: Int
11+
for (i in nums.indices) {
12+
if (nums[i] != -1) {
13+
index = i
14+
len = 0
15+
while (nums[index] != -1) {
16+
value = nums[index]
17+
nums[index] = -1
18+
index = value
19+
len++
20+
}
21+
maxLen = Math.max(len, maxLen)
22+
}
23+
}
24+
return maxLen
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
565\. Array Nesting
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n` where `nums` is a permutation of the numbers in the range `[0, n - 1]`.
6+
7+
You should build a set `s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }` subjected to the following rule:
8+
9+
* The first element in `s[k]` starts with the selection of the element `nums[k]` of `index = k`.
10+
* The next element in `s[k]` should be `nums[nums[k]]`, and then `nums[nums[nums[k]]]`, and so on.
11+
* We stop adding right before a duplicate element occurs in `s[k]`.
12+
13+
Return _the longest length of a set_ `s[k]`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [5,4,0,3,1,6,2]
18+
19+
**Output:** 4
20+
21+
**Explanation:** nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2. One of the longest sets s[k]: s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [0,1,2]
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
32+
* `0 <= nums[i] < nums.length`
33+
* All the values of `nums` are **unique**.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0501_0600.s0566_reshape_the_matrix
2+
3+
// #Easy #Array #Matrix #Simulation #Data_Structure_I_Day_4_Array #Programming_Skills_I_Day_7_Array
4+
// #2023_01_23_Time_239_ms_(99.05%)_Space_36.7_MB_(99.05%)
5+
6+
class Solution {
7+
fun matrixReshape(mat: Array<IntArray>, r: Int, c: Int): Array<IntArray> {
8+
if (mat.size * mat[0].size != r * c) {
9+
return mat
10+
}
11+
var p = 0
12+
val flatArr = IntArray(mat.size * mat[0].size)
13+
for (ints in mat) {
14+
for (anInt in ints) {
15+
flatArr[p++] = anInt
16+
}
17+
}
18+
val ansMat = Array(r) { IntArray(c) }
19+
var k = 0
20+
for (i in ansMat.indices) {
21+
for (j in ansMat[i].indices) {
22+
ansMat[i][j] = flatArr[k++]
23+
}
24+
}
25+
return ansMat
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
566\. Reshape the Matrix
2+
3+
Easy
4+
5+
In MATLAB, there is a handy function called `reshape` which can reshape an `m x n` matrix into a new one with a different size `r x c` keeping its original data.
6+
7+
You are given an `m x n` matrix `mat` and two integers `r` and `c` representing the number of rows and the number of columns of the wanted reshaped matrix.
8+
9+
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
10+
11+
If the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/04/24/reshape1-grid.jpg)
16+
17+
**Input:** mat = [[1,2],[3,4]], r = 1, c = 4
18+
19+
**Output:** [[1,2,3,4]]
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/04/24/reshape2-grid.jpg)
24+
25+
**Input:** mat = [[1,2],[3,4]], r = 2, c = 4
26+
27+
**Output:** [[1,2],[3,4]]
28+
29+
**Constraints:**
30+
31+
* `m == mat.length`
32+
* `n == mat[i].length`
33+
* `1 <= m, n <= 100`
34+
* `-1000 <= mat[i][j] <= 1000`
35+
* `1 <= r, c <= 300`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0501_0600.s0567_permutation_in_string
2+
3+
// #Medium #String #Hash_Table #Two_Pointers #Sliding_Window #Algorithm_I_Day_6_Sliding_Window
4+
// #2023_01_23_Time_169_ms_(100.00%)_Space_35.6_MB_(85.86%)
5+
6+
class Solution {
7+
fun checkInclusion(s1: String, s2: String): Boolean {
8+
val n = s1.length
9+
val m = s2.length
10+
if (n > m) {
11+
return false
12+
}
13+
val cntS1 = IntArray(26)
14+
val cntS2 = IntArray(26)
15+
for (i in 0 until n) {
16+
cntS1[s1[i].code - 'a'.code]++
17+
}
18+
for (i in 0 until n) {
19+
cntS2[s2[i].code - 'a'.code]++
20+
}
21+
if (check(cntS1, cntS2)) {
22+
return true
23+
}
24+
for (i in n until m) {
25+
cntS2[s2[i - n].code - 'a'.code]--
26+
cntS2[s2[i].code - 'a'.code]++
27+
if (check(cntS1, cntS2)) {
28+
return true
29+
}
30+
}
31+
return false
32+
}
33+
34+
private fun check(cnt1: IntArray, cnt2: IntArray): Boolean {
35+
for (i in 0..25) {
36+
if (cnt1[i] != cnt2[i]) {
37+
return false
38+
}
39+
}
40+
return true
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
567\. Permutation in String
2+
3+
Medium
4+
5+
Given two strings `s1` and `s2`, return `true` _if_ `s2` _contains a permutation of_ `s1`_, or_ `false` _otherwise_.
6+
7+
In other words, return `true` if one of `s1`'s permutations is the substring of `s2`.
8+
9+
**Example 1:**
10+
11+
**Input:** s1 = "ab", s2 = "eidbaooo"
12+
13+
**Output:** true
14+
15+
**Explanation:** s2 contains one permutation of s1 ("ba").
16+
17+
**Example 2:**
18+
19+
**Input:** s1 = "ab", s2 = "eidboaoo"
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* <code>1 <= s1.length, s2.length <= 10<sup>4</sup></code>
26+
* `s1` and `s2` consist of lowercase English letters.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0564_find_the_closest_palindrome
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 nearestPalindromic() {
10+
assertThat(Solution().nearestPalindromic("123"), equalTo("121"))
11+
}
12+
13+
@Test
14+
fun nearestPalindromic2() {
15+
assertThat(Solution().nearestPalindromic("1"), equalTo("0"))
16+
}
17+
}

0 commit comments

Comments
 (0)