-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.java
More file actions
100 lines (81 loc) · 2.11 KB
/
Copy pathday10.java
File metadata and controls
100 lines (81 loc) · 2.11 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
88
89
90
91
92
93
94
95
96
97
98
99
100
//ques1:2149. Rearrange Array Elements by Sign
//link:https://leetcode.com/problems/rearrange-array-elements-by-sign/description/
class Solution {
public int[] rearrangeArray(int[] nums) {
int[] result = new int[nums.length];
int positive = 0, negative = 1;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (num > 0) {
result[positive] = num;
positive += 2;
} else {
result[negative] = num;
negative += 2;
}
}
return result;
}
}
//TC O(N)
//SC O(N)
//ques 2:121.Best Time to Buy and Sell Stock
//link:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
class Solution2 {
public int maxProfit(int[] prices) {
int i=prices[0];
int prof=0;
for(int j=1;j<prices.length;j++){
if(i>prices[j]){
i=prices[j];
}
else{
prof=Math.max(prof,prices[j]-i);
}
}
return prof;
}
}
//TC O(N)
//SC O(1)
//ques3: 31. Next Permutation
//link:https://leetcode.com/problems/next-permutation/description/
class Solution3 {
public void nextPermutation(int[] nums) {
int ind1=-1;
int ind2=-1;
for(int i=nums.length-2;i>=0;i--){
if(nums[i]<nums[i+1]){
ind1=i;
break;
}
}
if(ind1==-1){
reverse(nums,0);
}
else{
for(int i=nums.length-1;i>=0;i--){
if(nums[i]>nums[ind1]){
ind2=i;
break;
}
}
swap(nums,ind1,ind2);
reverse(nums,ind1+1);
}
}
void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
void reverse(int[] nums,int start){
int i=start;
int j=nums.length-1;
while(i<j){
swap(nums,i,j);
i++;
j--;
}
}
}