-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumBagsWithFullCapacityofRocks.html
More file actions
118 lines (111 loc) · 4.53 KB
/
MaximumBagsWithFullCapacityofRocks.html
File metadata and controls
118 lines (111 loc) · 4.53 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode Day 1</title>
<style>
body{
background: greenyellow;
}
h2{
color : rgb(17, 0, 255);
font-size: 20px;
}
</style>
</head>
<body >
<h1>Question : You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and
rocks. The ith bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also
given an integer additionalRocks, the number of additional rocks you can place in any of the bags.
Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.
</h1>
<h2 > Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
</h2>
<script>
// var capacity = [2, 3, 4, 5];
// var rocks = [1, 2, 4, 4];
// var additionalRocks = 2;
var capacity = [54, 18, 91, 49, 51, 45, 58, 54, 47, 91, 90, 20, 85, 20, 90, 49, 10, 84, 59, 29, 40, 9, 100, 1, 64, 71, 30, 46, 91]
var rocks = [14, 13, 16, 44, 8, 20, 51, 15, 46, 76, 51, 20, 77, 13, 14, 35, 6, 34, 34, 13, 3, 8, 1, 1, 61, 5, 2, 15, 18]
var additionalRocks = 77
var fullbag = 0;
for (var j = additionalRocks - 1; j < additionalRocks; j++) {
for (var i = 0; i < j + 1; i++) {
var max = capacity[i];
var rocksInBag = rocks[i];
if (rocksInBag < max) {
rocks[i] = rocks[i] + 1
console.log("rocks array is", rocks);
}
}
}
for (var i = 0; i < capacity.length; i++) {
var max = capacity[i];
var rocksInBag = rocks[i];
if (rocksInBag == max) {
fullbag++
}
}
console.log(fullbag);
//adding one more method if we want maximum numbers of bag and number of additionals rocks are greater than space
var additionalRocks = 77
var capacity = [54, 18, 91, 49, 51, 45, 58, 54, 47, 91, 90, 20, 85, 20, 90, 49, 10, 84, 59, 29, 40, 9, 100, 1, 64, 71, 30, 46, 91]
var rocks = [14, 13, 16, 44, 8, 20, 51, 15, 46, 76, 51, 20, 77, 13, 14, 35, 6, 34, 34, 13, 3, 8, 1, 1, 61, 5, 2, 15, 18]
console.log(call(capacity, rocks, additionalRocks));
function call(capacity, rocks, additionalRocks) {
var fullcapacity = 0
var availablespace = []
var index = []
for (var i = 0; i < capacity.length; i++) {
var space = capacity[i] - rocks[i]
var fullcapacity = space + fullcapacity
availablespace.push(space)
index.push(i)
}
console.log(additionalRocks);
if (fullcapacity < additionalRocks) {
additionalRocks = fullcapacity
}
console.log(additionalRocks);
console.log(additionalRocks);
for (var i = 1; i < availablespace.length; i++) {
for (var j = 0; j < availablespace.length + 1; j++) {
if (availablespace[i] < availablespace[j]) {
var temp = availablespace[i]
availablespace[i] = availablespace[j]
availablespace[j] = temp
var temp2 = index[i]
index[i] = index[j]
index[j] = temp2
}
}
}
var i = 0;
while (additionalRocks > 0) {
if (availablespace[i] > 0) {
var indexforRocks = index[i]
for (var j = 0; j < availablespace[i]; j++) {
if (additionalRocks > 0) {
rocks[indexforRocks] = rocks[indexforRocks] + 1
additionalRocks = additionalRocks - 1
}
}
}
i++
}
var fullbag = 0;
for (var i = 0; i < rocks.length; i++) {
var max = capacity[i];
var rocksInBag = rocks[i];
if (rocksInBag == max) {
fullbag++
}
}
// console.log(fullbag);
return fullbag
}
</script>
</body>
</html>