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
- integer array nums, find the whole combination of 3 nums, and the sum of the 3 nums equal to 0. And don't allow reusing same indiced number(but can duplicate in value)
6
+
2. solve strategy
7
+
- brute force
8
+
- in every combination, validate sum of the nums equal to 0
9
+
- but it can take O(N^3) times where N is the length of input array, and given that the N can be 3000 at most(3 * 10^3), time can be 27 * 10^9, which takes too long...
10
+
- sort and two pointers
11
+
- sort nums in ascending order, so move the left pointer to right means the sum of window is getting bigger.
12
+
- and mid pointer set to left + 1 index
13
+
- if sum of pointers is less than 0, then move mid pointer to right, until the sum is bigger than 0, and while processing them, if the sum of pointers is 0, then add the combination to the return list.
14
+
- [-4, -1, -1, 0, 1, 2]:
15
+
16
+
3. complexity
17
+
- time: O(N^2) -> each left pointer, you can search at most N-1, and left pointer's range is [0, N-1), so the max length is N-1 for left index pointer.
// 3. move the mid pointer from left to right to find the combination of which's sum is 0, and if the sum is over 0, and then move right pointer to the left. else if the sum is under 0, then move left pointer to right direction.
28
+
for (intleft = 0; left < nums.length - 1; left++) {
- I answer to this part, along with coding upon each line description.
18
+
*/
19
+
20
+
// 1. create array to demonstrate each stairs way count to reach that position.
21
+
// the maximun step count is 45, so maybe there is over 2^32(approximately 2 billion; so i worry about the overflow), I assign long type array. Oh.. but i catch that return type of this method is integer, so i can assume that maximum value is under integer range. So, assign as integer.
22
+
int[] c = newint[n + 1]; // the extra plus 1 means 0th stair state
23
+
// space complexity: O(n)
24
+
for (intstair = 0; stair <= n; stair++) { // time complexity O(n)
0 commit comments