You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Array/1D.md
+105-1
Original file line number
Diff line number
Diff line change
@@ -308,6 +308,72 @@ public class Solution {
308
308
}
309
309
~~~
310
310
311
+
## 456. 132 Pattern
312
+
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.
313
+
314
+
Note: n will be less than 15,000.
315
+
316
+
Example 1:
317
+
~~~
318
+
Input: [1, 2, 3, 4]
319
+
320
+
Output: False
321
+
322
+
Explanation: There is no 132 pattern in the sequence.
323
+
~~~
324
+
Example 2:
325
+
~~~
326
+
Input: [3, 1, 4, 2]
327
+
328
+
Output: True
329
+
330
+
Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
331
+
~~~
332
+
Example 3:
333
+
~~~
334
+
Input: [-1, 3, 2, 0]
335
+
336
+
Output: True
337
+
338
+
Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
339
+
~~~
340
+
341
+
#### Solution
342
+
1. O(n^2) method : fix j, find min from range(0, j - 1), check whether in range(j + 1, nums.length - 1) has a number less than nums[j] and greater than min.
343
+
2. O(n) method : using stack.
344
+
345
+
Method 1 O(n^2) <br>
346
+
Attempt: 1
347
+
~~~
348
+
public class Solution {
349
+
public boolean find132pattern(int[] nums) {
350
+
if (nums == null || nums.length < 3) return false;
351
+
352
+
// fix j
353
+
for (int j = 1; j < nums.length - 1; j++) {
354
+
// find min num in range from 0 to j - 1, and make it as i
355
+
int min = nums[0];
356
+
// int index = 0; // no need to track index
357
+
for (int i = 1; i < j; i++) {
358
+
if (nums[i] < min) {
359
+
min = nums[i];
360
+
// index = i;
361
+
}
362
+
}
363
+
364
+
// found a valid i
365
+
if (min < nums[j]) {
366
+
// find k in range from j + 1 to nums.length - 1
367
+
for (int k = j + 1; k < nums.length; k++) {
368
+
if (nums[k] < nums[j] && nums[k] > min) return true;
369
+
}
370
+
}
371
+
}
372
+
373
+
return false;
374
+
}
375
+
}
376
+
~~~
311
377
## 18. 4Sum (Medium)
312
378
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
313
379
@@ -462,7 +528,7 @@ Given n non-negative integers a1, a2, ..., an, where each represents a point at
462
528
Note: You may not slant the container and n is at least 2.
463
529
464
530
#### Solution
465
-
Two pointers
531
+
Two pointers <br>
466
532
Old version of code will get TLE with new leetcode test code.
467
533
How to improve?
468
534
@@ -701,6 +767,44 @@ public class Solution {
701
767
~~~
702
768
703
769
# Binary Search
770
+
## 287. Find the Duplicate Number
771
+
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
772
+
773
+
Note:
774
+
1. You must not modify the array (assume the array is read only).
775
+
2. You must use only constant, O(1) extra space.
776
+
3. Your runtime complexity should be less than O(n2).
777
+
4. There is only one duplicate number in the array, but it could be repeated more than once.
778
+
779
+
#### Solution
780
+
题目限定只有一个重复的数字让问题变得简单
781
+
782
+
Attempt: 2 (bug when counting)
783
+
~~~
784
+
public class Solution {
785
+
public int findDuplicate(int[] nums) {
786
+
if (nums == null || nums.length == 0) return 0;
787
+
int l = 1;
788
+
int r = nums.length - 1;
789
+
while (l < r) {
790
+
int m = (r - l) / 2 + l;
791
+
int count = 0;
792
+
for (int i = 0; i < nums.length; i++) {
793
+
// if (nums[i] <= m) count++; // bug
794
+
if (nums[i] >= l && nums[i] <= m) count++;
795
+
}
796
+
if (count > m - l + 1) {
797
+
r = m;
798
+
}
799
+
else {
800
+
l = m + 1;
801
+
}
802
+
}
803
+
return l;
804
+
}
805
+
}
806
+
~~~
807
+
704
808
## 4. Median of Two Sorted Arrays (Hard) *
705
809
There are two sorted arrays nums1 and nums2 of size m and n respectively.
0 commit comments