added hindex
This commit is contained in:
@@ -14,3 +14,4 @@ add_subdirectory(rmdupfromsortedarray2)
|
||||
add_subdirectory(maxprofit2)
|
||||
add_subdirectory(canJump)
|
||||
add_subdirectory(jump)
|
||||
add_subdirectory(hindex)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
SUBDIRS := canJump jump majorityelement maxprofit maxprofit2 rmdupfromsortedarray rmdupfromsortedarray2 rotate twopointers
|
||||
SUBDIRS := canJump jump majorityelement maxprofit maxprofit2 rmdupfromsortedarray rmdupfromsortedarray2 rotate twopointers hindex
|
||||
|
||||
all: $(SUBDIRS)
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
FILE(GLOB_RECURSE SOURCES src/*.cpp)
|
||||
|
||||
add_executable(
|
||||
hindex
|
||||
${SOURCES}
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
CXX = g++
|
||||
CFLAGS = -Wall -g
|
||||
SRCS = $(wildcard src/*/*.cpp src/*.cpp)
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
TARGET = hindex
|
||||
CROSS_COMPILE =
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CROSS_COMPILE)$(CXX) $(LDFLAGS) $^ -o $@ $(LIBS)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CROSS_COMPILE)$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
@@ -0,0 +1 @@
|
||||
executable('hindex', 'src/main.cpp', install: true)
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <vector>
|
||||
|
||||
inline int hIndex(std::vector<int> &citations) {
|
||||
int n = citations.size();
|
||||
std::vector<int> count(n + 1, 0);
|
||||
for (auto c : citations) {
|
||||
c >= n ? count[n] += 1 : count[c] += 1;
|
||||
}
|
||||
int total = 0;
|
||||
for (int i = n; i > -1; i--) {
|
||||
total += count[i];
|
||||
if (total >= i) return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include "hindex.hpp"
|
||||
|
||||
int main() {
|
||||
std::vector<int> citations = {3, 0, 6, 1, 5};
|
||||
int output = hIndex(citations);
|
||||
assert(output == 3);
|
||||
citations = {1, 3, 1};
|
||||
output = hIndex(citations);
|
||||
assert(output == 1);
|
||||
return 0;
|
||||
}
|
||||
@@ -8,3 +8,4 @@ subdir('rmdupfromsortedarray2')
|
||||
subdir('maxprofit2')
|
||||
subdir('canJump')
|
||||
subdir('jump')
|
||||
subdir('hindex')
|
||||
|
||||
Reference in New Issue
Block a user