Skip to content

Commit de91ffc

Browse files
committed
🚀 15-Oct-2021
1 parent 3cf0ef4 commit de91ffc

File tree

3 files changed

+281
-0
lines changed

3 files changed

+281
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.
3+
4+
You've got string s?=?s1s2... sn (n is the length of the string), consisting only of characters "." and "#" and m queries. Each query is described by a pair of integers li,?ri (1?=?li?<?ri?=?n). The answer to the query li,?ri is the number of such integers i (li?=?i?<?ri), that si?=?si?+?1.
5+
6+
Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.
7+
8+
Input
9+
The first line contains string s of length n (2?=?n?=?105). It is guaranteed that the given string only consists of characters "." and "#".
10+
11+
The next line contains integer m (1?=?m?=?105) — the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li,?ri (1?=?li?<?ri?=?n).
12+
13+
Output
14+
Print m integers — the answers to the queries in the order in which they are given in the input.
15+
16+
Examples
17+
inputCopy
18+
......
19+
4
20+
3 4
21+
2 3
22+
1 6
23+
2 6
24+
outputCopy
25+
1
26+
1
27+
5
28+
4
29+
inputCopy
30+
#..###
31+
5
32+
1 3
33+
5 6
34+
1 5
35+
3 6
36+
3 4
37+
outputCopy
38+
1
39+
1
40+
2
41+
2
42+
0
43+
44+
*/
45+
46+
47+
48+
49+
50+
51+
#include<bits/stdc++.h>
52+
using namespace std;
53+
54+
void solve() {
55+
string s;
56+
cin >> s;
57+
58+
int m;
59+
cin >> m;
60+
61+
vector <int> v(s.length(), 0);
62+
63+
for (int i = 0; i < s.length(); i++) {
64+
if (s[i] == s[i + 1]) v[i + 1] = v[i] + 1;
65+
else v[i + 1] = v[i];
66+
}
67+
68+
while (m--) {
69+
int l, r;
70+
cin >> l >> r;
71+
72+
cout << v[r - 1] - v[l - 1] << "\n";
73+
}
74+
75+
}
76+
77+
int main(){
78+
ios_base::sync_with_stdio(false);
79+
cin.tie(NULL);
80+
81+
int t = 1;
82+
//cin >> t;
83+
84+
while (t--) solve();
85+
86+
return 0;
87+
}
88+
89+
90+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Given a binary tree root, a node X in the tree is named good if in the path
2+
from root to X there are no nodes with a value greater than X.
3+
4+
Return the number of good nodes in the binary tree.
5+
6+
7+
8+
Example 1:
9+
10+
11+
12+
Input: root = [3,1,4,3,null,1,5]
13+
Output: 4
14+
Explanation: Nodes in blue are good.
15+
Root Node (3) is always a good node.
16+
Node 4 -> (3,4) is the maximum value in the path starting from the root.
17+
Node 5 -> (3,4,5) is the maximum value in the path
18+
Node 3 -> (3,1,3) is the maximum value in the path.
19+
Example 2:
20+
21+
22+
23+
Input: root = [3,3,null,4,2]
24+
Output: 3
25+
Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
26+
Example 3:
27+
28+
Input: root = [1]
29+
Output: 1
30+
Explanation: Root is considered as good.
31+
32+
33+
Constraints:
34+
35+
The number of nodes in the binary tree is in the range [1, 10^5].
36+
Each node's value is between [-10^4, 10^4].
37+
Hide Hint #1
38+
Use DFS (Depth First Search) to traverse the tree, and constantly keep track of the current path maximum.
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
/**
51+
* Definition for a binary tree node.
52+
* struct TreeNode {
53+
* int val;
54+
* TreeNode *left;
55+
* TreeNode *right;
56+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
57+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
58+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
59+
* };
60+
*/
61+
class Solution {
62+
public:
63+
int res = 0;
64+
65+
void traversal(TreeNode *root, int maxi) {
66+
if (!root) return;
67+
68+
if (root->val >= maxi) res++;
69+
70+
maxi = max(maxi, root->val);
71+
72+
traversal(root->left, maxi);
73+
traversal(root->right, maxi);
74+
}
75+
76+
int goodNodes(TreeNode* root) {
77+
if (!root) return res;
78+
79+
traversal(root, root->val);
80+
81+
return res;
82+
}
83+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells
2+
need to be validated according to the following rules:
3+
4+
Each row must contain the digits 1-9 without repetition.
5+
Each column must contain the digits 1-9 without repetition.
6+
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
7+
Note:
8+
9+
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
10+
Only the filled cells need to be validated according to the mentioned rules.
11+
12+
13+
Example 1:
14+
15+
16+
Input: board =
17+
[["5","3",".",".","7",".",".",".","."]
18+
,["6",".",".","1","9","5",".",".","."]
19+
,[".","9","8",".",".",".",".","6","."]
20+
,["8",".",".",".","6",".",".",".","3"]
21+
,["4",".",".","8",".","3",".",".","1"]
22+
,["7",".",".",".","2",".",".",".","6"]
23+
,[".","6",".",".",".",".","2","8","."]
24+
,[".",".",".","4","1","9",".",".","5"]
25+
,[".",".",".",".","8",".",".","7","9"]]
26+
Output: true
27+
Example 2:
28+
29+
Input: board =
30+
[["8","3",".",".","7",".",".",".","."]
31+
,["6",".",".","1","9","5",".",".","."]
32+
,[".","9","8",".",".",".",".","6","."]
33+
,["8",".",".",".","6",".",".",".","3"]
34+
,["4",".",".","8",".","3",".",".","1"]
35+
,["7",".",".",".","2",".",".",".","6"]
36+
,[".","6",".",".",".",".","2","8","."]
37+
,[".",".",".","4","1","9",".",".","5"]
38+
,[".",".",".",".","8",".",".","7","9"]]
39+
Output: false
40+
Explanation: Same as Example 1, except with the 5 in the top left corner being
41+
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
42+
43+
44+
Constraints:
45+
46+
board.length == 9
47+
board[i].length == 9
48+
board[i][j] is a digit or '.'.
49+
50+
51+
52+
53+
54+
55+
56+
class Solution {
57+
public:
58+
59+
bool checkRow(vector<vector<char>>& board) {
60+
for (int j = 0; j < 9; j++) {
61+
vector <int> freq(10, 0);
62+
for (int i = 0; i < 9; i++) {
63+
if (board[i][j] == '.') continue;
64+
freq[board[i][j] - '0']++;
65+
}
66+
for (auto &x: freq) {
67+
if (x > 1) return false; // multiple same elements in one column
68+
}
69+
}
70+
return true;
71+
}
72+
73+
bool checkCol(vector<vector<char>>& board) {
74+
for (int i = 0; i < 9; i++) {
75+
vector <int> freq(10, 0);
76+
for (int j = 0; j < 9; j++) {
77+
if (board[i][j] == '.') continue;
78+
freq[board[i][j] - '0']++;
79+
}
80+
for (auto &x: freq) {
81+
if (x > 1) return false; // multiple same elements in one row
82+
}
83+
}
84+
return true;
85+
}
86+
87+
bool checkSubBox(vector<vector<char>>& board) {
88+
for (int i = 0; i < 9; i += 3) {
89+
for (int j = 0; j < 9; j += 3) {
90+
vector <int> freq(10, 0);
91+
for (int p = i; p < i + 3; p++){
92+
for (int q = j; q < j + 3; q++) {
93+
if (board[p][q] == '.') continue;
94+
freq[board[p][q] - '0']++;
95+
}
96+
}
97+
for (auto &x: freq) {
98+
if (x > 1) return false; // multiple same elements in one sub box
99+
}
100+
}
101+
}
102+
return true;
103+
}
104+
105+
bool isValidSudoku(vector<vector<char>>& board) {
106+
return checkRow(board) && checkCol(board) && checkSubBox(board);
107+
}
108+
};

0 commit comments

Comments
 (0)