-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcontest leetcode 296
More file actions
95 lines (72 loc) · 2.22 KB
/
contest leetcode 296
File metadata and controls
95 lines (72 loc) · 2.22 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
class Solution:
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
temp={}
for i in range(len(nums)):
temp[num[i]]=i
for i in range(len(op)):
if op[i][0] not in temp:
temp[op[i][0]]=temp[op[i][1]]
nums[temp[op[i][0]]]=op[i][1]
temp[op[i][0]]=temp[op[i][1]]
return nums
class Solution {
public:
vector<int> arrayChange(vector<int>& A, vector<vector<int>>& op) {
unordered_map<int,int> store;
for(int i=0;i<A.size();i++) store[A[i]]=i;
for(auto i:op){
A[store[i[0]]]= i[1]; //replace value to its index
store[i[1]]= store[i[0]]; //update new value with its index
}
return A;
}
};
class Solution {
public:
int minMaxGame(vector<int>& nums) {
for(int n=size(a);n>1;n-=(n/2))
{
for(int i=0;i<n/2;i++)
nums[i]=(i%2)?max(nums[2*i],nums[2*i+1]):min(nums[2*i],nums[2*i+1]);
}
return nums[0];
}
};
class Solution:
def minMaxGame(self, nums: List[int]) -> int:
l=nums
while(l)>1:
ismin=True
temp=[]
for i in range(0,len(l),2):
if ismin:
temp.append(min(l[i:i+2]))
else:
temp.append(max(l[i:i+2]))
ismin=not ismin
l=temp
return l[0]
class Solution {
public:
int partitionArray(vector<int>& A, int k) {
sort(A.begin(), A.end());
check for subsequences having sum =k ==3
[1,2,3,1,1] [1 1 1 ]
[2, 1]
[3]
op--3
2 sum
recursion
int res = 1,
mn = A[0], mx = A[0];
for (int& a: A) {
mn = min(mn, a);
mx = max(mx, a);
if (mx - mn > k) {
res++;
mn = mx = a; 3
}
}
return res;
}
};