Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anqiwa
myLeetcodeSolu
Commits
79e7474f
Commit
79e7474f
authored
Aug 10, 2021
by
anqiwa
😲
Browse files
8,10 update
parent
bd4bedd1
Changes
41
Hide whitespace changes
Inline
Side-by-side
102.binary-tree-level-order-traversal.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=102 lang=cpp
*
* [102] Binary Tree Level Order Traversal
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class
Solution
{
public:
vector
<
vector
<
int
>>
levelOrder
(
TreeNode
*
root
)
{
if
(
!
root
)
return
{};
vector
<
vector
<
int
>>
res
;
queue
<
TreeNode
*>
q
;
q
.
push
(
root
);
while
(
!
q
.
empty
())
{
int
levelSize
=
q
.
size
();
vector
<
int
>
levelRes
;
levelRes
.
reserve
(
levelSize
);
for
(
int
i
=
0
;
i
<
levelSize
;
++
i
)
{
TreeNode
*
curr
=
q
.
front
();
q
.
pop
();
levelRes
.
push_back
(
curr
->
val
);
if
(
curr
->
left
)
q
.
push
(
curr
->
left
);
if
(
curr
->
right
)
q
.
push
(
curr
->
right
);
}
//for
res
.
push_back
(
levelRes
);
}
//while
return
res
;
}
};
// @lc code=end
1054.distant-barcodes.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1054 lang=cpp
*
* [1054] Distant Barcodes
*/
// @lc code=start
class
Solution
{
public:
vector
<
int
>
rearrangeBarcodes
(
vector
<
int
>
&
barcodes
)
{
using
PII
=
pair
<
int
,
int
>
;
unordered_map
<
int
,
int
>
freq
;
//{barcode,freq}
for
(
auto
&
i
:
barcodes
)
freq
[
i
]
++
;
priority_queue
<
PII
,
vector
<
PII
>
,
less
<>>
pq
;
// {frq , barcode}
for
(
auto
&
pii
:
freq
)
pq
.
push
({
pii
.
second
,
pii
.
first
});
vector
<
int
>
res
;
res
.
reserve
(
barcodes
.
size
());
while
(
!
pq
.
empty
())
{
int
k
=
min
((
int
)
pq
.
size
(),
2
);
vector
<
PII
>
store
;
store
.
reserve
(
k
);
for
(
int
i
=
0
;
i
<
k
;
++
i
)
{
auto
[
frq
,
barcode
]
=
pq
.
top
();
pq
.
pop
();
res
.
push_back
(
barcode
);
--
frq
;
if
(
frq
>
0
)
store
.
push_back
({
frq
,
barcode
});
}
//for
for
(
auto
&
x
:
store
)
pq
.
push
(
x
);
}
//while
return
res
;
}
};
// @lc code=end
111.minimum-depth-of-binary-tree.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=111 lang=cpp
*
* [111] Minimum Depth of Binary Tree
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class
Solution
{
public:
int
minDepth
(
TreeNode
*
root
)
{
/* dfs:
if(!root) return 0;
if(!root->left) return minDepth(root->right)+1;
else if(!root->right) return minDepth(root->left)+1;
else
return min(minDepth(root->left), minDepth(root->right))+1;
*/
if
(
!
root
)
return
0
;
// bfs or level order treversal
int
depth
=
1
;
queue
<
TreeNode
*>
q
;
q
.
push
(
root
);
while
(
!
q
.
empty
())
{
int
levelSize
=
q
.
size
();
for
(
int
i
=
0
;
i
<
levelSize
;
++
i
)
{
TreeNode
*
curr
=
q
.
front
();
q
.
pop
();
if
(
!
curr
->
left
&&
!
curr
->
right
)
return
depth
;
if
(
curr
->
left
)
q
.
push
(
curr
->
left
);
if
(
curr
->
right
)
q
.
push
(
curr
->
right
);
}
//for
++
depth
;
}
//while
return
depth
;
}
};
// @lc code=end
112.path-sum.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=112 lang=cpp
*
* [112] Path Sum
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class
Solution
{
public:
bool
hasPathSum
(
TreeNode
*
root
,
int
targetSum
)
{
if
(
!
root
)
return
false
;
// if is a leaf
if
(
!
root
->
left
&&
!
root
->
right
)
return
targetSum
==
root
->
val
;
int
newTar
=
targetSum
-
root
->
val
;
return
hasPathSum
(
root
->
left
,
newTar
)
||
hasPathSum
(
root
->
right
,
newTar
);
}
};
// @lc code=end
113.path-sum-ii.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=113 lang=cpp
*
* [113] Path Sum II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class
Solution
{
public:
vector
<
vector
<
int
>>
pathSum
(
TreeNode
*
root
,
int
targetSum
)
{
vector
<
int
>
currList
;
vector
<
vector
<
int
>>
res
;
preorder
(
root
,
currList
,
targetSum
,
res
);
return
res
;
}
void
preorder
(
TreeNode
*
root
,
vector
<
int
>
&
currList
,
const
int
targetSum
,
vector
<
vector
<
int
>>
&
res
)
{
if
(
!
root
)
return
;
int
newSum
=
targetSum
-
root
->
val
;
currList
.
push_back
(
root
->
val
);
if
(
!
root
->
left
&&
!
root
->
right
&&
root
->
val
==
targetSum
)
res
.
push_back
(
currList
);
if
(
root
->
left
)
preorder
(
root
->
left
,
currList
,
newSum
,
res
);
if
(
root
->
right
)
preorder
(
root
->
right
,
currList
,
newSum
,
res
);
currList
.
pop_back
();
}
};
// @lc code=end
127.word-ladder.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=127 lang=cpp
*
* [127] Word Ladder
*/
// @lc code=start
class
Solution
{
public:
int
ladderLength
(
string
beginWord
,
string
endWord
,
vector
<
string
>
&
wordList
)
{
// 双向dfs
unordered_set
<
string
>
dict
(
wordList
.
begin
(),
wordList
.
end
());
if
(
dict
.
find
(
endWord
)
==
dict
.
end
())
return
0
;
const
int
len
=
beginWord
.
size
();
int
step
=
1
;
// one from begin, one from end
unordered_set
<
string
>
q1
{
beginWord
};
unordered_set
<
string
>
q2
{
endWord
};
while
(
!
q1
.
empty
()
&&
!
q2
.
empty
())
{
++
step
;
//* 这一步是核心优化
//* 交替扩展头尾,但是优先拓展短的
if
(
q1
.
size
()
>
q1
.
size
())
swap
(
q1
,
q2
);
unordered_set
<
string
>
next
;
for
(
auto
w
:
q1
)
{
for
(
int
i
=
0
;
i
<
w
.
size
();
++
i
)
{
char
orig
=
w
[
i
];
for
(
char
letter
=
'a'
;
letter
<=
'z'
;
++
letter
)
{
if
(
letter
!=
orig
)
{
w
[
i
]
=
letter
;
if
(
q2
.
find
(
w
)
!=
q2
.
end
())
return
step
;
if
(
dict
.
find
(
w
)
!=
dict
.
end
())
{
dict
.
erase
(
w
);
next
.
insert
(
w
);
}
//if
}
}
//for change letter
w
[
i
]
=
orig
;
// change back to orig to ready for change next pos in word
}
//for each pos in that word
}
//for treversal all words in q1
swap
(
next
,
q1
);
}
//while
return
0
;
}
};
// @lc code=end
1383.maximum-performance-of-a-team.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1383 lang=cpp
*
* [1383] Maximum Performance of a Team
*/
// @lc code=start
class
Solution
{
public:
int
maxPerformance
(
int
n
,
vector
<
int
>
&
speed
,
vector
<
int
>
&
efficiency
,
int
k
)
{
// greedy:
// sort all engineers by their efficent in de.
// so for person[i], all the engineer before person[i] has greater efficiency
// from them. pick the k max speed
vector
<
pair
<
long
long
,
long
long
>>
persons
;
const
long
long
M
=
1e9
+
7
;
long
long
speedSum
=
0
;
long
long
res
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
persons
.
push_back
({
efficiency
[
i
],
speed
[
i
]});
sort
(
persons
.
begin
(),
persons
.
end
(),
greater
<>
());
priority_queue
<
int
,
vector
<
int
>
,
greater
<>>
pq
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
speedSum
+=
persons
[
i
].
second
;
pq
.
push
(
persons
[
i
].
second
);
if
(
pq
.
size
()
>
k
)
{
speedSum
-=
pq
.
top
();
pq
.
pop
();
}
res
=
max
(
res
,
speedSum
*
persons
[
i
].
first
);
}
//for
return
res
%
M
;
}
//
};
// @lc code=end
1405.longest-happy-string.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1405 lang=cpp
*
* [1405] Longest Happy String
*/
// @lc code=start
class
Solution
{
public:
string
longestDiverseString
(
int
a
,
int
b
,
int
c
)
{
using
PIC
=
pair
<
int
,
char
>
;
string
res
;
res
.
reserve
(
a
+
b
+
c
);
priority_queue
<
PIC
>
pq
;
// max heap
if
(
a
>
0
)
pq
.
push
({
a
,
'a'
});
if
(
b
>
0
)
pq
.
push
({
b
,
'b'
});
if
(
c
>
0
)
pq
.
push
({
c
,
'c'
});
while
(
!
pq
.
empty
())
{
if
(
pq
.
size
()
>=
2
)
{
auto
[
f_x
,
ch_x
]
=
pq
.
top
();
pq
.
pop
();
auto
[
f_y
,
ch_y
]
=
pq
.
top
();
pq
.
pop
();
int
k
=
min
(
2
,
f_x
-
f_y
+
1
);
for
(
int
i
=
0
;
i
<
k
;
++
i
)
res
.
push_back
(
ch_x
);
res
.
push_back
(
ch_y
);
if
(
f_x
-
k
>
0
)
pq
.
push
({
f_x
-
k
,
ch_x
});
if
(
f_y
-
1
>
0
)
pq
.
push
({
f_y
-
1
,
ch_y
});
}
//if
else
{
auto
[
f_x
,
ch_x
]
=
pq
.
top
();
int
k
=
min
(
f_x
,
2
);
for
(
int
i
=
0
;
i
<
k
;
++
i
)
res
.
push_back
(
ch_x
);
pq
.
pop
();
}
}
//while
return
res
;
}
};
// @lc code=end
1439.find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1439 lang=cpp
*
* [1439] Find the Kth Smallest Sum of a Matrix With Sorted Rows
*/
// @lc code=start
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
mat
,
int
k
)
{
using
PIV
=
pair
<
int
,
vector
<
int
>>
;
const
int
m
=
mat
.
size
();
if
(
m
==
0
)
return
-
1
;
const
int
n
=
mat
[
0
].
size
();
priority_queue
<
PIV
,
vector
<
PIV
>
,
greater
<>>
pq
;
// smallest first
//用一个set来去重,
//因为vector本身是可排序的,必须用set,而不是unordered_set
set
<
vector
<
int
>>
visited
;
//push in the seed
int
sum
=
0
;
vector
<
int
>
pos
;
// index of each row
pos
.
reserve
(
m
);
for
(
int
i
=
0
;
i
<
m
;
++
i
)
{
sum
+=
mat
[
i
][
0
];
pos
.
push_back
(
0
);
}
visited
.
insert
(
pos
);
pq
.
push
(
make_pair
(
sum
,
pos
));
// do k time
for
(
int
counter
=
0
;
counter
<
k
;
++
counter
)
{
sum
=
pq
.
top
().
first
;
pos
=
pq
.
top
().
second
;
pq
.
pop
();
if
(
counter
==
k
-
1
)
return
sum
;
// push all the possible "a bit greater"
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
pos
[
i
]
++
;
if
(
pos
[
i
]
<
n
&&
visited
.
find
(
pos
)
==
visited
.
end
())
{
visited
.
insert
(
pos
);
pq
.
push
({
sum
+
mat
[
i
][
pos
[
i
]]
-
mat
[
i
][
pos
[
i
]
-
1
],
pos
});
}
pos
[
i
]
--
;
}
}
//for
return
-
1
;
}
};
// @lc code=end
1642.furthest-building-you-can-reach.cpp
0 → 100644
View file @
79e7474f
class
Solution
{
public:
int
furthestBuilding
(
vector
<
int
>
&
heights
,
int
bricks
,
int
ladders
)
{
int
count
=
0
;
priority_queue
<
int
,
vector
<
int
>
,
greater
<>>
pq
;
// start from building 0
for
(
int
i
=
1
;
i
<
heights
.
size
();
++
i
)
{
if
(
heights
[
i
]
>
heights
[
i
-
1
])
{
pq
.
push
(
heights
[
i
]
-
heights
[
i
-
1
]);
if
(
count
<
ladders
)
{
++
count
;
}
else
{
if
(
!
pq
.
empty
()
&&
bricks
<
pq
.
top
())
{
// we cannot keep going
return
i
-
1
;
}
bricks
-=
pq
.
top
();
pq
.
pop
();
}
//else
}
//if
}
//for
return
heights
.
size
()
-
1
;
}
};
\ No newline at end of file
1705.maximum-number-of-eaten-apples.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1705 lang=cpp
*
* [1705] Maximum Number of Eaten Apples
*/
// @lc code=start
class
Solution
{
public:
int
eatenApples
(
vector
<
int
>
&
apples
,
vector
<
int
>
&
days
)
{
using
PII
=
pair
<
int
,
int
>
;
priority_queue
<
PII
,
vector
<
PII
>
,
greater
<>>
pq
;
int
res
=
0
;
// nums of apple eaten
int
i
=
0
;
// date of today
while
(
i
<
apples
.
size
()
||
!
pq
.
empty
())
{
// throw rotton apples
while
(
!
pq
.
empty
()
&&
pq
.
top
().
first
<=
i
)
pq
.
pop
();
// push in new apples
if
(
i
<
apples
.
size
()
&&
apples
[
i
]
>
0
)
pq
.
push
({
i
+
days
[
i
],
apples
[
i
]});
// eat apple
if
(
!
pq
.
empty
())
{
auto
pair
=
pq
.
top
();
pq
.
pop
();
res
++
;
if
(
pair
.
second
>=
2
)
pq
.
push
({
pair
.
first
,
pair
.
second
-
1
});
}
i
++
;
}
return
res
;
}
};
// @lc code=end
1792.maximum-average-pass-ratio.cpp
0 → 100644
View file @
79e7474f
/*
* @lc app=leetcode id=1792 lang=cpp
*
* [1792] Maximum Average Pass Ratio
*/
// @lc code=start
class
Solution
{
public:
double
maxAverageRatio
(
vector
<
vector
<
int
>>
&
classes
,
int
extraStudents
)
{
// my init thought, greedy on total students
// better: use gain as key
// gain = (pi+1)/(total+1) - pi/ti
//*may be gain is not necessary to store
//* write a comparator instead
const
int
n
=
classes
.
size
();
typedef
array
<
double
,
3
>
PQEentry
;
priority_queue
<
PQEentry
,
vector
<
PQEentry
>
,
less
<>>
pq
;
for
(
auto
c
:
classes
)
{
double
p
=
c
[
0
];
double
t
=
c
[
1
];
pq
.
push
({(
p
+
1
)
/
(
t
+
1
)
-
p
/
t
,
p
,
t
});
}
//for
for
(;
extraStudents
>
0
;
--
extraStudents
)
{
auto
[
r
,
p
,
t
]
=
pq
.
top
();
pq
.
pop
();
p
+=
1
;
t
+=
1
;