Цена за 48 часов в ленте | 1500,00 |
Цена за 1 час закрепления | — |
Взаимопиар | Нет |
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int consumedBottles = 0;
while (numBottles >= numExchange) {
consumedBottles += numExchange;
numBottles -= numExchange;
numBottles++;
}
return consumedBottles + numBottles;
}
};
Input: maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], ball = [4,3], hole = [0,1]
Output: "lul"
#include
#include
#include
#include
using namespace std;
struct State {
int row, col, dist;
string path;
bool operator>(const State& other) const {
return dist == other.dist ? path > other.path : dist > other.dist;
}
};
class Solution {
public:
string findShortestWay(vector>& maze, vector& ball, vector& hole) {
int m = maze.size(), n = maze[0].size();
vector> directions = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
vector textDirections = {"l", "u", "r", "d"};
priority_queue, greater> heap;
vector> seen(m, vector(n));
heap.push({ball[0], ball[1], 0, ""});
while (!heap.empty()) {
auto [row, col, dist, path] = heap.top(); heap.pop();
if (seen[row][col]) continue;
if (row == hole[0] && col == hole[1]) return path;
seen[row][col] = true;
for (int i = 0; i < 4; ++i) {
int r = row, c = col, d = 0;
while (valid(r + directions[i][0], c + directions[i][1], maze)) {
r += directions[i][0]; c += directions[i][1]; d++;
if (r == hole[0] && c == hole[1]) break;
}
heap.push({r, c, dist + d, path + textDirections[i]});
}
}
return "impossible";
}
bool valid(int row, int col, vector>& maze) {
return row >= 0 && row < maze.size() && col >= 0 && col < maze[0].size() && maze[row][col] == 0;
}
};
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* sentinel = new ListNode(0, head);
ListNode* pred = sentinel;
while (head != NULL) {
if (head->next != NULL && head->val == head->next->val) {
while (head->next != NULL && head->val == head->next->val) {
head = head->next;
}
pred->next = head->next;
} else {
pred = pred->next;
}
head = head->next;
}
return sentinel->next;
}
};
Input
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
[[], [100], [80], [60], [70], [60], [75], [85]]
Output
[null, 1, 1, 1, 2, 1, 4, 6]
class StockSpanner {
stack> stk;
public:
StockSpanner() {}
int next(int price) {
int span = 1;
while (!stk.empty() && stk.top().first <= price) {
span += stk.top().second;
stk.pop();
}
stk.push({price, span});
return span;
}
};
Input: nums = [3,4,-1,1] Output: 2
int firstMissingPositive(std::vector& nums) {
for (int i = 0; i < nums.size(); ) {
if (nums[i] <= 0 || nums[i] > nums.size()) {
++i;
continue;
}
if (nums[i] == i + 1) {
++i;
continue;
}
int j = nums[i];
if (nums[j - 1] == nums[i]) {
++i;
continue;
}
std::swap(nums[j - 1], nums[i]);
}
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return nums.size() + 1;
}
Input
["Solution", "randPoint", "randPoint", "randPoint"]
[[1.0, 0.0, 0.0], [], [], []]
Output
[null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
Explanation
Solution solution = new Solution(1.0, 0.0, 0.0);
solution.randPoint(); // return [-0.02493, -0.38077]
solution.randPoint(); // return [0.82314, 0.38945]
solution.randPoint(); // return [0.36572, 0.17248]
#include
#include
#include
class Solution {
double rad, xc, yc;
public:
Solution(double radius, double x_center, double y_center) : rad(radius), xc(x_center), yc(y_center) {}
std::vector randPoint() {
double x0 = xc - rad;
double y0 = yc - rad;
while (true) {
double xg = x0 + static_cast(rand()) / RAND_MAX * rad * 2;
double yg = y0 + static_cast(rand()) / RAND_MAX * rad * 2;
if (sqrt(pow(xg - xc, 2) + pow(yg - yc, 2)) <= rad) {
return {xg, yg};
}
}
}
};
Input: a = "abcd", b = "cdabcdab"
Output: 3
Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int q = 1;
string S = A;
while (S.length() < B.length()) {
S += A;
q++;
}
if (S.find(B) != string::npos) return q;
if ((S + A).find(B) != string::npos) return q + 1;
return -1;
}
};
Input: n = 3, wells = [1,2,2], pipes = [[1,2,1],[2,3,1]]
Output: 3
Explanation: The image shows the costs of connecting houses using pipes.
The best strategy is to build a well in the first house with cost 1 and connect the other houses to it with cost 2 so the total cost is 3.
class Solution {
public:
int minCostToSupplyWater(int n, vector& wells, vector>& pipes) {
priority_queue, vector>, greater>> edgesHeap;
vector>> graph(n + 1);
for (int i = 0; i < wells.size(); ++i) {
graph[0].emplace_back(wells[i], i + 1);
edgesHeap.emplace(wells[i], i + 1);
}
for (const auto& pipe : pipes) {
int house1 = pipe[0], house2 = pipe[1], cost = pipe[2];
graph[house1].emplace_back(cost, house2);
graph[house2].emplace_back(cost, house1);
}
unordered_set mstSet{0};
int totalCost = 0;
while (mstSet.size() < n + 1) {
auto [cost, nextHouse] = edgesHeap.top(); edgesHeap.pop();
if (mstSet.count(nextHouse)) continue;
mstSet.insert(nextHouse);
totalCost += cost;
for (const auto& [nextCost, neighbor] : graph[nextHouse]) {
if (!mstSet.count(neighbor)) {
edgesHeap.emplace(nextCost, neighbor);
}
}
}
return totalCost;
}
};
Input: nums = [1,2,2,4]
Output: [2,3]
class Solution {
public:
vector findErrorNums(vector& nums) {
int n = nums.size();
unordered_set numSet;
int duplicate = -1;
for (int num : nums) {
if (!numSet.insert(num).second) {
duplicate = num;
}
}
int missing = (n * (n + 1)) / 2 - accumulate(numSet.begin(), numSet.end(), 0);
return {duplicate, missing};
}
};
Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
class Solution {
public:
bool hasPath(vector>& maze, vector& start, vector& destination) {
int m = maze.size(), n = maze[0].size();
vector> visit(m, vector(n, false));
return dfs(m, n, maze, start, destination, visit);
}
private:
vector> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool dfs(int m, int n, vector>& maze, vector& curr, vector& destination, vector>& visit) {
if (visit[curr[0]][curr[1]]) return false;
if (curr == destination) return true;
visit[curr[0]][curr[1]] = true;
for (auto [dx, dy] : directions) {
int r = curr[0], c = curr[1];
while (r >= 0 && r < m && c >= 0 && c < n && maze[r][c] == 0) {
r += dx;
c += dy;
}
vector newCurr = {r - dx, c - dy};
if (dfs(m, n, maze, newCurr, destination, visit)) return true;
}
return false;
}
};