-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.java
More file actions
87 lines (76 loc) · 2.39 KB
/
Copy pathDay10.java
File metadata and controls
87 lines (76 loc) · 2.39 KB
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
//Problem:https://leetcode.com/problems/rearrange-array-elements-by-sign/
//Approach: Created an array ans of the same size as nums and initialized two variables, pos = 0 and neg = 1. Traversed nums, placed positive numbers at pos and negative numbers at neg, incrementing them by 2 each time to maintain their alternating order.
class Solution1 {
public int[] rearrangeArray(int[] nums) {
int[] ans=new int[nums.length];
int pos=0,neg=1,i=0;
while(i<nums.length){
if(nums[i]>0 && pos<nums.length){
ans[pos]=nums[i];
pos=pos+2;
}
else if(nums[i]<0 && neg<nums.length){
ans[neg]=nums[i];
neg=neg+2;
}
i++;
}
return ans;
}
}
// TC:O(N)
// SC:O(N)
//Problem:Best Time to buy and sell stock
//https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/1570306944/
//Approach:Traversed through the array prices and at each iteration checked if we have the lesser buying price otherwise calculated the profit and
//updated maxpro if current profit was greater than maximum profit.
class Solution2 {
public int maxProfit(int[] prices) {
int price = prices[0];
int maxpro = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i]<price) {
price = prices[i];
} else {
int currentpro = prices[i] - price;
maxpro = Math.max(currentpro, maxpro);
}
}
return maxpro;
}
}
//TC:O(N)
//SC:O(1)
//Problem:https://leetcode.com/problems/next-permutation/submissions/1570210316/
//NEXT PERMUTATION
class Solution3 {
public void nextPermutation(int[] nums) {
int n = nums.length;
int i = n - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = n - 1;
while (nums[j] <= nums[i]) {
j--;
}
swap(nums, i, j);
}
reverse(nums, i + 1, n - 1);
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
}
}
//TC:O(N)
//SC:O(1)