-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2.java
More file actions
288 lines (243 loc) · 6.98 KB
/
Array2.java
File metadata and controls
288 lines (243 loc) · 6.98 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* MAX SUBARRAY SUM....................................(I)
public class Array2 {
public static void maxSubarraySum(int number[]){
int currsum =0;
int maxsum = Integer.MIN_VALUE;
for(int i=0;i< number.length;i++){
int start = i;
for(int j=i;j< number.length;j++){
int end =j;
currsum=0;
for(int k=start;k<=end;k++){
currsum+=number[k];
}
System.out.println(currsum);
if(maxsum<currsum){
maxsum=currsum;
}
}
}
System.out.println("max sum="+ maxsum);
}
public static void main(String[] args) {
int number[]={1,-2,6,-1,3};
maxSubarraySum(number);
}
}
*/
/* prefix sum of subarray..............................O(n^2)
public class Array2 {
public static void prefixsumOfsubarray(int number[]){
int currsum =0;
int maxsum = Integer.MIN_VALUE;
int prefix[ ] = new int [number.length];
prefix[0] = number[0];
// calculate prefix array
for(int i = 1; i< prefix.length; i++){
prefix[i] = prefix[i-1]+number[i];
}
for(int i =0; i< number.length; i++){
int start = i;
for(int j=i; j<number.length; j++){
int end = j;
currsum= start==0?prefix[end]:prefix[end]-prefix[start-1];
System.out.println(currsum);
if(maxsum<currsum){
maxsum = currsum;
}
}
}
System.out.println("max sum = "+ maxsum);
}
public static void main(String[] args) {
int number[] ={1,-2,6,-1,3};
prefixsumOfsubarray(number);
}
}
*/
/* Kadane max sub array sum .........................O(n) [optimal sol.]
public class Array2 {
public static void kadanes(int number[]){
int ms = Integer.MIN_VALUE;
int cs = 0;
for(int i = 0; i< number.length; i++){
cs = cs+ number[i];
if (cs<0){
cs = 0;
}
ms= Math.max(cs,ms);
}
System.out.println("our max sum is ="+ ms);
}
public static void main(String[] args) {
int number[]= {-2,-3,4,-1,-2,1,5,-3};
kadanes(number);
}
}
*/
/*
//Rainwater trapped Problem ...........................
public class Array2 {
public static int TrappedRainwater(int height[]) {
int n = height.length;
int leftmax[] = new int[n];
leftmax[0] = height[0];
for(int i = 1; i < n; i++) {
leftmax[i] = Math.max(height[i], leftmax[i-1]);
}
int rightmax[] = new int[n];
rightmax[n-1] = height[n-1];
for (int i = n-2; i >= 0; i--) {
rightmax[i] = Math.max(height[i], rightmax[i+1]);
}
int trappedWater = 0;
for (int i = 0; i <n; i++) {
int Waterlevel = Math.min(leftmax[i], rightmax[i]);
trappedWater += Waterlevel - height[i];
}
return trappedWater;
}
public static void main(String[] args) {
int height[] = {4, 2, 0, 6, 3, 2, 5};
System.out.println(TrappedRainwater(height));
}
}
*/
/*
public class Array2 {
public static int buyAndsellStock(int prices[]){
int maxprofit = 0;
int buyprice = Integer.MAX_VALUE;
for(int i= 1; i < prices.length;i++){
if(buyprice < prices[i]){
int profit = prices[i]- buyprice;
maxprofit = Math.max(maxprofit, profit);
}else{
buyprice= prices[i];
}
}
return maxprofit;
}
public static void main(String[] args) {
int prices[] = {7,1,5,3,6,4};
System.out.println(buyAndsellStock(prices));
}
}*/
/*27. Remove Element
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.*/
class Solution {
public int removeElement(int[] nums, int val) {
int k = 0;
for(int i = 0; i<nums.length;i++){
if(val!=nums[i]){
nums[k]= nums[i];
k++;
}
}
return k;
}
}
/*Array 2 pointer Short colors
class Solution {
public void sortColors(int[] nums) {
int low = 0; // for 0
int mid = 0; // for 1
int high = nums.length - 1; // for 2
while (mid <= high) {
if (nums[mid] == 0) {
// swap nums[low] and nums[mid]
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
low++;
mid++;
}
else if (nums[mid] == 1) {
mid++;
}
else { // nums[mid] == 2
// swap nums[mid] and nums[high]
int temp = nums[mid];
nums[mid] = nums[high];
nums[high] = temp;
high--;
}
}
}
}
*/
/*
3. Product of Array Except Self
Problem:
Return an array where each element is product of all other elements except itself.
Example
Input: [1,2,3,4]
Output: [24,12,8,6]
Java Solution
class ProductArray {
public static int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] result = new int[n];
int left = 1;
for (int i = 0; i < n; i++) {
result[i] = left;
left *= nums[i];
}
int right = 1;
for (int i = n - 1; i >= 0; i--) {
result[i] *= right;
right *= nums[i];
}
return result;
}
}
Time Complexity: O(n)
*/
/* 217 leetcode contain duplicate
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet <Integer> set = new HashSet<>();
for(int num : nums){
if(set.contains(num)){
return true;
}
set.add(num);
}
return false;
}
}
*/
/*242 valid anagram
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length() ){
return false;
}
int[] count = new int[26];
for(int i = 0; i < s.length(); i++){
count[s.charAt(i) - 'a']++;
count[t.charAt(i) - 'a']--;
}
for(int c : count){
if(c != 0) return false;
}
return true;
}
}*/
/*
// grou[p of anagaram
import java.util.*;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
}
return new ArrayList<>(map.values());
}
}
*/