Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5de493

Browse files
committed
Pass test case one for duplicate two problem
1 parent67b2351 commita5de493

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#Problem
2+
Given an integer array nums and an integer k,
3+
return true if there are two`distinct indices`
4+
i and j in the array such that nums[i] == nums[j]
5+
and abs(i - j) <= k.
6+
7+
Example 1:
8+
Input: nums =[1,2,3,1], k = 3
9+
Output: true
10+
11+
Example 2:
12+
Input: nums =[1,0,1,1], k = 1
13+
Output: true
14+
15+
Example 3:
16+
Input: nums =[1,2,3,1,2,3], k = 2
17+
Output: false
18+
19+
Constraints:
20+
- 1 <= nums.length <= 10^5
21+
- -10^9 <= nums[i] <= 10^9
22+
- 0 <= k <= 10^5
23+
24+
25+
#Solution
26+
##First
27+
28+
Key points:
29+
nums[i] == nums[j] : Equal Numbers.
30+
abs(i - j) <= k : Difference of Indices < k.
31+
32+
So we cannot sort as index needs to be preserved.
33+
34+
###Pseudocode
35+
36+
for (i=0; i < nums.len; i++) // loop all nums
37+
for (j=i+1; j < nums.len; j++) // loop all numbs except i
38+
39+
if nums[i]=nums[j] and abs(i-j)<=k, return true
40+
41+
return false.
42+
43+
###Data structure
44+
Max numeric length is 10^5
45+
Max value is 10^9.
46+
47+
`int` would suffice.
48+
49+
```
50+
4 bytes.
51+
Stores whole numbers from -2,147,483,648 to 2,147,483,647.
52+
```
53+
54+
###Understanding Case 1
55+
Initial logic below was failing case 1
56+
```
57+
for (int i = 0; i < nums.length - 1; i++) {
58+
for ( int j = i+1; j < nums.length-1; j++ ) {
59+
60+
if ( nums[i] == nums[j] && abs(i-j) <= k )
61+
return true;
62+
}
63+
}
64+
```
65+
nums =[1,2,3,1], k = 3
66+
There are two equal numbers`1` at nums[0] and nums[3].
67+
Their index abs difference 0-3 = 3.
68+
This case returns true.
69+
70+
The loop was not reaching all the numbers. Minor change
71+
was made.
72+
```
73+
for ( int j = i+1; j < nums.length; j++ )
74+
```
75+
76+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packageleetcode.datastructure.duplicate.two;
2+
3+
importstaticjava.lang.Math.abs;
4+
5+
classSolution {
6+
publicbooleancontainsNearbyDuplicate(int[]nums,intk) {
7+
8+
for (inti =0;i <nums.length -1;i++) {
9+
for (intj =i+1;j <nums.length;j++ ) {
10+
11+
if (nums[i] ==nums[j] &&abs(i-j) <=k )
12+
returntrue;
13+
}
14+
}
15+
16+
returnfalse;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packageleetcode.datastructure.duplicate.two;
2+
3+
importorg.junit.jupiter.api.Assertions;
4+
importorg.junit.jupiter.api.Test;
5+
6+
7+
classSolutionTest {
8+
@Test
9+
voidshouldPassWithExample1() {
10+
Solutionsolution =newSolution();
11+
Assertions.assertTrue(solution.containsNearbyDuplicate(newint[]{1,2,3,1},3));
12+
}
13+
14+
15+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp