Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a72dcbe

Browse files
committed
Arrays & Heaps
1 parent 76eb3d7 commit a72dcbe

3 files changed

+107
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.arrays;
2+
3+
/**
4+
*
5+
* Complexity Analysis:
6+
* Time Complexity: O(n), Only one traversal of original array is needed. So the time complexity is O(n).
7+
*
8+
* Question
9+
* Stickler the thief wants to loot money from a society having n houses in a single line.
10+
* Rule 1 - never loot two consecutive houses.
11+
* he wants to maximize the amount he loots.
12+
* find the maximum money he can get if he strictly follows the rule.
13+
*
14+
*/
15+
public class MaxSumWithoutAdjacents {
16+
17+
public static void main(String[] args) {
18+
19+
int[] input = {3, 2, 5, 10, 7};
20+
int counter = 0;
21+
int evenSum = 0;
22+
int oddSum = 0;
23+
24+
while (counter != input.length) {
25+
26+
if (counter % 2 == 0) {
27+
evenSum += input[counter];
28+
} else {
29+
oddSum += input[counter];
30+
}
31+
counter++;
32+
}
33+
int max = Integer.max(evenSum, oddSum);
34+
System.out.println(max);
35+
}
36+
}

src/main/java/com/arrays/StockBuySellToMaximizeProfit_PeakValleyApproach.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.arrays;
22

33
/**
4+
* https://onedrive.live.com/redir?resid=26052E8C3C647484%21105&page=Edit&wd=target%28Programs.one%7C6d1885b7-0ca6-4a4e-81df-92e3ac05fb6f%2FStockBuySellMaximizeProfit%7C9427c9b7-0a0a-41ec-b618-85ab5bbc1447%2F%29&wdorigin=703
5+
*
46
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/
57
* Approach-3
68
*
@@ -28,7 +30,9 @@
2830
* Time complexity : O(n). Single pass.
2931
*
3032
* Space complexity: O(1). Constant space needed.
31-
*/
33+
*
34+
*
35+
* */
3236
public class StockBuySellToMaximizeProfit_PeakValleyApproach {
3337

3438
public static int maxProfit(int[] prices) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* This method uses the method of Sorting and Two-pointer Technique to solve the above problem.
7+
* This execution will involve O(n2)) time complexity and O(1) space complexity.
8+
*/
9+
10+
11+
/**
12+
* 1. Sort all element of array
13+
* 2. Run loop from i=0 to n-2.
14+
* Initialize two index variables l=i+1 and r=n-1
15+
* 4. while (l < r)
16+
* Check sum of arr[i], arr[l], arr[r] is
17+
* given sum or not if sum is 'sum', then print
18+
* the triplet and do l++ and r--.
19+
* 5. If sum is less than given sum then l++
20+
* 6. If sum is greater than given sum then r--
21+
* 7. If not exist in array then print not found.
22+
*/
23+
24+
/**
25+
* Complexity Analysis:
26+
* Time Complexity: O(n2).
27+
* Use of a nested loop (one for iterating , other for two-pointer technique) brings the time complexity to O(n2).
28+
* Auxiliary Space: O(1).
29+
* As no use of additional data structure is used.
30+
*/
31+
32+
public class TripletSumInArray_Sorting_TwoPointerAlgo {
33+
34+
public static void find_all_triplets(int[] arr, int sum) {
35+
36+
// sort array elements
37+
Arrays.sort(arr); /*important*/
38+
39+
int n = arr.length;
40+
int left;
41+
int right;
42+
for (int i = 0; i < n - 2; i++) {
43+
left = i + 1;
44+
right = n - 1;
45+
while (left < right) {
46+
int tempSum = arr[i] + arr[left] + arr[right];
47+
if (tempSum == sum) {
48+
System.out.println("triplet:{" + arr[i] + "," + arr[left] + "," + arr[right] + "}");
49+
left++;
50+
right--;
51+
} else if (tempSum < sum) {
52+
left++;
53+
} else {
54+
right--;
55+
}
56+
}
57+
}
58+
}
59+
60+
61+
public static void main(String args[]) {
62+
int arr[] = {1, 7, 4, 3, 4, 5, 2};
63+
int sum = 9;
64+
find_all_triplets(arr, sum);
65+
}
66+
}

0 commit comments

Comments
 (0)