-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindes.js
247 lines (137 loc) · 5.58 KB
/
indes.js
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
//merge sorted array
// function mergeArrays(arr1, arr2) {
// let mergedArray = [];
// let i = 0;
// let j = 0;
// while (i < arr1.length && j < arr2.length) {
// if (arr1[i] < arr2[j]) {
// mergedArray.push(arr1[i]);
// i++;
// } else {
// mergedArray.push(arr2[j]);
// j++;
// }
// }
// // If there are remaining elements in arr1, append them to mergedArray
// while (i < arr1.length) {
// mergedArray.push(arr1[i]);
// i++;
// }
// // If there are remaining elements in arr2, append them to mergedArray
// while (j < arr2.length) {
// mergedArray.push(arr2[j]);
// j++;
// }
// return mergedArray;
// }
// // Test cases
// console.log(mergeArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
// // console.log(mergeArrays([-2, 0, 4, 7], [-1, 2, 6])); // Output: [-2, -1, 0, 2, 4, 6, 7]
// gfg maximum sum problem
// function maxSum(n) {
// let dp = [];
// function findMaxSum(n) {
// console.log(n)
// if (n <= 0) return 0;
// if (dp[n]) return dp[n];
// let sum = findMaxSum(Math.floor(n / 2)) + findMaxSum(Math.floor(n / 3)) + findMaxSum(Math.floor(n / 4));
// console.log('sum',sum)
// dp[n] = Math.max(n, sum);
// return dp[n];
// }
// return findMaxSum(n);
// }
// // Test cases
// // console.log(maxSum(12)); // Output: 13
// console.log(maxSum(24)); // Output: 27
// function maxSum(n) {
// let dp = [];
// dp[0] = 0; // Base case
// for (let i = 1; i <= n; i++) {
// let sum = dp[Math.floor(i / 2)] + dp[Math.floor(i / 3)] + dp[Math.floor(i / 4)];
// dp[i] = Math.max(i, sum);
// }
// return dp[n];
// }
// // Test cases
// console.log(maxSum(12)); // Output: 13
// console.log(maxSum(24)); // Output: 27
//buy and sell share
// function maxProfit(prices) {
// if (prices.length < 2) {
// return 0; // Not enough data to make a profit
// }
// // Initialize variables to store the maximum profit
// let firstBuy = Number.MIN_SAFE_INTEGER;
// let firstSell = 0;
// let secondBuy = Number.MIN_SAFE_INTEGER;
// let secondSell = 0;
// for (let price of prices) {
// // Calculate the maximum profit after the first buy
// firstBuy = Math.max(firstBuy, -price);
// // Calculate the maximum profit after the first sell
// firstSell = Math.max(firstSell, firstBuy + price);
// // Calculate the maximum profit after the second buy
// secondBuy = Math.max(secondBuy, firstSell - price);
// // Calculate the maximum profit after the second sell
// secondSell = Math.max(secondSell, secondBuy + price);
// }
// // The maximum profit achievable is the maximum value of secondSell
// return secondSell;
// }
// const prices = [10, 22, 5, 75, 65, 80];
// console.log("Maximum profit: ", maxProfit(prices));
// function maxSumFind(n){
// let dp = []
// //initial value
// dp[0] = 0;
// for(let i = 1; i<= n; i++){
// let sum = dp[Math.floor(i / 2)] + dp[Math.floor( i / 3)] + dp[Math.floor( i / 4)]
// dp[i] = Math.max(i,sum)
// }
// return dp[n]
// }
// console.log(maxSumFind(24))
function count(n) {
// Initialize an array to store the counts for each score up to n
const dp = new Array(n+1).fill(0);
console.log('dp',dp);
// Base case: There is one way to score 0 points (by not making any move)
dp[0] = 1;
// Iterate through each possible move (3, 5, 10) and update counts
[3, 5, 10].forEach(score => {
for (let i = score; i <= n; i++) {
console.log('log',dp[i])
dp[i] += dp[i - score];
console.log('inside loop',dp[i])
}
});
return dp[n];
}
// Example usage:
const n1 = 10;
console.log(count(n1)); // Output: 2
// const n2 = 20;
// console.log(count(n2)); // Output: 4
// Given an array of N integers arr[] where each element represents the maximum length of the jump that can be made forward from that element. This means if arr[i] = x, then we can jump any distance y such that y ≤ x.
// Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.
// Note: Return -1 if you can't reach the end of the array.
// if (n <= 1) return 0; // If array size is 0 or 1, no jumps needed
function minJumps(arr, n) {
if (n <= 1) return 0; // If array size is 0 or 1, no jumps needed
if (arr[0] === 0) return -1; // If the first element is 0, we cannot move forward
let maxReach = arr[0]; // Maximum index that can be reached from current index
let steps = arr[0]; // Number of steps remaining at current index
let jumps = 1; // Initial jump from the first index
for (let i = 1; i < n; i++) {
if (i === n - 1) return jumps; // If reached the last index, return number of jumps
maxReach = Math.max(maxReach, i + arr[i]); // Update the maximum reachable index
steps--; // Decrement steps as we move forward
if (steps === 0) { // If no more steps remaining
jumps++; // Increment jumps
if (i >= maxReach) return -1; // If the current index cannot be reached from the previous reachable indices, return -1
steps = maxReach - i; // Update steps to the difference between maxReach and current index
}
}
return -1; // If the last index cannot be reached
}