Skip to content

Commit bc38c70

Browse files
authored
Added tasks 786, 787, 788.
1 parent a18bbf3 commit bc38c70

File tree

9 files changed

+339
-0
lines changed

9 files changed

+339
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g0701_0800.s0786_k_th_smallest_prime_fraction;
2+
3+
// #Hard #Array #Binary_Search #Heap_Priority_Queue
4+
5+
public class Solution {
6+
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
7+
int n = arr.length;
8+
double low = 0;
9+
double high = 1.0;
10+
while (low < high) {
11+
double mid = (low + high) / 2;
12+
int[] res = getFractionsLessThanMid(arr, n, mid);
13+
if (res[0] == k) {
14+
return new int[] {arr[res[1]], arr[res[2]]};
15+
} else if (res[0] > k) {
16+
high = mid;
17+
} else {
18+
low = mid;
19+
}
20+
}
21+
return new int[] {};
22+
}
23+
24+
private int[] getFractionsLessThanMid(int[] arr, int n, double mid) {
25+
double maxLessThanMid = 0.0;
26+
// stores indices of max fraction less than mid;
27+
int x = 0;
28+
int y = 0;
29+
// for storing fractions less than mid
30+
int total = 0;
31+
int j = 1;
32+
for (int i = 0; i < n - 1; i++) {
33+
// while fraction is greater than mid increment j
34+
while (j < n && arr[i] > arr[j] * mid) {
35+
j++;
36+
}
37+
if (j == n) {
38+
break;
39+
}
40+
// j fractions greater than mid, n-j fractions smaller than mid, add fractions smaller
41+
// than mid to total
42+
total += (n - j);
43+
double fraction = (double) arr[i] / arr[j];
44+
if (fraction > maxLessThanMid) {
45+
maxLessThanMid = fraction;
46+
x = i;
47+
y = j;
48+
}
49+
}
50+
return new int[] {total, x, y};
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
786\. K-th Smallest Prime Fraction
2+
3+
Hard
4+
5+
You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
6+
7+
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
8+
9+
Return _the_ <code>k<sup>th</sup></code> _smallest fraction considered_. Return your answer as an array of integers of size `2`, where `answer[0] == arr[i]` and `answer[1] == arr[j]`.
10+
11+
**Example 1:**
12+
13+
**Input:** arr = [1,2,3,5], k = 3
14+
15+
**Output:** [2,5]
16+
17+
**Explanation:**
18+
19+
The fractions to be considered in sorted order are:
20+
1/5, 1/3, 2/5, 1/2, 3/5, and 2/3.
21+
The third fraction is 2/5.
22+
23+
**Example 2:**
24+
25+
**Input:** arr = [1,7], k = 1
26+
27+
**Output:** [1,7]
28+
29+
**Constraints:**
30+
31+
* `2 <= arr.length <= 1000`
32+
* <code>1 <= arr[i] <= 3 * 10<sup>4</sup></code>
33+
* `arr[0] == 1`
34+
* `arr[i]` is a **prime** number for `i > 0`.
35+
* All the numbers of `arr` are **unique** and sorted in **strictly increasing** order.
36+
* `1 <= k <= arr.length * (arr.length - 1) / 2`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0701_0800.s0787_cheapest_flights_within_k_stops;
2+
3+
// #Medium #Dynamic_Programming #Depth_First_Search #Breadth_First_Search #Heap_Priority_Queue
4+
// #Graph #Shortest_Path
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
10+
// k + 2 becase there are total of k(intermediate stops) + 1(src) + 1(dst)
11+
// dp[i][j] = cost to reach j using atmost i edges from src
12+
int[][] dp = new int[k + 2][n];
13+
for (int[] row : dp) {
14+
Arrays.fill(row, Integer.MAX_VALUE);
15+
}
16+
// cost to reach src is always 0
17+
for (int i = 0; i <= k + 1; i++) {
18+
dp[i][src] = 0;
19+
}
20+
// k+1 because k stops + dst
21+
for (int i = 1; i <= k + 1; i++) {
22+
for (int[] flight : flights) {
23+
int srcAirport = flight[0];
24+
int destAirport = flight[1];
25+
int cost = flight[2];
26+
// if cost to reach srcAirport in i - 1 steps is already found out then
27+
// the cost to reach destAirport will be min(cost to reach destAirport computed
28+
// already from some other srcAirport OR cost to reach srcAirport in i - 1 steps +
29+
// the cost to reach destAirport from srcAirport)
30+
if (dp[i - 1][srcAirport] != Integer.MAX_VALUE) {
31+
dp[i][destAirport] = Math.min(dp[i][destAirport], dp[i - 1][srcAirport] + cost);
32+
}
33+
}
34+
}
35+
// checking for dp[k + 1][dst] because there are 'k + 2' airports in a path and distance
36+
// covered between 'k + 2' airports is 'k + 1'
37+
return dp[k + 1][dst] == Integer.MAX_VALUE ? -1 : dp[k + 1][dst];
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
787\. Cheapest Flights Within K Stops
2+
3+
Medium
4+
5+
There are `n` cities connected by some number of flights. You are given an array `flights` where <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a flight from city <code>from<sub>i</sub></code> to city <code>to<sub>i</sub></code> with cost <code>price<sub>i</sub></code>.
6+
7+
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _to_ `dst` _with at most_ `k` _stops._ If there is no such route, return `-1`.
8+
9+
**Example 1:**
10+
11+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png)
12+
13+
**Input:** n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
14+
15+
**Output:** 200
16+
17+
**Explanation:**
18+
19+
The graph is shown.
20+
21+
The cheapest price from city `0` to city `2` with at most 1 stop costs 200, as marked red in the picture.
22+
23+
**Example 2:**
24+
25+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png)
26+
27+
**Input:** n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
28+
29+
**Output:** 500
30+
31+
**Explanation:**
32+
33+
The graph is shown.
34+
35+
The cheapest price from city `0` to city `2` with at most 0 stop costs 500, as marked blue in the picture.
36+
37+
**Constraints:**
38+
39+
* `1 <= n <= 100`
40+
* `0 <= flights.length <= (n * (n - 1) / 2)`
41+
* `flights[i].length == 3`
42+
* <code>0 <= from<sub>i</sub>, to<sub>i</sub> < n</code>
43+
* <code>from<sub>i</sub> != to<sub>i</sub></code>
44+
* <code>1 <= price<sub>i</sub> <= 10<sup>4</sup></code>
45+
* There will not be any multiple flights between two cities.
46+
* `0 <= src, dst, k < n`
47+
* `src != dst`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0701_0800.s0788_rotated_digits;
2+
3+
// #Medium #Dynamic_Programming #Math
4+
5+
public class Solution {
6+
public int rotatedDigits(int n) {
7+
int[] flag = new int[n + 1];
8+
flag[0] = 2;
9+
if (n >= 1) {
10+
flag[1] = 2;
11+
}
12+
if (n >= 8) {
13+
flag[8] = 2;
14+
}
15+
if (n >= 2) {
16+
flag[2] = 1;
17+
}
18+
if (n >= 5) {
19+
flag[5] = 1;
20+
}
21+
if (n >= 6) {
22+
flag[6] = 1;
23+
}
24+
if (n >= 9) {
25+
flag[9] = 1;
26+
}
27+
int rs = 0;
28+
for (int i = 1; i <= n; i++) {
29+
int residual = i % 10;
30+
if (flag[residual] != 0) {
31+
if ((residual == 1 || residual == 0 || residual == 8) && (flag[i / 10] == 2)) {
32+
flag[i] = 2;
33+
continue;
34+
}
35+
if (flag[i / 10] != 0) {
36+
flag[i] = 1;
37+
rs++;
38+
}
39+
}
40+
}
41+
return rs;
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
788\. Rotated Digits
2+
3+
Medium
4+
5+
An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
6+
7+
A number is valid if each digit remains a digit after rotation. For example:
8+
9+
* `0`, `1`, and `8` rotate to themselves,
10+
* `2` and `5` rotate to each other (in this case they are rotated in a different direction, in other words, `2` or `5` gets mirrored),
11+
* `6` and `9` rotate to each other, and
12+
* the rest of the numbers do not rotate to any other number and become invalid.
13+
14+
Given an integer `n`, return _the number of **good** integers in the range_ `[1, n]`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 10
19+
20+
**Output:** 4
21+
22+
**Explanation:**
23+
24+
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
25+
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 1
30+
31+
**Output:** 0
32+
33+
**Example 3:**
34+
35+
**Input:** n = 2
36+
37+
**Output:** 1
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0701_0800.s0786_k_th_smallest_prime_fraction;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void kthSmallestPrimeFraction() {
11+
assertThat(
12+
new Solution().kthSmallestPrimeFraction(new int[] {1, 2, 3, 5}, 3),
13+
equalTo(new int[] {2, 5}));
14+
}
15+
16+
@Test
17+
void kthSmallestPrimeFraction2() {
18+
assertThat(
19+
new Solution().kthSmallestPrimeFraction(new int[] {1, 7}, 1),
20+
equalTo(new int[] {1, 7}));
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0701_0800.s0787_cheapest_flights_within_k_stops;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findCheapestPrice() {
11+
assertThat(
12+
new Solution()
13+
.findCheapestPrice(
14+
3, new int[][] {{0, 1, 100}, {1, 2, 100}, {0, 2, 500}}, 0, 2, 1),
15+
equalTo(200));
16+
}
17+
18+
@Test
19+
void findCheapestPrice2() {
20+
assertThat(
21+
new Solution()
22+
.findCheapestPrice(
23+
3, new int[][] {{0, 1, 100}, {1, 2, 100}, {0, 2, 500}}, 0, 2, 0),
24+
equalTo(500));
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0701_0800.s0788_rotated_digits;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void rotatedDigits() {
11+
assertThat(new Solution().rotatedDigits(10), equalTo(4));
12+
}
13+
14+
@Test
15+
void rotatedDigits2() {
16+
assertThat(new Solution().rotatedDigits(1), equalTo(0));
17+
}
18+
19+
@Test
20+
void rotatedDigits3() {
21+
assertThat(new Solution().rotatedDigits(2), equalTo(1));
22+
}
23+
24+
@Test
25+
void rotatedDigits4() {
26+
assertThat(new Solution().rotatedDigits(857), equalTo(247));
27+
}
28+
29+
@Test
30+
void rotatedDigits5() {
31+
assertThat(new Solution().rotatedDigits(15), equalTo(6));
32+
}
33+
}

0 commit comments

Comments
 (0)