-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay09.java
More file actions
80 lines (75 loc) · 1.74 KB
/
Copy pathDay09.java
File metadata and controls
80 lines (75 loc) · 1.74 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
//Problem 1:Two Sum
//https://leetcode.com/problems/two-sum/
import java.util.*;
class Solution1 {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map=new HashMap<>();
int i=0;
int[]ans=new int[2];
while(i<nums.length){
if(map.containsKey(target-nums[i])){
ans[0]=map.get(target-nums[i]);
ans[1]=i;
return ans;
}
else{
map.put(nums[i],i);
}
i++;
}
return ans;
}
}
//TC:O(n)
//SC:O(n)
//Problem 2:max score from subarray minimums
//https://www.geeksforgeeks.org/problems/max-sum-in-sub-arrays0824/0
class Solution2 {
// Function to find pair with maximum sum
public int pairWithMaxSum(int arr[]) {
// Your code goes here
int i=0;
int j=i+1;
int sum=0;
int max=-1;
while(j<arr.length){
sum=arr[i++]+arr[j++];
max=Math.max(sum,max);
}
return max;
}
}
// SC:O(1);
// TC:O(N);
//Problem 3:Sort colors
//https://leetcode.com/problems/sort-colors/description/
class Solution3 {
public void sortColors(int[] nums) {
int zero=0,one=0,two=0,k=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
zero++;
}
else if(nums[i]==1){
one++;
}
else{
two++;
}
}
for(int i=0;i<zero;i++){
nums[k]=0;
k++;
}
for(int i=0;i<one;i++){
nums[k]=1;
k++;
}
for(int i=0;i<two;i++){
nums[k]=2;
k++;
}
}
}
// TC:O(n)
// SC:O(1)