added hindex

This commit is contained in:
wt
2026-04-08 16:00:29 +07:00
parent 13c21194e9
commit ea3d15f694
8 changed files with 55 additions and 1 deletions
+1
View File
@@ -14,3 +14,4 @@ add_subdirectory(rmdupfromsortedarray2)
add_subdirectory(maxprofit2)
add_subdirectory(canJump)
add_subdirectory(jump)
add_subdirectory(hindex)
+1 -1
View File
@@ -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)
+6
View File
@@ -0,0 +1,6 @@
FILE(GLOB_RECURSE SOURCES src/*.cpp)
add_executable(
hindex
${SOURCES}
)
+17
View File
@@ -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)
+1
View File
@@ -0,0 +1 @@
executable('hindex', 'src/main.cpp', install: true)
+15
View File
@@ -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;
}
+13
View File
@@ -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;
}
+1
View File
@@ -8,3 +8,4 @@ subdir('rmdupfromsortedarray2')
subdir('maxprofit2')
subdir('canJump')
subdir('jump')
subdir('hindex')