Skip to content

Commit 687fa4c

Browse files
committed
Refine 1981 and 1539 format and link
1 parent ecdb282 commit 687fa4c

4 files changed

+64
-65
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
236236
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py) | If number is divisible by 2, divide the number by 2, else subtract 1 from the number, and output the number of steps, O(logn) and O(1) |
237237
| 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.java) | 1. Sort and get position in sorted nums, O(nlogn) and O(n)<br>2. Fill count into 0-100, O(n) and O(1) |
238238
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1480_Running_Sum_of_1d_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1480_Running_Sum_of_1d_Array.java) | 1. Go through the array, O(n) and O(1)<br>2. Accumulate API |
239+
| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1539_Kth_Missing_Positive_Number.java) | Binary search, num of missing = arr[i]-i-1 |
240+
| 1981 | [Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/) | [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1981_Minimize_the_Difference_Between_Target_and_Chosen_Elements.java) | DP memo[row][sum] to avoid recomputing |
239241

240242
| # | To Understand |
241243
|---| ----- |

java/1533_Kth_Missing_Positive_Number.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int findKthPositive(int[] a, int k) {
3+
int B[] = new int[a.length];
4+
5+
// equation (A)
6+
// B[i]=a[i]-i-1
7+
// B[i]=number of missing numbers BEFORE a[i]
8+
for (int i = 0; i < a.length; i++)
9+
B[i] = a[i] - i - 1; // -1 is done as here missing numbers start from 1 and not 0
10+
11+
// binary search upper bound of k
12+
// smallest value>=k
13+
14+
int lo = 0, hi = B.length - 1;
15+
16+
while (lo <= hi) {
17+
int mid = lo + (hi - lo) / 2;
18+
19+
if (B[mid] >= k)
20+
hi = mid - 1;
21+
else
22+
lo = mid + 1;
23+
}
24+
25+
// lo is the answer
26+
27+
/*
28+
* now the number to return is a[lo]-(B[lo]-k+1) (EQUATION B)
29+
* where (B[lo]-k+1) is the number of steps we need to go back
30+
* from lo to retrieve kth missing number, since we need to find
31+
* the kth missing number BEFORE a[lo], we do +1 here as
32+
* a[lo] is not a missing number when B[lo]==k
33+
* putting lo in equation(A) above
34+
* B[i]=a[i]-i-1
35+
* B[lo]=a[lo]-lo-1
36+
* and using this value of B[lo] in equation B
37+
* we return a[lo]-(a[lo]-lo-1-k+1)
38+
* we get lo+k as ans
39+
* so return it
40+
*/
41+
return lo + k;
42+
}
43+
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
class Solution {
22
public int minimizeTheDifference(int[][] a, int k) {
3-
n=a.length;
4-
m=a[0].length;
5-
min=Integer.MAX_VALUE;
6-
dp=new boolean[n][5000];
7-
solve(a,k,0,0,0);
3+
n = a.length;
4+
m = a[0].length;
5+
min = Integer.MAX_VALUE;
6+
dp = new boolean[n][5000];
7+
solve(a, k, 0, 0, 0);
88
return min;
99
}
10-
private void solve(int a[][],int k,int sum,int row,int col)
11-
{
12-
if(dp[row][sum]) return;
13-
if(n-1==row)
14-
{
15-
for(int i=0;i<m;i++)
16-
min=Math.min(min,Math.abs(k-sum-a[row][i]));
17-
dp[row][sum]=true;
10+
11+
private void solve(int a[][], int k, int sum, int row, int col) {
12+
if (dp[row][sum])
13+
return;
14+
if (n - 1 == row) {
15+
for (int i = 0; i < m; i++)
16+
min = Math.min(min, Math.abs(k - sum - a[row][i]));
17+
dp[row][sum] = true;
1818
return;
1919
}
20-
21-
for(int i=0;i<m;i++)
22-
solve(a,k,sum+a[row][i],row+1,col);
23-
dp[row][sum]=true;
20+
21+
for (int i = 0; i < m; i++)
22+
solve(a, k, sum + a[row][i], row + 1, col);
23+
dp[row][sum] = true;
2424
}
25+
2526
private int min;
26-
private int dy[],n,m;
27+
private int dy[], n, m;
2728
private boolean dp[][];
2829
}

0 commit comments

Comments
 (0)