diff --git a/CMakeLists.txt b/CMakeLists.txt index a3e6829..0a5498a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,3 +14,4 @@ add_subdirectory(rmdupfromsortedarray2) add_subdirectory(maxprofit2) add_subdirectory(canJump) add_subdirectory(jump) +add_subdirectory(hindex) diff --git a/Makefile b/Makefile index ffbc5a2..186b690 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/hindex/CMakeLists.txt b/hindex/CMakeLists.txt new file mode 100644 index 0000000..0c5e55a --- /dev/null +++ b/hindex/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB_RECURSE SOURCES src/*.cpp) + +add_executable( + hindex + ${SOURCES} +) diff --git a/hindex/Makefile b/hindex/Makefile new file mode 100644 index 0000000..395d1ee --- /dev/null +++ b/hindex/Makefile @@ -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) diff --git a/hindex/meson.build b/hindex/meson.build new file mode 100644 index 0000000..4b04ed7 --- /dev/null +++ b/hindex/meson.build @@ -0,0 +1 @@ +executable('hindex', 'src/main.cpp', install: true) diff --git a/hindex/src/hindex.hpp b/hindex/src/hindex.hpp new file mode 100644 index 0000000..eebc591 --- /dev/null +++ b/hindex/src/hindex.hpp @@ -0,0 +1,15 @@ +#include + +inline int hIndex(std::vector &citations) { + int n = citations.size(); + std::vector 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; +} diff --git a/hindex/src/main.cpp b/hindex/src/main.cpp new file mode 100644 index 0000000..1f6061f --- /dev/null +++ b/hindex/src/main.cpp @@ -0,0 +1,13 @@ +#include +#include +#include "hindex.hpp" + +int main() { + std::vector 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; +} diff --git a/meson.build b/meson.build index 33592e2..cef21eb 100644 --- a/meson.build +++ b/meson.build @@ -8,3 +8,4 @@ subdir('rmdupfromsortedarray2') subdir('maxprofit2') subdir('canJump') subdir('jump') +subdir('hindex')