-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path[lint]. 2 Sum II.java
executable file
·126 lines (108 loc) · 3.61 KB
/
[lint]. 2 Sum II.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
M
1516439472
tags: Array, Two Pointers, Binary Search, Lint
与 2sum II - input array is sorted类似. 都是sort array, 然后two pointer.
LintCode的题. 注意找的是greater/bigger than target。
由于给定条件允许O(nLogn):
sort
two pointer
while里面two pointer移动。每次如果num[left]+num[right] > target,那么其中所有num[left++]的加上num[right]都>target.
也就是,num[right]不动,计算加入挪动left能有多少组,那就是: right-left这么多。 全部加到count上去。
然后right--.换个right去和前面的left部分作比较。
```
/*
Given an array of integers, find how many pairs in the array such that
their sum is bigger than a specific target number. Please return the number of pairs.
Example
numbers=[2, 7, 11, 15], target=24
return 1
Challenge
Either of the following solutions are acceptable:
O(1) Space, O(nlogn) Time
Tags Expand
Two Pointers
*/
/*
Thoughts:
After doing Trigle Count...Can we just do the same for this, move while pointers to center?
OMG. The original idea was sooooooo complicated.
*/
public class Solution {
public int twoSum2(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
int count = 0;
int left = 0;
int right = nums.length - 1;
Arrays.sort(nums);
while (left < right) {
if (nums[left] + nums[right] > target) {
count += (right - left);
right--;
} else {
left++;
}
}
return count;
}
}
//Below are bad solutions. Don't worry about them
/*
Thoughts:
1. For loop to try each element from (i ~ end). O(n)
2. In for, do binary search on nums[i] + nums[j] > target,
3. count += (length - j)
Note: when not found, return nums.length, because at then end, (length - length) == 0, which will be added to count.
Note: Always pin target down, and move mid to compare. Don't get confused
Also, take care of corner cases.
*/
public class Solution {
public int twoSum2(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
int count = 0;
Arrays.sort(nums);
for (int i = 1; i < nums.length; i++) {
int index = binarySearch(nums, target - nums[i - 1], i, nums.length - 1);
count += nums.length - index;
}
return count;
}
public int binarySearch(int[] nums, int target, int start, int end) {
int mid;
int sum;
while (start + 1 < end) {
mid = start + (end - start) /2;
if (mid - 1 >= 0 && nums[mid-1] <= target && target < nums[mid]) {
return mid;
} else if (mid + 1 < nums.length && nums[mid] <= target && target < nums[mid + 1]) {
return mid + 1;
} else if (target < nums[mid]) {
end = mid;
} else {
start = mid;
}
}
if (nums[start] > target) {
return start;
}
return (nums[end] > target) ? end : nums.length;
}
}
//Brutle force, O(n^2)
public class Solution {
public int twoSum2(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
count += (nums[i] + nums[j] > target) ? 1 : 0;
}
}
}
}
```