Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
fpdjsns / kotlin_list_groupBy.kt
Last active Oct 15, 2019
Kotlin list.groupBy
View kotlin_list_groupBy.kt
data class Member(val no:Long, val name: String)
fun main() {
val list = listOf(Member(1, ""), Member(1, ""), Member(3, ""))
val result = list.groupBy({it.no}, {it.name})
println(result) // {1=[일, 이], 3=[삼]}
println(result[1]) // [일, 이]
println(result[2]) // null
}
View Grumpy Bookstore Owner.cpp
class Solution {
public:
/*
* 시간복잡도 : O(N)
*/
// sliding window
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
int l = 0;
View 베스트앨범.cpp
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool compare(pair<int,int>& a, pair<int,int>& b){
// 3. 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록합니다.
if(a.first == b.first){
View Recover a Tree From Preorder Traversal.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
View Number of Enclaves.cpp
class Solution {
private:
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
public:
int getAnswer(int x, int y, vector<vector<int>>& A){
int r = A.size();
int c = A[0].size();
View Best Sightseeing Pair.cpp
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& A) {
int l = 0, r = 0;
int answer = -1e5;
for(int r=1; r<A.size(); r++){
answer =max(answer, A[l] + A[r] + l - r);
if(A[l] <= A[r] + r - l)
l = r;
}
View Capacity To Ship Packages Within D Days.cpp
class Solution {
public:
int shipWithinDays(vector<int>& weights, int D) {
// find minimum weights can Answer
int l = 0;
int r = 0;
for(int i=0;i<weights.size();i++){
int nowWeight = weights[i];
l = max(l, nowWeight);
r += nowWeight;
View Pairs of Songs With Total Durations Divisible by 60.cpp
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
map<int, int> m;
int answer = 0;
for(int i=0;i<time.size();i++){
int rest = time[i] % 60;
m[rest]++;
}
for(map<int,int>::iterator it = m.begin(); it != m.end(); it++){
View Complement of Base 10 Integer.cpp
class Solution {
public:
int bitwiseComplement(int N) {
if(N == 0) return 1;
int tmp = 1;
int answer = 0;
while(0 < N){
answer += ((N + 1) % 2) * tmp;
N /= 2;
tmp *= 2;
View Maximize Sum Of Array After K Negations.cpp
class Solution {
public:
int largestSumAfterKNegations(vector<int>& A, int K) {
sort(A.begin(), A.end());
int answer = 0;
int lastMinus = 1e9;
for(int i=0;i<A.size();i++){
if(A[i] < 0){
if(0 < K){
A[i] *= -1; K--;